backfill Vec2::rotate tests

This commit is contained in:
2022-08-23 23:49:43 -07:00
parent b16316b75b
commit ab6496d5f6
4 changed files with 42 additions and 2 deletions

1
Cargo.lock generated
View File

@@ -346,6 +346,7 @@ name = "coremem_cross"
version = "0.2.0" version = "0.2.0"
dependencies = [ dependencies = [
"coremem_cross", "coremem_cross",
"float_eq",
"serde", "serde",
] ]

View File

@@ -17,7 +17,6 @@ crossterm = "0.24" # MIT
csv = "1.1" # MIT or Unlicense csv = "1.1" # MIT or Unlicense
dashmap = "5.3" # MIT dashmap = "5.3" # MIT
env_logger = "0.9" # MIT or Apache 2.0 env_logger = "0.9" # MIT or Apache 2.0
float_eq = "1.0" # MIT or Apache 2.0
font8x8 = "0.3" # MIT font8x8 = "0.3" # MIT
futures = "0.3" # MIT or Apache 2.0 futures = "0.3" # MIT or Apache 2.0
image = "0.24" # MIT image = "0.24" # MIT
@@ -48,6 +47,7 @@ coremem_cross = { path = "../cross", features = ["iter", "fmt", "serde", "std"]
[dev-dependencies] [dev-dependencies]
criterion = "0.3" criterion = "0.3"
float_eq = "1.0" # MIT or Apache 2.0
[[bench]] [[bench]]
name = "driver" name = "driver"

View File

@@ -13,5 +13,7 @@ std = []
[dependencies] [dependencies]
serde = { version = "1.0", optional = true } # MIT or Apache 2.0 serde = { version = "1.0", optional = true } # MIT or Apache 2.0
[dev-dependencies] [dev-dependencies]
coremem_cross = { path = ".", default-features = false, features = ["iter", "fmt", "std"] } coremem_cross = { path = ".", default-features = false, features = ["iter", "fmt", "std"] }
float_eq = "1.0" # MIT or Apache 2.0

View File

@@ -144,7 +144,6 @@ impl<R: Real> Vec2<R> {
// self.y.to_f64().atan2(self.x.to_f64()).cast() // self.y.to_f64().atan2(self.x.to_f64()).cast()
// } // }
// TODO test
pub fn rotate(&self, angle: R) -> Self { pub fn rotate(&self, angle: R) -> Self {
let (sin, cos) = angle.sin_cos(); let (sin, cos) = angle.sin_cos();
let map_1x_0y = Vec2::new(cos, sin); let map_1x_0y = Vec2::new(cos, sin);
@@ -492,3 +491,41 @@ impl<R: Real + fmt::Display> fmt::Display for Vec3<R> {
fmt::Display::fmt(")", f) fmt::Display::fmt(")", f)
} }
} }
#[cfg(test)]
mod test {
use super::*;
use float_eq::assert_float_eq;
fn assert_vec2(got: Vec2<f32>, want: Vec2<f32>) {
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
}