| 1 |
#[macro_use] |
| 2 |
mod support; |
| 3 |
|
| 4 |
macro_rules! impl_affine2_tests { |
| 5 |
($t:ident, $affine2:ident, $vec2:ident) => { |
| 6 |
const MATRIX2D: [[$t; 2]; 3] = [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]; |
| 7 |
|
| 8 |
use core::$t::NAN; |
| 9 |
use core::$t::NEG_INFINITY; |
| 10 |
|
| 11 |
glam_test!(test_affine2_identity, { |
| 12 |
assert_eq!($affine2::IDENTITY, $affine2::IDENTITY * $affine2::IDENTITY); |
| 13 |
assert_eq!($affine2::IDENTITY, $affine2::default()); |
| 14 |
}); |
| 15 |
|
| 16 |
glam_test!(test_affine2_zero, { |
| 17 |
assert_eq!( |
| 18 |
$affine2::ZERO.transform_point2($vec2::new(1., 2.)), |
| 19 |
$vec2::ZERO |
| 20 |
); |
| 21 |
}); |
| 22 |
|
| 23 |
glam_test!(test_affine2_nan, { |
| 24 |
assert!($affine2::NAN.is_nan()); |
| 25 |
assert!(!$affine2::NAN.is_finite()); |
| 26 |
}); |
| 27 |
|
| 28 |
glam_test!(test_affine2_translation, { |
| 29 |
let translate = $affine2::from_translation($vec2::new(1.0, 2.0)); |
| 30 |
assert_eq!(translate.translation, $vec2::new(1.0, 2.0).into()); |
| 31 |
assert_eq!( |
| 32 |
translate.transform_point2($vec2::new(2.0, 3.0)), |
| 33 |
$vec2::new(3.0, 5.0), |
| 34 |
); |
| 35 |
}); |
| 36 |
|
| 37 |
glam_test!(test_affine2_mul, { |
| 38 |
let m = $affine2::from_angle(deg(90.0)); |
| 39 |
let result3 = m.transform_vector2($vec2::Y); |
| 40 |
assert_approx_eq!($vec2::new(-1.0, 0.0), result3); |
| 41 |
|
| 42 |
let m = $affine2::from_scale_angle_translation( |
| 43 |
$vec2::new(0.5, 1.5), |
| 44 |
deg(90.0), |
| 45 |
$vec2::new(1.0, 2.0), |
| 46 |
); |
| 47 |
let result3 = m.transform_vector2($vec2::Y); |
| 48 |
assert_approx_eq!($vec2::new(-1.5, 0.0), result3, 1.0e-6); |
| 49 |
|
| 50 |
let result3 = m.transform_point2($vec2::Y); |
| 51 |
assert_approx_eq!($vec2::new(-0.5, 2.0), result3, 1.0e-6); |
| 52 |
}); |
| 53 |
|
| 54 |
glam_test!(test_from_scale, { |
| 55 |
let m = $affine2::from_scale($vec2::new(2.0, 4.0)); |
| 56 |
assert_approx_eq!( |
| 57 |
m.transform_point2($vec2::new(1.0, 1.0)), |
| 58 |
$vec2::new(2.0, 4.0) |
| 59 |
); |
| 60 |
}); |
| 61 |
|
| 62 |
glam_test!(test_affine2_inverse, { |
| 63 |
let inv = $affine2::IDENTITY.inverse(); |
| 64 |
assert_approx_eq!($affine2::IDENTITY, inv); |
| 65 |
|
| 66 |
let rot = $affine2::from_angle(deg(90.0)); |
| 67 |
let rot_inv = rot.inverse(); |
| 68 |
assert_approx_eq!($affine2::IDENTITY, rot * rot_inv); |
| 69 |
assert_approx_eq!($affine2::IDENTITY, rot_inv * rot); |
| 70 |
|
| 71 |
let trans = $affine2::from_translation($vec2::new(1.0, 2.0)); |
| 72 |
let trans_inv = trans.inverse(); |
| 73 |
assert_approx_eq!($affine2::IDENTITY, trans * trans_inv); |
| 74 |
assert_approx_eq!($affine2::IDENTITY, trans_inv * trans); |
| 75 |
|
| 76 |
let scale = $affine2::from_scale($vec2::new(4.0, 5.0)); |
| 77 |
let scale_inv = scale.inverse(); |
| 78 |
assert_approx_eq!($affine2::IDENTITY, scale * scale_inv); |
| 79 |
assert_approx_eq!($affine2::IDENTITY, scale_inv * scale); |
| 80 |
|
| 81 |
let m = scale * rot * trans; |
| 82 |
let m_inv = m.inverse(); |
| 83 |
assert_approx_eq!($affine2::IDENTITY, m * m_inv, 1.0e-5); |
| 84 |
assert_approx_eq!($affine2::IDENTITY, m_inv * m, 1.0e-5); |
| 85 |
assert_approx_eq!(m_inv, trans_inv * rot_inv * scale_inv, 1.0e-6); |
| 86 |
|
| 87 |
// Make sure we can invert a shear matrix: |
| 88 |
let m = $affine2::from_angle(0.5) |
| 89 |
* $affine2::from_scale($vec2::new(1.0, 0.5)) |
| 90 |
* $affine2::from_angle(-0.5); |
| 91 |
let m_inv = m.inverse(); |
| 92 |
assert_approx_eq!($affine2::IDENTITY, m * m_inv, 1.0e-5); |
| 93 |
assert_approx_eq!($affine2::IDENTITY, m_inv * m, 1.0e-5); |
| 94 |
|
| 95 |
should_glam_assert!({ $affine2::ZERO.inverse() }); |
| 96 |
}); |
| 97 |
|
| 98 |
glam_test!(test_affine2_ops, { |
| 99 |
let m0 = $affine2::from_cols_array_2d(&MATRIX2D); |
| 100 |
let m0x2 = $affine2::from_cols_array_2d(&[[2.0, 4.0], [6.0, 8.0], [10.0, 12.0]]); |
| 101 |
assert_eq!(m0x2, m0 * 2.0); |
| 102 |
assert_eq!(m0x2, 2.0 * m0); |
| 103 |
assert_eq!(m0x2, m0 + m0); |
| 104 |
assert_eq!($affine2::ZERO, m0 - m0); |
| 105 |
assert_approx_eq!(m0, m0 * $affine2::IDENTITY); |
| 106 |
assert_approx_eq!(m0, $affine2::IDENTITY * m0); |
| 107 |
}); |
| 108 |
|
| 109 |
glam_test!(test_affine2_fmt, { |
| 110 |
let a = $affine2::from_cols_array_2d(&MATRIX2D); |
| 111 |
assert_eq!(format!("{}", a), "[[1, 2], [3, 4], [5, 6]]"); |
| 112 |
}); |
| 113 |
|
| 114 |
glam_test!(test_affine2_to_from_slice, { |
| 115 |
const MATRIX1D: [$t; 6] = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]; |
| 116 |
let m = $affine2::from_cols_slice(&MATRIX1D); |
| 117 |
assert_eq!($affine2::from_cols_array(&MATRIX1D), m); |
| 118 |
assert_eq!(MATRIX1D, m.to_cols_array()); |
| 119 |
assert_eq!(MATRIX2D, m.to_cols_array_2d()); |
| 120 |
let mut out: [$t; 6] = Default::default(); |
| 121 |
m.write_cols_to_slice(&mut out); |
| 122 |
assert_eq!(MATRIX1D, out); |
| 123 |
assert_eq!( |
| 124 |
m, |
| 125 |
$affine2::from_cols(MATRIX2D[0].into(), MATRIX2D[1].into(), MATRIX2D[2].into()) |
| 126 |
); |
| 127 |
|
| 128 |
should_panic!({ $affine2::from_cols_slice(&[0.0; 5]) }); |
| 129 |
should_panic!({ $affine2::IDENTITY.write_cols_to_slice(&mut [0.0; 5]) }); |
| 130 |
}); |
| 131 |
|
| 132 |
glam_test!(test_product, { |
| 133 |
let ident = $affine2::IDENTITY; |
| 134 |
assert_eq!( |
| 135 |
vec![ident, ident].iter().product::<$affine2>(), |
| 136 |
ident * ident |
| 137 |
); |
| 138 |
}); |
| 139 |
|
| 140 |
glam_test!(test_affine2_is_finite, { |
| 141 |
assert!($affine2::from_scale($vec2::new(1.0, 1.0)).is_finite()); |
| 142 |
assert!($affine2::from_scale($vec2::new(0.0, 1.0)).is_finite()); |
| 143 |
assert!(!$affine2::from_scale($vec2::new(1.0, NAN)).is_finite()); |
| 144 |
assert!(!$affine2::from_scale($vec2::new(1.0, NEG_INFINITY)).is_finite()); |
| 145 |
}); |
| 146 |
}; |
| 147 |
} |
| 148 |
|
| 149 |
mod affine2 { |
| 150 |
use super::support::{deg, FloatCompare}; |
| 151 |
use glam::{Affine2, Vec2}; |
| 152 |
|
| 153 |
impl FloatCompare for Affine2 { |
| 154 |
#[inline] |
| 155 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 156 |
self.abs_diff_eq(*other, max_abs_diff) |
| 157 |
} |
| 158 |
#[inline] |
| 159 |
fn abs_diff(&self, other: &Self) -> Self { |
| 160 |
Self { |
| 161 |
matrix2: self.matrix2.abs_diff(&other.matrix2), |
| 162 |
translation: self.translation.abs_diff(&other.translation), |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
glam_test!(test_align, { |
| 168 |
use std::mem; |
| 169 |
if cfg!(not(feature = "scalar-math")) { |
| 170 |
assert_eq!(32, mem::size_of::<Affine2>()); |
| 171 |
assert_eq!(16, mem::align_of::<Affine2>()); |
| 172 |
} else { |
| 173 |
assert_eq!(24, mem::size_of::<Affine2>()); |
| 174 |
assert_eq!(4, mem::align_of::<Affine2>()); |
| 175 |
} |
| 176 |
}); |
| 177 |
|
| 178 |
impl_affine2_tests!(f32, Affine2, Vec2); |
| 179 |
} |
| 180 |
|
| 181 |
mod daffine2 { |
| 182 |
use super::support::{deg, FloatCompare}; |
| 183 |
use glam::{DAffine2, DVec2}; |
| 184 |
|
| 185 |
impl FloatCompare for DAffine2 { |
| 186 |
#[inline] |
| 187 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 188 |
self.abs_diff_eq(*other, max_abs_diff as f64) |
| 189 |
} |
| 190 |
#[inline] |
| 191 |
fn abs_diff(&self, other: &Self) -> Self { |
| 192 |
Self { |
| 193 |
matrix2: self.matrix2.abs_diff(&other.matrix2), |
| 194 |
translation: self.translation.abs_diff(&other.translation), |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
glam_test!(test_align, { |
| 200 |
use std::mem; |
| 201 |
assert_eq!(48, mem::size_of::<DAffine2>()); |
| 202 |
assert_eq!(mem::align_of::<f64>(), mem::align_of::<DAffine2>()); |
| 203 |
}); |
| 204 |
|
| 205 |
impl_affine2_tests!(f64, DAffine2, DVec2); |
| 206 |
} |