Browse Source

Use float-cmp for nicer assertions

pull/28/head
Ian Chamberlain 1 year ago
parent
commit
8cd28b7ca5
No known key found for this signature in database
GPG Key ID: AE5484D09405AA60
  1. 1
      citro3d/Cargo.toml
  2. 22
      citro3d/src/math/fvec.rs
  3. 4
      citro3d/src/math/ops.rs

1
citro3d/Cargo.toml

@ -15,4 +15,5 @@ ctru-sys = { git = "https://github.com/rust3ds/ctru-rs.git" } @@ -15,4 +15,5 @@ ctru-sys = { git = "https://github.com/rust3ds/ctru-rs.git" }
libc = "0.2.125"
[dev-dependencies]
float-cmp = "0.9.0"
test-runner = { git = "https://github.com/rust3ds/test-runner.git" }

22
citro3d/src/math/fvec.rs

@ -128,7 +128,9 @@ impl<const N: usize> FVec<N> { @@ -128,7 +128,9 @@ impl<const N: usize> FVec<N> {
#[cfg(test)]
mod tests {
use super::*;
use float_cmp::assert_approx_eq;
// TODO: These could probably all be doctests instead it's just sort of a pain
#[test]
fn fvec4() {
let l = FVec4::new(2.0, 2.0, 2.0, 2.0);
@ -136,20 +138,20 @@ mod tests { @@ -136,20 +138,20 @@ mod tests {
assert_eq!(l, FVec4::splat(2.0));
for component in [l.x(), l.y(), l.z(), l.w()] {
assert!((component - 2.0).abs() < f32::EPSILON);
assert_approx_eq!(f32, component, 2.0);
}
assert_eq!(l.perspective_divide(), FVec4::splat(1.0));
let dot = l.dot(&FVec4::splat(3.0));
assert!((dot - 24.0).abs() < f32::EPSILON);
assert_approx_eq!(f32, dot, 24.0);
assert!((l.magnitude() - 4.0).abs() < f32::EPSILON);
assert_approx_eq!(f32, l.magnitude(), 4.0);
let norm = l.normalize();
assert!((norm.magnitude() - 1.0).abs() < f32::EPSILON);
assert_approx_eq!(f32, norm.magnitude(), 1.0);
for component in [l.y(), l.z(), l.w()] {
assert!((component - l.x()).abs() < f32::EPSILON);
assert_approx_eq!(f32, component, l.x());
}
}
@ -160,18 +162,18 @@ mod tests { @@ -160,18 +162,18 @@ mod tests {
assert_eq!(l, FVec3::splat(2.0));
for component in [l.x(), l.y(), l.z()] {
assert!((component - 2.0).abs() < f32::EPSILON);
assert_approx_eq!(f32, component, 2.0);
}
let dot = l.dot(&FVec3::splat(3.0));
assert!((dot - 18.0).abs() < f32::EPSILON);
assert_approx_eq!(f32, dot, 18.0);
assert!((l.magnitude() - f32::sqrt(12.0)).abs() < f32::EPSILON);
assert_approx_eq!(f32, l.magnitude(), f32::sqrt(12.0));
let norm = l.normalize();
assert!((norm.magnitude() - 1.0).abs() < f32::EPSILON);
assert_approx_eq!(f32, norm.magnitude(), 1.0);
for component in [l.y(), l.z()] {
assert!((l.x() - component).abs() < f32::EPSILON);
assert_approx_eq!(f32, l.x(), component);
}
}
}

4
citro3d/src/math/ops.rs

@ -85,7 +85,7 @@ mod tests { @@ -85,7 +85,7 @@ mod tests {
use super::*;
#[test]
fn vec3_ops() {
fn vec3() {
let l = FVec3::splat(1.0);
let r = FVec3::splat(2.0);
@ -99,7 +99,7 @@ mod tests { @@ -99,7 +99,7 @@ mod tests {
}
#[test]
fn vec4_ops() {
fn vec4() {
let l = FVec4::splat(1.0);
let r = FVec4::splat(2.0);

Loading…
Cancel
Save