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