Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

track sampling tests #70

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 188 additions & 0 deletions src/track_sampling_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f32> = TrackSamplingJob::default();
assert!(!job.validate());
assert!(job.run().unwrap_err().is_invalid_job());

let mut job: TrackSamplingJob<Vec3> = TrackSamplingJob::default();
job.set_track(Rc::new(Track::new(0, 0)));
assert!(job.validate());
assert!(job.run().is_ok());
}

fn execute_test<V, T>(job: &mut TrackSamplingJob<V, T>, ratio: f32, result: V)
where
V: TrackValue,
T: OzzObj<Track<V>>,
{
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));
}
}
Loading