From 65b175b4a763f2fe07f06666a94b23fed0aa203b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Tue, 21 May 2013 23:15:03 +0000 Subject: [PATCH] Refactored tests using macros. --- src/tests/mat.rs | 48 +++++------- src/tests/vec.rs | 191 ++++++++++++++--------------------------------- 2 files changed, 75 insertions(+), 164 deletions(-) diff --git a/src/tests/mat.rs b/src/tests/mat.rs index 8eb4b34e..8ddb4ae0 100644 --- a/src/tests/mat.rs +++ b/src/tests/mat.rs @@ -19,47 +19,33 @@ use dim3::mat3::Mat3; #[test] use adaptors::rotmat::Rotmat; -// FIXME: this one fails with an ICE: node_id_to_type: no type for node [...] -// #[test] -// fn test_inv_nmat() -// { -// let randmat : NMat = random(); -// -// assert!((randmat.inverse() * randmat).approx_eq(&One::one())); -// } +macro_rules! test_inv_mat_impl( + ($t:ty) => ( + for uint::range(0u, 10000u) |_| + { + let randmat : $t = random(); + + assert!((randmat.inverse() * randmat).approx_eq(&One::one())); + } + ); +) #[test] fn test_inv_mat1() -{ - for uint::range(0u, 10000u) |_| - { - let randmat : Mat1 = random(); - - assert!((randmat.inverse() * randmat).approx_eq(&One::one())); - } -} +{ test_inv_mat_impl!(Mat1); } #[test] fn test_inv_mat2() -{ - for uint::range(0u, 10000u) |_| - { - let randmat : Mat2 = random(); - - assert!((randmat.inverse() * randmat).approx_eq(&One::one())); - } -} +{ test_inv_mat_impl!(Mat2); } #[test] fn test_inv_mat3() -{ - for uint::range(0u, 10000u) |_| - { - let randmat : Mat3 = random(); +{ test_inv_mat_impl!(Mat3); } - assert!((randmat.inverse() * randmat).approx_eq(&One::one())); - } -} +// FIXME: this one fails with an ICE: node_id_to_type: no type for node [...] +// #[test] +// fn test_inv_nmat() +// { test_inv_mat_impl!(NMat); } #[test] fn test_rotation2() diff --git a/src/tests/vec.rs b/src/tests/vec.rs index 72d552f8..a4201e85 100644 --- a/src/tests/vec.rs +++ b/src/tests/vec.rs @@ -25,6 +25,49 @@ use traits::dot::Dot; #[test] use traits::norm::Norm; +macro_rules! test_commut_dot_impl( + ($t:ty) => ( + for uint::range(0u, 10000u) |_| + { + let v1 : $t = random(); + let v2 : $t = random(); + + assert!(v1.dot(&v2).approx_eq(&v2.dot(&v1))); + } + ); +) + +macro_rules! test_basis_impl( + ($t:ty) => ( + for uint::range(0u, 10000u) |_| + { + let basis = Basis::canonical_basis::<$t>(); + + // check vectors form an ortogonal basis + assert!(all2(basis, basis, |e1, e2| e1 == e2 || e1.dot(e2).approx_eq(&Zero::zero()))); + // check vectors form an orthonormal basis + assert!(all(basis, |e| e.norm().approx_eq(&One::one()))); + } + ); +) + +macro_rules! test_subspace_basis_impl( + ($t:ty) => ( + for uint::range(0u, 10000u) |_| + { + let v : Vec3 = random(); + let v1 = v.normalized(); + let subbasis = v1.orthogonal_subspace_basis(); + + // check vectors are orthogonal to v1 + assert!(all(subbasis, |e| v1.dot(e).approx_eq(&Zero::zero()))); + // check vectors form an ortogonal basis + assert!(all2(subbasis, subbasis, |e1, e2| e1 == e2 || e1.dot(e2).approx_eq(&Zero::zero()))); + // check vectors form an orthonormal basis + assert!(all(subbasis, |e| e.norm().approx_eq(&One::one()))); + } + ); +) #[test] fn test_cross_vec3() @@ -41,167 +84,49 @@ fn test_cross_vec3() } #[test] -fn test_dot_nvec() -{ - for uint::range(0u, 10000u) |_| - { - let v1 : NVec = random(); - let v2 : NVec = random(); - - assert!(v1.dot(&v2).approx_eq(&v2.dot(&v1))); - } -} +fn test_commut_dot_nvec() +{ test_commut_dot_impl!(NVec); } #[test] fn test_commut_dot_vec3() -{ - for uint::range(0u, 10000u) |_| - { - let v1 : Vec3 = random(); - let v2 : Vec3 = random(); - - assert!(v1.dot(&v2).approx_eq(&v2.dot(&v1))); - } -} +{ test_commut_dot_impl!(Vec3); } #[test] fn test_commut_dot_vec2() -{ - for uint::range(0u, 10000u) |_| - { - let v1 : Vec2 = random(); - let v2 : Vec2 = random(); - - assert!(v1.dot(&v2).approx_eq(&v2.dot(&v1))); - } -} +{ test_commut_dot_impl!(Vec2); } #[test] fn test_commut_dot_vec1() -{ - for uint::range(0u, 10000u) |_| - { - let v1 : Vec1 = random(); - let v2 : Vec1 = random(); - - assert!(v1.dot(&v2).approx_eq(&v2.dot(&v1))); - } -} +{ test_commut_dot_impl!(Vec1); } #[test] fn test_basis_vec1() -{ - let basis = Basis::canonical_basis::>(); - - // check vectors form an ortogonal basis - assert!(all2(basis, basis, |e1, e2| e1 == e2 || e1.dot(e2).approx_eq(&Zero::zero()))); - // check vectors form an orthonormal basis - assert!(all(basis, |e| e.norm().approx_eq(&One::one()))); -} +{ test_basis_impl!(Vec1); } #[test] fn test_basis_vec2() -{ - let basis = Basis::canonical_basis::>(); - - // check vectors form an ortogonal basis - assert!(all2(basis, basis, |e1, e2| e1 == e2 || e1.dot(e2).approx_eq(&Zero::zero()))); - // check vectors form an orthonormal basis - assert!(all(basis, |e| e.norm().approx_eq(&One::one()))); -} +{ test_basis_impl!(Vec2); } #[test] fn test_basis_vec3() -{ - let basis = Basis::canonical_basis::>(); - - // check vectors form an ortogonal basis - assert!(all2(basis, basis, |e1, e2| e1 == e2 || e1.dot(e2).approx_eq(&Zero::zero()))); - // check vectors form an orthonormal basis - assert!(all(basis, |e| e.norm().approx_eq(&One::one()))); -} +{ test_basis_impl!(Vec3); } #[test] fn test_basis_nvec() -{ - let basis = Basis::canonical_basis::>(); - - // check vectors form an ortogonal basis - assert!(all2(basis, basis, |e1, e2| e1 == e2 || e1.dot(e2).approx_eq(&Zero::zero()))); - // check vectors form an orthonormal basis - assert!(all(basis, |e| e.norm().approx_eq(&One::one()))); -} +{ test_basis_impl!(NVec); } #[test] fn test_subspace_basis_vec1() -{ - for uint::range(0u, 10000u) |_| - { - let v : Vec1 = random(); - let v1 = v.normalized(); - let subbasis = v1.orthogonal_subspace_basis(); - - // check vectors are orthogonal to v1 - assert!(all(subbasis, |e| v1.dot(e).approx_eq(&Zero::zero()))); - // check vectors form an ortogonal basis - assert!(all2(subbasis, subbasis, |e1, e2| e1 == e2 || e1.dot(e2).approx_eq(&Zero::zero()))); - // check vectors form an orthonormal basis - assert!(all(subbasis, |e| e.norm().approx_eq(&One::one()))); - } -} +{ test_subspace_basis_impl!(Vec1); } #[test] fn test_subspace_basis_vec2() -{ - for uint::range(0u, 10000u) |_| - { - let v : Vec2 = random(); - let v1 = v.normalized(); - let subbasis = v1.orthogonal_subspace_basis(); - - // check vectors are orthogonal to v1 - assert!(all(subbasis, |e| v1.dot(e).approx_eq(&Zero::zero()))); - // check vectors form an ortogonal basis - assert!(all2(subbasis, subbasis, |e1, e2| e1 == e2 || e1.dot(e2).approx_eq(&Zero::zero()))); - // check vectors form an orthonormal basis - assert!(all(subbasis, |e| e.norm().approx_eq(&One::one()))); - } -} +{ test_subspace_basis_impl!(Vec2); } #[test] fn test_subspace_basis_vec3() -{ - for uint::range(0u, 10000u) |_| - { - let v : Vec3 = random(); - let v1 = v.normalized(); - let subbasis = v1.orthogonal_subspace_basis(); +{ test_subspace_basis_impl!(Vec3); } - // check vectors are orthogonal to v1 - assert!(all(subbasis, |e| v1.dot(e).approx_eq(&Zero::zero()))); - // check vectors form an ortogonal basis - assert!(all2(subbasis, subbasis, |e1, e2| e1 == e2 || e1.dot(e2).approx_eq(&Zero::zero()))); - // check vectors form an orthonormal basis - assert!(all(subbasis, |e| e.norm().approx_eq(&One::one()))); - } -} - -// ICE -// -// #[test] -// fn test_subspace_basis_vecn() -// { -// for uint::range(0u, 10000u) |_| -// { -// let v : NVec = random(); -// let v1 = v.normalized(); -// let subbasis = v1.orthogonal_subspace_basis(); -// -// // check vectors are orthogonal to v1 -// assert!(all(subbasis, |e| v1.dot(e).approx_eq(&Zero::zero()))); -// // check vectors form an ortogonal basis -// assert!(all2(subbasis, subbasis, |e1, e2| e1 == e2 || e1.dot(e2).approx_eq(&Zero::zero()))); -// // check vectors form an orthonormal basis -// assert!(all(subbasis, |e| e.norm().approx_eq(&One::one()))); -// } -// } +#[test] +fn test_subspace_basis_nvec() +{ test_subspace_basis_impl!(NVec); }