diff --git a/Cargo.lock b/Cargo.lock index d3bc293..043962e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -346,6 +346,7 @@ name = "coremem_cross" version = "0.2.0" dependencies = [ "coremem_cross", + "float_eq", "serde", ] diff --git a/crates/coremem/Cargo.toml b/crates/coremem/Cargo.toml index 91a019d..d8c0542 100644 --- a/crates/coremem/Cargo.toml +++ b/crates/coremem/Cargo.toml @@ -17,7 +17,6 @@ crossterm = "0.24" # MIT csv = "1.1" # MIT or Unlicense dashmap = "5.3" # MIT env_logger = "0.9" # MIT or Apache 2.0 -float_eq = "1.0" # MIT or Apache 2.0 font8x8 = "0.3" # MIT futures = "0.3" # MIT or Apache 2.0 image = "0.24" # MIT @@ -48,6 +47,7 @@ coremem_cross = { path = "../cross", features = ["iter", "fmt", "serde", "std"] [dev-dependencies] criterion = "0.3" +float_eq = "1.0" # MIT or Apache 2.0 [[bench]] name = "driver" diff --git a/crates/cross/Cargo.toml b/crates/cross/Cargo.toml index 8bd65f0..f4dde1a 100644 --- a/crates/cross/Cargo.toml +++ b/crates/cross/Cargo.toml @@ -13,5 +13,7 @@ std = [] [dependencies] serde = { version = "1.0", optional = true } # MIT or Apache 2.0 + [dev-dependencies] coremem_cross = { path = ".", default-features = false, features = ["iter", "fmt", "std"] } +float_eq = "1.0" # MIT or Apache 2.0 diff --git a/crates/cross/src/vec.rs b/crates/cross/src/vec.rs index fbdbcb6..2ac670f 100644 --- a/crates/cross/src/vec.rs +++ b/crates/cross/src/vec.rs @@ -144,7 +144,6 @@ impl Vec2 { // self.y.to_f64().atan2(self.x.to_f64()).cast() // } - // TODO test pub fn rotate(&self, angle: R) -> Self { let (sin, cos) = angle.sin_cos(); let map_1x_0y = Vec2::new(cos, sin); @@ -492,3 +491,41 @@ impl fmt::Display for Vec3 { fmt::Display::fmt(")", f) } } + +#[cfg(test)] +mod test { + use super::*; + use float_eq::assert_float_eq; + + fn assert_vec2(got: Vec2, want: Vec2) { + assert_float_eq!(got.x(), want.x(), abs <= 1e-6); + assert_float_eq!(got.y(), want.y(), abs <= 1e-6); + } + #[test] + fn vec2_rotate_trivial() { + // no-op rotate + assert_vec2(Vec2::new(1.0, 0.0).rotate(0.0), Vec2::new(1.0, 0.0)); + assert_vec2(Vec2::new(1.0, 1.0).rotate(f32::two_pi()), Vec2::new(1.0, 1.0)); + assert_vec2(Vec2::new(-2.0, 3.0).rotate(-f32::two_pi()), Vec2::new(-2.0, 3.0)); + } + + #[test] + fn vec2_rotate_quarter_turns() { + assert_vec2(Vec2::new(1.0, 0.0).rotate(f32::pi()), Vec2::new(-1.0, 0.0)); + assert_vec2(Vec2::new(1.0, -1.0).rotate(f32::pi()), Vec2::new(-1.0, 1.0)); + + assert_vec2(Vec2::new(1.0, 0.0).rotate(0.5*f32::pi()), Vec2::new(0.0, 1.0)); + assert_vec2(Vec2::new(1.0, -1.0).rotate(0.5*f32::pi()), Vec2::new(1.0, 1.0)); + + assert_vec2(Vec2::new(-1.0, 0.0).rotate(-0.5*f32::pi()), Vec2::new(0.0, 1.0)); + assert_vec2(Vec2::new(-1.0, -1.0).rotate(-0.5*f32::pi()), Vec2::new(-1.0, 1.0)); + } + + #[test] + fn vec2_rotate_zero() { + assert_vec2(Vec2::new(0.0, 0.0).rotate(1.0), Vec2::new(0.0, 0.0)); + assert_vec2(Vec2::new(0.0, 0.0).rotate(1e9), Vec2::new(0.0, 0.0)); + } + + // TODO: lots more Vec, Vec2 tests need backfilling +}