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