From a544fddbc5a4296bf4a3666b89c0c584143f6f56 Mon Sep 17 00:00:00 2001 From: Nan Date: Thu, 11 Apr 2024 10:32:55 +0800 Subject: [PATCH] track sampling tests --- src/track_sampling_job.rs | 188 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) diff --git a/src/track_sampling_job.rs b/src/track_sampling_job.rs index 646ce4a..0dc47af 100644 --- a/src/track_sampling_job.rs +++ b/src/track_sampling_job.rs @@ -133,3 +133,191 @@ where } } +#[cfg(test)] +mod track_sampling_tests { + use glam::{Quat, Vec2, Vec3, Vec4}; + use wasm_bindgen_test::*; + + use super::*; + + #[test] + #[wasm_bindgen_test] + fn test_validity() { + let mut job: TrackSamplingJob = TrackSamplingJob::default(); + assert!(!job.validate()); + assert!(job.run().unwrap_err().is_invalid_job()); + + let mut job: TrackSamplingJob = TrackSamplingJob::default(); + job.set_track(Rc::new(Track::new(0, 0))); + assert!(job.validate()); + assert!(job.run().is_ok()); + } + + fn execute_test(job: &mut TrackSamplingJob, ratio: f32, result: V) + where + V: TrackValue, + T: OzzObj>, + { + job.set_ratio(ratio); + job.run().unwrap(); + assert!( + V::abs_diff_eq(job.result(), result, 1e-5f32), + "{:?} != {:?}", + job.result(), + result + ); + } + + #[test] + #[wasm_bindgen_test] + fn test_bounds() { + let mut job = TrackSamplingJob::default(); + let track = Rc::new(Track::from_raw(&[0.0, 46.0, 0.0, 0.0], &[0.0, 0.5, 0.7, 1.0], &[0x2]).unwrap()); + job.set_track(track.clone()); + + execute_test(&mut job, -1e-7, 0.0); + execute_test(&mut job, 0.0, 0.0); + execute_test(&mut job, 0.5, 46.0); + execute_test(&mut job, 1.0, 0.0); + execute_test(&mut job, 1.0 + 1e-7, 0.0); + execute_test(&mut job, 1.5, 0.0); + } + + #[test] + #[wasm_bindgen_test] + fn test_float() { + let mut job = TrackSamplingJob::default(); + let track = Rc::new(Track::from_raw(&[0.0, 4.6, 9.2, 0.0, 0.0], &[0.0, 0.5, 0.7, 0.9, 1.0], &[0x2]).unwrap()); + job.set_track(track.clone()); + + execute_test(&mut job, 0.0, 0.0); + execute_test(&mut job, 0.25, 2.3); + execute_test(&mut job, 0.5, 4.6); + execute_test(&mut job, 0.6, 4.6); + execute_test(&mut job, 0.7, 9.2); + execute_test(&mut job, 0.8, 4.6); + execute_test(&mut job, 0.9, 0.0); + execute_test(&mut job, 1.0, 0.0); + } + + #[test] + #[wasm_bindgen_test] + fn test_vec2() { + let mut job = TrackSamplingJob::default(); + let track = Rc::new( + Track::from_raw( + &[ + Vec2::new(0.0, 0.0), + Vec2::new(2.3, 4.6), + Vec2::new(4.6, 9.2), + Vec2::new(0.0, 0.0), + Vec2::new(0.0, 0.0), + ], + &[0.0, 0.5, 0.7, 0.9, 1.0], + &[0x2], + ) + .unwrap(), + ); + job.set_track(track.clone()); + + execute_test(&mut job, 0.0, Vec2::new(0.0, 0.0)); + execute_test(&mut job, 0.25, Vec2::new(1.15, 2.3)); + execute_test(&mut job, 0.5, Vec2::new(2.3, 4.6)); + execute_test(&mut job, 0.6, Vec2::new(2.3, 4.6)); + execute_test(&mut job, 0.7, Vec2::new(4.6, 9.2)); + execute_test(&mut job, 0.8, Vec2::new(2.3, 4.6)); + execute_test(&mut job, 0.9, Vec2::new(0.0, 0.0)); + execute_test(&mut job, 1.0, Vec2::new(0.0, 0.0)); + } + + #[test] + #[wasm_bindgen_test] + fn test_vec3() { + let mut job = TrackSamplingJob::default(); + let track = Rc::new( + Track::from_raw( + &[ + Vec3::new(0.0, 0.0, 0.0), + Vec3::new(0.0, 2.3, 4.6), + Vec3::new(0.0, 4.6, 9.2), + Vec3::new(0.0, 0.0, 0.0), + Vec3::new(0.0, 0.0, 0.0), + ], + &[0.0, 0.5, 0.7, 0.9, 1.0], + &[0x2], + ) + .unwrap(), + ); + job.set_track(track.clone()); + + execute_test(&mut job, 0.0, Vec3::new(0.0, 0.0, 0.0)); + execute_test(&mut job, 0.25, Vec3::new(0.0, 1.15, 2.3)); + execute_test(&mut job, 0.5, Vec3::new(0.0, 2.3, 4.6)); + execute_test(&mut job, 0.6, Vec3::new(0.0, 2.3, 4.6)); + execute_test(&mut job, 0.7, Vec3::new(0.0, 4.6, 9.2)); + execute_test(&mut job, 0.8, Vec3::new(0.0, 2.3, 4.6)); + execute_test(&mut job, 0.9, Vec3::new(0.0, 0.0, 0.0)); + execute_test(&mut job, 1.0, Vec3::new(0.0, 0.0, 0.0)); + } + + #[test] + #[wasm_bindgen_test] + fn test_vec4() { + let mut job = TrackSamplingJob::default(); + let track = Rc::new( + Track::from_raw( + &[ + Vec4::new(0.0, 0.0, 0.0, 0.0), + Vec4::new(0.0, 2.3, 0.0, 4.6), + Vec4::new(0.0, 4.6, 0.0, 9.2), + Vec4::new(0.0, 0.0, 0.0, 0.0), + Vec4::new(0.0, 0.0, 0.0, 0.0), + ], + &[0.0, 0.5, 0.7, 0.9, 1.0], + &[0x2], + ) + .unwrap(), + ); + job.set_track(track.clone()); + + execute_test(&mut job, 0.0, Vec4::new(0.0, 0.0, 0.0, 0.0)); + execute_test(&mut job, 0.25, Vec4::new(0.0, 1.15, 0.0, 2.3)); + execute_test(&mut job, 0.5, Vec4::new(0.0, 2.3, 0.0, 4.6)); + execute_test(&mut job, 0.6, Vec4::new(0.0, 2.3, 0.0, 4.6)); + execute_test(&mut job, 0.7, Vec4::new(0.0, 4.6, 0.0, 9.2)); + execute_test(&mut job, 0.8, Vec4::new(0.0, 2.3, 0.0, 4.6)); + execute_test(&mut job, 0.9, Vec4::new(0.0, 0.0, 0.0, 0.0)); + execute_test(&mut job, 1.0, Vec4::new(0.0, 0.0, 0.0, 0.0)); + } + + #[test] + #[wasm_bindgen_test] + fn test_quat() { + let mut job = TrackSamplingJob::default(); + let track = Rc::new( + Track::from_raw( + &[ + Quat::from_xyzw(0.70710677, 0.0, 0.0, 0.70710677), + Quat::from_xyzw(0.0, 0.70710677, 0.0, 0.70710677), + Quat::from_xyzw(0.70710677, 0.0, 0.0, 0.70710677), + Quat::from_xyzw(0.0, 0.0, 0.0, 1.0), + Quat::from_xyzw(0.0, 0.0, 0.0, 1.0), + ], + &[0.0, 0.5, 0.7, 0.9, 1.0], + &[0x2], + ) + .unwrap(), + ); + job.set_track(track.clone()); + + execute_test(&mut job, 0.0, Quat::from_xyzw(0.70710677, 0.0, 0.0, 0.70710677)); + execute_test(&mut job, 0.1, Quat::from_xyzw(0.61721331, 0.15430345, 0.0, 0.77151674)); + execute_test(&mut job, 0.4999999, Quat::from_xyzw(0.0, 0.70710677, 0.0, 0.70710677)); + execute_test(&mut job, 0.5, Quat::from_xyzw(0.0, 0.70710677, 0.0, 0.70710677)); + execute_test(&mut job, 0.6, Quat::from_xyzw(0.0, 0.70710677, 0.0, 0.70710677)); + execute_test(&mut job, 0.7, Quat::from_xyzw(0.70710677, 0.0, 0.0, 0.7071067)); + execute_test(&mut job, 0.8, Quat::from_xyzw(0.38268333, 0.0, 0.0, 0.92387962)); + execute_test(&mut job, 0.9, Quat::from_xyzw(0.0, 0.0, 0.0, 1.0)); + execute_test(&mut job, 1.0, Quat::from_xyzw(0.0, 0.0, 0.0, 1.0)); + } +}