diff --git a/tests/mod.rs b/tests/mod.rs index ab98ebb..f9aa8e6 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -1,8 +1,55 @@ -use splines::{Interpolation, Key, Spline}; +use splines::{ Interpolate, Interpolation, Key, Spline}; +use splines::interpolate::{Additive, Linear}; +use std::ops::{Add, Div, Sub, Mul}; #[cfg(feature = "cgmath")] use cgmath as cg; #[cfg(feature = "nalgebra")] use nalgebra as na; +// Small utility 2D-point. +#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] +struct P2 { + x: T, + y: T +} + +impl Add for P2 where T: Add { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + P2 { + x: self.x + rhs.x, + y: self.y + rhs.y + } + } +} + +impl Sub for P2 where T: Sub { + type Output = Self; + + fn sub(self, rhs: Self) -> Self::Output { + P2 { + x: self.x - rhs.x, + y: self.y - rhs.y + } + } +} + +impl Linear for P2 where Self: Additive, T: Mul + Div { + fn outer_mul(self, t: T) -> Self { + P2 { + x: self.x * t, + y: self.y * t, + } + } + + fn outer_div(self, t: T) -> Self { + P2 { + x: self.x / t, + y: self.y / t, + } + } +} + #[test] fn step_interpolation_f32() { let start = Key::new(0., 0., Interpolation::Step(0.)); @@ -149,6 +196,22 @@ fn several_interpolations_several_keys() { assert_eq!(spline.clamped_sample(11.), Some(4.)); } +#[test] +fn stroke_bezier_straight() { + let keys = vec![ + Key::new(0.0, [0., 1.], Interpolation::StrokeBezier([0., 1.], [0., 1.])), + Key::new(5.0, [5., 1.], Interpolation::StrokeBezier([5., 1.], [5., 1.])) + ]; + let spline = Spline::from_vec(keys); + + assert_eq!(spline.clamped_sample(0.0)[1], 1.); + assert_eq!(spline.clamped_sample(1.0)[1], 1.); + assert_eq!(spline.clamped_sample(2.0)[1], 1.); + assert_eq!(spline.clamped_sample(3.0)[1], 1.); + assert_eq!(spline.clamped_sample(4.0)[1], 1.); + assert_eq!(spline.clamped_sample(5.0)[1], 1.); +} + #[cfg(feature = "cgmath")] #[test] fn cgmath_vector_interpolation() {