Debian Codegraph @

debian/missing-sources/glam-0.22.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_is_negative_bitmask, {
751 assert_eq!($vec4::ZERO.is_negative_bitmask(), 0b0000);
752 assert_eq!((-$vec4::ZERO).is_negative_bitmask(), 0b1111);
753 assert_eq!($vec4::ONE.is_negative_bitmask(), 0b0000);
754 assert_eq!((-$vec4::ONE).is_negative_bitmask(), 0b1111);
755 assert_eq!(
756 $vec4::new(-0.1, 0.2, 0.3, -0.4).is_negative_bitmask(),
757 0b1001
758 );
759 assert_eq!(
760 $vec4::new(0.8, 0.3, 0.1, -0.0).is_negative_bitmask(),
761 0b1000
762 );
763 assert_eq!(
764 $vec4::new(0.1, 0.5, -0.3, 0.7).is_negative_bitmask(),
765 0b0100
766 );
767 assert_eq!(
768 $vec4::new(0.3, -0.4, 0.1, 0.6).is_negative_bitmask(),
769 0b0010
770 );
771 assert_eq!(
772 $vec4::new(0.2, -0.6, 0.5, -0.3).is_negative_bitmask(),
773 0b1010
774 );
775 });
776
777 glam_test!(test_abs, {
778 assert_eq!($vec4::ZERO.abs(), $vec4::ZERO);
779 assert_eq!($vec4::ONE.abs(), $vec4::ONE);
780 assert_eq!((-$vec4::ONE).abs(), $vec4::ONE);
781 });
782
783 glam_test!(test_round, {
784 assert_eq!($vec4::new(1.35, 0.0, 0.0, 0.0).round().x, 1.0);
785 assert_eq!($vec4::new(0.0, 1.5, 0.0, 0.0).round().y, 2.0);
786 assert_eq!($vec4::new(0.0, 0.0, -15.5, 0.0).round().z, -16.0);
787 assert_eq!($vec4::new(0.0, 0.0, 0.0, 0.0).round().z, 0.0);
788 assert_eq!($vec4::new(0.0, 21.1, 0.0, 0.0).round().y, 21.0);
789 assert_eq!($vec4::new(0.0, 0.0, 0.0, 11.123).round().w, 11.0);
790 assert_eq!($vec4::new(0.0, 0.0, 11.501, 0.0).round().z, 12.0);
791 assert_eq!(
792 $vec4::new(NEG_INFINITY, INFINITY, 1.0, -1.0).round(),
793 $vec4::new(NEG_INFINITY, INFINITY, 1.0, -1.0)
794 );
795 assert!($vec4::new(NAN, 0.0, 0.0, 1.0).round().x.is_nan());
796 });
797
798 glam_test!(test_floor, {
799 assert_eq!(
800 $vec4::new(1.35, 1.5, -1.5, 1.999).floor(),
801 $vec4::new(1.0, 1.0, -2.0, 1.0)
802 );
803 assert_eq!(
804 $vec4::new(INFINITY, NEG_INFINITY, 0.0, 0.0).floor(),
805 $vec4::new(INFINITY, NEG_INFINITY, 0.0, 0.0)
806 );
807 assert!($vec4::new(0.0, NAN, 0.0, 0.0).floor().y.is_nan());
808 assert_eq!(
809 $vec4::new(-0.0, -2000000.123, 10000000.123, 1000.9).floor(),
810 $vec4::new(-0.0, -2000001.0, 10000000.0, 1000.0)
811 );
812 });
813
814 glam_test!(test_fract, {
815 assert_approx_eq!(
816 $vec4::new(1.35, 1.5, -1.5, 1.999).fract(),
817 $vec4::new(0.35, 0.5, 0.5, 0.999)
818 );
819 assert_approx_eq!(
820 $vec4::new(-0.0, -200000.123, 1000000.123, 1000.9).fract(),
821 $vec4::new(0.0, 0.877, 0.123, 0.9),
822 0.002
823 );
824 });
825
826 glam_test!(test_ceil, {
827 assert_eq!(
828 $vec4::new(1.35, 1.5, -1.5, 1234.1234).ceil(),
829 $vec4::new(2.0, 2.0, -1.0, 1235.0)
830 );
831 assert_eq!(
832 $vec4::new(INFINITY, NEG_INFINITY, 0.0, 0.0).ceil(),
833 $vec4::new(INFINITY, NEG_INFINITY, 0.0, 0.0)
834 );
835 assert!($vec4::new(0.0, 0.0, NAN, 0.0).ceil().z.is_nan());
836 assert_eq!(
837 $vec4::new(-1234.1234, -2000000.123, 1000000.123, 1000.9).ceil(),
838 $vec4::new(-1234.0, -2000000.0, 1000001.0, 1001.0)
839 );
840 });
841
842 glam_test!(test_lerp, {
843 let v0 = $vec4::new(-1.0, -1.0, -1.0, -1.0);
844 let v1 = $vec4::new(1.0, 1.0, 1.0, 1.0);
845 assert_approx_eq!(v0, v0.lerp(v1, 0.0));
846 assert_approx_eq!(v1, v0.lerp(v1, 1.0));
847 assert_approx_eq!($vec4::ZERO, v0.lerp(v1, 0.5));
848 });
849
850 glam_test!(test_is_finite, {
851 assert!($vec4::new(0.0, 0.0, 0.0, 0.0).is_finite());
852 assert!($vec4::new(-1e-10, 1.0, 1e10, 42.0).is_finite());
853 assert!(!$vec4::new(INFINITY, 0.0, 0.0, 0.0).is_finite());
854 assert!(!$vec4::new(0.0, NAN, 0.0, 0.0).is_finite());
855 assert!(!$vec4::new(0.0, 0.0, NEG_INFINITY, 0.0).is_finite());
856 assert!(!$vec4::new(0.0, 0.0, 0.0, NAN).is_finite());
857 });
858
859 glam_test!(test_powf, {
860 assert_eq!(
861 $vec4::new(2.0, 4.0, 8.0, 16.0).powf(2.0),
862 $vec4::new(4.0, 16.0, 64.0, 256.0)
863 );
864 });
865
866 glam_test!(test_exp, {
867 assert_approx_eq!(
868 $vec4::new(1.0, 2.0, 3.0, 4.0).exp(),
869 $vec4::new(
870 (1.0 as $t).exp(),
871 (2.0 as $t).exp(),
872 (3.0 as $t).exp(),
873 (4.0 as $t).exp()
874 ),
875 1e-5
876 );
877 });
878
879 glam_test!(test_clamp_length, {
880 // Too long gets shortened
881 assert_eq!(
882 $vec4::new(12.0, 16.0, 0.0, 0.0).clamp_length(7.0, 10.0),
883 $vec4::new(6.0, 8.0, 0.0, 0.0) // shortened to length 10.0
884 );
885 // In the middle is unchanged
886 assert_eq!(
887 $vec4::new(2.0, 1.0, 0.0, 0.0).clamp_length(0.5, 5.0),
888 $vec4::new(2.0, 1.0, 0.0, 0.0) // unchanged
889 );
890 // Too short gets lengthened
891 assert_eq!(
892 $vec4::new(0.6, 0.8, 0.0, 0.0).clamp_length(10.0, 20.0),
893 $vec4::new(6.0, 8.0, 0.0, 0.0) // lengthened to length 10.0
894 );
895 should_glam_assert!({ $vec4::ONE.clamp_length(1.0, 0.0) });
896 });
897
898 glam_test!(test_clamp_length_max, {
899 // Too long gets shortened
900 assert_eq!(
901 $vec4::new(12.0, 16.0, 0.0, 0.0).clamp_length_max(10.0),
902 $vec4::new(6.0, 8.0, 0.0, 0.0) // shortened to length 10.0
903 );
904 // Not too long is unchanged
905 assert_eq!(
906 $vec4::new(2.0, 1.0, 0.0, 0.0).clamp_length_max(5.0),
907 $vec4::new(2.0, 1.0, 0.0, 0.0) // unchanged
908 );
909 });
910
911 glam_test!(test_clamp_length_min, {
912 // Not too short is unchanged
913 assert_eq!(
914 $vec4::new(2.0, 1.0, 0.0, 0.0).clamp_length_min(0.5),
915 $vec4::new(2.0, 1.0, 0.0, 0.0) // unchanged
916 );
917 // Too short gets lengthened
918 assert_eq!(
919 $vec4::new(0.6, 0.8, 0.0, 0.0).clamp_length_min(10.0),
920 $vec4::new(6.0, 8.0, 0.0, 0.0) // lengthened to length 10.0
921 );
922 });
923
924 glam_test!(test_mul_add, {
925 assert_eq!(
926 $vec4::new(1.0, 1.0, 1.0, 1.0).mul_add(
927 $vec4::new(0.5, 2.0, -4.0, 0.0),
928 $vec4::new(-1.0, -1.0, -1.0, -1.0)
929 ),
930 $vec4::new(-0.5, 1.0, -5.0, -1.0)
931 );
932 });
933 };
934 }
935
936 macro_rules! impl_vec4_scalar_shift_op_test {
937 ($vec4:ident, $t_min:literal, $t_max:literal, $rhs_min:literal, $rhs_max:literal) => {
938 glam_test!(test_vec4_scalar_shift_ops, {
939 for x in $t_min..$t_max {
940 for y in $t_min..$t_max {
941 for z in $t_min..$t_max {
942 for w in $t_min..$t_max {
943 for rhs in $rhs_min..$rhs_max {
944 assert_eq!(
945 $vec4::new(x, y, z, w) << rhs,
946 $vec4::new(x << rhs, y << rhs, z << rhs, w << rhs)
947 );
948 assert_eq!(
949 $vec4::new(x, y, z, w) >> rhs,
950 $vec4::new(x >> rhs, y >> rhs, z >> rhs, w >> rhs)
951 );
952 }
953 }
954 }
955 }
956 }
957 });
958 };
959 }
960
961 macro_rules! impl_vec4_scalar_shift_op_tests {
962 ($vec4:ident, $t_min:literal, $t_max:literal) => {
963 mod shift_by_i8 {
964 use glam::$vec4;
965 impl_vec4_scalar_shift_op_test!($vec4, $t_min, $t_max, 0i8, 2);
966 }
967 mod shift_by_i16 {
968 use glam::$vec4;
969 impl_vec4_scalar_shift_op_test!($vec4, $t_min, $t_max, 0i16, 2);
970 }
971 mod shift_by_i32 {
972 use glam::$vec4;
973 impl_vec4_scalar_shift_op_test!($vec4, $t_min, $t_max, 0i32, 2);
974 }
975 mod shift_by_u8 {
976 use glam::$vec4;
977 impl_vec4_scalar_shift_op_test!($vec4, $t_min, $t_max, 0u8, 2);
978 }
979 mod shift_by_u16 {
980 use glam::$vec4;
981 impl_vec4_scalar_shift_op_test!($vec4, $t_min, $t_max, 0u16, 2);
982 }
983 mod shift_by_u32 {
984 use glam::$vec4;
985 impl_vec4_scalar_shift_op_test!($vec4, $t_min, $t_max, 0u32, 2);
986 }
987 };
988 }
989
990 macro_rules! impl_vec4_shift_op_test {
991 ($vec4:ident, $rhs:ident, $t_min:literal, $t_max:literal) => {
992 glam_test!(test_vec4_shift_ops, {
993 for x1 in $t_min..$t_max {
994 for y1 in $t_min..$t_max {
995 for z1 in $t_min..$t_max {
996 for w1 in $t_min..$t_max {
997 for x2 in $t_min..$t_max {
998 for y2 in $t_min..$t_max {
999 for z2 in $t_min..$t_max {
1000 for w2 in $t_min..$t_max {
1001 assert_eq!(
1002 $vec4::new(x1, y1, z1, w1)
1003 << $rhs::new(x2, y2, z2, w2),
1004 $vec4::new(x1 << x2, y1 << y2, z1 << z2, w1 << w2)
1005 );
1006 assert_eq!(
1007 $vec4::new(x1, y1, z1, w1)
1008 >> $rhs::new(x2, y2, z2, w2),
1009 $vec4::new(x1 >> x2, y1 >> y2, z1 >> z2, w1 >> w2)
1010 );
1011 }
1012 }
1013 }
1014 }
1015 }
1016 }
1017 }
1018 }
1019 });
1020 };
1021 }
1022
1023 macro_rules! impl_vec4_shift_op_tests {
1024 ($vec4:ident) => {
1025 mod shift_ivec4_by_ivec4 {
1026 use super::*;
1027 impl_vec4_shift_op_test!($vec4, IVec4, 0, 2);
1028 }
1029 mod shift_ivec4_by_uvec4 {
1030 use super::*;
1031 impl_vec4_shift_op_test!($vec4, UVec4, 0, 2);
1032 }
1033 };
1034 }
1035
1036 macro_rules! impl_vec4_scalar_bit_op_tests {
1037 ($vec4:ident, $t_min:literal, $t_max:literal) => {
1038 glam_test!(test_vec4_scalar_bit_ops, {
1039 for x in $t_min..$t_max {
1040 for y in $t_min..$t_max {
1041 for z in $t_min..$t_max {
1042 for w in $t_min..$t_max {
1043 for rhs in $t_min..$t_max {
1044 assert_eq!(
1045 $vec4::new(x, y, z, w) & rhs,
1046 $vec4::new(x & rhs, y & rhs, z & rhs, w & rhs)
1047 );
1048 assert_eq!(
1049 $vec4::new(x, y, z, w) | rhs,
1050 $vec4::new(x | rhs, y | rhs, z | rhs, w | rhs)
1051 );
1052 assert_eq!(
1053 $vec4::new(x, y, z, w) ^ rhs,
1054 $vec4::new(x ^ rhs, y ^ rhs, z ^ rhs, w ^ rhs)
1055 );
1056 }
1057 }
1058 }
1059 }
1060 }
1061 });
1062 };
1063 }
1064
1065 macro_rules! impl_vec4_bit_op_tests {
1066 ($vec4:ident, $t_min:literal, $t_max:literal) => {
1067 glam_test!(test_vec4_bit_ops, {
1068 for x1 in $t_min..$t_max {
1069 for y1 in $t_min..$t_max {
1070 for z1 in $t_min..$t_max {
1071 for w1 in $t_min..$t_max {
1072 assert_eq!(!$vec4::new(x1, y1, z1, w1), $vec4::new(!x1, !y1, !z1, !w1));
1073
1074 for x2 in $t_min..$t_max {
1075 for y2 in $t_min..$t_max {
1076 for z2 in $t_min..$t_max {
1077 for w2 in $t_min..$t_max {
1078 assert_eq!(
1079 $vec4::new(x1, y1, z1, w1)
1080 & $vec4::new(x2, y2, z2, w2),
1081 $vec4::new(x1 & x2, y1 & y2, z1 & z2, w1 & w2)
1082 );
1083 assert_eq!(
1084 $vec4::new(x1, y1, z1, w1)
1085 | $vec4::new(x2, y2, z2, w2),
1086 $vec4::new(x1 | x2, y1 | y2, z1 | z2, w1 | w2)
1087 );
1088 assert_eq!(
1089 $vec4::new(x1, y1, z1, w1)
1090 ^ $vec4::new(x2, y2, z2, w2),
1091 $vec4::new(x1 ^ x2, y1 ^ y2, z1 ^ z2, w1 ^ w2)
1092 );
1093 }
1094 }
1095 }
1096 }
1097 }
1098 }
1099 }
1100 }
1101 });
1102 };
1103 }
1104 mod vec4 {
1105 #[cfg(any(
1106 not(any(target_feature = "sse2", target_feature = "simd128")),
1107 feature = "scalar-math"
1108 ))]
1109 use glam::BVec4;
1110 #[cfg(not(feature = "scalar-math"))]
1111 use glam::BVec4A;
1112 use glam::{vec4, Vec2, Vec3, Vec4};
1113
1114 glam_test!(test_align, {
1115 use std::mem;
1116 assert_eq!(16, mem::size_of::<Vec4>());
1117 if cfg!(any(not(feature = "scalar-math"), feature = "cuda")) {
1118 assert_eq!(16, mem::align_of::<Vec4>());
1119 } else {
1120 assert_eq!(4, mem::align_of::<Vec4>());
1121 }
1122 #[cfg(not(feature = "scalar-math"))]
1123 {
1124 assert_eq!(16, mem::size_of::<BVec4A>());
1125 assert_eq!(16, mem::align_of::<BVec4A>());
1126 }
1127 #[cfg(feature = "scalar-math")]
1128 {
1129 assert_eq!(4, mem::size_of::<BVec4>());
1130 assert_eq!(1, mem::align_of::<BVec4>());
1131 }
1132 });
1133
1134 #[cfg(all(
1135 target_feature = "sse2",
1136 not(any(feature = "core-simd", feature = "scalar-math"))
1137 ))]
1138 #[test]
1139 fn test_m128() {
1140 #[cfg(target_arch = "x86")]
1141 use core::arch::x86::*;
1142 #[cfg(target_arch = "x86_64")]
1143 use core::arch::x86_64::*;
1144
1145 #[repr(C, align(16))]
1146 struct F32x4_A16([f32; 4]);
1147
1148 let v0 = Vec4::new(1.0, 2.0, 3.0, 4.0);
1149 let m0: __m128 = v0.into();
1150 let mut a0 = F32x4_A16([0.0, 0.0, 0.0, 0.0]);
1151 unsafe {
1152 _mm_store_ps(a0.0.as_mut_ptr(), m0);
1153 }
1154 assert_eq!([1.0, 2.0, 3.0, 4.0], a0.0);
1155 let v1 = Vec4::from(m0);
1156 assert_eq!(v0, v1);
1157
1158 #[repr(C, align(16))]
1159 struct U32x4_A16([u32; 4]);
1160
1161 let v0 = BVec4A::new(true, false, true, false);
1162 let m0: __m128 = v0.into();
1163 let mut a0 = U32x4_A16([1, 2, 3, 4]);
1164 unsafe {
1165 _mm_store_ps(a0.0.as_mut_ptr() as *mut f32, m0);
1166 }
1167 assert_eq!([0xffffffff, 0, 0xffffffff, 0], a0.0);
1168 }
1169
1170 glam_test!(test_as, {
1171 use glam::{DVec4, IVec4, UVec4};
1172 assert_eq!(
1173 DVec4::new(-1.0, -2.0, -3.0, -4.0),
1174 Vec4::new(-1.0, -2.0, -3.0, -4.0).as_dvec4()
1175 );
1176 assert_eq!(
1177 IVec4::new(-1, -2, -3, -4),
1178 Vec4::new(-1.0, -2.0, -3.0, -4.0).as_ivec4()
1179 );
1180 assert_eq!(
1181 UVec4::new(1, 2, 3, 4),
1182 Vec4::new(1.0, 2.0, 3.0, 4.0).as_uvec4()
1183 );
1184
1185 assert_eq!(
1186 IVec4::new(-1, -2, -3, -4),
1187 DVec4::new(-1.0, -2.0, -3.0, -4.0).as_ivec4()
1188 );
1189 assert_eq!(
1190 UVec4::new(1, 2, 3, 4),
1191 DVec4::new(1.0, 2.0, 3.0, 4.0).as_uvec4()
1192 );
1193 assert_eq!(
1194 Vec4::new(-1.0, -2.0, -3.0, -4.0),
1195 DVec4::new(-1.0, -2.0, -3.0, -4.0).as_vec4()
1196 );
1197
1198 assert_eq!(
1199 DVec4::new(-1.0, -2.0, -3.0, -4.0),
1200 IVec4::new(-1, -2, -3, -4).as_dvec4()
1201 );
1202 assert_eq!(UVec4::new(1, 2, 3, 4), IVec4::new(1, 2, 3, 4).as_uvec4());
1203 assert_eq!(
1204 Vec4::new(-1.0, -2.0, -3.0, -4.0),
1205 IVec4::new(-1, -2, -3, -4).as_vec4()
1206 );
1207
1208 assert_eq!(
1209 DVec4::new(1.0, 2.0, 3.0, 4.0),
1210 UVec4::new(1, 2, 3, 4).as_dvec4()
1211 );
1212 assert_eq!(IVec4::new(1, 2, 3, 4), UVec4::new(1, 2, 3, 4).as_ivec4());
1213 assert_eq!(
1214 Vec4::new(1.0, 2.0, 3.0, 4.0),
1215 UVec4::new(1, 2, 3, 4).as_vec4()
1216 );
1217 });
1218
1219 glam_test!(test_vec3a, {
1220 use glam::Vec3A;
1221 assert_eq!(
1222 Vec4::new(1.0, 2.0, 3.0, 4.0),
1223 Vec4::from((Vec3A::new(1.0, 2.0, 3.0), 4.0))
1224 );
1225 assert_eq!(
1226 Vec4::new(1.0, 2.0, 3.0, 4.0),
1227 Vec4::from((1.0, Vec3A::new(2.0, 3.0, 4.0)))
1228 );
1229 });
1230
1231 #[cfg(all(
1232 any(target_feature = "sse2", target_feature = "simd128"),
1233 not(feature = "scalar-math")
1234 ))]
1235 impl_vec4_float_tests!(f32, vec4, Vec4, Vec3, Vec2, BVec4A);
1236
1237 #[cfg(any(
1238 not(any(target_feature = "sse2", target_feature = "simd128")),
1239 feature = "scalar-math"
1240 ))]
1241 impl_vec4_float_tests!(f32, vec4, Vec4, Vec3, Vec2, BVec4);
1242 }
1243
1244 mod dvec4 {
1245 use glam::{dvec4, BVec4, DVec2, DVec3, DVec4};
1246
1247 glam_test!(test_align, {
1248 use std::mem;
1249 assert_eq!(32, mem::size_of::<DVec4>());
1250 #[cfg(not(feature = "cuda"))]
1251 assert_eq!(mem::align_of::<f64>(), mem::align_of::<DVec4>());
1252 #[cfg(feature = "cuda")]
1253 assert_eq!(16, mem::align_of::<DVec4>());
1254 assert_eq!(4, mem::size_of::<BVec4>());
1255 assert_eq!(1, mem::align_of::<BVec4>());
1256 });
1257
1258 impl_vec4_float_tests!(f64, dvec4, DVec4, DVec3, DVec2, BVec4);
1259 }
1260
1261 mod ivec4 {
1262 use glam::{ivec4, BVec4, IVec2, IVec3, IVec4, UVec4};
1263
1264 glam_test!(test_align, {
1265 use std::mem;
1266 assert_eq!(16, mem::size_of::<IVec4>());
1267 #[cfg(not(feature = "cuda"))]
1268 assert_eq!(4, mem::align_of::<IVec4>());
1269 #[cfg(feature = "cuda")]
1270 assert_eq!(16, mem::align_of::<IVec4>());
1271 assert_eq!(4, mem::size_of::<BVec4>());
1272 assert_eq!(1, mem::align_of::<BVec4>());
1273 });
1274
1275 impl_vec4_signed_tests!(i32, ivec4, IVec4, IVec3, IVec2, BVec4);
1276 impl_vec4_eq_hash_tests!(i32, ivec4);
1277
1278 impl_vec4_scalar_shift_op_tests!(IVec4, -2, 2);
1279 impl_vec4_shift_op_tests!(IVec4);
1280
1281 impl_vec4_scalar_bit_op_tests!(IVec4, -2, 2);
1282 impl_vec4_bit_op_tests!(IVec4, -2, 2);
1283 }
1284
1285 mod uvec4 {
1286 use glam::{uvec4, BVec4, IVec4, UVec2, UVec3, UVec4};
1287
1288 glam_test!(test_align, {
1289 use std::mem;
1290 assert_eq!(16, mem::size_of::<UVec4>());
1291 #[cfg(not(feature = "cuda"))]
1292 assert_eq!(4, mem::align_of::<UVec4>());
1293 #[cfg(feature = "cuda")]
1294 assert_eq!(16, mem::align_of::<UVec4>());
1295 assert_eq!(4, mem::size_of::<BVec4>());
1296 assert_eq!(1, mem::align_of::<BVec4>());
1297 });
1298
1299 impl_vec4_tests!(u32, uvec4, UVec4, UVec3, UVec2, BVec4);
1300 impl_vec4_eq_hash_tests!(u32, uvec4);
1301
1302 impl_vec4_scalar_shift_op_tests!(UVec4, 0, 2);
1303 impl_vec4_shift_op_tests!(UVec4);
1304
1305 impl_vec4_scalar_bit_op_tests!(UVec4, 0, 2);
1306 impl_vec4_bit_op_tests!(UVec4, 0, 2);
1307 }