1 /* 2 Copyright (c) 2014, Randolph Voorhies, Shane Grant 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions are met: 7 * Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 * Redistributions in binary form must reproduce the above copyright 10 notice, this list of conditions and the following disclaimer in the 11 documentation and/or other materials provided with the distribution. 12 * Neither the name of cereal nor the 13 names of its contributors may be used to endorse or promote products 14 derived from this software without specific prior written permission. 15 16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES AND SHANE GRANT BE LIABLE FOR ANY 20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN 28 #include "portable_binary_archive.hpp" 29 30 TEST_SUITE("portable_binary_archive"); 31 32 #ifdef _MSC_VER 33 TEST_CASE("util") 34 { 35 CHECK_EQ( cereal::util::demangledName<mynamespace::MyCustomClass>(), "struct mynamespace::MyCustomClass" ); 36 } 37 #else 38 TEST_CASE("util") 39 { 40 CHECK_EQ( cereal::util::demangledName<mynamespace::MyCustomClass>(), "mynamespace::MyCustomClass" ); 41 } 42 #endif 43 44 TEST_CASE("portable_binary_archive_endian_conversions") 45 { 46 // (last parameter is whether we load as little endian) 47 test_endian_serialization<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>( 48 cereal::PortableBinaryInputArchive::Options::BigEndian(), cereal::PortableBinaryOutputArchive::Options::BigEndian(), false ); 49 test_endian_serialization<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>( 50 cereal::PortableBinaryInputArchive::Options::LittleEndian(), cereal::PortableBinaryOutputArchive::Options::BigEndian(), true ); 51 test_endian_serialization<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>( 52 cereal::PortableBinaryInputArchive::Options::BigEndian(), cereal::PortableBinaryOutputArchive::Options::LittleEndian(), false ); 53 test_endian_serialization<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>( 54 cereal::PortableBinaryInputArchive::Options::LittleEndian(), cereal::PortableBinaryOutputArchive::Options::LittleEndian(), true ); 55 } 56 57 // Tests the default behavior to swap bytes to current machine's endianness 58 TEST_CASE("portable_binary_archive_default_behavior") 59 { 60 std::random_device rd; 61 std::mt19937 gen(rd()); 62 63 for(size_t i=0; i<100; ++i) 64 { 65 bool o_bool = random_value<uint8_t>(gen) % 2 ? true : false; 66 uint8_t o_uint8 = random_value<uint8_t>(gen); 67 int8_t o_int8 = random_value<int8_t>(gen); 68 uint16_t o_uint16 = random_value<uint16_t>(gen); 69 int16_t o_int16 = random_value<int16_t>(gen); 70 uint32_t o_uint32 = random_value<uint32_t>(gen); 71 int32_t o_int32 = random_value<int32_t>(gen); 72 uint64_t o_uint64 = random_value<uint64_t>(gen); 73 int64_t o_int64 = random_value<int64_t>(gen); 74 float o_float = random_value<float>(gen); 75 double o_double = random_value<double>(gen); 76 77 // swap the bytes on all of the data 78 CEREAL_TEST_SWAP_OUTPUT 79 80 std::ostringstream os; 81 { 82 cereal::BinaryOutputArchive oar(os); 83 // manually insert incorrect endian encoding 84 oar(!cereal::portable_binary_detail::is_little_endian()); 85 86 oar(o_bool); 87 oar(o_uint8); 88 oar(o_int8); 89 oar(o_uint16); 90 oar(o_int16); 91 oar(o_uint32); 92 oar(o_int32); 93 oar(o_uint64); 94 oar(o_int64); 95 oar(o_float); 96 oar(o_double); 97 } 98 99 // swap back to original values 100 CEREAL_TEST_SWAP_OUTPUT 101 102 bool i_bool = false; 103 uint8_t i_uint8 = 0; 104 int8_t i_int8 = 0; 105 uint16_t i_uint16 = 0; 106 int16_t i_int16 = 0; 107 uint32_t i_uint32 = 0; 108 int32_t i_int32 = 0; 109 uint64_t i_uint64 = 0; 110 int64_t i_int64 = 0; 111 float i_float = 0; 112 double i_double = 0; 113 114 std::istringstream is(os.str()); 115 { 116 cereal::PortableBinaryInputArchive iar(is); 117 iar(i_bool); 118 iar(i_uint8); 119 iar(i_int8); 120 iar(i_uint16); 121 iar(i_int16); 122 iar(i_uint32); 123 iar(i_int32); 124 iar(i_uint64); 125 iar(i_int64); 126 iar(i_float); 127 iar(i_double); 128 } 129 130 CEREAL_TEST_CHECK_EQUAL 131 } 132 } 133 134 TEST_SUITE_END(); 135