intersections: moving another doctest to unit

This commit is contained in:
Bill Thiede 2021-07-30 21:49:48 -07:00
parent 3838efd134
commit 9006671a26

View File

@ -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<Intersection<'i>>);
@ -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::*;