From dc399c3c49db2861394c7a21f66dae3a44e193f3 Mon Sep 17 00:00:00 2001 From: James Robert Rooke Date: Fri, 8 May 2020 10:47:46 +0800 Subject: [PATCH] Animation test (#278) * animation test * fix formatting error --- .../cursor_renderer/animation_utils.rs | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) diff --git a/src/renderer/cursor_renderer/animation_utils.rs b/src/renderer/cursor_renderer/animation_utils.rs index 0a410f0..76a6c65 100644 --- a/src/renderer/cursor_renderer/animation_utils.rs +++ b/src/renderer/cursor_renderer/animation_utils.rs @@ -79,3 +79,141 @@ pub fn ease_point(ease_func: fn(f32) -> f32, start: Point, end: Point, t: f32) - y: ease(ease_func, start.y, end.y, t), } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_lerp() { + assert_eq!(lerp(1.0, 0.0, 1.0), 0.0); + } + + #[test] + fn test_ease_linear() { + assert_eq!(ease(ease_linear, 1.0, 0.0, 1.0), 0.0); + } + + #[test] + fn test_ease_in_quad() { + assert_eq!(ease(ease_in_quad, 1.00, 0.0, 1.0), 0.0); + } + + #[test] + fn test_ease_out_quad() { + assert_eq!(ease(ease_out_quad, 1.0, 0.0, 1.0), 0.0); + } + + #[test] + fn test_ease_in_expo() { + assert_eq!(ease(ease_in_expo, 1.0, 0.0, 1.0), 0.0); + assert_eq!(ease(ease_in_expo, 1.0, 0.0, 0.0), 1.0); + } + + #[test] + fn test_ease_out_expo() { + assert_eq!(ease(ease_out_expo, 1.0, 0.0, 1.0), 0.0); + assert_eq!(ease(ease_out_expo, 1.0, 0.0, 1.1), 0.00048828125); + } + + #[test] + fn test_ease_in_out_quad() { + assert_eq!(ease(ease_in_out_quad, 1.0, 0.0, 1.0), 0.0); + assert_eq!(ease(ease_in_out_quad, 1.00, 0.0, 0.4), 0.67999995); + } + + #[test] + fn test_ease_in_cubic() { + assert_eq!(ease(ease_in_cubic, 1.0, 0.0, 1.0), 0.0); + } + + #[test] + fn test_ease_out_cubic() { + assert_eq!(ease(ease_out_cubic, 1.0, 0.0, 1.0), 0.0); + } + + #[test] + fn test_ease_in_out_cubic() { + assert_eq!(ease(ease_in_out_cubic, 1.0, 0.0, 1.0), 0.0); + assert_eq!(ease(ease_in_out_cubic, 1.0, 0.0, 0.25), 0.9375); + } + + #[test] + fn test_ease_point_linear() { + let start = Point { x: 0.0, y: 0.0 }; + let end = Point { x: 1.0, y: 1.0 }; + assert_eq!(ease_point(ease_linear, start, end, 1.0), end); + } + + #[test] + fn test_ease_point_in_quad() { + let start = Point { x: 0.0, y: 0.0 }; + let end = Point { x: 1.0, y: 1.0 }; + assert_eq!(ease_point(ease_in_quad, start, end, 1.0), end); + } + + #[test] + fn test_ease_point_out_quad() { + let start = Point { x: 0.0, y: 0.0 }; + let end = Point { x: 1.0, y: 1.0 }; + assert_eq!(ease_point(ease_out_quad, start, end, 1.0), end); + } + + #[test] + fn test_ease_point_in_out_quad() { + let start = Point { x: 0.0, y: 0.0 }; + let end = Point { x: 1.0, y: 1.0 }; + let expected = Point { + x: 0.68000007, + y: 0.68000007, + }; + assert_eq!(ease_point(ease_in_out_quad, start, end, 1.0), end); + assert_eq!(ease_point(ease_in_out_quad, start, end, 1.4), expected); + } + + #[test] + fn test_ease_point_in_cubic() { + let start = Point { x: 0.0, y: 0.0 }; + let end = Point { x: 1.0, y: 1.0 }; + assert_eq!(ease_point(ease_in_cubic, start, end, 1.0), end); + } + + #[test] + fn test_ease_point_out_cubic() { + let start = Point { x: 0.0, y: 0.0 }; + let end = Point { x: 1.0, y: 1.0 }; + assert_eq!(ease_point(ease_out_cubic, start, end, 1.0), end); + } + + #[test] + fn test_ease_point_in_out_cubic() { + let start = Point { x: 0.0, y: 0.0 }; + let end = Point { x: 1.0, y: 1.0 }; + let expected = Point { + x: 0.0625, + y: 0.0625, + }; + assert_eq!(ease_point(ease_in_out_cubic, start, end, 1.0), end); + assert_eq!(ease_point(ease_in_out_cubic, start, end, 0.25), expected); + } + + #[test] + fn test_ease_point_in_expo() { + let start = Point { x: 0.0, y: 0.0 }; + let end = Point { x: 1.0, y: 1.0 }; + assert_eq!(ease_point(ease_in_expo, start, end, 1.0), end); + assert_eq!(ease_point(ease_in_expo, start, end, 0.0), start); + } + + #[test] + fn test_ease_point_out_expo() { + let start = Point { x: 0.0, y: 0.0 }; + let end = Point { x: 1.0, y: 1.0 }; + let expected = Point { + x: 0.9995117, + y: 0.9995117, + }; + assert_eq!(ease_point(ease_out_expo, start, end, 1.0), end); + assert_eq!(ease_point(ease_out_expo, start, end, 1.1), expected); + } +}