game-algorithms/wasm/o-x-rust/tests/web.rs

56 lines
1.5 KiB
Rust
Raw Permalink Normal View History

2022-08-09 09:06:10 +00:00
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
use o_x_rust::*;
#[wasm_bindgen_test]
fn find_winner_test() {
assert_eq!(find_winner(&[0, 0, 0, 1, 1, 1, 0, 0, 0]), 1);
assert_eq!(find_winner(&[2, 1, 1, 1, 2, 1, 0, 0, 2]), 2);
assert_eq!(find_winner(&[2, 1, 1, 1, 2, 1, 0, 0, 1]), 1);
assert_eq!(find_winner(&[2, 1, 2, 1, 2, 1, 2, 1, 0]), 2);
}
#[wasm_bindgen_test]
fn count_empty_test() {
assert_eq!(count_empty(&[0, 0, 1, 0, 2, 0, 0, 0, 0]), 7);
assert_eq!(count_empty(&[1, 2, 1, 1, 2, 1, 2, 2, 1]), 0);
}
#[wasm_bindgen_test]
fn get_turn_test() {
assert_eq!(get_turn(1, 2, true, 6), 2);
assert_eq!(get_turn(1, 2, false, 7), 2);
assert_eq!(get_turn(2, 1, true, 2), 1);
assert_eq!(get_turn(2, 1, false, 2), 2);
}
// Go through the flow, testing whether anything panics (which it shouldn't)
// and that predict doesn't return impossible moves
#[wasm_bindgen_test]
fn test_scores() {
let board1: &[u8] = &[1, 2, 1, 2, 1, 2, 0, 1, 2];
let board2: &[u8] = &[0, 0, 0, 0, 1, 0, 2, 0, 0];
let me = 2;
let other = 1;
let first = false;
let algorithms = &["sa", "sa+rd", "sa+r-d", "mm", "mm+d"];
algorithms.iter()
.for_each(|&algorithm| {
let p1 = predict(me, other, first, board1, algorithm);
assert_eq!(p1, 6);
let p2 = predict(me, other, first, board2, algorithm);
assert!(board2.iter()
.enumerate()
.filter_map(|(idx, &v)| {
if v != 0 {
Some(idx)
} else { None }
})
.all(|idx| p2 != idx));
});
}