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