Debian Codegraph @

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