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