Debian Codegraph @

debian/missing-sources/glam-0.23.0/tests/vec4.rs

raw
1 #![allow(clippy::excessive_precision)]
2
3 #[macro_use]
4 mod support;
5
6 macro_rules! impl_vec4_tests {
7 ($t:ident, $new:ident, $vec4:ident, $vec3:ident, $vec2:ident, $mask:ident) => {
8 glam_test!(test_const, {
9 const V0: $vec4 = $vec4::splat(1 as $t);
10 const V1: $vec4 = $vec4::new(1 as $t, 2 as $t, 3 as $t, 4 as $t);
11 const V2: $vec4 = $vec4::from_array([1 as $t, 2 as $t, 3 as $t, 4 as $t]);
12 assert_eq!([1 as $t, 1 as $t, 1 as $t, 1 as $t], *V0.as_ref());
13 assert_eq!([1 as $t, 2 as $t, 3 as $t, 4 as $t], *V1.as_ref());
14 assert_eq!([1 as $t, 2 as $t, 3 as $t, 4 as $t], *V2.as_ref());
15 });
16
17 glam_test!(test_vec4_consts, {
18 assert_eq!($vec4::ZERO, $new(0 as $t, 0 as $t, 0 as $t, 0 as $t));
19 assert_eq!($vec4::ONE, $new(1 as $t, 1 as $t, 1 as $t, 1 as $t));
20 assert_eq!($vec4::X, $new(1 as $t, 0 as $t, 0 as $t, 0 as $t));
21 assert_eq!($vec4::Y, $new(0 as $t, 1 as $t, 0 as $t, 0 as $t));
22 assert_eq!($vec4::Z, $new(0 as $t, 0 as $t, 1 as $t, 0 as $t));
23 assert_eq!($vec4::W, $new(0 as $t, 0 as $t, 0 as $t, 1 as $t));
24 });
25
26 glam_test!(test_new, {
27 let v = $new(1 as $t, 2 as $t, 3 as $t, 4 as $t);
28
29 assert_eq!(v.x, 1 as $t);
30 assert_eq!(v.y, 2 as $t);
31 assert_eq!(v.z, 3 as $t);
32 assert_eq!(v.w, 4 as $t);
33
34 let t = (1 as $t, 2 as $t, 3 as $t, 4 as $t);
35 let v = $vec4::from(t);
36 assert_eq!(t, v.into());
37
38 let a = [1 as $t, 2 as $t, 3 as $t, 4 as $t];
39 let v = $vec4::from(a);
40 let a1: [$t; 4] = v.into();
41 assert_eq!(a, a1);
42
43 assert_eq!(a, v.to_array());
44 assert_eq!(a, *v.as_ref());
45
46 let mut v2 = $vec4::default();
47 *v2.as_mut() = a;
48 assert_eq!(a, v2.to_array());
49
50 let v = $vec4::new(t.0, t.1, t.2, t.3);
51 assert_eq!(t, v.into());
52
53 assert_eq!($vec4::new(1 as $t, 0 as $t, 0 as $t, 0 as $t), $vec4::X);
54 assert_eq!($vec4::new(0 as $t, 1 as $t, 0 as $t, 0 as $t), $vec4::Y);
55 assert_eq!($vec4::new(0 as $t, 0 as $t, 1 as $t, 0 as $t), $vec4::Z);
56 assert_eq!($vec4::new(0 as $t, 0 as $t, 0 as $t, 1 as $t), $vec4::W);
57
58 assert_eq!(
59 v,
60 $vec4::from(($vec3::new(1 as $t, 2 as $t, 3 as $t), 4 as $t))
61 );
62
63 assert_eq!(
64 v,
65 $vec4::from((1 as $t, $vec3::new(2 as $t, 3 as $t, 4 as $t)))
66 );
67
68 assert_eq!(
69 v,
70 $vec4::from(($vec2::new(1 as $t, 2 as $t), 3 as $t, 4 as $t))
71 );
72 assert_eq!(
73 v,
74 $vec4::from(($vec2::new(1 as $t, 2 as $t), $vec2::new(3 as $t, 4 as $t)))
75 );
76 });
77
78 glam_test!(test_fmt, {
79 let a = $vec4::new(1 as $t, 2 as $t, 3 as $t, 4 as $t);
80 assert_eq!(
81 format!("{:?}", a),
82 format!(
83 "{}({:?}, {:?}, {:?}, {:?})",
84 stringify!($vec4),
85 a.x,
86 a.y,
87 a.z,
88 a.w
89 )
90 );
91 assert_eq!(
92 format!("{:#?}", a),
93 format!(
94 "{}(\n {:#?},\n {:#?},\n {:#?},\n {:#?},\n)",
95 stringify!($vec4),
96 a.x,
97 a.y,
98 a.z,
99 a.w
100 )
101 );
102 assert_eq!(format!("{}", a), "[1, 2, 3, 4]");
103 });
104
105 glam_test!(test_zero, {
106 let v = $vec4::ZERO;
107 assert_eq!((0 as $t, 0 as $t, 0 as $t, 0 as $t), v.into());
108 assert_eq!(v, $vec4::default());
109 });
110
111 glam_test!(test_splat, {
112 let v = $vec4::splat(1 as $t);
113 assert_eq!($vec4::ONE, v);
114 });
115
116 glam_test!(test_accessors, {
117 let mut a = $vec4::ZERO;
118 a.x = 1 as $t;
119 a.y = 2 as $t;
120 a.z = 3 as $t;
121 a.w = 4 as $t;
122 assert_eq!(1 as $t, a.x);
123 assert_eq!(2 as $t, a.y);
124 assert_eq!(3 as $t, a.z);
125 assert_eq!(4 as $t, a.w);
126 assert_eq!((1 as $t, 2 as $t, 3 as $t, 4 as $t), a.into());
127
128 let mut a = $vec4::ZERO;
129 a[0] = 1 as $t;
130 a[1] = 2 as $t;
131 a[2] = 3 as $t;
132 a[3] = 4 as $t;
133 assert_eq!(1 as $t, a[0]);
134 assert_eq!(2 as $t, a[1]);
135 assert_eq!(3 as $t, a[2]);
136 assert_eq!(4 as $t, a[3]);
137 assert_eq!((1 as $t, 2 as $t, 3 as $t, 4 as $t), a.into());
138 });
139
140 glam_test!(test_dot_unsigned, {
141 let x = $new(1 as $t, 0 as $t, 0 as $t, 0 as $t);
142 let y = $new(0 as $t, 1 as $t, 0 as $t, 0 as $t);
143 let z = $new(0 as $t, 0 as $t, 1 as $t, 0 as $t);
144 let w = $new(0 as $t, 0 as $t, 0 as $t, 1 as $t);
145 assert_eq!(1 as $t, x.dot(x));
146 assert_eq!(0 as $t, x.dot(y));
147 assert_eq!(0 as $t, y.dot(z));
148 assert_eq!(0 as $t, z.dot(w));
149 });
150
151 glam_test!(test_ops, {
152 let a = $new(2 as $t, 4 as $t, 8 as $t, 16 as $t);
153 assert_eq!($new(4 as $t, 8 as $t, 16 as $t, 32 as $t), a + a);
154 assert_eq!($new(2 as $t, 4 as $t, 8 as $t, 16 as $t), 0 as $t + a);
155 assert_eq!($new(0 as $t, 0 as $t, 0 as $t, 0 as $t), a - a);
156 assert_eq!($new(14 as $t, 12 as $t, 8 as $t, 0 as $t), 16 as $t - a);
157 assert_eq!($new(4 as $t, 16 as $t, 64 as $t, 256 as $t), a * a);
158 assert_eq!($new(4 as $t, 8 as $t, 16 as $t, 32 as $t), a * 2 as $t);
159 assert_eq!($new(4 as $t, 8 as $t, 16 as $t, 32 as $t), 2 as $t * a);
160 assert_eq!($new(1 as $t, 1 as $t, 1 as $t, 1 as $t), a / a);
161 assert_eq!($new(1 as $t, 2 as $t, 4 as $t, 8 as $t), a / 2 as $t);
162 assert_eq!($new(8 as $t, 4 as $t, 2 as $t, 1 as $t), 16 as $t / a);
163 assert_eq!($new(0 as $t, 0 as $t, 0 as $t, 0 as $t), a % a);
164 assert_eq!($new(0 as $t, 1 as $t, 1 as $t, 1 as $t), a % (a - 1 as $t));
165 assert_eq!($new(0 as $t, 0 as $t, 0 as $t, 0 as $t), a % 1 as $t);
166 assert_eq!($new(2 as $t, 1 as $t, 2 as $t, 1 as $t), a % 3 as $t);
167 assert_eq!($new(1 as $t, 1 as $t, 1 as $t, 1 as $t), 17 as $t % a);
168 assert_eq!($new(2 as $t, 4 as $t, 0 as $t, 0 as $t), a % 8 as $t);
169 });
170
171 glam_test!(test_assign_ops, {
172 let a = $new(1 as $t, 2 as $t, 3 as $t, 4 as $t);
173 let mut b = a;
174
175 b += 2 as $t;
176 assert_eq!($new(3 as $t, 4 as $t, 5 as $t, 6 as $t), b);
177 b -= 2 as $t;
178 assert_eq!($new(1 as $t, 2 as $t, 3 as $t, 4 as $t), b);
179 b *= 2 as $t;
180 assert_eq!($new(2 as $t, 4 as $t, 6 as $t, 8 as $t), b);
181 b /= 2 as $t;
182 assert_eq!($new(1 as $t, 2 as $t, 3 as $t, 4 as $t), b);
183 b %= 2 as $t;
184 assert_eq!($new(1 as $t, 0 as $t, 1 as $t, 0 as $t), b);
185
186 b = a;
187 b += a;
188 assert_eq!($new(2 as $t, 4 as $t, 6 as $t, 8 as $t), b);
189 b -= a;
190 assert_eq!($new(1 as $t, 2 as $t, 3 as $t, 4 as $t), b);
191 b *= a;
192 assert_eq!($new(1 as $t, 4 as $t, 9 as $t, 16 as $t), b);
193 b /= a;
194 assert_eq!($new(1 as $t, 2 as $t, 3 as $t, 4 as $t), b);
195 b *= 2 as $t;
196 assert_eq!($new(2 as $t, 4 as $t, 6 as $t, 8 as $t), b);
197 b /= 2 as $t;
198 assert_eq!($new(1 as $t, 2 as $t, 3 as $t, 4 as $t), b);
199 b %= (b + 1 as $t);
200 assert_eq!($new(1 as $t, 2 as $t, 3 as $t, 4 as $t), b);
201 b %= b;
202 assert_eq!($new(0 as $t, 0 as $t, 0 as $t, 0 as $t), b);
203 });
204
205 glam_test!(test_min_max, {
206 let a = $new(4 as $t, 6 as $t, 2 as $t, 8 as $t);
207 let b = $new(5 as $t, 3 as $t, 7 as $t, 1 as $t);
208 assert_eq!((4 as $t, 3 as $t, 2 as $t, 1 as $t), a.min(b).into());
209 assert_eq!((4 as $t, 3 as $t, 2 as $t, 1 as $t), b.min(a).into());
210 assert_eq!((5 as $t, 6 as $t, 7 as $t, 8 as $t), a.max(b).into());
211 assert_eq!((5 as $t, 6 as $t, 7 as $t, 8 as $t), b.max(a).into());
212 });
213
214 glam_test!(test_clamp, {
215 fn vec(x: i32, y: i32, z: i32, w: i32) -> $vec4 {
216 $vec4::new(x as $t, y as $t, z as $t, w as $t)
217 }
218 let min = vec(1, 1, 3, 3);
219 let max = vec(6, 6, 8, 8);
220 assert_eq!(vec(0, 0, 0, 0).clamp(min, max), vec(1, 1, 3, 3));
221 assert_eq!(vec(2, 2, 2, 2).clamp(min, max), vec(2, 2, 3, 3));
222 assert_eq!(vec(4, 4, 5, 5).clamp(min, max), vec(4, 4, 5, 5));
223 assert_eq!(vec(6, 6, 6, 6).clamp(min, max), vec(6, 6, 6, 6));
224 assert_eq!(vec(7, 7, 7, 7).clamp(min, max), vec(6, 6, 7, 7));
225 assert_eq!(vec(9, 9, 9, 9).clamp(min, max), vec(6, 6, 8, 8));
226
227 should_glam_assert!({ $vec4::clamp($vec4::ZERO, $vec4::ONE, $vec4::ZERO) });
228 });
229
230 glam_test!(test_hmin_hmax, {
231 assert_eq!(
232 1 as $t,
233 $new(1 as $t, 2 as $t, 3 as $t, 4 as $t).min_element()
234 );
235 assert_eq!(
236 1 as $t,
237 $new(4 as $t, 1 as $t, 2 as $t, 3 as $t).min_element()
238 );
239 assert_eq!(
240 1 as $t,
241 $new(3 as $t, 4 as $t, 1 as $t, 2 as $t).min_element()
242 );
243 assert_eq!(
244 1 as $t,
245 $new(2 as $t, 3 as $t, 4 as $t, 1 as $t).min_element()
246 );
247 assert_eq!(
248 4 as $t,
249 $new(1 as $t, 2 as $t, 3 as $t, 4 as $t).max_element()
250 );
251 assert_eq!(
252 4 as $t,
253 $new(4 as $t, 1 as $t, 2 as $t, 3 as $t).max_element()
254 );
255 assert_eq!(
256 4 as $t,
257 $new(3 as $t, 4 as $t, 1 as $t, 2 as $t).max_element()
258 );
259 assert_eq!(
260 4 as $t,
261 $new(2 as $t, 3 as $t, 4 as $t, 1 as $t).max_element()
262 );
263 assert_eq!(
264 3 as $t,
265 $new(1 as $t, 2 as $t, 3 as $t, 4 as $t)
266 .truncate()
267 .max_element()
268 );
269 assert_eq!(
270 2 as $t,
271 $new(4 as $t, 3 as $t, 2 as $t, 1 as $t)
272 .truncate()
273 .min_element()
274 );
275 });
276
277 glam_test!(test_eq, {
278 let a = $new(1 as $t, 1 as $t, 1 as $t, 1 as $t);
279 let b = $new(1 as $t, 2 as $t, 3 as $t, 4 as $t);
280 assert!(a.cmpeq(a).all());
281 assert!(b.cmpeq(b).all());
282 assert!(a.cmpne(b).any());
283 assert!(b.cmpne(a).any());
284 assert!(b.cmpeq(a).any());
285 });
286
287 glam_test!(test_cmp, {
288 assert!(!$mask::default().any());
289 assert!(!$mask::default().all());
290 assert_eq!($mask::default().bitmask(), 0x0);
291 let a = $new(1 as $t, 1 as $t, 1 as $t, 1 as $t);
292 let b = $new(2 as $t, 2 as $t, 2 as $t, 2 as $t);
293 let c = $new(1 as $t, 1 as $t, 2 as $t, 2 as $t);
294 let d = $new(2 as $t, 1 as $t, 1 as $t, 2 as $t);
295 assert_eq!(a.cmplt(a).bitmask(), 0x0);
296 assert_eq!(a.cmplt(b).bitmask(), 0xf);
297 assert_eq!(a.cmplt(c).bitmask(), 0xc);
298 assert_eq!(c.cmple(a).bitmask(), 0x3);
299 assert_eq!(a.cmplt(d).bitmask(), 0x9);
300 assert!(a.cmplt(b).all());
301 assert!(a.cmplt(c).any());
302 assert!(a.cmple(b).all());
303 assert!(a.cmple(a).all());
304 assert!(b.cmpgt(a).all());
305 assert!(b.cmpge(a).all());
306 assert!(b.cmpge(b).all());
307 assert!(!(a.cmpge(c).all()));
308 assert!(c.cmple(c).all());
309 assert!(c.cmpge(c).all());
310 assert!(a.cmpeq(a).all());
311 assert!(!a.cmpeq(b).all());
312 assert!(a.cmpeq(c).any());
313 assert!(!a.cmpne(a).all());
314 assert!(a.cmpne(b).all());
315 assert!(a.cmpne(c).any());
316 assert!(a == a);
317 });
318
319 glam_test!(test_slice, {
320 let a = [1 as $t, 2 as $t, 3 as $t, 4 as $t];
321 let b = $vec4::from_slice(&a);
322 let c: [$t; 4] = b.into();
323 assert_eq!(a, c);
324 let mut d = [0 as $t, 0 as $t, 0 as $t, 0 as $t];
325 b.write_to_slice(&mut d[..]);
326 assert_eq!(a, d);
327
328 should_panic!({ $vec4::ONE.write_to_slice(&mut [0 as $t; 3]) });
329 should_panic!({ $vec4::from_slice(&[0 as $t; 3]) });
330 });
331
332 glam_test!(test_mask_into_array_u32, {
333 assert_eq!(
334 Into::<[u32; 4]>::into($mask::new(false, false, false, false)),
335 [0, 0, 0, 0]
336 );
337 assert_eq!(
338 Into::<[u32; 4]>::into($mask::new(false, false, true, true)),
339 [0, 0, !0, !0]
340 );
341 assert_eq!(
342 Into::<[u32; 4]>::into($mask::new(true, true, false, false)),
343 [!0, !0, 0, 0]
344 );
345 assert_eq!(
346 Into::<[u32; 4]>::into($mask::new(false, true, false, true)),
347 [0, !0, 0, !0]
348 );
349 assert_eq!(
350 Into::<[u32; 4]>::into($mask::new(true, false, true, false)),
351 [!0, 0, !0, 0]
352 );
353 assert_eq!(
354 Into::<[u32; 4]>::into($mask::new(true, true, true, true)),
355 [!0, !0, !0, !0]
356 );
357 });
358
359 glam_test!(test_mask_into_array_bool, {
360 assert_eq!(
361 Into::<[bool; 4]>::into($mask::new(false, false, false, false)),
362 [false, false, false, false]
363 );
364 assert_eq!(
365 Into::<[bool; 4]>::into($mask::new(false, false, true, true)),
366 [false, false, true, true]
367 );
368 assert_eq!(
369 Into::<[bool; 4]>::into($mask::new(true, true, false, false)),
370 [true, true, false, false]
371 );
372 assert_eq!(
373 Into::<[bool; 4]>::into($mask::new(false, true, false, true)),
374 [false, true, false, true]
375 );
376 assert_eq!(
377 Into::<[bool; 4]>::into($mask::new(true, false, true, false)),
378 [true, false, true, false]
379 );
380 assert_eq!(
381 Into::<[bool; 4]>::into($mask::new(true, true, true, true)),
382 [true, true, true, true]
383 );
384 });
385
386 glam_test!(test_mask_splat, {
387 assert_eq!($mask::splat(false), $mask::new(false, false, false, false));
388 assert_eq!($mask::splat(true), $mask::new(true, true, true, true));
389 });
390
391 glam_test!(test_mask_bitmask, {
392 assert_eq!($mask::new(false, false, false, false).bitmask(), 0b0000);
393 assert_eq!($mask::new(false, false, true, true).bitmask(), 0b1100);
394 assert_eq!($mask::new(true, true, false, false).bitmask(), 0b0011);
395 assert_eq!($mask::new(false, true, false, true).bitmask(), 0b1010);
396 assert_eq!($mask::new(true, false, true, false).bitmask(), 0b0101);
397 assert_eq!($mask::new(true, true, true, true).bitmask(), 0b1111);
398 });
399
400 glam_test!(test_mask_any, {
401 assert_eq!($mask::new(false, false, false, false).any(), false);
402 assert_eq!($mask::new(true, false, false, false).any(), true);
403 assert_eq!($mask::new(false, true, false, false).any(), true);
404 assert_eq!($mask::new(false, false, true, false).any(), true);
405 assert_eq!($mask::new(false, false, false, true).any(), true);
406 });
407
408 glam_test!(test_mask_all, {
409 assert_eq!($mask::new(true, true, true, true).all(), true);
410 assert_eq!($mask::new(false, true, true, true).all(), false);
411 assert_eq!($mask::new(true, false, true, true).all(), false);
412 assert_eq!($mask::new(true, true, false, true).all(), false);
413 assert_eq!($mask::new(true, true, true, false).all(), false);
414 });
415
416 glam_test!(test_mask_select, {
417 let a = $vec4::new(1 as $t, 2 as $t, 3 as $t, 4 as $t);
418 let b = $vec4::new(5 as $t, 6 as $t, 7 as $t, 8 as $t);
419 assert_eq!(
420 $vec4::select($mask::new(true, true, true, true), a, b),
421 $vec4::new(1 as $t, 2 as $t, 3 as $t, 4 as $t),
422 );
423 assert_eq!(
424 $vec4::select($mask::new(true, false, true, false), a, b),
425 $vec4::new(1 as $t, 6 as $t, 3 as $t, 8 as $t),
426 );
427 assert_eq!(
428 $vec4::select($mask::new(false, true, false, true), a, b),
429 $vec4::new(5 as $t, 2 as $t, 7 as $t, 4 as $t),
430 );
431 assert_eq!(
432 $vec4::select($mask::new(false, false, false, false), a, b),
433 $vec4::new(5 as $t, 6 as $t, 7 as $t, 8 as $t),
434 );
435 });
436
437 glam_test!(test_mask_and, {
438 assert_eq!(
439 ($mask::new(false, false, false, false) & $mask::new(false, false, false, false))
440 .bitmask(),
441 0b0000,
442 );
443 assert_eq!(
444 ($mask::new(true, true, true, true) & $mask::new(true, true, true, true)).bitmask(),
445 0b1111,
446 );
447 assert_eq!(
448 ($mask::new(true, false, true, false) & $mask::new(false, true, false, true))
449 .bitmask(),
450 0b0000,
451 );
452 assert_eq!(
453 ($mask::new(true, false, true, true) & $mask::new(true, true, true, false))
454 .bitmask(),
455 0b0101,
456 );
457
458 let mut mask = $mask::new(true, true, false, false);
459 mask &= $mask::new(true, false, true, false);
460 assert_eq!(mask.bitmask(), 0b0001);
461 });
462
463 glam_test!(test_mask_or, {
464 assert_eq!(
465 ($mask::new(false, false, false, false) | $mask::new(false, false, false, false))
466 .bitmask(),
467 0b0000,
468 );
469 assert_eq!(
470 ($mask::new(true, true, true, true) | $mask::new(true, true, true, true)).bitmask(),
471 0b1111,
472 );
473 assert_eq!(
474 ($mask::new(true, false, true, false) | $mask::new(false, true, false, true))
475 .bitmask(),
476 0b1111,
477 );
478 assert_eq!(
479 ($mask::new(true, false, true, false) | $mask::new(true, false, true, false))
480 .bitmask(),
481 0b0101,
482 );
483
484 let mut mask = $mask::new(true, true, false, false);
485 mask |= $mask::new(true, false, true, false);
486 assert_eq!(mask.bitmask(), 0b0111);
487 });
488
489 glam_test!(test_mask_xor, {
490 assert_eq!(
491 ($mask::new(false, false, false, false) ^ $mask::new(false, false, false, false))
492 .bitmask(),
493 0b0000,
494 );
495 assert_eq!(
496 ($mask::new(true, true, true, true) ^ $mask::new(true, true, true, true)).bitmask(),
497 0b0000,
498 );
499 assert_eq!(
500 ($mask::new(true, false, true, false) ^ $mask::new(false, true, false, true))
501 .bitmask(),
502 0b1111,
503 );
504 assert_eq!(
505 ($mask::new(true, false, true, false) ^ $mask::new(true, false, true, false))
506 .bitmask(),
507 0b0000,
508 );
509
510 let mut mask = $mask::new(true, true, false, false);
511 mask ^= $mask::new(true, false, true, false);
512 assert_eq!(mask.bitmask(), 0b0110);
513 });
514
515 glam_test!(test_mask_not, {
516 assert_eq!((!$mask::new(false, false, false, false)).bitmask(), 0b1111);
517 assert_eq!((!$mask::new(true, true, true, true)).bitmask(), 0b0000);
518 assert_eq!((!$mask::new(true, false, true, false)).bitmask(), 0b1010);
519 assert_eq!((!$mask::new(false, true, false, true)).bitmask(), 0b0101);
520 });
521
522 glam_test!(test_mask_fmt, {
523 let a = $mask::new(true, false, true, false);
524
525 assert_eq!(format!("{}", a), "[true, false, true, false]");
526 assert_eq!(
527 format!("{:?}", a),
528 format!("{}(0xffffffff, 0x0, 0xffffffff, 0x0)", stringify!($mask))
529 );
530 });
531
532 glam_test!(test_mask_eq, {
533 let a = $mask::new(true, false, true, false);
534 let b = $mask::new(true, false, true, false);
535 let c = $mask::new(false, true, true, false);
536
537 assert_eq!(a, b);
538 assert_eq!(b, a);
539 assert_ne!(a, c);
540 assert_ne!(b, c);
541 });
542
543 glam_test!(test_mask_hash, {
544 use std::collections::hash_map::DefaultHasher;
545 use std::hash::Hash;
546 use std::hash::Hasher;
547
548 let a = $mask::new(true, false, true, false);
549 let b = $mask::new(true, false, true, false);
550 let c = $mask::new(false, true, true, false);
551
552 let mut hasher = DefaultHasher::new();
553 a.hash(&mut hasher);
554 let a_hashed = hasher.finish();
555
556 let mut hasher = DefaultHasher::new();
557 b.hash(&mut hasher);
558 let b_hashed = hasher.finish();
559
560 let mut hasher = DefaultHasher::new();
561 c.hash(&mut hasher);
562 let c_hashed = hasher.finish();
563
564 assert_eq!(a, b);
565 assert_eq!(a_hashed, b_hashed);
566 assert_ne!(a, c);
567 assert_ne!(a_hashed, c_hashed);
568 });
569
570 glam_test!(test_to_from_slice, {
571 let v = $vec4::new(1 as $t, 2 as $t, 3 as $t, 4 as $t);
572 let mut a = [0 as $t, 0 as $t, 0 as $t, 0 as $t];
573 v.write_to_slice(&mut a);
574 assert_eq!(v, $vec4::from_slice(&a));
575 });
576
577 glam_test!(test_sum, {
578 let one = $vec4::ONE;
579 assert_eq!(vec![one, one].iter().sum::<$vec4>(), one + one);
580 assert_eq!(vec![one, one].into_iter().sum::<$vec4>(), one + one);
581 });
582
583 glam_test!(test_product, {
584 let two = $vec4::new(2 as $t, 2 as $t, 2 as $t, 2 as $t);
585 assert_eq!(vec![two, two].iter().product::<$vec4>(), two * two);
586 assert_eq!(vec![two, two].into_iter().product::<$vec4>(), two * two);
587 });
588 };
589 }
590
591 macro_rules! impl_vec4_signed_tests {
592 ($t:ident, $new:ident, $vec4:ident, $vec3:ident, $vec2:ident, $mask:ident) => {
593 impl_vec4_tests!($t, $new, $vec4, $vec3, $vec2, $mask);
594
595 glam_test!(test_neg, {
596 let a = $new(1 as $t, 2 as $t, 3 as $t, 4 as $t);
597 assert_eq!((-1 as $t, -2 as $t, -3 as $t, -4 as $t), (-a).into());
598 assert_eq!(
599 $new(-0.0 as $t, -0.0 as $t, -0.0 as $t, -0.0 as $t),
600 -$new(0.0 as $t, 0.0 as $t, 0.0 as $t, 0.0 as $t)
601 );
602 assert_eq!(
603 $new(0.0 as $t, -0.0 as $t, -0.0 as $t, -0.0 as $t),
604 -$new(-0.0 as $t, 0.0 as $t, 0.0 as $t, 0.0 as $t)
605 );
606 });
607
608 glam_test!(test_dot_signed, {
609 let x = $new(1 as $t, 0 as $t, 0 as $t, 0 as $t);
610 let y = $new(0 as $t, 1 as $t, 0 as $t, 0 as $t);
611 let z = $new(0 as $t, 0 as $t, 1 as $t, 0 as $t);
612 let w = $new(0 as $t, 0 as $t, 0 as $t, 1 as $t);
613 assert_eq!(1 as $t, x.dot(x));
614 assert_eq!(0 as $t, x.dot(y));
615 assert_eq!(0 as $t, x.dot(-z));
616 assert_eq!(-1 as $t, w.dot(-w));
617 });
618 };
619 }
620
621 macro_rules! impl_vec4_eq_hash_tests {
622 ($t:ident, $new:ident) => {
623 glam_test!(test_ve2_hash, {
624 use std::collections::hash_map::DefaultHasher;
625 use std::hash::Hash;
626 use std::hash::Hasher;
627
628 let a = $new(1 as $t, 2 as $t, 3 as $t, 4 as $t);
629 let b = $new(1 as $t, 2 as $t, 3 as $t, 4 as $t);
630 let c = $new(3 as $t, 2 as $t, 1 as $t, 4 as $t);
631
632 let mut hasher = DefaultHasher::new();
633 a.hash(&mut hasher);
634 let a_hashed = hasher.finish();
635
636 let mut hasher = DefaultHasher::new();
637 b.hash(&mut hasher);
638 let b_hashed = hasher.finish();
639
640 let mut hasher = DefaultHasher::new();
641 c.hash(&mut hasher);
642 let c_hashed = hasher.finish();
643
644 assert_eq!(a, b);
645 assert_eq!(a_hashed, b_hashed);
646 assert_ne!(a, c);
647 assert_ne!(a_hashed, c_hashed);
648 });
649 };
650 }
651
652 macro_rules! impl_vec4_float_tests {
653 ($t:ident, $new:ident, $vec4:ident, $vec3:ident, $vec2:ident, $mask:ident) => {
654 impl_vec4_signed_tests!($t, $new, $vec4, $vec3, $vec2, $mask);
655 impl_vec_float_normalize_tests!($t, $vec4);
656
657 use core::$t::INFINITY;
658 use core::$t::NAN;
659 use core::$t::NEG_INFINITY;
660
661 glam_test!(test_vec4_nan, {
662 assert!($vec4::NAN.is_nan());
663 assert!(!$vec4::NAN.is_finite());
664 });
665
666 glam_test!(test_funcs, {
667 let x = $new(1.0, 0.0, 0.0, 0.0);
668 let y = $new(0.0, 1.0, 0.0, 0.0);
669 let z = $new(0.0, 0.0, 1.0, 0.0);
670 let w = $new(0.0, 0.0, 0.0, 1.0);
671 assert_eq!(4.0, (2.0 * x).length_squared());
672 assert_eq!(9.0, (-3.0 * y).length_squared());
673 assert_eq!(16.0, (4.0 * z).length_squared());
674 assert_eq!(64.0, (8.0 * w).length_squared());
675 assert_eq!(2.0, (-2.0 * x).length());
676 assert_eq!(3.0, (3.0 * y).length());
677 assert_eq!(4.0, (-4.0 * z).length());
678 assert_eq!(5.0, (-5.0 * w).length());
679 assert_eq!(2.0, x.distance_squared(y));
680 assert_eq!(13.0, (2.0 * x).distance_squared(-3.0 * z));
681 assert_eq!((2.0 as $t).sqrt(), w.distance(y));
682 assert_eq!(5.0, (3.0 * x).distance(-4.0 * y));
683 assert_eq!(13.0, (-5.0 * w).distance(12.0 * y));
684 assert_eq!(x, (2.0 * x).normalize());
685 assert_eq!(
686 1.0 * 5.0 + 2.0 * 6.0 + 3.0 * 7.0 + 4.0 * 8.0,
687 $new(1.0, 2.0, 3.0, 4.0).dot($new(5.0, 6.0, 7.0, 8.0))
688 );
689 assert_eq!(
690 $new(28.0, 28.0, 28.0, 28.0),
691 $new(0.0, 5.0, 3.0, 6.0).dot_into_vec($new(7.0, 2.0, 4.0, 1.0))
692 );
693 assert_eq!(
694 2.0 * 2.0 + 3.0 * 3.0 + 4.0 * 4.0 + 5.0 * 5.0,
695 $new(2.0, 3.0, 4.0, 5.0).length_squared()
696 );
697 assert_eq!(
698 (2.0 as $t * 2.0 + 3.0 * 3.0 + 4.0 * 4.0 + 5.0 * 5.0).sqrt(),
699 $new(2.0, 3.0, 4.0, 5.0).length()
700 );
701 assert_eq!(
702 1.0 / (2.0 as $t * 2.0 + 3.0 * 3.0 + 4.0 * 4.0 + 5.0 * 5.0).sqrt(),
703 $new(2.0, 3.0, 4.0, 5.0).length_recip()
704 );
705 assert!($new(2.0, 3.0, 4.0, 5.0).normalize().is_normalized());
706 assert_approx_eq!(
707 $new(2.0, 3.0, 4.0, 5.0)
708 / (2.0 as $t * 2.0 + 3.0 * 3.0 + 4.0 * 4.0 + 5.0 * 5.0).sqrt(),
709 $new(2.0, 3.0, 4.0, 5.0).normalize()
710 );
711 assert_eq!(
712 $new(0.5, 0.25, 0.125, 0.0625),
713 $new(2.0, 4.0, 8.0, 16.0).recip()
714 );
715 });
716
717 glam_test!(test_project_reject, {
718 assert_eq!(
719 $new(0.0, 0.0, 0.0, 1.0),
720 $new(0.0, 1.0, 0.0, 1.0).project_onto($new(0.0, 0.0, 0.0, 2.0))
721 );
722 assert_eq!(
723 $new(0.0, 1.0, 0.0, 0.0),
724 $new(0.0, 1.0, 0.0, 1.0).reject_from($new(0.0, 0.0, 0.0, 2.0))
725 );
726 assert_eq!(
727 $new(0.0, 0.0, 0.0, 1.0),
728 $new(0.0, 1.0, 0.0, 1.0).project_onto_normalized($new(0.0, 0.0, 0.0, 1.0))
729 );
730 assert_eq!(
731 $new(0.0, 1.0, 0.0, 0.0),
732 $new(0.0, 1.0, 0.0, 1.0).reject_from_normalized($new(0.0, 0.0, 0.0, 1.0))
733 );
734 should_glam_assert!({ $vec4::ONE.project_onto($vec4::ZERO) });
735 should_glam_assert!({ $vec4::ONE.reject_from($vec4::ZERO) });
736 should_glam_assert!({ $vec4::ONE.project_onto_normalized($vec4::ONE) });
737 should_glam_assert!({ $vec4::ONE.reject_from_normalized($vec4::ONE) });
738 });
739
740 glam_test!(test_signum, {
741 assert_eq!($vec4::ZERO.signum(), $vec4::ONE);
742 assert_eq!((-$vec4::ZERO).signum(), -$vec4::ONE);
743 assert_eq!($vec4::ONE.signum(), $vec4::ONE);
744 assert_eq!((-$vec4::ONE).signum(), -$vec4::ONE);
745 assert_eq!($vec4::splat(INFINITY).signum(), $vec4::ONE);
746 assert_eq!($vec4::splat(NEG_INFINITY).signum(), -$vec4::ONE);
747 assert!($vec4::splat(NAN).signum().is_nan_mask().all());
748 });
749
750 glam_test!(test_copysign, {
751 assert_eq!($vec4::ZERO.copysign(-$vec4::ZERO), -$vec4::ZERO);
752 assert_eq!((-$vec4::ZERO).copysign(-$vec4::ZERO), -$vec4::ZERO);
753 assert_eq!($vec4::ZERO.copysign($vec4::ZERO), $vec4::ZERO);
754 assert_eq!((-$vec4::ZERO).copysign($vec4::ZERO), $vec4::ZERO);
755 assert_eq!($vec4::ONE.copysign(-$vec4::ZERO), -$vec4::ONE);
756 assert_eq!((-$vec4::ONE).copysign(-$vec4::ZERO), -$vec4::ONE);
757 assert_eq!($vec4::ONE.copysign($vec4::ZERO), $vec4::ONE);
758 assert_eq!((-$vec4::ONE).copysign($vec4::ZERO), $vec4::ONE);
759 assert_eq!($vec4::ZERO.copysign(-$vec4::ONE), -$vec4::ZERO);
760 assert_eq!((-$vec4::ZERO).copysign(-$vec4::ONE), -$vec4::ZERO);
761 assert_eq!($vec4::ZERO.copysign($vec4::ONE), $vec4::ZERO);
762 assert_eq!((-$vec4::ZERO).copysign($vec4::ONE), $vec4::ZERO);
763 assert_eq!($vec4::ONE.copysign(-$vec4::ONE), -$vec4::ONE);
764 assert_eq!((-$vec4::ONE).copysign(-$vec4::ONE), -$vec4::ONE);
765 assert_eq!($vec4::ONE.copysign($vec4::ONE), $vec4::ONE);
766 assert_eq!((-$vec4::ONE).copysign($vec4::ONE), $vec4::ONE);
767 assert_eq!(
768 $vec4::splat(INFINITY).copysign($vec4::ONE),
769 $vec4::splat(INFINITY)
770 );
771 assert_eq!(
772 $vec4::splat(INFINITY).copysign(-$vec4::ONE),
773 $vec4::splat(NEG_INFINITY)
774 );
775 assert_eq!(
776 $vec4::splat(NEG_INFINITY).copysign($vec4::ONE),
777 $vec4::splat(INFINITY)
778 );
779 assert_eq!(
780 $vec4::splat(NEG_INFINITY).copysign(-$vec4::ONE),
781 $vec4::splat(NEG_INFINITY)
782 );
783 assert!($vec4::splat(NAN).copysign($vec4::ONE).is_nan_mask().all());
784 assert!($vec4::splat(NAN).copysign(-$vec4::ONE).is_nan_mask().all());
785 });
786
787 glam_test!(test_is_negative_bitmask, {
788 assert_eq!($vec4::ZERO.is_negative_bitmask(), 0b0000);
789 assert_eq!((-$vec4::ZERO).is_negative_bitmask(), 0b1111);
790 assert_eq!($vec4::ONE.is_negative_bitmask(), 0b0000);
791 assert_eq!((-$vec4::ONE).is_negative_bitmask(), 0b1111);
792 assert_eq!(
793 $vec4::new(-0.1, 0.2, 0.3, -0.4).is_negative_bitmask(),
794 0b1001
795 );
796 assert_eq!(
797 $vec4::new(0.8, 0.3, 0.1, -0.0).is_negative_bitmask(),
798 0b1000
799 );
800 assert_eq!(
801 $vec4::new(0.1, 0.5, -0.3, 0.7).is_negative_bitmask(),
802 0b0100
803 );
804 assert_eq!(
805 $vec4::new(0.3, -0.4, 0.1, 0.6).is_negative_bitmask(),
806 0b0010
807 );
808 assert_eq!(
809 $vec4::new(0.2, -0.6, 0.5, -0.3).is_negative_bitmask(),
810 0b1010
811 );
812 });
813
814 glam_test!(test_abs, {
815 assert_eq!($vec4::ZERO.abs(), $vec4::ZERO);
816 assert_eq!($vec4::ONE.abs(), $vec4::ONE);
817 assert_eq!((-$vec4::ONE).abs(), $vec4::ONE);
818 });
819
820 glam_test!(test_round, {
821 assert_eq!($vec4::new(1.35, 0.0, 0.0, 0.0).round().x, 1.0);
822 assert_eq!($vec4::new(0.0, 1.5, 0.0, 0.0).round().y, 2.0);
823 assert_eq!($vec4::new(0.0, 0.0, -15.5, 0.0).round().z, -16.0);
824 assert_eq!($vec4::new(0.0, 0.0, 0.0, 0.0).round().z, 0.0);
825 assert_eq!($vec4::new(0.0, 21.1, 0.0, 0.0).round().y, 21.0);
826 assert_eq!($vec4::new(0.0, 0.0, 0.0, 11.123).round().w, 11.0);
827 assert_eq!($vec4::new(0.0, 0.0, 11.501, 0.0).round().z, 12.0);
828 assert_eq!(
829 $vec4::new(NEG_INFINITY, INFINITY, 1.0, -1.0).round(),
830 $vec4::new(NEG_INFINITY, INFINITY, 1.0, -1.0)
831 );
832 assert!($vec4::new(NAN, 0.0, 0.0, 1.0).round().x.is_nan());
833 });
834
835 glam_test!(test_floor, {
836 assert_eq!(
837 $vec4::new(1.35, 1.5, -1.5, 1.999).floor(),
838 $vec4::new(1.0, 1.0, -2.0, 1.0)
839 );
840 assert_eq!(
841 $vec4::new(INFINITY, NEG_INFINITY, 0.0, 0.0).floor(),
842 $vec4::new(INFINITY, NEG_INFINITY, 0.0, 0.0)
843 );
844 assert!($vec4::new(0.0, NAN, 0.0, 0.0).floor().y.is_nan());
845 assert_eq!(
846 $vec4::new(-0.0, -2000000.123, 10000000.123, 1000.9).floor(),
847 $vec4::new(-0.0, -2000001.0, 10000000.0, 1000.0)
848 );
849 });
850
851 glam_test!(test_fract, {
852 assert_approx_eq!(
853 $vec4::new(1.35, 1.5, -1.5, 1.999).fract(),
854 $vec4::new(0.35, 0.5, 0.5, 0.999)
855 );
856 assert_approx_eq!(
857 $vec4::new(-0.0, -200000.123, 1000000.123, 1000.9).fract(),
858 $vec4::new(0.0, 0.877, 0.123, 0.9),
859 0.002
860 );
861 });
862
863 glam_test!(test_ceil, {
864 assert_eq!(
865 $vec4::new(1.35, 1.5, -1.5, 1234.1234).ceil(),
866 $vec4::new(2.0, 2.0, -1.0, 1235.0)
867 );
868 assert_eq!(
869 $vec4::new(INFINITY, NEG_INFINITY, 0.0, 0.0).ceil(),
870 $vec4::new(INFINITY, NEG_INFINITY, 0.0, 0.0)
871 );
872 assert!($vec4::new(0.0, 0.0, NAN, 0.0).ceil().z.is_nan());
873 assert_eq!(
874 $vec4::new(-1234.1234, -2000000.123, 1000000.123, 1000.9).ceil(),
875 $vec4::new(-1234.0, -2000000.0, 1000001.0, 1001.0)
876 );
877 });
878
879 glam_test!(test_lerp, {
880 let v0 = $vec4::new(-1.0, -1.0, -1.0, -1.0);
881 let v1 = $vec4::new(1.0, 1.0, 1.0, 1.0);
882 assert_approx_eq!(v0, v0.lerp(v1, 0.0));
883 assert_approx_eq!(v1, v0.lerp(v1, 1.0));
884 assert_approx_eq!($vec4::ZERO, v0.lerp(v1, 0.5));
885 });
886
887 glam_test!(test_is_finite, {
888 assert!($vec4::new(0.0, 0.0, 0.0, 0.0).is_finite());
889 assert!($vec4::new(-1e-10, 1.0, 1e10, 42.0).is_finite());
890 assert!(!$vec4::new(INFINITY, 0.0, 0.0, 0.0).is_finite());
891 assert!(!$vec4::new(0.0, NAN, 0.0, 0.0).is_finite());
892 assert!(!$vec4::new(0.0, 0.0, NEG_INFINITY, 0.0).is_finite());
893 assert!(!$vec4::new(0.0, 0.0, 0.0, NAN).is_finite());
894 });
895
896 glam_test!(test_powf, {
897 assert_eq!(
898 $vec4::new(2.0, 4.0, 8.0, 16.0).powf(2.0),
899 $vec4::new(4.0, 16.0, 64.0, 256.0)
900 );
901 });
902
903 glam_test!(test_exp, {
904 assert_approx_eq!(
905 $vec4::new(1.0, 2.0, 3.0, 4.0).exp(),
906 $vec4::new(
907 (1.0 as $t).exp(),
908 (2.0 as $t).exp(),
909 (3.0 as $t).exp(),
910 (4.0 as $t).exp()
911 ),
912 1e-5
913 );
914 });
915
916 glam_test!(test_clamp_length, {
917 // Too long gets shortened
918 assert_eq!(
919 $vec4::new(12.0, 16.0, 0.0, 0.0).clamp_length(7.0, 10.0),
920 $vec4::new(6.0, 8.0, 0.0, 0.0) // shortened to length 10.0
921 );
922 // In the middle is unchanged
923 assert_eq!(
924 $vec4::new(2.0, 1.0, 0.0, 0.0).clamp_length(0.5, 5.0),
925 $vec4::new(2.0, 1.0, 0.0, 0.0) // unchanged
926 );
927 // Too short gets lengthened
928 assert_eq!(
929 $vec4::new(0.6, 0.8, 0.0, 0.0).clamp_length(10.0, 20.0),
930 $vec4::new(6.0, 8.0, 0.0, 0.0) // lengthened to length 10.0
931 );
932 should_glam_assert!({ $vec4::ONE.clamp_length(1.0, 0.0) });
933 });
934
935 glam_test!(test_clamp_length_max, {
936 // Too long gets shortened
937 assert_eq!(
938 $vec4::new(12.0, 16.0, 0.0, 0.0).clamp_length_max(10.0),
939 $vec4::new(6.0, 8.0, 0.0, 0.0) // shortened to length 10.0
940 );
941 // Not too long is unchanged
942 assert_eq!(
943 $vec4::new(2.0, 1.0, 0.0, 0.0).clamp_length_max(5.0),
944 $vec4::new(2.0, 1.0, 0.0, 0.0) // unchanged
945 );
946 });
947
948 glam_test!(test_clamp_length_min, {
949 // Not too short is unchanged
950 assert_eq!(
951 $vec4::new(2.0, 1.0, 0.0, 0.0).clamp_length_min(0.5),
952 $vec4::new(2.0, 1.0, 0.0, 0.0) // unchanged
953 );
954 // Too short gets lengthened
955 assert_eq!(
956 $vec4::new(0.6, 0.8, 0.0, 0.0).clamp_length_min(10.0),
957 $vec4::new(6.0, 8.0, 0.0, 0.0) // lengthened to length 10.0
958 );
959 });
960
961 glam_test!(test_mul_add, {
962 assert_eq!(
963 $vec4::new(1.0, 1.0, 1.0, 1.0).mul_add(
964 $vec4::new(0.5, 2.0, -4.0, 0.0),
965 $vec4::new(-1.0, -1.0, -1.0, -1.0)
966 ),
967 $vec4::new(-0.5, 1.0, -5.0, -1.0)
968 );
969 });
970 };
971 }
972
973 macro_rules! impl_vec4_scalar_shift_op_test {
974 ($vec4:ident, $t_min:literal, $t_max:literal, $rhs_min:literal, $rhs_max:literal) => {
975 glam_test!(test_vec4_scalar_shift_ops, {
976 for x in $t_min..$t_max {
977 for y in $t_min..$t_max {
978 for z in $t_min..$t_max {
979 for w in $t_min..$t_max {
980 for rhs in $rhs_min..$rhs_max {
981 assert_eq!(
982 $vec4::new(x, y, z, w) << rhs,
983 $vec4::new(x << rhs, y << rhs, z << rhs, w << rhs)
984 );
985 assert_eq!(
986 $vec4::new(x, y, z, w) >> rhs,
987 $vec4::new(x >> rhs, y >> rhs, z >> rhs, w >> rhs)
988 );
989 }
990 }
991 }
992 }
993 }
994 });
995 };
996 }
997
998 macro_rules! impl_vec4_scalar_shift_op_tests {
999 ($vec4:ident, $t_min:literal, $t_max:literal) => {
1000 mod shift_by_i8 {
1001 use glam::$vec4;
1002 impl_vec4_scalar_shift_op_test!($vec4, $t_min, $t_max, 0i8, 2);
1003 }
1004 mod shift_by_i16 {
1005 use glam::$vec4;
1006 impl_vec4_scalar_shift_op_test!($vec4, $t_min, $t_max, 0i16, 2);
1007 }
1008 mod shift_by_i32 {
1009 use glam::$vec4;
1010 impl_vec4_scalar_shift_op_test!($vec4, $t_min, $t_max, 0i32, 2);
1011 }
1012 mod shift_by_u8 {
1013 use glam::$vec4;
1014 impl_vec4_scalar_shift_op_test!($vec4, $t_min, $t_max, 0u8, 2);
1015 }
1016 mod shift_by_u16 {
1017 use glam::$vec4;
1018 impl_vec4_scalar_shift_op_test!($vec4, $t_min, $t_max, 0u16, 2);
1019 }
1020 mod shift_by_u32 {
1021 use glam::$vec4;
1022 impl_vec4_scalar_shift_op_test!($vec4, $t_min, $t_max, 0u32, 2);
1023 }
1024 };
1025 }
1026
1027 macro_rules! impl_vec4_shift_op_test {
1028 ($vec4:ident, $rhs:ident, $t_min:literal, $t_max:literal) => {
1029 glam_test!(test_vec4_shift_ops, {
1030 for x1 in $t_min..$t_max {
1031 for y1 in $t_min..$t_max {
1032 for z1 in $t_min..$t_max {
1033 for w1 in $t_min..$t_max {
1034 for x2 in $t_min..$t_max {
1035 for y2 in $t_min..$t_max {
1036 for z2 in $t_min..$t_max {
1037 for w2 in $t_min..$t_max {
1038 assert_eq!(
1039 $vec4::new(x1, y1, z1, w1)
1040 << $rhs::new(x2, y2, z2, w2),
1041 $vec4::new(x1 << x2, y1 << y2, z1 << z2, w1 << w2)
1042 );
1043 assert_eq!(
1044 $vec4::new(x1, y1, z1, w1)
1045 >> $rhs::new(x2, y2, z2, w2),
1046 $vec4::new(x1 >> x2, y1 >> y2, z1 >> z2, w1 >> w2)
1047 );
1048 }
1049 }
1050 }
1051 }
1052 }
1053 }
1054 }
1055 }
1056 });
1057 };
1058 }
1059
1060 macro_rules! impl_vec4_shift_op_tests {
1061 ($vec4:ident) => {
1062 mod shift_ivec4_by_ivec4 {
1063 use super::*;
1064 impl_vec4_shift_op_test!($vec4, IVec4, 0, 2);
1065 }
1066 mod shift_ivec4_by_uvec4 {
1067 use super::*;
1068 impl_vec4_shift_op_test!($vec4, UVec4, 0, 2);
1069 }
1070 };
1071 }
1072
1073 macro_rules! impl_vec4_scalar_bit_op_tests {
1074 ($vec4:ident, $t_min:literal, $t_max:literal) => {
1075 glam_test!(test_vec4_scalar_bit_ops, {
1076 for x in $t_min..$t_max {
1077 for y in $t_min..$t_max {
1078 for z in $t_min..$t_max {
1079 for w in $t_min..$t_max {
1080 for rhs in $t_min..$t_max {
1081 assert_eq!(
1082 $vec4::new(x, y, z, w) & rhs,
1083 $vec4::new(x & rhs, y & rhs, z & rhs, w & rhs)
1084 );
1085 assert_eq!(
1086 $vec4::new(x, y, z, w) | rhs,
1087 $vec4::new(x | rhs, y | rhs, z | rhs, w | rhs)
1088 );
1089 assert_eq!(
1090 $vec4::new(x, y, z, w) ^ rhs,
1091 $vec4::new(x ^ rhs, y ^ rhs, z ^ rhs, w ^ rhs)
1092 );
1093 }
1094 }
1095 }
1096 }
1097 }
1098 });
1099 };
1100 }
1101
1102 macro_rules! impl_vec4_bit_op_tests {
1103 ($vec4:ident, $t_min:literal, $t_max:literal) => {
1104 glam_test!(test_vec4_bit_ops, {
1105 for x1 in $t_min..$t_max {
1106 for y1 in $t_min..$t_max {
1107 for z1 in $t_min..$t_max {
1108 for w1 in $t_min..$t_max {
1109 assert_eq!(!$vec4::new(x1, y1, z1, w1), $vec4::new(!x1, !y1, !z1, !w1));
1110
1111 for x2 in $t_min..$t_max {
1112 for y2 in $t_min..$t_max {
1113 for z2 in $t_min..$t_max {
1114 for w2 in $t_min..$t_max {
1115 assert_eq!(
1116 $vec4::new(x1, y1, z1, w1)
1117 & $vec4::new(x2, y2, z2, w2),
1118 $vec4::new(x1 & x2, y1 & y2, z1 & z2, w1 & w2)
1119 );
1120 assert_eq!(
1121 $vec4::new(x1, y1, z1, w1)
1122 | $vec4::new(x2, y2, z2, w2),
1123 $vec4::new(x1 | x2, y1 | y2, z1 | z2, w1 | w2)
1124 );
1125 assert_eq!(
1126 $vec4::new(x1, y1, z1, w1)
1127 ^ $vec4::new(x2, y2, z2, w2),
1128 $vec4::new(x1 ^ x2, y1 ^ y2, z1 ^ z2, w1 ^ w2)
1129 );
1130 }
1131 }
1132 }
1133 }
1134 }
1135 }
1136 }
1137 }
1138 });
1139 };
1140 }
1141 mod vec4 {
1142 #[cfg(any(
1143 not(any(target_feature = "sse2", target_feature = "simd128")),
1144 feature = "scalar-math"
1145 ))]
1146 use glam::BVec4;
1147 #[cfg(not(feature = "scalar-math"))]
1148 use glam::BVec4A;
1149 use glam::{vec4, Vec2, Vec3, Vec4};
1150
1151 glam_test!(test_align, {
1152 use std::mem;
1153 assert_eq!(16, mem::size_of::<Vec4>());
1154 if cfg!(any(not(feature = "scalar-math"), feature = "cuda")) {
1155 assert_eq!(16, mem::align_of::<Vec4>());
1156 } else {
1157 assert_eq!(4, mem::align_of::<Vec4>());
1158 }
1159 #[cfg(not(feature = "scalar-math"))]
1160 {
1161 assert_eq!(16, mem::size_of::<BVec4A>());
1162 assert_eq!(16, mem::align_of::<BVec4A>());
1163 }
1164 #[cfg(feature = "scalar-math")]
1165 {
1166 assert_eq!(4, mem::size_of::<BVec4>());
1167 assert_eq!(1, mem::align_of::<BVec4>());
1168 }
1169 });
1170
1171 #[cfg(all(
1172 target_feature = "sse2",
1173 not(any(feature = "core-simd", feature = "scalar-math"))
1174 ))]
1175 #[test]
1176 fn test_m128() {
1177 #[cfg(target_arch = "x86")]
1178 use core::arch::x86::*;
1179 #[cfg(target_arch = "x86_64")]
1180 use core::arch::x86_64::*;
1181
1182 #[repr(C, align(16))]
1183 struct F32x4_A16([f32; 4]);
1184
1185 let v0 = Vec4::new(1.0, 2.0, 3.0, 4.0);
1186 let m0: __m128 = v0.into();
1187 let mut a0 = F32x4_A16([0.0, 0.0, 0.0, 0.0]);
1188 unsafe {
1189 _mm_store_ps(a0.0.as_mut_ptr(), m0);
1190 }
1191 assert_eq!([1.0, 2.0, 3.0, 4.0], a0.0);
1192 let v1 = Vec4::from(m0);
1193 assert_eq!(v0, v1);
1194
1195 #[repr(C, align(16))]
1196 struct U32x4_A16([u32; 4]);
1197
1198 let v0 = BVec4A::new(true, false, true, false);
1199 let m0: __m128 = v0.into();
1200 let mut a0 = U32x4_A16([1, 2, 3, 4]);
1201 unsafe {
1202 _mm_store_ps(a0.0.as_mut_ptr() as *mut f32, m0);
1203 }
1204 assert_eq!([0xffffffff, 0, 0xffffffff, 0], a0.0);
1205 }
1206
1207 glam_test!(test_as, {
1208 use glam::{DVec4, IVec4, UVec4};
1209 assert_eq!(
1210 DVec4::new(-1.0, -2.0, -3.0, -4.0),
1211 Vec4::new(-1.0, -2.0, -3.0, -4.0).as_dvec4()
1212 );
1213 assert_eq!(
1214 IVec4::new(-1, -2, -3, -4),
1215 Vec4::new(-1.0, -2.0, -3.0, -4.0).as_ivec4()
1216 );
1217 assert_eq!(
1218 UVec4::new(1, 2, 3, 4),
1219 Vec4::new(1.0, 2.0, 3.0, 4.0).as_uvec4()
1220 );
1221
1222 assert_eq!(
1223 IVec4::new(-1, -2, -3, -4),
1224 DVec4::new(-1.0, -2.0, -3.0, -4.0).as_ivec4()
1225 );
1226 assert_eq!(
1227 UVec4::new(1, 2, 3, 4),
1228 DVec4::new(1.0, 2.0, 3.0, 4.0).as_uvec4()
1229 );
1230 assert_eq!(
1231 Vec4::new(-1.0, -2.0, -3.0, -4.0),
1232 DVec4::new(-1.0, -2.0, -3.0, -4.0).as_vec4()
1233 );
1234
1235 assert_eq!(
1236 DVec4::new(-1.0, -2.0, -3.0, -4.0),
1237 IVec4::new(-1, -2, -3, -4).as_dvec4()
1238 );
1239 assert_eq!(UVec4::new(1, 2, 3, 4), IVec4::new(1, 2, 3, 4).as_uvec4());
1240 assert_eq!(
1241 Vec4::new(-1.0, -2.0, -3.0, -4.0),
1242 IVec4::new(-1, -2, -3, -4).as_vec4()
1243 );
1244
1245 assert_eq!(
1246 DVec4::new(1.0, 2.0, 3.0, 4.0),
1247 UVec4::new(1, 2, 3, 4).as_dvec4()
1248 );
1249 assert_eq!(IVec4::new(1, 2, 3, 4), UVec4::new(1, 2, 3, 4).as_ivec4());
1250 assert_eq!(
1251 Vec4::new(1.0, 2.0, 3.0, 4.0),
1252 UVec4::new(1, 2, 3, 4).as_vec4()
1253 );
1254 });
1255
1256 glam_test!(test_vec3a, {
1257 use glam::Vec3A;
1258 assert_eq!(
1259 Vec4::new(1.0, 2.0, 3.0, 4.0),
1260 Vec4::from((Vec3A::new(1.0, 2.0, 3.0), 4.0))
1261 );
1262 assert_eq!(
1263 Vec4::new(1.0, 2.0, 3.0, 4.0),
1264 Vec4::from((1.0, Vec3A::new(2.0, 3.0, 4.0)))
1265 );
1266 });
1267
1268 #[cfg(all(
1269 any(target_feature = "sse2", target_feature = "simd128"),
1270 not(feature = "scalar-math")
1271 ))]
1272 impl_vec4_float_tests!(f32, vec4, Vec4, Vec3, Vec2, BVec4A);
1273
1274 #[cfg(any(
1275 not(any(target_feature = "sse2", target_feature = "simd128")),
1276 feature = "scalar-math"
1277 ))]
1278 impl_vec4_float_tests!(f32, vec4, Vec4, Vec3, Vec2, BVec4);
1279 }
1280
1281 mod dvec4 {
1282 use glam::{dvec4, BVec4, DVec2, DVec3, DVec4};
1283
1284 glam_test!(test_align, {
1285 use std::mem;
1286 assert_eq!(32, mem::size_of::<DVec4>());
1287 #[cfg(not(feature = "cuda"))]
1288 assert_eq!(mem::align_of::<f64>(), mem::align_of::<DVec4>());
1289 #[cfg(feature = "cuda")]
1290 assert_eq!(16, mem::align_of::<DVec4>());
1291 assert_eq!(4, mem::size_of::<BVec4>());
1292 assert_eq!(1, mem::align_of::<BVec4>());
1293 });
1294
1295 impl_vec4_float_tests!(f64, dvec4, DVec4, DVec3, DVec2, BVec4);
1296 }
1297
1298 mod ivec4 {
1299 use glam::{ivec4, BVec4, IVec2, IVec3, IVec4, UVec4};
1300
1301 glam_test!(test_align, {
1302 use std::mem;
1303 assert_eq!(16, mem::size_of::<IVec4>());
1304 #[cfg(not(feature = "cuda"))]
1305 assert_eq!(4, mem::align_of::<IVec4>());
1306 #[cfg(feature = "cuda")]
1307 assert_eq!(16, mem::align_of::<IVec4>());
1308 assert_eq!(4, mem::size_of::<BVec4>());
1309 assert_eq!(1, mem::align_of::<BVec4>());
1310 });
1311
1312 impl_vec4_signed_tests!(i32, ivec4, IVec4, IVec3, IVec2, BVec4);
1313 impl_vec4_eq_hash_tests!(i32, ivec4);
1314
1315 impl_vec4_scalar_shift_op_tests!(IVec4, -2, 2);
1316 impl_vec4_shift_op_tests!(IVec4);
1317
1318 impl_vec4_scalar_bit_op_tests!(IVec4, -2, 2);
1319 impl_vec4_bit_op_tests!(IVec4, -2, 2);
1320 }
1321
1322 mod uvec4 {
1323 use glam::{uvec4, BVec4, IVec4, UVec2, UVec3, UVec4};
1324
1325 glam_test!(test_align, {
1326 use std::mem;
1327 assert_eq!(16, mem::size_of::<UVec4>());
1328 #[cfg(not(feature = "cuda"))]
1329 assert_eq!(4, mem::align_of::<UVec4>());
1330 #[cfg(feature = "cuda")]
1331 assert_eq!(16, mem::align_of::<UVec4>());
1332 assert_eq!(4, mem::size_of::<BVec4>());
1333 assert_eq!(1, mem::align_of::<BVec4>());
1334 });
1335
1336 impl_vec4_tests!(u32, uvec4, UVec4, UVec3, UVec2, BVec4);
1337 impl_vec4_eq_hash_tests!(u32, uvec4);
1338
1339 impl_vec4_scalar_shift_op_tests!(UVec4, 0, 2);
1340 impl_vec4_shift_op_tests!(UVec4);
1341
1342 impl_vec4_scalar_bit_op_tests!(UVec4, 0, 2);
1343 impl_vec4_bit_op_tests!(UVec4, 0, 2);
1344 }