diff --git a/unittests.cpp b/unittests.cpp index b1b97444..c87c5c15 100644 --- a/unittests.cpp +++ b/unittests.cpp @@ -45,6 +45,7 @@ #include #include +#include #include #include #include @@ -286,6 +287,11 @@ BOOST_AUTO_TEST_CASE( binary_pod ) test_pod(); } +BOOST_AUTO_TEST_CASE( portable_binary_pod ) +{ + test_pod(); +} + BOOST_AUTO_TEST_CASE( xml_pod ) { test_pod(); @@ -339,6 +345,11 @@ BOOST_AUTO_TEST_CASE( binary_structs ) test_structs(); } +BOOST_AUTO_TEST_CASE( portable_binary_structs ) +{ + test_structs(); +} + BOOST_AUTO_TEST_CASE( xml_structs ) { test_structs(); @@ -419,6 +430,11 @@ BOOST_AUTO_TEST_CASE( binary_array ) test_array(); } +BOOST_AUTO_TEST_CASE( portable_binary_array ) +{ + test_array(); +} + BOOST_AUTO_TEST_CASE( xml_array ) { test_array(); @@ -505,6 +521,11 @@ BOOST_AUTO_TEST_CASE( binary_dequeue ) test_deque(); } +BOOST_AUTO_TEST_CASE( portable_binary_dequeue ) +{ + test_deque(); +} + BOOST_AUTO_TEST_CASE( xml_dequeue ) { test_deque(); @@ -585,6 +606,11 @@ BOOST_AUTO_TEST_CASE( binary_forward_list ) test_forward_list(); } +BOOST_AUTO_TEST_CASE( portable_binary_forward_list ) +{ + test_forward_list(); +} + BOOST_AUTO_TEST_CASE( xml_forward_list ) { test_forward_list(); @@ -665,6 +691,11 @@ BOOST_AUTO_TEST_CASE( binary_list ) test_list(); } +BOOST_AUTO_TEST_CASE( portable_binary_list ) +{ + test_list(); +} + BOOST_AUTO_TEST_CASE( xml_list ) { test_list(); @@ -765,6 +796,11 @@ BOOST_AUTO_TEST_CASE( binary_map ) test_map(); } +BOOST_AUTO_TEST_CASE( portable_binary_map ) +{ + test_map(); +} + BOOST_AUTO_TEST_CASE( xml_map ) { test_map(); @@ -879,6 +915,11 @@ BOOST_AUTO_TEST_CASE( binary_multimap ) test_multimap(); } +BOOST_AUTO_TEST_CASE( portable_binary_multimap ) +{ + test_multimap(); +} + BOOST_AUTO_TEST_CASE( xml_multimap ) { test_multimap(); @@ -947,6 +988,11 @@ BOOST_AUTO_TEST_CASE( binary_memory ) test_memory(); } +BOOST_AUTO_TEST_CASE( portable_binary_memory ) +{ + test_memory(); +} + BOOST_AUTO_TEST_CASE( xml_memory ) { test_memory(); @@ -1039,6 +1085,11 @@ BOOST_AUTO_TEST_CASE( binary_queue ) test_queue(); } +BOOST_AUTO_TEST_CASE( portable_binary_queue ) +{ + test_queue(); +} + BOOST_AUTO_TEST_CASE( xml_queue ) { test_queue(); @@ -1131,6 +1182,11 @@ BOOST_AUTO_TEST_CASE( binary_priority_queue ) test_priority_queue(); } +BOOST_AUTO_TEST_CASE( portable_binary_priority_queue ) +{ + test_priority_queue(); +} + BOOST_AUTO_TEST_CASE( xml_priority_queue ) { test_priority_queue(); @@ -1211,6 +1267,11 @@ BOOST_AUTO_TEST_CASE( binary_set ) test_set(); } +BOOST_AUTO_TEST_CASE( portable_binary_set ) +{ + test_set(); +} + BOOST_AUTO_TEST_CASE( xml_set ) { test_set(); @@ -1330,6 +1391,11 @@ BOOST_AUTO_TEST_CASE( binary_multiset ) test_multiset(); } +BOOST_AUTO_TEST_CASE( portable_binary_multiset ) +{ + test_multiset(); +} + BOOST_AUTO_TEST_CASE( xml_multiset ) { test_multiset(); @@ -1422,6 +1488,11 @@ BOOST_AUTO_TEST_CASE( binary_stack ) test_stack(); } +BOOST_AUTO_TEST_CASE( portable_binary_stack ) +{ + test_stack(); +} + BOOST_AUTO_TEST_CASE( xml_stack ) { test_stack(); @@ -1511,6 +1582,11 @@ BOOST_AUTO_TEST_CASE( binary_string ) test_string_all(); } +BOOST_AUTO_TEST_CASE( portable_binary_string ) +{ + test_string_all(); +} + BOOST_AUTO_TEST_CASE( xml_string_basic ) { test_string_basic(); @@ -1620,6 +1696,11 @@ BOOST_AUTO_TEST_CASE( binary_unordered_map ) test_unordered_map(); } +BOOST_AUTO_TEST_CASE( portable_binary_unordered_map ) +{ + test_unordered_map(); +} + BOOST_AUTO_TEST_CASE( xml_unordered_map ) { test_unordered_map(); @@ -1760,6 +1841,11 @@ BOOST_AUTO_TEST_CASE( binary_unordered_multimap ) test_unordered_multimap(); } +BOOST_AUTO_TEST_CASE( portable_binary_unordered_multimap ) +{ + test_unordered_multimap(); +} + BOOST_AUTO_TEST_CASE( xml_unordered_multimap ) { test_unordered_multimap(); @@ -1859,6 +1945,11 @@ BOOST_AUTO_TEST_CASE( binary_unordered_set ) test_unordered_set(); } +BOOST_AUTO_TEST_CASE( portable_binary_unordered_set ) +{ + test_unordered_set(); +} + BOOST_AUTO_TEST_CASE( xml_unordered_set ) { test_unordered_set(); @@ -1978,6 +2069,11 @@ BOOST_AUTO_TEST_CASE( binary_unordered_multiset ) test_unordered_multiset(); } +BOOST_AUTO_TEST_CASE( portable_binary_unordered_multiset ) +{ + test_unordered_multiset(); +} + BOOST_AUTO_TEST_CASE( xml_unordered_multiset ) { test_unordered_multiset(); @@ -2064,6 +2160,11 @@ BOOST_AUTO_TEST_CASE( binary_vector ) test_vector(); } +BOOST_AUTO_TEST_CASE( portable_binary_vector ) +{ + test_vector(); +} + BOOST_AUTO_TEST_CASE( xml_vector ) { test_vector(); @@ -2141,6 +2242,11 @@ BOOST_AUTO_TEST_CASE( binary_pair ) test_pair(); } +BOOST_AUTO_TEST_CASE( portable_binary_pair ) +{ + test_pair(); +} + BOOST_AUTO_TEST_CASE( xml_pair ) { test_pair(); @@ -2220,6 +2326,11 @@ BOOST_AUTO_TEST_CASE( binary_tuple ) test_tuple(); } +BOOST_AUTO_TEST_CASE( portable_binary_tuple ) +{ + test_tuple(); +} + BOOST_AUTO_TEST_CASE( xml_tuple ) { test_tuple(); @@ -2282,6 +2393,11 @@ BOOST_AUTO_TEST_CASE( binary_complex ) test_complex(); } +BOOST_AUTO_TEST_CASE( portable_binary_complex ) +{ + test_complex(); +} + BOOST_AUTO_TEST_CASE( xml_complex ) { test_complex(); @@ -2342,6 +2458,11 @@ BOOST_AUTO_TEST_CASE( binary_bitset ) test_bitset(); } +BOOST_AUTO_TEST_CASE( portable_binary_bitset ) +{ + test_bitset(); +} + BOOST_AUTO_TEST_CASE( xml_bitset ) { test_bitset(); @@ -2411,6 +2532,11 @@ BOOST_AUTO_TEST_CASE( binary_chrono ) test_chrono(); } +BOOST_AUTO_TEST_CASE( portable_binary_chrono ) +{ + test_chrono(); +} + BOOST_AUTO_TEST_CASE( xml_chrono ) { test_chrono(); @@ -2538,6 +2664,11 @@ BOOST_AUTO_TEST_CASE( binary_structs_specialized ) test_structs_specialized(); } +BOOST_AUTO_TEST_CASE( portable_binary_structs_specialized ) +{ + test_structs_specialized(); +} + BOOST_AUTO_TEST_CASE( xml_structs_specialized ) { test_structs_specialized(); @@ -2653,6 +2784,11 @@ BOOST_AUTO_TEST_CASE( binary_polymorphic ) test_polymorphic(); } +BOOST_AUTO_TEST_CASE( portable_binary_polymorphic ) +{ + test_polymorphic(); +} + BOOST_AUTO_TEST_CASE( xml_polymorphic ) { test_polymorphic(); @@ -2669,3 +2805,115 @@ BOOST_AUTO_TEST_CASE( util ) { BOOST_CHECK_EQUAL( cereal::util::demangledName(), "mynamespace::MyCustomClass" ); } + +template +inline void swapBytes( T & t ) +{ + cereal::portable_binary_detail::swap_bytes( reinterpret_cast(&t) ); +} + +BOOST_AUTO_TEST_CASE( portable_binary_archive ) +{ + std::random_device rd; + std::mt19937 gen(rd()); + + for(size_t i=0; i<100; ++i) + { + bool o_bool = random_value(gen) % 2 ? true : false; + uint8_t o_uint8 = random_value(gen); + int8_t o_int8 = random_value(gen); + uint16_t o_uint16 = random_value(gen); + int16_t o_int16 = random_value(gen); + uint32_t o_uint32 = random_value(gen); + int32_t o_int32 = random_value(gen); + uint64_t o_uint64 = random_value(gen); + int64_t o_int64 = random_value(gen); + float o_float = random_value(gen); + double o_double = random_value(gen); + + // swap the bytes on all of the data + swapBytes(o_bool); + swapBytes(o_uint8); + swapBytes(o_int8); + swapBytes(o_uint16); + swapBytes(o_int16); + swapBytes(o_uint32); + swapBytes(o_int32); + swapBytes(o_uint64); + swapBytes(o_int64); + swapBytes(o_float); + swapBytes(o_double); + + std::ostringstream os; + { + cereal::BinaryOutputArchive oar(os); + // manually insert incorrect endian encoding + oar(!cereal::portable_binary_detail::is_little_endian()); + + oar(o_bool); + oar(o_uint8); + oar(o_int8); + oar(o_uint16); + oar(o_int16); + oar(o_uint32); + oar(o_int32); + oar(o_uint64); + oar(o_int64); + oar(o_float); + oar(o_double); + } + + // swap back to original values + swapBytes(o_bool); + swapBytes(o_uint8); + swapBytes(o_int8); + swapBytes(o_uint16); + swapBytes(o_int16); + swapBytes(o_uint32); + swapBytes(o_int32); + swapBytes(o_uint64); + swapBytes(o_int64); + swapBytes(o_float); + swapBytes(o_double); + + bool i_bool = false; + uint8_t i_uint8 = 0.0; + int8_t i_int8 = 0.0; + uint16_t i_uint16 = 0.0; + int16_t i_int16 = 0.0; + uint32_t i_uint32 = 0.0; + int32_t i_int32 = 0.0; + uint64_t i_uint64 = 0.0; + int64_t i_int64 = 0.0; + float i_float = 0.0; + double i_double = 0.0; + + std::istringstream is(os.str()); + { + cereal::PortableBinaryInputArchive iar(is); + iar(i_bool); + iar(i_uint8); + iar(i_int8); + iar(i_uint16); + iar(i_int16); + iar(i_uint32); + iar(i_int32); + iar(i_uint64); + iar(i_int64); + iar(i_float); + iar(i_double); + } + + BOOST_CHECK_EQUAL(i_bool , o_bool); + BOOST_CHECK_EQUAL(i_uint8 , o_uint8); + BOOST_CHECK_EQUAL(i_int8 , o_int8); + BOOST_CHECK_EQUAL(i_uint16 , o_uint16); + BOOST_CHECK_EQUAL(i_int16 , o_int16); + BOOST_CHECK_EQUAL(i_uint32 , o_uint32); + BOOST_CHECK_EQUAL(i_int32 , o_int32); + BOOST_CHECK_EQUAL(i_uint64 , o_uint64); + BOOST_CHECK_EQUAL(i_int64 , o_int64); + BOOST_CHECK_CLOSE(i_float , o_float, 1e-5); + BOOST_CHECK_CLOSE(i_double , o_double, 1e-5); + } +}