Debian Codegraph @

debian/missing-sources/glam-0.19.0/tests/support/mod.rs

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