From f2b356b78d8da558f7cf1685d10f05671c6659ce Mon Sep 17 00:00:00 2001 From: Dimitri Sabadie Date: Tue, 22 Oct 2019 18:13:51 +0200 Subject: [PATCH] Working on tests. --- tests/mod.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) 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() {