Debian Codegraph @

debian/missing-sources/glam-0.15.2/tests/support/macros.rs

raw
1 #[macro_export]
2 macro_rules! assert_approx_eq {
3 ($a:expr, $b:expr) => {{
4 #[allow(unused_imports)]
5 use crate::support::FloatCompare;
6 let eps = core::f32::EPSILON;
7 let (a, b) = (&$a, &$b);
8 assert!(
9 a.approx_eq(b, eps),
10 "assertion failed: `(left !== right)` \
11 (left: `{:?}`, right: `{:?}`, expect diff: `{:?}`, real diff: `{:?}`)",
12 *a,
13 *b,
14 eps,
15 a.abs_diff(b)
16 );
17 }};
18 ($a:expr, $b:expr, $eps:expr) => {{
19 use crate::support::FloatCompare;
20 let (a, b) = (&$a, &$b);
21 let eps = $eps;
22 assert!(
23 a.approx_eq(b, $eps),
24 "assertion failed: `(left !== right)` \
25 (left: `{:?}`, right: `{:?}`, expect diff: `{:?}`, real diff: `{:?}`)",
26 *a,
27 *b,
28 eps,
29 a.abs_diff(b)
30 );
31 }};
32 }
33
34 /// Test vector normalization for float vector
35 #[macro_export]
36 macro_rules! impl_vec_float_normalize_tests {
37 ($t:ident, $vec:ident) => {
38 use core::$t::MAX;
39 use core::$t::MIN_POSITIVE;
40
41 /// Works for vec2, vec3, vec4
42 fn from_x_y(x: $t, y: $t) -> $vec {
43 let mut v = $vec::ZERO;
44 v.x = x;
45 v.y = y;
46 v
47 }
48
49 #[test]
50 fn test_normalize() {
51 assert_eq!(from_x_y(-42.0, 0.0).normalize(), from_x_y(-1.0, 0.0));
52 assert_eq!(from_x_y(MAX.sqrt(), 0.0).normalize(), from_x_y(1.0, 0.0));
53 // assert_eq!(from_x_y(MAX, 0.0).normalize(), from_x_y(1.0, 0.0)); // normalize fails for huge vectors and returns zero
54
55 // We expect not to be able to normalize small numbers:
56 assert!(!from_x_y(0.0, 0.0).normalize().is_finite());
57 assert!(!from_x_y(MIN_POSITIVE, 0.0).normalize().is_finite());
58
59 // We expect not to be able to normalize non-finite vectors:
60 assert!(!from_x_y(INFINITY, 0.0).normalize().is_finite());
61 assert!(!from_x_y(NAN, 0.0).normalize().is_finite());
62 }
63
64 #[test]
65 fn test_try_normalize() {
66 assert_eq!(
67 from_x_y(-42.0, 0.0).try_normalize(),
68 Some(from_x_y(-1.0, 0.0))
69 );
70 assert_eq!(
71 from_x_y(MAX.sqrt(), 0.0).try_normalize(),
72 Some(from_x_y(1.0, 0.0))
73 );
74
75 // We expect `try_normalize` to return None when inputs are very small:
76 assert_eq!(from_x_y(0.0, 0.0).try_normalize(), None);
77 assert_eq!(from_x_y(MIN_POSITIVE, 0.0).try_normalize(), None);
78
79 // We expect `try_normalize` to return None when inputs are non-finite:
80 assert_eq!(from_x_y(INFINITY, 0.0).try_normalize(), None);
81 assert_eq!(from_x_y(NAN, 0.0).try_normalize(), None);
82
83 // We expect `try_normalize` to return None when inputs are very large:
84 assert_eq!(from_x_y(MAX, 0.0).try_normalize(), None);
85 assert_eq!(from_x_y(MAX, MAX).try_normalize(), None);
86 }
87
88 #[test]
89 fn test_normalize_or_zero() {
90 assert_eq!(
91 from_x_y(-42.0, 0.0).normalize_or_zero(),
92 from_x_y(-1.0, 0.0)
93 );
94 assert_eq!(
95 from_x_y(MAX.sqrt(), 0.0).normalize_or_zero(),
96 from_x_y(1.0, 0.0)
97 );
98
99 // We expect `normalize_or_zero` to return zero when inputs are very small:
100 assert_eq!(from_x_y(0.0, 0.0).normalize_or_zero(), $vec::ZERO);
101 assert_eq!(from_x_y(MIN_POSITIVE, 0.0).normalize_or_zero(), $vec::ZERO);
102
103 // We expect `normalize_or_zero` to return zero when inputs are non-finite:
104 assert_eq!(from_x_y(INFINITY, 0.0).normalize_or_zero(), $vec::ZERO);
105 assert_eq!(from_x_y(NAN, 0.0).normalize_or_zero(), $vec::ZERO);
106
107 // We expect `normalize_or_zero` to return zero when inputs are very large:
108 assert_eq!(from_x_y(MAX, 0.0).normalize_or_zero(), $vec::ZERO);
109 assert_eq!(from_x_y(MAX, MAX).normalize_or_zero(), $vec::ZERO);
110 }
111 };
112 }
113
114 /// Useful test vectors
115 #[macro_export]
116 macro_rules! vec3_float_test_vectors {
117 ($vec3:ident) => {
118 [
119 $vec3::X,
120 $vec3::Y,
121 $vec3::Z,
122 -$vec3::X,
123 -$vec3::Y,
124 -$vec3::Z,
125 $vec3::new(1.0, 1e-3, 0.0),
126 $vec3::new(1.0, 1e-4, 0.0),
127 $vec3::new(1.0, 1e-5, 0.0),
128 $vec3::new(1.0, 1e-6, 0.0),
129 $vec3::new(1.0, 1e-7, 0.0),
130 $vec3::new(1.0, 1e-14, 0.0),
131 $vec3::new(1.0, 1e-15, 0.0),
132 $vec3::new(1.0, 1e-16, 0.0),
133 $vec3::new(0.1, 0.2, 0.3),
134 $vec3::new(0.2, 0.3, 0.4),
135 $vec3::new(4.0, -5.0, 6.0),
136 $vec3::new(-2.0, 0.5, -1.0),
137 // Pathalogical cases from <https://graphics.pixar.com/library/OrthonormalB/paper.pdf>:
138 $vec3::new(0.00038527316, 0.00038460016, -0.99999988079),
139 $vec3::new(-0.00019813581, -0.00008946839, -0.99999988079),
140 ]
141 };
142 }