diff --git a/members/revsqrt/Cargo.toml b/members/revsqrt/Cargo.toml index c1072c7..b654fb9 100644 --- a/members/revsqrt/Cargo.toml +++ b/members/revsqrt/Cargo.toml @@ -25,3 +25,7 @@ path = "src/main.rs" [[test]] name = "revsqrt" harness = false # allows Cucumber to print output instead of libtest + +[[test]] +name = "basic-revsqrt" +harness = true diff --git a/members/revsqrt/tests/basic-revsqrt.rs b/members/revsqrt/tests/basic-revsqrt.rs new file mode 100644 index 0000000..23a511a --- /dev/null +++ b/members/revsqrt/tests/basic-revsqrt.rs @@ -0,0 +1,71 @@ +use std::iter::zip; + +use revsqrt; + +use rand; + +// is n about the same as m? +// This is actually not so easy! How do you measure "about same"ness? +// Also, it is not transitive, as 1 ≈ 1.1 ≈ 1.2 ≈ 1.3 ≈ ... ≈ 2 ≈ ... ≈ 3 ≈ ... ≈ infinity, that's +// a thought of me at least? +#[inline] +fn about_same(n: f32, m: f32) -> bool { + // dbg!((n, m)); + // dbg!((n - m).abs()); + // dbg!(calc_gate(n, m)); + // dbg!((n - m).abs() < calc_gate(n, m)); + (n - m).abs() <= calc_gate(n, m) +} + +#[inline] +fn calc_gate(n: f32, m: f32) -> f32 { + 0.01 + ((n.abs().sqrt().min(m.abs().sqrt())).abs() / 10f32) +} + +#[test] +fn test_calc_fast_rsqrt() { + assert_ne!(0.0, revsqrt::fast_inverse_sqrt(rand::random())) +} + +#[test] +fn test_calc_regular_rsqrt() { + assert_ne!(0.0, revsqrt::regular_inverse_sqrt(rand::random())) +} + +#[test] +fn test_calc_specific_fast_rsqrt() { + let params: &[f32] = &[1.0, 1.1, 100.0, 1337.0, 123.45678900, 1337.1337]; + let results: &[f32] = &[ + 1.0, + 0.9534625892455922, + 0.1, + 0.02734854943722097, + 0.0900000004095, + 0.027347182112297627, + ]; + for (n, m) in zip(params, results) { + assert!(about_same(revsqrt::fast_inverse_sqrt(*n), *m)) + } +} + +#[test] +fn test_calc_specific_reqular_rsqrt() { + let params: &[f32] = &[1.0, 1.1, 100.0, 1337.0, 123.45678900, 1337.1337]; + let results: &[f32] = &[ + 1.0, + 0.9534625892455922, + 0.1, + 0.02734854943722097, + 0.0900000004095, + 0.027347182112297627, + ]; + for (n, m) in zip(params, results) { + assert_eq!(revsqrt::regular_inverse_sqrt(*n), *m) + } +} + +#[test] +fn test_fail() { + println!("the stdout will be printed on fail!"); + assert!(false) +} diff --git a/members/revsqrt/tests/revsqrt.rs b/members/revsqrt/tests/revsqrt.rs index 56e8fe0..f7e2260 100644 --- a/members/revsqrt/tests/revsqrt.rs +++ b/members/revsqrt/tests/revsqrt.rs @@ -1,23 +1,22 @@ use std::iter::zip; +use revsqrt; + use cucumber::{gherkin::Step, given, then, when, World}; use rand; +/// stores the current information for each scenario #[derive(Debug, Default, World)] -pub struct NumWorld { +struct NumWorld { numbers: Vec<(f32, f32)>, } -// is n about the same as m? -// This is actually not so easy! How do you measure "about same"ness? -// Also, it is not transitive, as 1 ≈ 1.1 ≈ 1.2 ≈ 1.3 ≈ ... ≈ 2 ≈ ... ≈ 3 ≈ ... ≈ infinity, that's -// a thought of me at least? +/// is n about the same as m? +/// +/// This is actually not so easy! How do you measure *about same*ness? +/// Also, I don't think it is transitive, as 1 ≈ 1.1 ≈ 1.2 ≈ 1.3 ≈ ... ≈ 2 ≈ ... ≈ 3 ≈ ... ≈ infinity #[inline] fn about_same(n: f32, m: f32) -> bool { - // dbg!((n, m)); - // dbg!((n - m).abs()); - // dbg!(calc_gate(n, m)); - // dbg!((n - m).abs() < calc_gate(n, m)); (n - m).abs() <= calc_gate(n, m) }