Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Matrix2D and Matrix4D. #163

Merged
merged 1 commit into from Sep 9, 2016
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Add tests for Matrix2D and Matrix4D.

  • Loading branch information
nical committed Sep 9, 2016
commit 339c99af16519853e9b2a9499cc829d2d4d98e91
@@ -16,6 +16,7 @@ use std::ops::{Add, Mul, Div, Sub};
use std::marker::PhantomData;
use approxeq::ApproxEq;
use trig::Trig;
use std::fmt;

define_matrix! {
/// A 2d transform stored as a 2 by 3 matrix in row-major order in memory,
@@ -293,12 +294,77 @@ impl<T: ApproxEq<T>, Src, Dst> TypedMatrix2D<T, Src, Dst> {
}
}

impl<T: Copy + fmt::Debug, Src, Dst> fmt::Debug for TypedMatrix2D<T, Src, Dst> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.to_row_major_array().fmt(f)
}
}

#[cfg(test)]
mod test {
use super::*;
use approxeq::ApproxEq;
use point::Point2D;
use Radians;

use std::f32::consts::FRAC_PI_2;

type Mat = Matrix2D<f32>;

fn rad(v: f32) -> Radians<f32> { Radians::new(v) }

#[test]
pub fn test_translation() {
let t1 = Mat::create_translation(1.0, 2.0);
let t2 = Mat::identity().pre_translated(1.0, 2.0);
let t3 = Mat::identity().post_translated(1.0, 2.0);
assert_eq!(t1, t2);
assert_eq!(t1, t3);

assert_eq!(t1.transform_point(&Point2D::new(1.0, 1.0)), Point2D::new(2.0, 3.0));

assert_eq!(t1.post_mul(&t1), Mat::create_translation(2.0, 4.0));
}

#[test]
pub fn test_rotation() {
let r1 = Mat::create_rotation(rad(FRAC_PI_2));
let r2 = Mat::identity().pre_rotated(rad(FRAC_PI_2));
let r3 = Mat::identity().post_rotated(rad(FRAC_PI_2));
assert_eq!(r1, r2);
assert_eq!(r1, r3);

assert!(r1.transform_point(&Point2D::new(1.0, 2.0)).approx_eq(&Point2D::new(2.0, -1.0)));

assert!(r1.post_mul(&r1).approx_eq(&Mat::create_rotation(rad(FRAC_PI_2*2.0))));
}

#[test]
pub fn test_scale() {
let s1 = Mat::create_scale(2.0, 3.0);
let s2 = Mat::identity().pre_scaled(2.0, 3.0);
let s3 = Mat::identity().post_scaled(2.0, 3.0);
assert_eq!(s1, s2);
assert_eq!(s1, s3);

assert!(s1.transform_point(&Point2D::new(2.0, 2.0)).approx_eq(&Point2D::new(4.0, 6.0)));
}

#[test]
fn test_column_major() {
assert_eq!(
Mat::row_major(
1.0, 2.0,
3.0, 4.0,
5.0, 6.0
),
Mat::column_major(
1.0, 3.0, 5.0,
2.0, 4.0, 6.0,
)
);
}

#[test]
pub fn test_inverse_simple() {
let m1 = Mat::identity();
@@ -320,10 +386,36 @@ mod test {
assert!(m1.pre_mul(&m2).approx_eq(&Mat::identity()));
}

#[test]
fn test_inverse_none() {
assert!(Mat::create_scale(2.0, 0.0).inverse().is_none());
assert!(Mat::create_scale(2.0, 2.0).inverse().is_some());
}

#[test]
pub fn test_pre_post() {
let m1 = Matrix2D::identity().post_scaled(1.0, 2.0).post_translated(1.0, 2.0);
let m2 = Matrix2D::identity().pre_translated(1.0, 2.0).pre_scaled(1.0, 2.0);
assert!(m1.approx_eq(&m2));

let r = Mat::create_rotation(rad(FRAC_PI_2));
let t = Mat::create_translation(2.0, 3.0);

let a = Point2D::new(1.0, 1.0);

assert!(r.post_mul(&t).transform_point(&a).approx_eq(&Point2D::new(3.0, 2.0)));
assert!(t.post_mul(&r).transform_point(&a).approx_eq(&Point2D::new(4.0, -3.0)));
assert!(t.post_mul(&r).transform_point(&a).approx_eq(&r.transform_point(&t.transform_point(&a))));

assert!(r.pre_mul(&t).transform_point(&a).approx_eq(&Point2D::new(4.0, -3.0)));
assert!(t.pre_mul(&r).transform_point(&a).approx_eq(&Point2D::new(3.0, 2.0)));
assert!(t.pre_mul(&r).transform_point(&a).approx_eq(&t.transform_point(&r.transform_point(&a))));
}

#[test]
fn test_size_of() {
use std::mem::size_of;
assert_eq!(size_of::<Matrix2D<f32>>(), 6*size_of::<f32>());
assert_eq!(size_of::<Matrix2D<f64>>(), 6*size_of::<f64>());
}
}
@@ -553,15 +553,70 @@ impl<T: Copy + fmt::Debug, Src, Dst> fmt::Debug for TypedMatrix4D<T, Src, Dst> {

#[cfg(test)]
mod tests {
use point::Point2D;
use point::{Point2D, Point3D};
use matrix2d::Matrix2D;
use Radians;
use super::*;
use approxeq::ApproxEq;

use std::f32::consts::FRAC_PI_2;

type Mf32 = Matrix4D<f32>;

// For convenience.
fn rad(v: f32) -> Radians<f32> { Radians::new(v) }

#[test]
pub fn test_translation() {
let t1 = Mf32::create_translation(1.0, 2.0, 3.0);
let t2 = Mf32::identity().pre_translated(1.0, 2.0, 3.0);
let t3 = Mf32::identity().post_translated(1.0, 2.0, 3.0);
assert_eq!(t1, t2);
assert_eq!(t1, t3);

assert_eq!(t1.transform_point3d(&Point3D::new(1.0, 1.0, 1.0)), Point3D::new(2.0, 3.0, 4.0));
assert_eq!(t1.transform_point(&Point2D::new(1.0, 1.0)), Point2D::new(2.0, 3.0));

assert_eq!(t1.post_mul(&t1), Mf32::create_translation(2.0, 4.0, 6.0));

assert!(!t1.is_2d());
assert_eq!(Mf32::create_translation(1.0, 2.0, 3.0).to_2d(), Matrix2D::create_translation(1.0, 2.0));
}

#[test]
pub fn test_rotation() {
let r1 = Mf32::create_rotation(0.0, 0.0, 1.0, rad(FRAC_PI_2));
let r2 = Mf32::identity().pre_rotated(0.0, 0.0, 1.0, rad(FRAC_PI_2));
let r3 = Mf32::identity().post_rotated(0.0, 0.0, 1.0, rad(FRAC_PI_2));
assert_eq!(r1, r2);
assert_eq!(r1, r3);

assert!(r1.transform_point3d(&Point3D::new(1.0, 2.0, 3.0)).approx_eq(&Point3D::new(2.0, -1.0, 3.0)));
assert!(r1.transform_point(&Point2D::new(1.0, 2.0)).approx_eq(&Point2D::new(2.0, -1.0)));

assert!(r1.post_mul(&r1).approx_eq(&Mf32::create_rotation(0.0, 0.0, 1.0, rad(FRAC_PI_2*2.0))));

assert!(r1.is_2d());
assert!(r1.to_2d().approx_eq(&Matrix2D::create_rotation(rad(FRAC_PI_2))));
}

#[test]
pub fn test_scale() {
let s1 = Mf32::create_scale(2.0, 3.0, 4.0);
let s2 = Mf32::identity().pre_scaled(2.0, 3.0, 4.0);
let s3 = Mf32::identity().post_scaled(2.0, 3.0, 4.0);
assert_eq!(s1, s2);
assert_eq!(s1, s3);

assert!(s1.transform_point3d(&Point3D::new(2.0, 2.0, 2.0)).approx_eq(&Point3D::new(4.0, 6.0, 8.0)));
assert!(s1.transform_point(&Point2D::new(2.0, 2.0)).approx_eq(&Point2D::new(4.0, 6.0)));

assert_eq!(s1.post_mul(&s1), Mf32::create_scale(4.0, 9.0, 16.0));

assert!(!s1.is_2d());
assert_eq!(Mf32::create_scale(2.0, 3.0, 0.0).to_2d(), Matrix2D::create_scale(2.0, 3.0));
}

#[test]
pub fn test_ortho() {
let (left, right, bottom, top) = (0.0f32, 1.0f32, 0.1f32, 1.0f32);
@@ -596,6 +651,24 @@ mod tests {
assert_eq!(m1, m2);
}

#[test]
fn test_column_major() {
assert_eq!(
Mf32::row_major(
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,
),
Mf32::column_major(
1.0, 5.0, 9.0, 13.0,
2.0, 6.0, 10.0, 14.0,
3.0, 7.0, 11.0, 15.0,
4.0, 8.0, 12.0, 16.0,
)
);
}

#[test]
pub fn test_inverse_simple() {
let m1 = Mf32::identity();
@@ -638,10 +711,36 @@ mod tests {
assert!(p3.eq(&p1));
}

#[test]
fn test_inverse_none() {
assert!(Mf32::create_scale(2.0, 0.0, 2.0).inverse().is_none());
assert!(Mf32::create_scale(2.0, 2.0, 2.0).inverse().is_some());
}

#[test]
pub fn test_pre_post() {
let m1 = Matrix4D::identity().post_scaled(1.0, 2.0, 3.0).post_translated(1.0, 2.0, 3.0);
let m2 = Matrix4D::identity().pre_translated(1.0, 2.0, 3.0).pre_scaled(1.0, 2.0, 3.0);
assert!(m1.approx_eq(&m2));

let r = Mf32::create_rotation(0.0, 0.0, 1.0, rad(FRAC_PI_2));
let t = Mf32::create_translation(2.0, 3.0, 0.0);

let a = Point3D::new(1.0, 1.0, 1.0);

assert!(r.post_mul(&t).transform_point3d(&a).approx_eq(&Point3D::new(3.0, 2.0, 1.0)));
assert!(t.post_mul(&r).transform_point3d(&a).approx_eq(&Point3D::new(4.0, -3.0, 1.0)));
assert!(t.post_mul(&r).transform_point3d(&a).approx_eq(&r.transform_point3d(&t.transform_point3d(&a))));

assert!(r.pre_mul(&t).transform_point3d(&a).approx_eq(&Point3D::new(4.0, -3.0, 1.0)));
assert!(t.pre_mul(&r).transform_point3d(&a).approx_eq(&Point3D::new(3.0, 2.0, 1.0)));
assert!(t.pre_mul(&r).transform_point3d(&a).approx_eq(&t.transform_point3d(&r.transform_point3d(&a))));
}

#[test]
fn test_size_of() {
use std::mem::size_of;
assert_eq!(size_of::<Matrix4D<f32>>(), 16*size_of::<f32>());
assert_eq!(size_of::<Matrix4D<f64>>(), 16*size_of::<f64>());
}
}
@@ -12,7 +12,7 @@ use length::Length;
use scale_factor::ScaleFactor;
use size::TypedSize2D;
use num::*;

use approxeq::ApproxEq;
use num_traits::{Float, NumCast};
use std::fmt;
use std::ops::{Add, Neg, Mul, Sub, Div};
@@ -261,6 +261,23 @@ impl<T: NumCast + Copy, U> TypedPoint2D<T, U> {
}
}

impl<T: Copy+ApproxEq<T>, U> ApproxEq<TypedPoint2D<T, U>> for TypedPoint2D<T, U> {
#[inline]
fn approx_epsilon() -> Self {
TypedPoint2D::new(T::approx_epsilon(), T::approx_epsilon())
}

#[inline]
fn approx_eq(&self, other: &Self) -> bool {
self.x.approx_eq(&other.x) && self.y.approx_eq(&other.y)
}

#[inline]
fn approx_eq_eps(&self, other: &Self, eps: &Self) -> bool {
self.x.approx_eq_eps(&other.x, &eps.x) && self.y.approx_eq_eps(&other.y, &eps.y)
}
}

define_matrix! {
/// A 3d Point tagged with a unit.
#[derive(RustcDecodable, RustcEncodable)]
@@ -475,6 +492,27 @@ impl<T: NumCast + Copy, U> TypedPoint3D<T, U> {
}
}

impl<T: Copy+ApproxEq<T>, U> ApproxEq<TypedPoint3D<T, U>> for TypedPoint3D<T, U> {
#[inline]
fn approx_epsilon() -> Self {
TypedPoint3D::new(T::approx_epsilon(), T::approx_epsilon(), T::approx_epsilon())
}

#[inline]
fn approx_eq(&self, other: &Self) -> bool {
self.x.approx_eq(&other.x)
&& self.y.approx_eq(&other.y)
&& self.z.approx_eq(&other.z)
}

#[inline]
fn approx_eq_eps(&self, other: &Self, eps: &Self) -> bool {
self.x.approx_eq_eps(&other.x, &eps.x)
&& self.y.approx_eq_eps(&other.y, &eps.y)
&& self.z.approx_eq_eps(&other.z, &eps.z)
}
}

define_matrix! {
/// A 4d Point tagged with a unit.
#[derive(RustcDecodable, RustcEncodable)]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.