From a56bad8bbb770ee266e930c95d37fff2a5be7fea Mon Sep 17 00:00:00 2001 From: PeizeLin <78645006+PeizeLin@users.noreply.github.com> Date: Mon, 20 Jan 2025 13:00:41 +0800 Subject: [PATCH] Fix `std::array` (#848) * Fix std::array * Add unittests for std::array * update std::array --- include/cereal/types/array.hpp | 4 ++-- unittests/array.cpp | 12 ++++++++---- unittests/array.hpp | 22 +++++++++++----------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/include/cereal/types/array.hpp b/include/cereal/types/array.hpp index 8ae2a4a0..413d7cd5 100644 --- a/include/cereal/types/array.hpp +++ b/include/cereal/types/array.hpp @@ -42,7 +42,7 @@ namespace cereal && std::is_arithmetic::value, void>::type CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::array const & array ) { - ar( binary_data( array.data(), sizeof(array) ) ); + ar( binary_data( array.data(), N*sizeof(T) ) ); } //! Loading for std::array primitive types @@ -52,7 +52,7 @@ namespace cereal && std::is_arithmetic::value, void>::type CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::array & array ) { - ar( binary_data( array.data(), sizeof(array) ) ); + ar( binary_data( array.data(), N*sizeof(T) ) ); } //! Saving for std::array all other types diff --git a/unittests/array.cpp b/unittests/array.cpp index 00543fe6..78c3ef6c 100644 --- a/unittests/array.cpp +++ b/unittests/array.cpp @@ -31,22 +31,26 @@ TEST_SUITE_BEGIN("array"); TEST_CASE("binary_array") { - test_array(); + test_array(); + test_array(); } TEST_CASE("portable_binary_array") { - test_array(); + test_array(); + test_array(); } TEST_CASE("xml_array") { - test_array(); + test_array(); + test_array(); } TEST_CASE("json_array") { - test_array(); + test_array(); + test_array(); } TEST_SUITE_END(); diff --git a/unittests/array.hpp b/unittests/array.hpp index f782faab..35b50d8c 100644 --- a/unittests/array.hpp +++ b/unittests/array.hpp @@ -28,7 +28,7 @@ #define CEREAL_TEST_ARRAY_H_ #include "common.hpp" -template inline +template inline void test_array() { std::random_device rd; @@ -36,23 +36,23 @@ void test_array() for(int ii=0; ii<100; ++ii) { - std::array o_podarray; + std::array o_podarray; for(auto & elem : o_podarray) elem = random_value(gen); - std::array o_iserarray; + std::array o_iserarray; for(auto & elem : o_iserarray) elem = StructInternalSerialize( random_value(gen), random_value(gen) ); - std::array o_isplarray; + std::array o_isplarray; for(auto & elem : o_isplarray) elem = StructInternalSplit( random_value(gen), random_value(gen) ); - std::array o_eserarray; + std::array o_eserarray; for(auto & elem : o_eserarray) elem = StructExternalSerialize( random_value(gen), random_value(gen) ); - std::array o_esplarray; + std::array o_esplarray; for(auto & elem : o_esplarray) elem = StructExternalSplit( random_value(gen), random_value(gen) ); @@ -67,11 +67,11 @@ void test_array() oar(o_esplarray); } - std::array i_podarray; - std::array i_iserarray; - std::array i_isplarray; - std::array i_eserarray; - std::array i_esplarray; + std::array i_podarray; + std::array i_iserarray; + std::array i_isplarray; + std::array i_eserarray; + std::array i_esplarray; std::istringstream is(os.str()); {