From 9006671a266d66f680d69b6c9af3eab60f1e53c9 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Fri, 30 Jul 2021 21:49:48 -0700 Subject: [PATCH] intersections: moving another doctest to unit --- rtchallenge/src/intersections.rs | 62 ++++++++++++++------------------ 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/rtchallenge/src/intersections.rs b/rtchallenge/src/intersections.rs index cf6b525..e238920 100644 --- a/rtchallenge/src/intersections.rs +++ b/rtchallenge/src/intersections.rs @@ -20,47 +20,12 @@ impl<'i> PartialEq for Intersection<'i> { impl<'i> Intersection<'i> { /// Create new `Intersection` at the given `t` that hits the given `object`. - /// - /// # Examples - /// ``` - /// use rtchallenge::{intersections::Intersection, shapes::Shape}; - /// - /// // An intersection ecapsulates t and object. - /// let s = Shape::sphere(); - /// let i = Intersection::new(3.5, &s); - /// assert_eq!(i.t, 3.5); - /// assert_eq!(i.object, &s); - /// ``` pub fn new(t: Float, object: &Shape) -> Intersection { Intersection { t, object } } } /// Aggregates `Intersection`s. -/// -/// # Examples -/// ``` -/// use rtchallenge::{ -/// intersections::{Intersection, Intersections}, -/// rays::Ray, -/// shapes::{intersect, Shape}, -/// tuples::Tuple, -/// }; -/// -/// let s = Shape::sphere(); -/// let i1 = Intersection::new(1., &s); -/// let i2 = Intersection::new(2., &s); -/// let xs = Intersections::new(vec![i1, i2]); -/// assert_eq!(xs.len(), 2); -/// assert_eq!(xs[0].t, 1.); -/// assert_eq!(xs[1].t, 2.); -/// -/// let r = Ray::new(Tuple::point(0., 0., -5.), Tuple::vector(0., 0., 1.)); -/// let xs = intersect(&s, &r); -/// assert_eq!(xs.len(), 2); -/// assert_eq!(xs[0].object, &s); -/// assert_eq!(xs[1].object, &s); -/// ``` #[derive(Debug, Default, PartialEq)] pub struct Intersections<'i>(Vec>); @@ -209,11 +174,36 @@ mod tests { materials::MaterialBuilder, matrices::{scaling, translation}, rays::Ray, - shapes::{glass_sphere, Shape}, + shapes::{glass_sphere, intersect, Shape}, tuples::{point, vector}, Float, EPSILON, }; + #[test] + fn intersection() { + // An intersection ecapsulates t and object. + let s = Shape::sphere(); + let i = Intersection::new(3.5, &s); + assert_eq!(i.t, 3.5); + assert_eq!(i.object, &s); + } + #[test] + fn intersections() { + let s = Shape::sphere(); + let i1 = Intersection::new(1., &s); + let i2 = Intersection::new(2., &s); + let xs = Intersections::new(vec![i1, i2]); + assert_eq!(xs.len(), 2); + assert_eq!(xs[0].t, 1.); + assert_eq!(xs[1].t, 2.); + + let r = Ray::new(point(0., 0., -5.), vector(0., 0., 1.)); + let xs = intersect(&s, &r); + assert_eq!(xs.len(), 2); + assert_eq!(xs[0].object, &s); + assert_eq!(xs[1].object, &s); + } + mod hit { use super::*;