From bc566d91bf5dd1082eca5cddfa6009dc80b9c41c Mon Sep 17 00:00:00 2001 From: Peng Guanwen Date: Thu, 15 Sep 2022 09:35:21 +0000 Subject: [PATCH] Add test for rkyv --- .github/workflows/nalgebra-ci-build.yml | 2 +- Cargo.toml | 1 + tests/core/mod.rs | 2 ++ tests/core/rkyv.rs | 44 +++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/core/rkyv.rs diff --git a/.github/workflows/nalgebra-ci-build.yml b/.github/workflows/nalgebra-ci-build.yml index c00b6cbc..d1a6c923 100644 --- a/.github/workflows/nalgebra-ci-build.yml +++ b/.github/workflows/nalgebra-ci-build.yml @@ -61,7 +61,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: test - run: cargo test --features arbitrary,rand,serde-serialize,sparse,debug,io,compare,libm,proptest-support,slow-tests; + run: cargo test --features arbitrary,rand,serde-serialize,sparse,debug,io,compare,libm,proptest-support,slow-tests,rkyv-safe-deser; test-nalgebra-glm: runs-on: ubuntu-latest steps: diff --git a/Cargo.toml b/Cargo.toml index 89de73e5..4bd1e12f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -66,6 +66,7 @@ rand = [ "rand-no-std", "rand-package/std", "rand-package/std_rng", "rand arbitrary = [ "quickcheck" ] proptest-support = [ "proptest" ] slow-tests = [] +rkyv-safe-deser = [ "rkyv-serialize", "rkyv/validation" ] [dependencies] nalgebra-macros = { version = "0.1", path = "nalgebra-macros", optional = true } diff --git a/tests/core/mod.rs b/tests/core/mod.rs index 5fb8c82b..3c968a5d 100644 --- a/tests/core/mod.rs +++ b/tests/core/mod.rs @@ -8,6 +8,8 @@ mod matrix_slice; #[cfg(feature = "mint")] mod mint; mod serde; +#[cfg(feature = "rkyv-serialize-no-std")] +mod rkyv; #[cfg(feature = "compare")] mod matrixcompare; diff --git a/tests/core/rkyv.rs b/tests/core/rkyv.rs new file mode 100644 index 00000000..b89f5211 --- /dev/null +++ b/tests/core/rkyv.rs @@ -0,0 +1,44 @@ +use na::{ + Isometry3, IsometryMatrix2, IsometryMatrix3, Matrix3x4, Point2, Point3, Quaternion, Rotation2, + Rotation3, Similarity3, SimilarityMatrix2, SimilarityMatrix3, Translation2, Translation3, +}; +use rkyv::ser::{serializers::AllocSerializer, Serializer}; + +macro_rules! test_rkyv( + ($($test: ident, $ty: ident);* $(;)*) => {$( + #[test] + fn $test() { + let v: $ty = rand::random(); + // serialize + let mut serializer = AllocSerializer::<0>::default(); + serializer.serialize_value(&v).unwrap(); + let serialized = serializer.into_serializer().into_inner(); + + let deserialized: $ty = unsafe { rkyv::from_bytes_unchecked(&serialized).unwrap() }; + assert_eq!(v, deserialized); + + #[cfg(feature = "rkyv-safe-deser")] + { + let deserialized: $ty = rkyv::from_bytes(&serialized).unwrap(); + assert_eq!(v, deserialized); + } + } + )*} +); + +test_rkyv!( + rkyv_matrix3x4, Matrix3x4; + rkyv_point3, Point3; + rkyv_translation3, Translation3; + rkyv_rotation3, Rotation3; + rkyv_isometry3, Isometry3; + rkyv_isometry_matrix3, IsometryMatrix3; + rkyv_similarity3, Similarity3; + rkyv_similarity_matrix3, SimilarityMatrix3; + rkyv_quaternion, Quaternion; + rkyv_point2, Point2; + rkyv_translation2, Translation2; + rkyv_rotation2, Rotation2; + rkyv_isometry_matrix2, IsometryMatrix2; + rkyv_similarity_matrix2, SimilarityMatrix2; +);