Skip to content

Commit

Permalink
Fix std::array<T,0> (#848)
Browse files Browse the repository at this point in the history
* Fix std::array<T,0>

* Add unittests for std::array<T,0>

* update std::array
  • Loading branch information
PeizeLin authored Jan 20, 2025
1 parent b4703c4 commit a56bad8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions include/cereal/types/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace cereal
&& std::is_arithmetic<T>::value, void>::type
CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::array<T, N> const & array )
{
ar( binary_data( array.data(), sizeof(array) ) );
ar( binary_data( array.data(), N*sizeof(T) ) );
}

//! Loading for std::array primitive types
Expand All @@ -52,7 +52,7 @@ namespace cereal
&& std::is_arithmetic<T>::value, void>::type
CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::array<T, N> & array )
{
ar( binary_data( array.data(), sizeof(array) ) );
ar( binary_data( array.data(), N*sizeof(T) ) );
}

//! Saving for std::array all other types
Expand Down
12 changes: 8 additions & 4 deletions unittests/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,26 @@ TEST_SUITE_BEGIN("array");

TEST_CASE("binary_array")
{
test_array<cereal::BinaryInputArchive, cereal::BinaryOutputArchive>();
test_array<cereal::BinaryInputArchive, cereal::BinaryOutputArchive, 0>();
test_array<cereal::BinaryInputArchive, cereal::BinaryOutputArchive, 100>();
}

TEST_CASE("portable_binary_array")
{
test_array<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>();
test_array<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive, 0>();
test_array<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive, 100>();
}

TEST_CASE("xml_array")
{
test_array<cereal::XMLInputArchive, cereal::XMLOutputArchive>();
test_array<cereal::XMLInputArchive, cereal::XMLOutputArchive, 0>();
test_array<cereal::XMLInputArchive, cereal::XMLOutputArchive, 100>();
}

TEST_CASE("json_array")
{
test_array<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
test_array<cereal::JSONInputArchive, cereal::JSONOutputArchive, 0>();
test_array<cereal::JSONInputArchive, cereal::JSONOutputArchive, 100>();
}

TEST_SUITE_END();
22 changes: 11 additions & 11 deletions unittests/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,31 @@
#define CEREAL_TEST_ARRAY_H_
#include "common.hpp"

template <class IArchive, class OArchive> inline
template <class IArchive, class OArchive, size_t N> inline
void test_array()
{
std::random_device rd;
std::mt19937 gen(rd());

for(int ii=0; ii<100; ++ii)
{
std::array<int, 100> o_podarray;
std::array<int, N> o_podarray;
for(auto & elem : o_podarray)
elem = random_value<int>(gen);

std::array<StructInternalSerialize, 100> o_iserarray;
std::array<StructInternalSerialize, N> o_iserarray;
for(auto & elem : o_iserarray)
elem = StructInternalSerialize( random_value<int>(gen), random_value<int>(gen) );

std::array<StructInternalSplit, 100> o_isplarray;
std::array<StructInternalSplit, N> o_isplarray;
for(auto & elem : o_isplarray)
elem = StructInternalSplit( random_value<int>(gen), random_value<int>(gen) );

std::array<StructExternalSerialize, 100> o_eserarray;
std::array<StructExternalSerialize, N> o_eserarray;
for(auto & elem : o_eserarray)
elem = StructExternalSerialize( random_value<int>(gen), random_value<int>(gen) );

std::array<StructExternalSplit, 100> o_esplarray;
std::array<StructExternalSplit, N> o_esplarray;
for(auto & elem : o_esplarray)
elem = StructExternalSplit( random_value<int>(gen), random_value<int>(gen) );

Expand All @@ -67,11 +67,11 @@ void test_array()
oar(o_esplarray);
}

std::array<int, 100> i_podarray;
std::array<StructInternalSerialize, 100> i_iserarray;
std::array<StructInternalSplit, 100> i_isplarray;
std::array<StructExternalSerialize, 100> i_eserarray;
std::array<StructExternalSplit, 100> i_esplarray;
std::array<int, N> i_podarray;
std::array<StructInternalSerialize, N> i_iserarray;
std::array<StructInternalSplit, N> i_isplarray;
std::array<StructExternalSerialize, N> i_eserarray;
std::array<StructExternalSplit, N> i_esplarray;

std::istringstream is(os.str());
{
Expand Down

0 comments on commit a56bad8

Please sign in to comment.