Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitshifter committed Aug 2, 2024
1 parent 0d93d4e commit dd7d2f2
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 74 deletions.
71 changes: 44 additions & 27 deletions tests/mat2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ mod support;
macro_rules! impl_mat2_tests {
($t:ident, $newmat2:ident, $mat2:ident, $mat3:ident, $newvec2:ident, $vec2:ident) => {
const IDENTITY: [[$t; 2]; 2] = [[1.0, 0.0], [0.0, 1.0]];

const MATRIX: [[$t; 2]; 2] = [[1.0, 2.0], [3.0, 4.0]];

const MATRIX1D: [$t; 4] = [1.0, 2.0, 3.0, 4.0];
const ARRAY2X2: [[$t; 2]; 2] = [[1.0, 2.0], [3.0, 4.0]];
const ARRAY1X4: [$t; 4] = [1.0, 2.0, 3.0, 4.0];
const ARRAY3X3: [[$t; 3]; 3] = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];

glam_test!(test_const, {
const M0: $mat2 = $mat2::from_cols($newvec2(1.0, 2.0), $newvec2(3.0, 4.0));
const M1: $mat2 = $mat2::from_cols_array(&MATRIX1D);
const M2: $mat2 = $mat2::from_cols_array_2d(&MATRIX);
const M1: $mat2 = $mat2::from_cols_array(&ARRAY1X4);
const M2: $mat2 = $mat2::from_cols_array_2d(&ARRAY2X2);

assert_eq!(MATRIX1D, M0.to_cols_array());
assert_eq!(MATRIX1D, M1.to_cols_array());
assert_eq!(MATRIX1D, M2.to_cols_array());
assert_eq!(ARRAY1X4, M0.to_cols_array());
assert_eq!(ARRAY1X4, M1.to_cols_array());
assert_eq!(ARRAY1X4, M2.to_cols_array());
});

glam_test!(test_mat2_identity, {
Expand All @@ -42,7 +41,7 @@ macro_rules! impl_mat2_tests {
let mut m = $mat2::ZERO;
m.x_axis = $vec2::new(1.0, 2.0);
m.y_axis = $vec2::new(3.0, 4.0);
assert_eq!($mat2::from_cols_array_2d(&MATRIX), m);
assert_eq!($mat2::from_cols_array_2d(&ARRAY2X2), m);
assert_eq!($vec2::new(1.0, 2.0), m.x_axis);
assert_eq!($vec2::new(3.0, 4.0), m.y_axis);

Expand All @@ -66,8 +65,8 @@ macro_rules! impl_mat2_tests {
});

glam_test!(test_mat2_from_axes, {
let a = $mat2::from_cols_array_2d(&[[1.0, 2.0], [3.0, 4.0]]);
assert_eq!(MATRIX, a.to_cols_array_2d());
let a = $mat2::from_cols_array_2d(&ARRAY2X2);
assert_eq!(ARRAY2X2, a.to_cols_array_2d());
let b = $mat2::from_cols($newvec2(1.0, 2.0), $newvec2(3.0, 4.0));
assert_eq!(a, b);
let c = $newmat2($newvec2(1.0, 2.0), $newvec2(3.0, 4.0));
Expand Down Expand Up @@ -103,12 +102,21 @@ macro_rules! impl_mat2_tests {
});

glam_test!(test_from_mat3, {
let m3 =
$mat3::from_cols_array_2d(&[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]);
let m3 = $mat3::from_cols_array_2d(&ARRAY3X3);
let m2 = $mat2::from_mat3(m3);
assert_eq!($mat2::from_cols_array_2d(&[[1.0, 2.0], [4.0, 5.0]]), m2);
});

glam_test!(test_from_mat3_minor, {
let m3 = $mat3::from_cols_array_2d(&ARRAY3X3);
for i in 0..3 {
for j in 0..3 {
let m2 = $mat2::from_mat3_minor(m3, i, j);
test_matrix_minor!(3, m2, m3, i, j);
}
}
});

glam_test!(test_mat2_transpose, {
let m = $newmat2($newvec2(1.0, 2.0), $newvec2(3.0, 4.0));
let mt = m.transpose();
Expand All @@ -128,7 +136,7 @@ macro_rules! impl_mat2_tests {
);
assert_eq!(
1.0 * 4.0 - 2.0 * 3.0,
$mat2::from_cols_array(&[1.0, 2.0, 3.0, 4.0]).determinant()
$mat2::from_cols_array(&ARRAY1X4).determinant()
);
});

Expand Down Expand Up @@ -156,7 +164,7 @@ macro_rules! impl_mat2_tests {
});

glam_test!(test_mat2_ops, {
let m0 = $mat2::from_cols_array_2d(&MATRIX);
let m0 = $mat2::from_cols_array_2d(&ARRAY2X2);
let m0x2 = $mat2::from_cols_array_2d(&[[2.0, 4.0], [6.0, 8.0]]);
let m0_neg = $mat2::from_cols_array_2d(&[[-1.0, -2.0], [-3.0, -4.0]]);
assert_eq!(m0x2, m0 * 2.0);
Expand Down Expand Up @@ -191,17 +199,17 @@ macro_rules! impl_mat2_tests {
});

glam_test!(test_mat2_fmt, {
let a = $mat2::from_cols_array_2d(&MATRIX);
let a = $mat2::from_cols_array_2d(&ARRAY2X2);
assert_eq!(format!("{}", a), "[[1, 2], [3, 4]]");
assert_eq!(format!("{:.2}", a), "[[1.00, 2.00], [3.00, 4.00]]");
});

glam_test!(test_mat2_to_from_slice, {
let m = $mat2::from_cols_slice(&MATRIX1D);
assert_eq!($mat2::from_cols_array(&MATRIX1D), m);
let m = $mat2::from_cols_slice(&ARRAY1X4);
assert_eq!($mat2::from_cols_array(&ARRAY1X4), m);
let mut out: [$t; 4] = Default::default();
m.write_cols_to_slice(&mut out);
assert_eq!(MATRIX1D, out);
assert_eq!(ARRAY1X4, out);

should_panic!({ $mat2::from_cols_slice(&[0.0; 3]) });
should_panic!({ $mat2::IDENTITY.write_cols_to_slice(&mut [0.0; 3]) });
Expand Down Expand Up @@ -231,20 +239,20 @@ macro_rules! impl_mat2_tests {
macro_rules! impl_as_ref_tests {
($mat:ident) => {
glam_test!(test_as_ref, {
let m = $mat::from_cols_array_2d(&MATRIX);
assert_eq!(MATRIX1D, *m.as_ref());
let m = $mat::from_cols_array_2d(&ARRAY2X2);
assert_eq!(ARRAY1X4, *m.as_ref());
});
glam_test!(test_as_mut, {
let mut m = $mat::ZERO;
*m.as_mut() = MATRIX1D;
assert_eq!($mat::from_cols_array_2d(&MATRIX), m);
*m.as_mut() = ARRAY1X4;
assert_eq!($mat::from_cols_array_2d(&ARRAY2X2), m);
});
};
}

mod mat2 {
use super::support::deg;
use glam::{mat2, swizzles::*, vec2, Mat2, Mat3, Vec2};
use glam::{mat2, swizzles::*, vec2, Mat2, Mat3, Mat3A, Vec2};

glam_test!(test_align, {
use std::mem;
Expand All @@ -257,12 +265,21 @@ mod mat2 {
});

glam_test!(test_from_mat3a, {
use glam::Mat3A;
let m3 = Mat3A::from_cols_array_2d(&[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]);
let m3 = Mat3A::from_cols_array_2d(&ARRAY3X3);
let m2 = Mat2::from_mat3a(m3);
assert_eq!(Mat2::from_cols_array_2d(&[[1.0, 2.0], [4.0, 5.0]]), m2);
});

glam_test!(test_from_mat3a_minor, {
let m3 = Mat3A::from_cols_array_2d(&ARRAY3X3);
for i in 0..3 {
for j in 0..3 {
let m2 = Mat2::from_mat3a_minor(m3, i, j);
test_matrix_minor!(3, m2, m3, i, j);
}
}
});

glam_test!(test_as, {
use glam::DMat2;
assert_eq!(
Expand Down
61 changes: 35 additions & 26 deletions tests/mat3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@ mod support;
macro_rules! impl_mat3_tests {
($t:ident, $newmat3:ident, $mat3:ident, $mat2:ident, $mat4:ident, $quat:ident, $newvec3:ident, $vec3:ident, $vec2:ident) => {
const IDENTITY: [[$t; 3]; 3] = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];

const MATRIX: [[$t; 3]; 3] = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];

const MATRIX1D: [$t; 9] = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0];
const ARRAY3X3: [[$t; 3]; 3] = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];
const ARRAY1X9: [$t; 9] = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0];
const ARRAY4X4: [[$t; 4]; 4] = [
[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
[9.0, 10.0, 11.0, 12.0],
[13.0, 14.0, 15.0, 16.0],
];

glam_test!(test_const, {
const M0: $mat3 = $mat3::from_cols(
$newvec3(1.0, 2.0, 3.0),
$newvec3(4.0, 5.0, 6.0),
$newvec3(7.0, 8.0, 9.0),
);
const M1: $mat3 = $mat3::from_cols_array(&MATRIX1D);
const M2: $mat3 = $mat3::from_cols_array_2d(&MATRIX);
const M1: $mat3 = $mat3::from_cols_array(&ARRAY1X9);
const M2: $mat3 = $mat3::from_cols_array_2d(&ARRAY3X3);

assert_eq!(MATRIX1D, M0.to_cols_array());
assert_eq!(MATRIX1D, M1.to_cols_array());
assert_eq!(MATRIX1D, M2.to_cols_array());
assert_eq!(ARRAY1X9, M0.to_cols_array());
assert_eq!(ARRAY1X9, M1.to_cols_array());
assert_eq!(ARRAY1X9, M2.to_cols_array());
});

glam_test!(test_mat3_identity, {
Expand Down Expand Up @@ -57,7 +61,7 @@ macro_rules! impl_mat3_tests {
m.x_axis = $newvec3(1.0, 2.0, 3.0);
m.y_axis = $newvec3(4.0, 5.0, 6.0);
m.z_axis = $newvec3(7.0, 8.0, 9.0);
assert_eq!($mat3::from_cols_array_2d(&MATRIX), m);
assert_eq!($mat3::from_cols_array_2d(&ARRAY3X3), m);
assert_eq!($newvec3(1.0, 2.0, 3.0), m.x_axis);
assert_eq!($newvec3(4.0, 5.0, 6.0), m.y_axis);
assert_eq!($newvec3(7.0, 8.0, 9.0), m.z_axis);
Expand Down Expand Up @@ -87,7 +91,7 @@ macro_rules! impl_mat3_tests {

glam_test!(test_mat3_from_axes, {
let a = $mat3::from_cols_array_2d(&[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]);
assert_eq!(MATRIX, a.to_cols_array_2d());
assert_eq!(ARRAY3X3, a.to_cols_array_2d());
let b = $mat3::from_cols(
$newvec3(1.0, 2.0, 3.0),
$newvec3(4.0, 5.0, 6.0),
Expand Down Expand Up @@ -214,19 +218,24 @@ macro_rules! impl_mat3_tests {
});

glam_test!(test_from_mat4, {
let m4 = $mat4::from_cols_array_2d(&[
[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
[9.0, 10.0, 11.0, 12.0],
[13.0, 14.0, 15.0, 16.0],
]);
let m4 = $mat4::from_cols_array_2d(&ARRAY4X4);
let m3 = $mat3::from_mat4(m4);
assert_eq!(
$mat3::from_cols_array_2d(&[[1.0, 2.0, 3.0], [5.0, 6.0, 7.0], [9.0, 10.0, 11.0]]),
m3
);
});

glam_test!(test_from_mat4_minor, {
let m4 = $mat4::from_cols_array_2d(&ARRAY4X4);
for i in 0..4 {
for j in 0..4 {
let m3 = $mat3::from_mat4_minor(m4, i, j);
test_matrix_minor!(4, m3, m4, i, j);
}
}
});

glam_test!(test_mat3_transpose, {
let m = $newmat3(
$newvec3(1.0, 2.0, 3.0),
Expand Down Expand Up @@ -283,7 +292,7 @@ macro_rules! impl_mat3_tests {
});

glam_test!(test_mat3_ops, {
let m0 = $mat3::from_cols_array_2d(&MATRIX);
let m0 = $mat3::from_cols_array_2d(&ARRAY3X3);
let m0x2 = $mat3::from_cols_array_2d(&[
[2.0, 4.0, 6.0],
[8.0, 10.0, 12.0],
Expand Down Expand Up @@ -326,7 +335,7 @@ macro_rules! impl_mat3_tests {
});

glam_test!(test_mat3_fmt, {
let a = $mat3::from_cols_array_2d(&MATRIX);
let a = $mat3::from_cols_array_2d(&ARRAY3X3);
assert_eq!(format!("{}", a), "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]");
assert_eq!(
format!("{:.1}", a),
Expand All @@ -335,11 +344,11 @@ macro_rules! impl_mat3_tests {
});

glam_test!(test_mat3_to_from_slice, {
let m = $mat3::from_cols_slice(&MATRIX1D);
assert_eq!($mat3::from_cols_array(&MATRIX1D), m);
let m = $mat3::from_cols_slice(&ARRAY1X9);
assert_eq!($mat3::from_cols_array(&ARRAY1X9), m);
let mut out: [$t; 9] = Default::default();
m.write_cols_to_slice(&mut out);
assert_eq!(MATRIX1D, out);
assert_eq!(ARRAY1X9, out);

should_panic!({ $mat3::from_cols_slice(&[0.0; 8]) });
should_panic!({ $mat3::IDENTITY.write_cols_to_slice(&mut [0.0; 8]) });
Expand Down Expand Up @@ -369,13 +378,13 @@ macro_rules! impl_mat3_tests {
macro_rules! impl_as_ref_tests {
($mat:ident) => {
glam_test!(test_as_ref, {
let m = $mat::from_cols_array_2d(&MATRIX);
assert_eq!(MATRIX1D, *m.as_ref());
let m = $mat::from_cols_array_2d(&ARRAY3X3);
assert_eq!(ARRAY1X9, *m.as_ref());
});
glam_test!(test_as_mut, {
let mut m = $mat::ZERO;
*m.as_mut() = MATRIX1D;
assert_eq!($mat::from_cols_array_2d(&MATRIX), m);
*m.as_mut() = ARRAY1X9;
assert_eq!($mat::from_cols_array_2d(&ARRAY3X3), m);
});
};
}
Expand Down
Loading

0 comments on commit dd7d2f2

Please sign in to comment.