Add more unit tests for Ray

This commit is contained in:
Matthew Gordon 2019-11-06 17:32:47 -05:00
parent 9ebfae5898
commit 76a7299fb9
1 changed files with 21 additions and 5 deletions

View File

@ -27,17 +27,33 @@ extern crate quickcheck;
extern crate quickcheck_macros; extern crate quickcheck_macros;
mod tests { mod tests {
use super::*; use super::*;
use quickcheck::{Arbitrary,Gen}; use quickcheck::{Arbitrary, Gen};
impl<T:Arbitrary+RealField> Arbitrary for Ray<T> { impl<T: Arbitrary + RealField> Arbitrary for Ray<T> {
fn arbitrary<G:Gen>(g: &mut G) -> Ray<T> { fn arbitrary<G: Gen>(g: &mut G) -> Ray<T> {
let origin = <Vector3<T> as Arbitrary>::arbitrary(g); let origin = <Vector3<T> as Arbitrary>::arbitrary(g);
let direction = <Vector3<T> as Arbitrary>::arbitrary(g); let direction = <Vector3<T> as Arbitrary>::arbitrary(g);
return Ray{origin, direction} return Ray::new(origin, direction);
} }
} }
#[quickcheck] #[quickcheck]
fn test_t0_is_origin(ray:Ray<f64>) -> bool { fn t0_is_origin(ray: Ray<f64>) -> bool {
ray.point_at(0.0) == ray.origin ray.point_at(0.0) == ray.origin
} }
#[quickcheck]
fn t1_is_origin_plus_direction(ray: Ray<f64>) -> bool {
ray.point_at(1.0) == ray.origin + ray.direction
}
#[quickcheck]
fn points_are_colinear(ray: Ray<f64>, t1: f64, t2: f64, t3: f64) -> bool {
let p1 = ray.point_at(t1);
let p2 = ray.point_at(t2);
let p3 = ray.point_at(t3);
let epsilon = [t1, t2, t3, ray.origin[0], ray.origin[1], ray.origin[2]]
.iter()
.fold(0.0, |a, &b| a.max(b.abs())) * std::f64::EPSILON * 128.0;
(p2 - p1).cross(&(p3 - p2)).norm() < epsilon
}
} }