Skip to content

Commit

Permalink
Unify test vectors with a macro
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Feb 11, 2021
1 parent 89cbea7 commit fba71c4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 43 deletions.
28 changes: 2 additions & 26 deletions tests/quat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,33 +320,9 @@ macro_rules! impl_quat_tests {

#[test]
fn test_rotation_arc() {
let nonzero_test_vectors = [
$vec3::X,
$vec3::Y,
$vec3::Z,
-$vec3::X,
-$vec3::Y,
-$vec3::Z,
$vec3::new(1.0, 1e-3, 0.0),
$vec3::new(1.0, 1e-4, 0.0),
$vec3::new(1.0, 1e-5, 0.0),
$vec3::new(1.0, 1e-6, 0.0),
$vec3::new(1.0, 1e-7, 0.0),
$vec3::new(1.0, 1e-14, 0.0),
$vec3::new(1.0, 1e-15, 0.0),
$vec3::new(1.0, 1e-16, 0.0),
$vec3::new(0.1, 0.2, 0.3),
$vec3::new(0.2, 0.3, 0.4),
$vec3::new(4.0, -5.0, 6.0),
$vec3::new(-2.0, 0.5, -1.0),
// Pathalogical cases from <https://graphics.pixar.com/library/OrthonormalB/paper.pdf>:
$vec3::new(0.00038527316, 0.00038460016, -0.99999988079),
$vec3::new(-0.00019813581, -0.00008946839, -0.99999988079),
];

let eps = 2.0 * $t::EPSILON.sqrt();

for &from in &nonzero_test_vectors {
for &from in &vec3_float_test_vectors!($vec3) {
let from = from.normalize();

{
Expand All @@ -372,7 +348,7 @@ macro_rules! impl_quat_tests {
assert!(q.is_near_identity(), "from: {}, q: {}", from, q);
}

for &to in &nonzero_test_vectors {
for &to in &vec3_float_test_vectors!($vec3) {
let to = to.normalize();

let q = $quat::from_rotation_arc(from, to);
Expand Down
30 changes: 30 additions & 0 deletions tests/support/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,33 @@ macro_rules! impl_vec_float_normalize_tests {
}
};
}

/// Useful test vectors
#[macro_export]
macro_rules! vec3_float_test_vectors {
($vec3:ident) => {
[
$vec3::X,
$vec3::Y,
$vec3::Z,
-$vec3::X,
-$vec3::Y,
-$vec3::Z,
$vec3::new(1.0, 1e-3, 0.0),
$vec3::new(1.0, 1e-4, 0.0),
$vec3::new(1.0, 1e-5, 0.0),
$vec3::new(1.0, 1e-6, 0.0),
$vec3::new(1.0, 1e-7, 0.0),
$vec3::new(1.0, 1e-14, 0.0),
$vec3::new(1.0, 1e-15, 0.0),
$vec3::new(1.0, 1e-16, 0.0),
$vec3::new(0.1, 0.2, 0.3),
$vec3::new(0.2, 0.3, 0.4),
$vec3::new(4.0, -5.0, 6.0),
$vec3::new(-2.0, 0.5, -1.0),
// Pathalogical cases from <https://graphics.pixar.com/library/OrthonormalB/paper.pdf>:
$vec3::new(0.00038527316, 0.00038460016, -0.99999988079),
$vec3::new(-0.00019813581, -0.00008946839, -0.99999988079),
]
};
}
20 changes: 3 additions & 17 deletions tests/vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,23 +672,9 @@ macro_rules! impl_vec3_float_tests {

#[test]
fn test_any_ortho() {
let nonzero_test_vectors = [
$vec3::X,
$vec3::Y,
$vec3::Z,
$vec3::new(0.1, 0.2, 0.3),
$vec3::new(0.2, 0.3, 0.4),
$vec3::new(4.0, -5.0, 6.0),
$vec3::new(1.0, 1e-5, 0.0),
$vec3::new(-2.0, 0.5, -1.0),
// Pathalogical cases from <https://graphics.pixar.com/library/OrthonormalB/paper.pdf>:
$vec3::new(0.00038527316, 0.00038460016, -0.99999988079),
$vec3::new(-0.00019813581, -0.00008946839, -0.99999988079),
];

let eps = $t::EPSILON;

for &v in &nonzero_test_vectors {
let eps = 2.0 * $t::EPSILON;

for &v in &vec3_float_test_vectors!($vec3) {
let orthogonal = v.any_orthogonal_vector();
assert!(orthogonal != $vec3::ZERO && orthogonal.is_finite());
assert!(v.dot(orthogonal).abs() < eps);
Expand Down

0 comments on commit fba71c4

Please sign in to comment.