Skip to content

Commit

Permalink
Merge pull request #81 from SlimeYummy/feat/serde-test
Browse files Browse the repository at this point in the history
serde tests
  • Loading branch information
SlimeYummy authored Jun 12, 2024
2 parents 78c8317 + 846cfe6 commit 825b544
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exclude = ["/.circleci", "/.github", "/demo", "/expected", "/output", "/resource
resolver = "2"

[features]
default = ["rkyv"]
default = ["rkyv", "serde"]
serde = ["dep:serde", "glam/serde", "bimap/serde" ]
rkyv = ["dep:rkyv", "dep:bytecheck", "glam/rkyv", "glam/bytecheck"]
wasm = []
Expand All @@ -33,4 +33,5 @@ wasm-bindgen = { version = "0.2", optional = true }

[dev-dependencies]
miniz_oxide = "0.7"
serde_json = "1.0"
wasm-bindgen-test = "0.3"
15 changes: 15 additions & 0 deletions src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1514,4 +1514,19 @@ mod tests {
let transform_de: SoaTransform = archived.deserialize(&mut rkyv::Infallible).unwrap();
assert_eq!(transform_de, transform);
}

#[cfg(feature = "serde")]
#[test]
#[wasm_bindgen_test]
fn test_serde() {
let vec3 = SoaVec3::splat_col([2.0, 3.0, 4.0]);
let json = serde_json::to_string(&vec3).unwrap();
let vec3_de: SoaVec3 = serde_json::from_str(&json).unwrap();
assert_eq!(vec3_de, vec3);

let quat = SoaQuat::splat_col([2.0, 3.0, 4.0, 5.0]);
let json = serde_json::to_string(&quat).unwrap();
let quat_de: SoaQuat = serde_json::from_str(&json).unwrap();
assert_eq!(quat_de, quat);
}
}
42 changes: 38 additions & 4 deletions src/sampling_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,9 +776,8 @@ const _: () = {

#[cfg(feature = "serde")]
const _: () = {
use serde::de::value::Error;
use serde::de::{self, MapAccess, Visitor};
use serde::ser::{SerializeMap, SerializeSeq};
use serde::ser::SerializeMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

impl Serialize for SamplingContext {
Expand Down Expand Up @@ -808,8 +807,7 @@ const _: () = {

impl<'de> Deserialize<'de> for SamplingContext {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<SamplingContext, D::Error> {
let mut ctx = SamplingContext::new(0);
return Ok(ctx);
return deserializer.deserialize_map(SamplingContextVisitor);
}
}

Expand All @@ -827,6 +825,7 @@ const _: () = {
while let Some(key) = map.next_key::<&str>()? {
if key == "max_tracks" {
max_tracks = Some(map.next_value()?);
break;
} else {
map.next_value::<serde::de::IgnoredAny>()?;
}
Expand Down Expand Up @@ -1795,4 +1794,39 @@ mod sampling_tests {
assert_eq!(ctx_de.outdated_rotations(), ctx.outdated_rotations());
assert_eq!(ctx_de.outdated_scales(), ctx.outdated_scales());
}

#[cfg(feature = "serde")]
#[test]
#[wasm_bindgen_test]
fn test_serde() {
let animation = Rc::new(Animation::from_path("./resource/animation-blending-1.ozz").unwrap());
let aligned_tracks = animation.num_aligned_tracks();

let mut job = SamplingJob::default();
job.set_animation(animation.clone());
job.set_context(SamplingContext::new(aligned_tracks));
job.set_output(make_buf(vec![SoaTransform::default(); animation.num_soa_tracks()]));
job.set_ratio(0.5);
job.run().unwrap();

let ctx: SamplingContext = job.context().unwrap().clone();
let json = serde_json::to_string(&ctx).unwrap();
let ctx_de: SamplingContext = serde_json::from_str(&json).unwrap();

assert_eq!(ctx_de.size(), ctx.size());
assert_eq!(ctx_de.animation_id, ctx.animation_id);
assert_eq!(ctx_de.ratio, ctx.ratio);
assert_eq!(ctx_de.translation_cursor, ctx.translation_cursor);
assert_eq!(ctx_de.rotation_cursor, ctx.rotation_cursor);
assert_eq!(ctx_de.scale_cursor, ctx.scale_cursor);
assert_eq!(ctx_de.translations(), ctx.translations());
assert_eq!(ctx_de.rotations(), ctx.rotations());
assert_eq!(ctx_de.scales(), ctx.scales());
assert_eq!(ctx_de.translation_keys(), ctx.translation_keys());
assert_eq!(ctx_de.rotation_keys(), ctx.rotation_keys());
assert_eq!(ctx_de.scale_keys(), ctx.scale_keys());
assert_eq!(ctx_de.outdated_translations(), ctx.outdated_translations());
assert_eq!(ctx_de.outdated_rotations(), ctx.outdated_rotations());
assert_eq!(ctx_de.outdated_scales(), ctx.outdated_scales());
}
}
2 changes: 0 additions & 2 deletions src/skinning_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,7 @@ macro_rules! skinning_impl {

#[inline(always)]
fn mat4_col_mul(m: &Mat4, v: Vec4) -> Mat4 {
// println!("mat4_col_mul: {:?}, {:?}", m, v);
let res = Mat4::from_cols(m.x_axis * v, m.y_axis * v, m.z_axis * v, m.w_axis * v);
// println!("mat4_col_mul_res: {:?}", res);
return res;
}

Expand Down

0 comments on commit 825b544

Please sign in to comment.