Debian Codegraph @

debian/missing-sources/glam-0.24.2/tests/support.rs

raw
1 #[path = "support/macros.rs"]
2 #[macro_use]
3 mod macros;
4
5 #[cfg(target_arch = "wasm32")]
6 wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
7
8 use glam::{
9 DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4, Mat2, Mat3, Mat3A, Mat4, Quat, Vec2, Vec3,
10 Vec3A, Vec4,
11 };
12
13 pub trait Deg {
14 fn to_radians(self) -> Self;
15 }
16
17 impl Deg for f32 {
18 fn to_radians(self) -> f32 {
19 f32::to_radians(self)
20 }
21 }
22
23 impl Deg for f64 {
24 fn to_radians(self) -> f64 {
25 f64::to_radians(self)
26 }
27 }
28
29 /// Helper function for migrating away from `glam::angle::deg`.
30 #[allow(dead_code)]
31 #[inline]
32 pub fn deg<T: Deg>(angle: T) -> T {
33 angle.to_radians()
34 }
35
36 /// Helper function for migrating away from `glam::angle::rad`.
37 #[allow(dead_code)]
38 #[inline]
39 pub fn rad<T>(angle: T) -> T {
40 angle
41 }
42
43 /// Trait used by the `assert_approx_eq` macro for floating point comparisons.
44 pub trait FloatCompare<Rhs: ?Sized = Self> {
45 /// Return true if the absolute difference between `self` and `other` is
46 /// less then or equal to `max_abs_diff`.
47 fn approx_eq(&self, other: &Rhs, max_abs_diff: f32) -> bool;
48 /// Returns the absolute difference of `self` and `other` which is printed
49 /// if `assert_approx_eq` fails.
50 fn abs_diff(&self, other: &Rhs) -> Rhs;
51 }
52
53 impl FloatCompare for f32 {
54 #[inline]
55 fn approx_eq(&self, other: &f32, max_abs_diff: f32) -> bool {
56 (self - other).abs() <= max_abs_diff
57 }
58 #[inline]
59 fn abs_diff(&self, other: &f32) -> f32 {
60 (self - other).abs()
61 }
62 }
63
64 impl FloatCompare for f64 {
65 #[inline]
66 fn approx_eq(&self, other: &f64, max_abs_diff: f32) -> bool {
67 (self - other).abs() <= max_abs_diff as f64
68 }
69 #[inline]
70 fn abs_diff(&self, other: &f64) -> f64 {
71 (self - other).abs()
72 }
73 }
74
75 impl FloatCompare for Mat2 {
76 #[inline]
77 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
78 self.abs_diff_eq(*other, max_abs_diff)
79 }
80 #[inline]
81 fn abs_diff(&self, other: &Self) -> Self {
82 Self::from_cols(
83 (self.x_axis - other.x_axis).abs(),
84 (self.y_axis - other.y_axis).abs(),
85 )
86 }
87 }
88
89 impl FloatCompare for DMat2 {
90 #[inline]
91 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
92 self.abs_diff_eq(*other, max_abs_diff as f64)
93 }
94 #[inline]
95 fn abs_diff(&self, other: &Self) -> Self {
96 Self::from_cols(
97 (self.x_axis - other.x_axis).abs(),
98 (self.y_axis - other.y_axis).abs(),
99 )
100 }
101 }
102
103 impl FloatCompare for Mat3 {
104 #[inline]
105 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
106 self.abs_diff_eq(*other, max_abs_diff)
107 }
108 #[inline]
109 fn abs_diff(&self, other: &Self) -> Self {
110 Self::from_cols(
111 (self.x_axis - other.x_axis).abs(),
112 (self.y_axis - other.y_axis).abs(),
113 (self.z_axis - other.z_axis).abs(),
114 )
115 }
116 }
117
118 impl FloatCompare for Mat3A {
119 #[inline]
120 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
121 self.abs_diff_eq(*other, max_abs_diff)
122 }
123 #[inline]
124 fn abs_diff(&self, other: &Self) -> Self {
125 Self::from_cols(
126 (self.x_axis - other.x_axis).abs(),
127 (self.y_axis - other.y_axis).abs(),
128 (self.z_axis - other.z_axis).abs(),
129 )
130 }
131 }
132
133 impl FloatCompare for DMat3 {
134 #[inline]
135 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
136 self.abs_diff_eq(*other, max_abs_diff as f64)
137 }
138 #[inline]
139 fn abs_diff(&self, other: &Self) -> Self {
140 Self::from_cols(
141 (self.x_axis - other.x_axis).abs(),
142 (self.y_axis - other.y_axis).abs(),
143 (self.z_axis - other.z_axis).abs(),
144 )
145 }
146 }
147
148 impl FloatCompare for DMat4 {
149 #[inline]
150 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
151 self.abs_diff_eq(*other, max_abs_diff as f64)
152 }
153 #[inline]
154 fn abs_diff(&self, other: &Self) -> Self {
155 Self::from_cols(
156 (self.x_axis - other.x_axis).abs(),
157 (self.y_axis - other.y_axis).abs(),
158 (self.z_axis - other.z_axis).abs(),
159 (self.w_axis - other.w_axis).abs(),
160 )
161 }
162 }
163
164 impl FloatCompare for Mat4 {
165 #[inline]
166 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
167 self.abs_diff_eq(*other, max_abs_diff)
168 }
169 #[inline]
170 fn abs_diff(&self, other: &Self) -> Self {
171 Self::from_cols(
172 (self.x_axis - other.x_axis).abs(),
173 (self.y_axis - other.y_axis).abs(),
174 (self.z_axis - other.z_axis).abs(),
175 (self.w_axis - other.w_axis).abs(),
176 )
177 }
178 }
179
180 impl FloatCompare for Quat {
181 #[inline]
182 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
183 self.abs_diff_eq(*other, max_abs_diff)
184 }
185 #[inline]
186 fn abs_diff(&self, other: &Self) -> Self {
187 let a: Vec4 = (*self).into();
188 let b: Vec4 = (*other).into();
189 Quat::from_vec4((a - b).abs())
190 }
191 }
192
193 impl FloatCompare for Vec2 {
194 #[inline]
195 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
196 self.abs_diff_eq(*other, max_abs_diff)
197 }
198 #[inline]
199 fn abs_diff(&self, other: &Self) -> Self {
200 (*self - *other).abs()
201 }
202 }
203
204 impl FloatCompare for Vec3 {
205 #[inline]
206 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
207 self.abs_diff_eq(*other, max_abs_diff)
208 }
209 #[inline]
210 fn abs_diff(&self, other: &Self) -> Self {
211 (*self - *other).abs()
212 }
213 }
214
215 impl FloatCompare for Vec3A {
216 #[inline]
217 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
218 self.abs_diff_eq(*other, max_abs_diff)
219 }
220 #[inline]
221 fn abs_diff(&self, other: &Self) -> Self {
222 (*self - *other).abs()
223 }
224 }
225
226 impl FloatCompare for Vec4 {
227 #[inline]
228 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
229 self.abs_diff_eq(*other, max_abs_diff)
230 }
231 #[inline]
232 fn abs_diff(&self, other: &Self) -> Self {
233 (*self - *other).abs()
234 }
235 }
236
237 impl FloatCompare for DQuat {
238 #[inline]
239 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
240 self.abs_diff_eq(*other, max_abs_diff as f64)
241 }
242 #[inline]
243 fn abs_diff(&self, other: &Self) -> Self {
244 let a: DVec4 = (*self).into();
245 let b: DVec4 = (*other).into();
246 DQuat::from_vec4((a - b).abs())
247 }
248 }
249
250 impl FloatCompare for DVec2 {
251 #[inline]
252 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
253 self.abs_diff_eq(*other, max_abs_diff as f64)
254 }
255 #[inline]
256 fn abs_diff(&self, other: &Self) -> Self {
257 (*self - *other).abs()
258 }
259 }
260
261 impl FloatCompare for DVec3 {
262 #[inline]
263 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
264 self.abs_diff_eq(*other, max_abs_diff as f64)
265 }
266 #[inline]
267 fn abs_diff(&self, other: &Self) -> Self {
268 (*self - *other).abs()
269 }
270 }
271
272 impl FloatCompare for DVec4 {
273 #[inline]
274 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
275 self.abs_diff_eq(*other, max_abs_diff as f64)
276 }
277 #[inline]
278 fn abs_diff(&self, other: &Self) -> Self {
279 (*self - *other).abs()
280 }
281 }