From d8d6bb9a6789dc7e33c5a80b6b2095a197f3029b Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Fri, 12 Jan 2024 14:45:00 +0100 Subject: [PATCH] cuoolcumber --- Cargo.lock | 58 +++++++++++++++++++ members/cucumber-demo/Cargo.toml | 1 + members/revsqrt/Cargo.toml | 8 +++ .../tests/features/book/revsqrt.feature | 6 ++ members/revsqrt/tests/revsqrt.rs | 36 ++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 members/revsqrt/tests/features/book/revsqrt.feature create mode 100644 members/revsqrt/tests/revsqrt.rs diff --git a/Cargo.lock b/Cargo.lock index ededf00..916a993 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -416,6 +416,7 @@ version = "0.1.0" dependencies = [ "cucumber", "futures", + "rand", "tokio", ] @@ -561,6 +562,17 @@ dependencies = [ "slab", ] +[[package]] +name = "getrandom" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "gherkin" version = "0.14.0" @@ -949,6 +961,12 @@ dependencies = [ "plotters-backend", ] +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "proc-macro2" version = "1.0.76" @@ -967,6 +985,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "rayon" version = "1.8.0" @@ -1027,6 +1075,10 @@ name = "revsqrt" version = "0.1.0" dependencies = [ "criterion", + "cucumber", + "futures", + "rand", + "tokio", ] [[package]] @@ -1347,6 +1399,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasm-bindgen" version = "0.2.89" diff --git a/members/cucumber-demo/Cargo.toml b/members/cucumber-demo/Cargo.toml index 357b2bb..bf424b4 100644 --- a/members/cucumber-demo/Cargo.toml +++ b/members/cucumber-demo/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" futures = "0.3.30" cucumber = "0.20.2" tokio = { version = "1.10", features = ["macros", "rt-multi-thread", "time"] } +rand = "0.8.5" [[test]] name = "example" # this should be the same as the filename of your test target diff --git a/members/revsqrt/Cargo.toml b/members/revsqrt/Cargo.toml index 2a4dc44..c1072c7 100644 --- a/members/revsqrt/Cargo.toml +++ b/members/revsqrt/Cargo.toml @@ -5,6 +5,10 @@ edition = "2021" [dev-dependencies] criterion = "0.5.1" +futures = "0.3.30" +cucumber = "0.20.2" +tokio = { version = "1.10", features = ["macros", "rt-multi-thread", "time"] } +rand = "0.8.5" [[bench]] name = "rsqrt-bench" @@ -17,3 +21,7 @@ path = "src/lib.rs" [[bin]] name = "revsqrt" path = "src/main.rs" + +[[test]] +name = "revsqrt" +harness = false # allows Cucumber to print output instead of libtest diff --git a/members/revsqrt/tests/features/book/revsqrt.feature b/members/revsqrt/tests/features/book/revsqrt.feature new file mode 100644 index 0000000..af08e5d --- /dev/null +++ b/members/revsqrt/tests/features/book/revsqrt.feature @@ -0,0 +1,6 @@ +Feature: inverted square root feature + + Scenario: If we calculate the inverted square root of a number using fast inverted square root, it's about the same as if we calculate it normally + Given a number + When We calculate the the inverted square root of a number using fast inverted square root + Then The result is about the same as if we calculate it normally diff --git a/members/revsqrt/tests/revsqrt.rs b/members/revsqrt/tests/revsqrt.rs new file mode 100644 index 0000000..9fd059a --- /dev/null +++ b/members/revsqrt/tests/revsqrt.rs @@ -0,0 +1,36 @@ +use revsqrt::*; + +use cucumber::{given, then, when, World}; +use rand; + +#[derive(Debug, Default, World)] +pub struct NumWorld { + number: f32, + result: f32, +} + +// is n about the same as m? +fn about_same(n: f32, m: f32) -> bool { + (n - m)*1000f32 < 1f32 +} + +// Steps are defined with `given`, `when` and `then` attributes. +#[given(regex = r"^a number$")] +async fn hungry_cat(world: &mut NumWorld) { + world.number = rand::random(); +} + +#[when("We calculate the the inverted square root of a number using fast inverted square root")] +async fn calc_fast_inv_sqrt(world: &mut NumWorld) { + world.result = revsqrt::fast_inverse_sqrt(world.number); +} + +#[then("The result is about the same as if we calculate it normally")] +async fn comp_result_with_normal(world: &mut NumWorld) { + assert!(about_same(world.number, world.result)); +} + +#[tokio::main] +async fn main() { + futures::executor::block_on(NumWorld::run("tests/features/book/revsqrt.feature")); +}