Debian Codegraph @

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

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