From 846cfe678cdd76ffb631824952b0d7be99c56c89 Mon Sep 17 00:00:00 2001 From: Nan Date: Wed, 12 Jun 2024 19:57:01 +0800 Subject: [PATCH] serde tests --- Cargo.toml | 3 ++- src/math.rs | 15 +++++++++++++++ src/sampling_job.rs | 42 ++++++++++++++++++++++++++++++++++++++---- src/skinning_job.rs | 2 -- 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0189fcd..ead6620 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = [] @@ -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" diff --git a/src/math.rs b/src/math.rs index 2b3c5ef..1228a26 100644 --- a/src/math.rs +++ b/src/math.rs @@ -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); + } } diff --git a/src/sampling_job.rs b/src/sampling_job.rs index cc7c18d..ce5b320 100644 --- a/src/sampling_job.rs +++ b/src/sampling_job.rs @@ -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 { @@ -808,8 +807,7 @@ const _: () = { impl<'de> Deserialize<'de> for SamplingContext { fn deserialize>(deserializer: D) -> Result { - let mut ctx = SamplingContext::new(0); - return Ok(ctx); + return deserializer.deserialize_map(SamplingContextVisitor); } } @@ -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::()?; } @@ -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()); + } } diff --git a/src/skinning_job.rs b/src/skinning_job.rs index e0689d1..cf8ae06 100644 --- a/src/skinning_job.rs +++ b/src/skinning_job.rs @@ -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; }