nalgebra/src/tests/mat.rs

129 lines
2.4 KiB
Rust
Raw Normal View History

2013-05-19 01:04:03 +08:00
#[test]
use std::num::{Real, One, abs};
2013-05-19 01:04:03 +08:00
#[test]
use std::rand::random;
2013-05-19 01:04:03 +08:00
#[test]
use std::cmp::ApproxEq;
2013-05-19 01:04:03 +08:00
#[test]
2013-07-23 17:15:20 +08:00
use traits::norm::Norm;
#[test]
use traits::scalar_op::ScalarMul;
#[test]
2013-05-19 05:56:03 +08:00
use traits::inv::Inv;
#[test]
use traits::rotation::{Rotation, Rotatable};
2013-06-10 08:09:36 +08:00
#[test]
use traits::indexable::Indexable;
#[test]
use traits::transpose::Transpose;
#[test]
2013-07-23 17:15:20 +08:00
use vec::{Vec1, Vec3};
2013-05-19 19:53:19 +08:00
#[test]
use mat::{Mat1, Mat2, Mat3, Mat4, Mat5, Mat6};
2013-06-10 08:09:36 +08:00
#[test]
2013-05-19 19:53:19 +08:00
use adaptors::rotmat::Rotmat;
2013-05-19 01:04:03 +08:00
2013-05-22 07:15:03 +08:00
macro_rules! test_inv_mat_impl(
2013-06-09 22:04:54 +08:00
($t: ty) => (
2013-08-02 16:50:04 +08:00
do 10000.times
2013-05-22 07:15:03 +08:00
{
let randmat : $t = random();
assert!((randmat.inverse().unwrap() * randmat).approx_eq(&One::one()));
2013-05-22 07:15:03 +08:00
}
);
)
2013-05-19 01:04:03 +08:00
2013-07-23 17:15:20 +08:00
macro_rules! test_transpose_mat_impl(
($t: ty) => (
2013-08-02 16:50:04 +08:00
do 10000.times
2013-07-23 17:15:20 +08:00
{
let randmat : $t = random();
assert!(randmat.transposed().transposed().eq(&randmat));
}
);
)
#[test]
fn test_transpose_mat1()
{ test_transpose_mat_impl!(Mat1<f64>); }
#[test]
fn test_transpose_mat2()
{ test_transpose_mat_impl!(Mat2<f64>); }
#[test]
fn test_transpose_mat3()
{ test_transpose_mat_impl!(Mat3<f64>); }
#[test]
fn test_transpose_mat4()
{ test_transpose_mat_impl!(Mat4<f64>); }
#[test]
fn test_transpose_mat5()
{ test_transpose_mat_impl!(Mat5<f64>); }
#[test]
fn test_transpose_mat6()
{ test_transpose_mat_impl!(Mat6<f64>); }
2013-05-19 01:04:03 +08:00
#[test]
fn test_inv_mat1()
2013-05-22 07:15:03 +08:00
{ test_inv_mat_impl!(Mat1<f64>); }
2013-05-19 01:04:03 +08:00
#[test]
fn test_inv_mat2()
2013-05-22 07:15:03 +08:00
{ test_inv_mat_impl!(Mat2<f64>); }
2013-05-19 01:04:03 +08:00
#[test]
fn test_inv_mat3()
2013-05-22 07:15:03 +08:00
{ test_inv_mat_impl!(Mat3<f64>); }
2013-05-19 01:04:03 +08:00
#[test]
fn test_inv_mat4()
{ test_inv_mat_impl!(Mat4<f64>); }
#[test]
fn test_inv_mat5()
{ test_inv_mat_impl!(Mat5<f64>); }
#[test]
fn test_inv_mat6()
{ test_inv_mat_impl!(Mat6<f64>); }
2013-05-19 01:04:03 +08:00
#[test]
2013-05-19 19:53:19 +08:00
fn test_rotation2()
2013-05-19 01:04:03 +08:00
{
2013-08-02 16:50:04 +08:00
do 10000.times
2013-05-19 19:53:19 +08:00
{
let randmat = One::one::<Rotmat<Mat2<f64>>>();
let ang = &Vec1::new(abs::<f64>(random()) % Real::pi());
2013-05-19 19:53:19 +08:00
assert!(randmat.rotated(ang).rotation().approx_eq(ang));
2013-05-19 19:53:19 +08:00
}
2013-05-19 01:04:03 +08:00
}
#[test]
fn test_index_mat2()
{
let mat: Mat2<f64> = random();
assert!(mat.at((0, 1)) == mat.transposed().at((1, 0)));
}
2013-07-23 17:15:20 +08:00
#[test]
fn test_inv_rotation3()
{
2013-08-02 16:50:04 +08:00
do 10000.times
2013-07-23 17:15:20 +08:00
{
let randmat = One::one::<Rotmat<Mat3<f64>>>();
let dir: Vec3<f64> = random();
let ang = &dir.normalized().scalar_mul(&(abs::<f64>(random()) % Real::pi()));
let rot = randmat.rotated(ang);
assert!((rot.transposed() * rot).approx_eq(&One::one()));
}
}