From 49816e865931ffa9a37dcd00c017a68d45a98174 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Tue, 9 Aug 2022 09:06:10 +0000 Subject: [PATCH] o-x-rust intial complete --- .woodpecker/wasm-o-x-rust.yml | 19 +++++--- tools/wasm-pack-plugin/README.md | 3 ++ tools/wasm-pack-plugin/plugin.sh | 7 ++- wasm/o-x-rust/Cargo.lock | 74 +++++++++++++++++++++++++++++++- wasm/o-x-rust/Cargo.toml | 5 ++- wasm/o-x-rust/src/lib.rs | 8 ++-- wasm/o-x-rust/tests/web.rs | 55 ++++++++++++++++++++++++ 7 files changed, 159 insertions(+), 12 deletions(-) create mode 100644 wasm/o-x-rust/tests/web.rs diff --git a/.woodpecker/wasm-o-x-rust.yml b/.woodpecker/wasm-o-x-rust.yml index fd0eb3d..3ceea91 100644 --- a/.woodpecker/wasm-o-x-rust.yml +++ b/.woodpecker/wasm-o-x-rust.yml @@ -5,21 +5,30 @@ pipeline: settings: actions: build folder: wasm/o-x-rust + target: web scope: game-algorithms when: - branch: - exclude: [main] path: include: - wasm/o-x-rust/* - .woodpecker/wasm-o-x-rust.yml - build and publish: + test: image: git.koval.net/cyclane/game-algorithms/wasm-pack-plugin pull: true settings: - actions: build,publish + actions: test + folder: wasm/o-x-rust + when: + path: + include: + - wasm/o-x-rust/* + - .woodpecker/wasm-o-x-rust.yml + publish: + image: git.koval.net/cyclane/game-algorithms/wasm-pack-plugin + pull: true + settings: + actions: publish folder: wasm/o-x-rust - target: web scope: game-algorithms username: cyclane token: diff --git a/tools/wasm-pack-plugin/README.md b/tools/wasm-pack-plugin/README.md index 0f93a15..b691871 100644 --- a/tools/wasm-pack-plugin/README.md +++ b/tools/wasm-pack-plugin/README.md @@ -17,6 +17,9 @@ When using this plugin it is best to pull every time since it can be updated oft - `target` (default=`bundler`): Target to build for - `make`: Optionally run a make target building +### Test +(No options) + ### Publish - `username` (required): NPM registry username diff --git a/tools/wasm-pack-plugin/plugin.sh b/tools/wasm-pack-plugin/plugin.sh index 1e84076..a5f207a 100755 --- a/tools/wasm-pack-plugin/plugin.sh +++ b/tools/wasm-pack-plugin/plugin.sh @@ -2,9 +2,10 @@ # Properties: actions # username, token, registry=https://registry.npmjs.org, scope, folder=. -# profile=release, target=bundler, make +# profile=release, target=bundler, make, browser # publish: username, token +# test: # build: echo wasm-pack plugin @@ -34,6 +35,10 @@ if [[ "${PLUGIN_ACTIONS}" =~ '(^|,)build(,|$)' ]]; then fi fi +if [[ "${PLUGIN_ACTTIONS}" =~ '(^|,)test(,|$)' ]]; then + wasm-pack test --node +fi + if [[ "${PLUGIN_ACTIONS}" =~ '(^|,)publish(,|$)' ]]; then cd pkg npm pkg set type=module diff --git a/wasm/o-x-rust/Cargo.lock b/wasm/o-x-rust/Cargo.lock index b9fd77a..f6482fa 100644 --- a/wasm/o-x-rust/Cargo.lock +++ b/wasm/o-x-rust/Cargo.lock @@ -14,6 +14,25 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + +[[package]] +name = "js-sys" +version = "0.3.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "258451ab10b34f8af53416d1fdab72c22e805f0c92a1136d59470ec0b11138b2" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "log" version = "0.4.17" @@ -25,9 +44,10 @@ dependencies = [ [[package]] name = "o-x-rust" -version = "0.0.9" +version = "0.1.0" dependencies = [ "wasm-bindgen", + "wasm-bindgen-test", ] [[package]] @@ -54,6 +74,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "scoped-tls" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" + [[package]] name = "syn" version = "1.0.99" @@ -96,6 +122,18 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa76fb221a1f8acddf5b54ace85912606980ad661ac7a503b4570ffd3a624dad" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasm-bindgen-macro" version = "0.2.82" @@ -124,3 +162,37 @@ name = "wasm-bindgen-shared" version = "0.2.82" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6598dd0bd3c7d51095ff6531a5b23e02acdc81804e30d8f07afb77b7215a140a" + +[[package]] +name = "wasm-bindgen-test" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "513df541345bb9fcc07417775f3d51bbb677daf307d8035c0afafd87dc2e6599" +dependencies = [ + "console_error_panic_hook", + "js-sys", + "scoped-tls", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm-bindgen-test-macro", +] + +[[package]] +name = "wasm-bindgen-test-macro" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6150d36a03e90a3cf6c12650be10626a9902d70c5270fd47d7a47e5389a10d56" +dependencies = [ + "proc-macro2", + "quote", +] + +[[package]] +name = "web-sys" +version = "0.3.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed055ab27f941423197eb86b2035720b1a3ce40504df082cac2ecc6ed73335a1" +dependencies = [ + "js-sys", + "wasm-bindgen", +] diff --git a/wasm/o-x-rust/Cargo.toml b/wasm/o-x-rust/Cargo.toml index cad6eaf..555c93d 100644 --- a/wasm/o-x-rust/Cargo.toml +++ b/wasm/o-x-rust/Cargo.toml @@ -4,7 +4,7 @@ description = "Noughts and crosses WASM algorithms" repository = "https://git.koval.net/cyclane/game-algorithms/src/branch/main/wasm/o-x-rust" license = "GNU GPLv3" readme = "README.md" -version = "0.0.9" +version = "0.1.0" edition = "2021" [lib] @@ -12,3 +12,6 @@ crate-type = ["cdylib", "rlib"] [dependencies] wasm-bindgen = "0.2.82" + +[dev-dependencies] +wasm-bindgen-test = "0.3.32" diff --git a/wasm/o-x-rust/src/lib.rs b/wasm/o-x-rust/src/lib.rs index acc40cf..7646485 100644 --- a/wasm/o-x-rust/src/lib.rs +++ b/wasm/o-x-rust/src/lib.rs @@ -4,17 +4,17 @@ use wasm_bindgen::prelude::wasm_bindgen; pub fn find_winner(board: &[u8]) -> u8 { for i in 0..3 { let h = i*3; - if board[h] == board[h + 1] && board[h + 1] == board[h + 2] { + if board[h] != 0 && board[h] == board[h + 1] && board[h + 1] == board[h + 2] { return board[h]; } - if board[i] == board[i + 3] && board[i + 3] == board[i + 6] { + if board[i] != 0 && board[i] == board[i + 3] && board[i + 3] == board[i + 6] { return board[i]; } } - if board[0] == board[4] && board[4] == board[8] { + if board[0] != 0 && board[0] == board[4] && board[4] == board[8] { return board[0]; } - if board[2] == board[4] && board[4] == board[6] { + if board[2] != 0 && board[2] == board[4] && board[4] == board[6] { return board[2]; } 0 diff --git a/wasm/o-x-rust/tests/web.rs b/wasm/o-x-rust/tests/web.rs new file mode 100644 index 0000000..165a004 --- /dev/null +++ b/wasm/o-x-rust/tests/web.rs @@ -0,0 +1,55 @@ +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)); + }); +}