Skip to content

Commit

Permalink
Merge pull request #45 from SlimeYummy/feat/tests
Browse files Browse the repository at this point in the history
fix tests
  • Loading branch information
SlimeYummy authored Mar 2, 2024
2 parents 3c35d50 + a4b40a9 commit 1764465
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 36 deletions.
18 changes: 10 additions & 8 deletions tests/additive.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use glam::{Mat4, Vec4};
use ozz_animation_rs::*;
use ozz_animation_rs::math::*;
use ozz_animation_rs::*;
use std::rc::Rc;

mod common;

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
struct TestData {
Expand All @@ -27,7 +29,7 @@ fn test_additive() {
run_additive(
5..=5,
|_| {},
|_, data| test_utils::compare_with_cpp("additive", "additive", &data.l2m_out, 1e-6).unwrap(),
|_, data| common::compare_with_cpp("additive", "additive", &data.l2m_out, 1e-6).unwrap(),
);
}

Expand All @@ -36,8 +38,8 @@ fn test_additive() {
fn test_additive_deterministic() {
run_additive(
-1..=11,
|data| test_utils::compare_with_rkyv("additive", "additive_init", data).unwrap(),
|ratio, data| test_utils::compare_with_rkyv("additive", &format!("additive{:+.2}", ratio), data).unwrap(),
|data| common::compare_with_rkyv("additive", "additive_init", data).unwrap(),
|ratio, data| common::compare_with_rkyv("additive", &format!("additive{:+.2}", ratio), data).unwrap(),
);
}

Expand All @@ -47,10 +49,10 @@ where
T1: Fn(&TestDataInit),
T2: Fn(f32, &TestData),
{
let skeleton = Rc::new(Skeleton::from_file("./resource/additive/skeleton.ozz").unwrap());
let animation_base = Rc::new(Animation::from_file("./resource/additive/animation_base.ozz").unwrap());
let animation_splay = Rc::new(Animation::from_file("./resource/additive/animation_splay_additive.ozz").unwrap());
let animation_curl = Rc::new(Animation::from_file("./resource/additive/animation_curl_additive.ozz").unwrap());
let skeleton = Rc::new(Skeleton::from_path("./resource/additive/skeleton.ozz").unwrap());
let animation_base = Rc::new(Animation::from_path("./resource/additive/animation_base.ozz").unwrap());
let animation_splay = Rc::new(Animation::from_path("./resource/additive/animation_splay_additive.ozz").unwrap());
let animation_curl = Rc::new(Animation::from_path("./resource/additive/animation_curl_additive.ozz").unwrap());

let mut sample_job_base: SamplingJob = SamplingJob::default();
sample_job_base.set_animation(animation_base.clone());
Expand Down
18 changes: 10 additions & 8 deletions tests/blend.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use glam::Mat4;
use ozz_animation_rs::*;
use ozz_animation_rs::math::*;
use ozz_animation_rs::*;
use std::rc::Rc;

mod common;

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
struct TestData {
Expand All @@ -20,15 +22,15 @@ struct TestData {
#[test]
fn test_blend() {
run_blend(2..=2, |_, data| {
test_utils::compare_with_cpp("blend", "blend", &data.l2m_out, 1e-6).unwrap();
common::compare_with_cpp("blend", "blend", &data.l2m_out, 1e-6).unwrap();
});
}

#[cfg(feature = "rkyv")]
#[test]
fn test_blend_deterministic() {
run_blend(-1..=11, |ratio, data| {
test_utils::compare_with_rkyv("blend", &format!("blend{:+.2}", ratio), data).unwrap();
common::compare_with_rkyv("blend", &format!("blend{:+.2}", ratio), data).unwrap();
});
}

Expand All @@ -37,10 +39,10 @@ where
I: Iterator<Item = i32>,
T: Fn(f32, &TestData),
{
let skeleton = Rc::new(Skeleton::from_file("./resource/blend/skeleton.ozz").unwrap());
let animation1 = Rc::new(Animation::from_file("./resource/blend/animation1.ozz").unwrap());
let animation2 = Rc::new(Animation::from_file("./resource/blend/animation2.ozz").unwrap());
let animation3 = Rc::new(Animation::from_file("./resource/blend/animation3.ozz").unwrap());
let skeleton = Rc::new(Skeleton::from_path("./resource/blend/skeleton.ozz").unwrap());
let animation1 = Rc::new(Animation::from_path("./resource/blend/animation1.ozz").unwrap());
let animation2 = Rc::new(Animation::from_path("./resource/blend/animation2.ozz").unwrap());
let animation3 = Rc::new(Animation::from_path("./resource/blend/animation3.ozz").unwrap());

let mut sample_job1: SamplingJob = SamplingJob::default();
sample_job1.set_animation(animation1.clone());
Expand Down Expand Up @@ -86,8 +88,8 @@ where

blending_job.layers_mut()[0].weight = (1.0 - 2.0 * ratio).clamp(0.0, 1.0); // 0%=1.0, 50%=0.0, 100%=0.0
blending_job.layers_mut()[2].weight = (2.0 * ratio - 1.0).clamp(0.0, 1.0); // 0%=0.0, 50%=0.0, 100%=1.0
// 0%=0.0, 50%=1.0, 100%=0.0
if ratio < 0.5 {
// 0%=0.0, 50%=1.0, 100%=0.0
blending_job.layers_mut()[1].weight = (2.0 * ratio).clamp(0.0, 1.0);
} else {
blending_job.layers_mut()[1].weight = (2.0 * (1.0 - ratio)).clamp(0.0, 1.0);
Expand Down
86 changes: 86 additions & 0 deletions tests/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use glam::Mat4;
use std::env::consts::{ARCH, OS};
use std::error::Error;
use std::fs::{self, File};
use std::io::prelude::*;
use std::{env, mem, slice};

pub fn compare_with_cpp(folder: &str, name: &str, data: &[Mat4], diff: f32) -> Result<(), Box<dyn Error>> {
fs::create_dir_all(format!("./expected/{}", folder)).unwrap();
fs::create_dir_all(format!("./output/{}", folder)).unwrap();

let path = format!("./output/{0}/{1}_rust_{2}_{3}.bin", folder, name, OS, ARCH);
let mut file = File::create(path)?;
let data_size = data.len() * mem::size_of::<Mat4>();
file.write_all(unsafe { slice::from_raw_parts(data.as_ptr() as *mut _, data_size) })?;

let path = format!("./expected/{0}/{1}_cpp.bin", folder, name);
let mut file = File::open(&path)?;
if file.metadata()?.len() != data_size as u64 {
return Err(format!("compare_with_cpp() size:{}", data_size).into());
}

let mut expected: Vec<Mat4> = vec![Mat4::default(); data.len()];
file.read(unsafe { slice::from_raw_parts_mut(expected.as_mut_ptr() as *mut _, data_size) })?;
for i in 0..expected.len() {
if !Mat4::abs_diff_eq(&data[i], expected[i], diff) {
println!("actual: {:?}", data[i]);
println!("expected: {:?}", expected[i]);
return Err(format!("compare_with_cpp() idx:{}", i).into());
}
}
return Ok(());
}

#[cfg(feature = "rkyv")]
pub fn compare_with_rkyv<T>(folder: &str, name: &str, data: &T) -> Result<(), Box<dyn Error>>
where
T: PartialEq + rkyv::Serialize<rkyv::ser::serializers::AllocSerializer<30720>>,
T::Archived: rkyv::Deserialize<T, rkyv::Infallible>,
{
use miniz_oxide::deflate::compress_to_vec;
use miniz_oxide::inflate::decompress_to_vec;
use rkyv::ser::Serializer;
use rkyv::{AlignedVec, Deserialize};

fs::create_dir_all(format!("./expected/{}", folder)).unwrap();
fs::create_dir_all(format!("./output/{}", folder)).unwrap();

let to_expected = env::var("SAVE_TO_EXPECTED").is_ok();

let mut serializer = rkyv::ser::serializers::AllocSerializer::<30720>::default();
serializer.serialize_value(data)?;
let current_buf = serializer.into_serializer().into_inner();
let wbuf = compress_to_vec(&current_buf, 6);
let path = if to_expected {
format!("./expected/{0}/{1}.rkyv", folder, name)
} else {
format!("./output/{0}/{1}_{2}_{3}.rkyv", folder, name, OS, ARCH)
};
let mut file = File::create(path)?;
file.write_all(&wbuf)?;

if !to_expected {
let path = format!("./expected/{0}/{1}.rkyv", folder, name);
let mut file = File::open(&path)?;
let size = file.metadata().map(|m| m.len()).unwrap_or(0);
let mut rbuf = Vec::with_capacity(size as usize);
file.read_to_end(&mut rbuf)?;
let unaligned_buf = decompress_to_vec(&rbuf).map_err(|e| e.to_string())?;
let mut expected_buf = AlignedVec::new();
expected_buf.extend_from_slice(&unaligned_buf);

let archived = unsafe { rkyv::archived_root::<T>(&expected_buf) };
let mut deserializer = rkyv::Infallible::default();
let expected = archived.deserialize(&mut deserializer)?;
if data != &expected {
return Err(format!("compare_with_rkyv({})", path).into());
}
}
return Ok(());
}

#[cfg(not(feature = "rkyv"))]
pub fn compare_with_rkyv<T>(_folder: &str, _name: &str, _data: &T) -> Result<(), Box<dyn Error>> {
return Ok(());
}
12 changes: 7 additions & 5 deletions tests/look_at.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use glam::{Mat4, Quat, Vec3A};
use ozz_animation_rs::*;
use ozz_animation_rs::math::*;
use ozz_animation_rs::*;
use std::rc::Rc;

mod common;

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
struct TestData {
Expand All @@ -23,15 +25,15 @@ const EYES_OFFSET: Vec3A = Vec3A::new(0.07, 0.1, 0.0);
#[test]
fn test_look_at() {
run_look_at(1..=1, |_, data| {
test_utils::compare_with_cpp("look_at", "look_at", &data.models2, 1.5e-4).unwrap();
common::compare_with_cpp("look_at", "look_at", &data.models2, 1.5e-4).unwrap();
});
}

#[cfg(feature = "rkyv")]
#[test]
fn test_look_at_deterministic() {
run_look_at(0..=10, |idx, data| {
test_utils::compare_with_rkyv("look_at", &format!("look_at_{:02}", idx), data).unwrap();
common::compare_with_rkyv("look_at", &format!("look_at_{:02}", idx), data).unwrap();
});
}

Expand All @@ -40,8 +42,8 @@ where
I: Iterator<Item = i32>,
T: Fn(i32, &TestData),
{
let skeleton = Rc::new(Skeleton::from_file("./resource/look_at/skeleton.ozz").unwrap());
let animation = Rc::new(Animation::from_file("./resource/look_at/animation.ozz").unwrap());
let skeleton = Rc::new(Skeleton::from_path("./resource/look_at/skeleton.ozz").unwrap());
let animation = Rc::new(Animation::from_path("./resource/look_at/animation.ozz").unwrap());

let joints_chain = [
skeleton.joint_by_name(JOINT_NAMES[0]).unwrap(),
Expand Down
14 changes: 8 additions & 6 deletions tests/partial_blend.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use glam::{Mat4, Vec4};
use ozz_animation_rs::*;
use ozz_animation_rs::math::*;
use ozz_animation_rs::*;
use std::rc::Rc;

mod common;

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
struct TestData {
Expand All @@ -18,15 +20,15 @@ struct TestData {
#[test]
fn test_partial_blend() {
run_partial_blend(5..=5, |_, data| {
test_utils::compare_with_cpp("partial_blend", "partial_blend", &data.l2m_out, 1e-5).unwrap();
common::compare_with_cpp("partial_blend", "partial_blend", &data.l2m_out, 1e-5).unwrap();
});
}

#[cfg(feature = "rkyv")]
#[test]
fn test_partial_blend_deterministic() {
run_partial_blend(-1..=11, |ratio, data| {
test_utils::compare_with_rkyv("partial_blend", &format!("partial_blend{:+.2}", ratio), data).unwrap();
common::compare_with_rkyv("partial_blend", &format!("partial_blend{:+.2}", ratio), data).unwrap();
});
}

Expand All @@ -35,9 +37,9 @@ where
I: Iterator<Item = i32>,
T: Fn(f32, &TestData),
{
let skeleton = Rc::new(Skeleton::from_file("./resource/partial_blend/skeleton.ozz").unwrap());
let animation_lower = Rc::new(Animation::from_file("./resource/partial_blend/animation_base.ozz").unwrap());
let animation_upper = Rc::new(Animation::from_file("./resource/partial_blend/animation_partial.ozz").unwrap());
let skeleton = Rc::new(Skeleton::from_path("./resource/partial_blend/skeleton.ozz").unwrap());
let animation_lower = Rc::new(Animation::from_path("./resource/partial_blend/animation_base.ozz").unwrap());
let animation_upper = Rc::new(Animation::from_path("./resource/partial_blend/animation_partial.ozz").unwrap());

let mut sample_job_lower: SamplingJob = SamplingJob::default();
sample_job_lower.set_animation(animation_lower.clone());
Expand Down
12 changes: 7 additions & 5 deletions tests/playback.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use glam::Mat4;
use ozz_animation_rs::*;
use ozz_animation_rs::math::*;
use ozz_animation_rs::*;
use std::rc::Rc;

mod common;

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
struct TestData {
Expand All @@ -15,15 +17,15 @@ struct TestData {
#[test]
fn test_playback() {
run_playback(5..=5, |_, data| {
test_utils::compare_with_cpp("playback", "playback", &data.l2m_out, 1e-5).unwrap()
common::compare_with_cpp("playback", "playback", &data.l2m_out, 1e-5).unwrap()
});
}

#[cfg(feature = "rkyv")]
#[test]
fn test_playback_deterministic() {
run_playback(-1..=11, |ratio, data| {
test_utils::compare_with_rkyv("playback", &format!("playback{:+.2}", ratio), data).unwrap()
common::compare_with_rkyv("playback", &format!("playback{:+.2}", ratio), data).unwrap()
});
}

Expand All @@ -32,8 +34,8 @@ where
I: Iterator<Item = i32>,
T: Fn(f32, &TestData),
{
let skeleton = Rc::new(Skeleton::from_file("./resource/playback/skeleton.ozz").unwrap());
let animation = Rc::new(Animation::from_file("./resource/playback/animation.ozz").unwrap());
let skeleton = Rc::new(Skeleton::from_path("./resource/playback/skeleton.ozz").unwrap());
let animation = Rc::new(Animation::from_path("./resource/playback/animation.ozz").unwrap());

if skeleton.num_joints() != animation.num_tracks() {
panic!("skeleton.num_joints() != animation.num_tracks()");
Expand Down
10 changes: 6 additions & 4 deletions tests/two_bone_ik.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use glam::{Mat4, Quat, Vec3A};
use ozz_animation_rs::*;
use ozz_animation_rs::math::*;
use ozz_animation_rs::*;
use std::rc::Rc;

mod common;

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
struct TestData {
Expand All @@ -20,15 +22,15 @@ const TARGET_OFFSET: Vec3A = Vec3A::new(0.0, 0.2, 0.1);
#[test]
fn test_two_bone_ik() {
run_two_bone_ik(1..=1, |_, data| {
test_utils::compare_with_cpp("two_bone_ik", "two_bone_ik", &data.models2, 1e-6).unwrap();
common::compare_with_cpp("two_bone_ik", "two_bone_ik", &data.models2, 1e-6).unwrap();
});
}

#[cfg(feature = "rkyv")]
#[test]
fn test_two_bone_ik_deterministic() {
run_two_bone_ik(0..=10, |idx, data| {
test_utils::compare_with_rkyv("two_bone_ik", &format!("two_bone_ik_{:02}", idx), data).unwrap();
common::compare_with_rkyv("two_bone_ik", &format!("two_bone_ik_{:02}", idx), data).unwrap();
});
}

Expand All @@ -37,7 +39,7 @@ where
I: Iterator<Item = i32>,
T: Fn(i32, &TestData),
{
let skeleton = Rc::new(Skeleton::from_file("./resource/two_bone_ik/skeleton.ozz").unwrap());
let skeleton = Rc::new(Skeleton::from_path("./resource/two_bone_ik/skeleton.ozz").unwrap());

let start_joint = skeleton.joint_by_name("shoulder").unwrap();
let mid_joint = skeleton.joint_by_name("forearm").unwrap();
Expand Down

0 comments on commit 1764465

Please sign in to comment.