2013-05-19 01:04:03 +08:00
|
|
|
#[test]
|
2013-06-10 08:09:36 +08:00
|
|
|
use std::vec;
|
|
|
|
#[test]
|
2013-06-09 20:09:22 +08:00
|
|
|
use std::iterator::IteratorUtil;
|
|
|
|
#[test]
|
2013-06-02 02:50:00 +08:00
|
|
|
use std::num::{Zero, One};
|
2013-05-19 01:04:03 +08:00
|
|
|
#[test]
|
2013-06-02 02:50:00 +08:00
|
|
|
use std::rand::{random};
|
2013-05-19 01:04:03 +08:00
|
|
|
#[test]
|
2013-06-02 02:50:00 +08:00
|
|
|
use std::cmp::ApproxEq;
|
2013-05-19 01:04:03 +08:00
|
|
|
#[test]
|
|
|
|
use dim3::vec3::Vec3;
|
|
|
|
#[test]
|
|
|
|
use dim2::vec2::Vec2;
|
|
|
|
#[test]
|
|
|
|
use dim1::vec1::Vec1;
|
|
|
|
#[test]
|
|
|
|
use ndim::nvec::NVec;
|
|
|
|
#[test]
|
|
|
|
use traits::dim::d7;
|
|
|
|
#[test]
|
|
|
|
use traits::basis::Basis;
|
2013-05-19 05:56:03 +08:00
|
|
|
#[test]
|
|
|
|
use traits::cross::Cross;
|
|
|
|
#[test]
|
|
|
|
use traits::dot::Dot;
|
|
|
|
#[test]
|
|
|
|
use traits::norm::Norm;
|
2013-06-10 08:09:36 +08:00
|
|
|
#[test]
|
|
|
|
use traits::flatten::Flatten;
|
2013-06-19 18:26:59 +08:00
|
|
|
#[test]
|
|
|
|
use traits::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
|
2013-05-19 05:56:03 +08:00
|
|
|
|
2013-05-22 07:15:03 +08:00
|
|
|
macro_rules! test_commut_dot_impl(
|
2013-06-09 22:04:54 +08:00
|
|
|
($t: ty) => (
|
2013-06-02 02:50:00 +08:00
|
|
|
for 10000.times
|
2013-05-22 07:15:03 +08:00
|
|
|
{
|
|
|
|
let v1 : $t = random();
|
|
|
|
let v2 : $t = random();
|
|
|
|
|
|
|
|
assert!(v1.dot(&v2).approx_eq(&v2.dot(&v1)));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
)
|
|
|
|
|
2013-06-19 18:26:59 +08:00
|
|
|
macro_rules! test_scalar_op_impl(
|
|
|
|
($t: ty, $n: ty) => (
|
|
|
|
for 10000.times
|
|
|
|
{
|
|
|
|
let v1 : $t = random();
|
|
|
|
let n : $n = random();
|
|
|
|
|
|
|
|
assert!(v1.scalar_mul(&n).scalar_div(&n).approx_eq(&v1));
|
|
|
|
assert!(v1.scalar_div(&n).scalar_mul(&n).approx_eq(&v1));
|
|
|
|
assert!(v1.scalar_sub(&n).scalar_add(&n).approx_eq(&v1));
|
|
|
|
assert!(v1.scalar_add(&n).scalar_sub(&n).approx_eq(&v1));
|
|
|
|
|
|
|
|
let mut v1 : $t = random();
|
|
|
|
let v0 : $t = copy v1;
|
|
|
|
let n : $n = random();
|
|
|
|
|
|
|
|
v1.scalar_mul_inplace(&n);
|
|
|
|
v1.scalar_div_inplace(&n);
|
|
|
|
|
|
|
|
assert!(v1.approx_eq(&v0));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
)
|
|
|
|
|
2013-05-22 07:15:03 +08:00
|
|
|
macro_rules! test_basis_impl(
|
2013-06-09 22:04:54 +08:00
|
|
|
($t: ty) => (
|
2013-06-02 02:50:00 +08:00
|
|
|
for 10000.times
|
2013-05-22 07:15:03 +08:00
|
|
|
{
|
|
|
|
let basis = Basis::canonical_basis::<$t>();
|
|
|
|
|
|
|
|
// check vectors form an ortogonal basis
|
2013-06-09 20:09:22 +08:00
|
|
|
assert!(
|
|
|
|
do basis.iter().zip(basis.iter()).all
|
|
|
|
|(e1, e2)| { e1 == e2 || e1.dot(e2).approx_eq(&Zero::zero()) }
|
|
|
|
);
|
2013-05-22 07:15:03 +08:00
|
|
|
// check vectors form an orthonormal basis
|
2013-06-24 00:19:13 +08:00
|
|
|
assert!(basis.iter().all(|e| e.norm().approx_eq(&One::one())));
|
2013-05-22 07:15:03 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! test_subspace_basis_impl(
|
2013-06-09 22:04:54 +08:00
|
|
|
($t: ty) => (
|
2013-06-02 02:50:00 +08:00
|
|
|
for 10000.times
|
2013-05-22 07:15:03 +08:00
|
|
|
{
|
2013-06-19 18:26:59 +08:00
|
|
|
let v : $t = random();
|
|
|
|
let v1 = v.normalized();
|
|
|
|
let subbasis = v1.orthogonal_subspace_basis();
|
2013-05-22 07:15:03 +08:00
|
|
|
|
|
|
|
// check vectors are orthogonal to v1
|
2013-06-24 00:19:13 +08:00
|
|
|
assert!(subbasis.iter().all(|e| v1.dot(e).approx_eq(&Zero::zero())));
|
2013-05-22 07:15:03 +08:00
|
|
|
// check vectors form an ortogonal basis
|
2013-06-09 20:09:22 +08:00
|
|
|
assert!(
|
|
|
|
do subbasis.iter().zip(subbasis.iter()).all
|
|
|
|
|(e1, e2)| { e1 == e2 || e1.dot(e2).approx_eq(&Zero::zero()) }
|
|
|
|
);
|
2013-05-22 07:15:03 +08:00
|
|
|
// check vectors form an orthonormal basis
|
2013-06-24 00:19:13 +08:00
|
|
|
assert!(subbasis.iter().all(|e| e.norm().approx_eq(&One::one())));
|
2013-05-22 07:15:03 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
)
|
2013-05-19 01:04:03 +08:00
|
|
|
|
2013-06-10 08:09:36 +08:00
|
|
|
macro_rules! test_flatten_impl(
|
|
|
|
($t: ty, $n: ty) => (
|
|
|
|
for 10000.times
|
|
|
|
{
|
|
|
|
let v: $t = random();
|
|
|
|
let mut l: ~[$n] = vec::from_elem(42 + Flatten::flat_size::<$n, $t>(), Zero::zero::<$n>());
|
|
|
|
|
2013-06-14 00:48:28 +08:00
|
|
|
v.flatten_to(l, 42);
|
2013-06-10 08:09:36 +08:00
|
|
|
|
2013-06-14 00:48:28 +08:00
|
|
|
assert!(Flatten::from_flattened::<$n, $t>(v.flatten(), 0) == v);
|
2013-06-10 08:09:36 +08:00
|
|
|
assert!(Flatten::from_flattened::<$n, $t>(l, 42) == v);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-05-19 01:04:03 +08:00
|
|
|
#[test]
|
|
|
|
fn test_cross_vec3()
|
|
|
|
{
|
2013-06-02 02:50:00 +08:00
|
|
|
for 10000.times
|
2013-05-19 01:04:03 +08:00
|
|
|
{
|
|
|
|
let v1 : Vec3<f64> = random();
|
|
|
|
let v2 : Vec3<f64> = random();
|
|
|
|
let v3 : Vec3<f64> = v1.cross(&v2);
|
|
|
|
|
2013-05-21 23:25:01 +08:00
|
|
|
assert!(v3.dot(&v2).approx_eq(&Zero::zero()));
|
|
|
|
assert!(v3.dot(&v1).approx_eq(&Zero::zero()));
|
2013-05-19 01:04:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-05-22 07:15:03 +08:00
|
|
|
fn test_commut_dot_nvec()
|
|
|
|
{ test_commut_dot_impl!(NVec<d7, f64>); }
|
2013-06-19 18:26:59 +08:00
|
|
|
|
2013-05-19 01:04:03 +08:00
|
|
|
#[test]
|
|
|
|
fn test_commut_dot_vec3()
|
2013-05-22 07:15:03 +08:00
|
|
|
{ test_commut_dot_impl!(Vec3<f64>); }
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_commut_dot_vec2()
|
2013-05-22 07:15:03 +08:00
|
|
|
{ test_commut_dot_impl!(Vec2<f64>); }
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_commut_dot_vec1()
|
2013-05-22 07:15:03 +08:00
|
|
|
{ test_commut_dot_impl!(Vec1<f64>); }
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_basis_vec1()
|
2013-05-22 07:15:03 +08:00
|
|
|
{ test_basis_impl!(Vec1<f64>); }
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_basis_vec2()
|
2013-05-22 07:15:03 +08:00
|
|
|
{ test_basis_impl!(Vec2<f64>); }
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_basis_vec3()
|
2013-05-22 07:15:03 +08:00
|
|
|
{ test_basis_impl!(Vec3<f64>); }
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_basis_nvec()
|
2013-05-22 07:15:03 +08:00
|
|
|
{ test_basis_impl!(NVec<d7, f64>); }
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_subspace_basis_vec1()
|
2013-05-22 07:15:03 +08:00
|
|
|
{ test_subspace_basis_impl!(Vec1<f64>); }
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_subspace_basis_vec2()
|
2013-05-22 07:15:03 +08:00
|
|
|
{ test_subspace_basis_impl!(Vec2<f64>); }
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_subspace_basis_vec3()
|
2013-05-22 07:15:03 +08:00
|
|
|
{ test_subspace_basis_impl!(Vec3<f64>); }
|
2013-05-19 01:04:03 +08:00
|
|
|
|
2013-05-22 07:15:03 +08:00
|
|
|
#[test]
|
|
|
|
fn test_subspace_basis_nvec()
|
|
|
|
{ test_subspace_basis_impl!(NVec<d7, f64>); }
|
2013-06-10 08:09:36 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flatten_vec1()
|
|
|
|
{ test_flatten_impl!(Vec1<f64>, f64); }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flatten_vec2()
|
|
|
|
{ test_flatten_impl!(Vec2<f64>, f64); }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flatten_vec3()
|
|
|
|
{ test_flatten_impl!(Vec3<f64>, f64); }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flatten_nvec()
|
|
|
|
{ test_flatten_impl!(NVec<d7, f64>, f64); }
|
2013-06-19 18:26:59 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_scalar_op_vec1()
|
|
|
|
{ test_scalar_op_impl!(Vec1<f64>, f64); }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_scalar_op_vec2()
|
|
|
|
{ test_scalar_op_impl!(Vec2<f64>, f64); }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_scalar_op_vec3()
|
|
|
|
{ test_scalar_op_impl!(Vec3<f64>, f64); }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_scalar_op_nvec()
|
|
|
|
{ test_scalar_op_impl!(NVec<d7, f64>, f64); }
|