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 #ifndef CEREAL_TEST_PORTABLE_BINARY_ARCHIVE_H_
28 #define CEREAL_TEST_PORTABLE_BINARY_ARCHIVE_H_
29 #include "common.hpp"
30
31 #include <cmath>
32
33 namespace mynamespace { struct MyCustomClass {}; }
34
35 template <class T>
swapBytes(T & t)36 inline void swapBytes( T & t )
37 {
38 cereal::portable_binary_detail::swap_bytes<sizeof(T)>( reinterpret_cast<std::uint8_t*>(&t) );
39 }
40
41 // swaps all output data
42 #define CEREAL_TEST_SWAP_OUTPUT \
43 swapBytes(o_bool); \
44 swapBytes(o_uint8); \
45 swapBytes(o_int8); \
46 swapBytes(o_uint16); \
47 swapBytes(o_int16); \
48 swapBytes(o_uint32); \
49 swapBytes(o_int32); \
50 swapBytes(o_uint64); \
51 swapBytes(o_int64); \
52 swapBytes(o_float); \
53 swapBytes(o_double);
54
55 #define CEREAL_TEST_CHECK_EQUAL \
56 CHECK_EQ(i_bool , o_bool); \
57 CHECK_EQ(i_uint8 , o_uint8); \
58 CHECK_EQ(i_int8 , o_int8); \
59 CHECK_EQ(i_uint16 , o_uint16); \
60 CHECK_EQ(i_int16 , o_int16); \
61 CHECK_EQ(i_uint32 , o_uint32); \
62 CHECK_EQ(i_int32 , o_int32); \
63 CHECK_EQ(i_uint64 , o_uint64); \
64 CHECK_EQ(i_int64 , o_int64); \
65 if( !std::isnan(i_float) && !std::isnan(o_float) ) CHECK_EQ(i_float , doctest::Approx(o_float).epsilon(1e-5F)); \
66 if( !std::isnan(i_float) && !std::isnan(o_float) ) CHECK_EQ(i_double, doctest::Approx(o_double).epsilon(1e-5));
67
68 // Last parameter exists to keep everything hidden in options
69 template <class IArchive, class OArchive> inline
test_endian_serialization(typename IArchive::Options const & iOptions,typename OArchive::Options const & oOptions,const std::uint8_t inputLittleEndian)70 void test_endian_serialization( typename IArchive::Options const & iOptions, typename OArchive::Options const & oOptions, const std::uint8_t inputLittleEndian )
71 {
72 std::random_device rd;
73 std::mt19937 gen(rd());
74
75 for(size_t i=0; i<100; ++i)
76 {
77 bool o_bool = random_value<uint8_t>(gen) % 2 ? true : false;
78 uint8_t o_uint8 = random_value<uint8_t>(gen);
79 int8_t o_int8 = random_value<int8_t>(gen);
80 uint16_t o_uint16 = random_value<uint16_t>(gen);
81 int16_t o_int16 = random_value<int16_t>(gen);
82 uint32_t o_uint32 = random_value<uint32_t>(gen);
83 int32_t o_int32 = random_value<int32_t>(gen);
84 uint64_t o_uint64 = random_value<uint64_t>(gen);
85 int64_t o_int64 = random_value<int64_t>(gen);
86 float o_float = random_value<float>(gen);
87 double o_double = random_value<double>(gen);
88
89 std::vector<int32_t> o_vector(100);
90 for(auto & elem : o_vector)
91 elem = random_value<uint32_t>(gen);
92
93 std::ostringstream os;
94 {
95 OArchive oar(os, oOptions);
96 oar(o_bool);
97 oar(o_uint8);
98 oar(o_int8);
99 oar(o_uint16);
100 oar(o_int16);
101 oar(o_uint32);
102 oar(o_int32);
103 oar(o_uint64);
104 oar(o_int64);
105 oar(o_float);
106 oar(o_double);
107 // We can't test vector directly here since we are artificially interfering with the endianness,
108 // which can result in the size being incorrect
109 oar(cereal::binary_data( o_vector.data(), static_cast<std::size_t>( o_vector.size() * sizeof(int32_t) ) ));
110 }
111
112 bool i_bool = false;
113 uint8_t i_uint8 = 0;
114 int8_t i_int8 = 0;
115 uint16_t i_uint16 = 0;
116 int16_t i_int16 = 0;
117 uint32_t i_uint32 = 0;
118 int32_t i_int32 = 0;
119 uint64_t i_uint64 = 0;
120 int64_t i_int64 = 0;
121 float i_float = 0;
122 double i_double = 0;
123 std::vector<int32_t> i_vector(100);
124
125 std::istringstream is(os.str());
126 {
127 IArchive iar(is, iOptions);
128 iar(i_bool);
129 iar(i_uint8);
130 iar(i_int8);
131 iar(i_uint16);
132 iar(i_int16);
133 iar(i_uint32);
134 iar(i_int32);
135 iar(i_uint64);
136 iar(i_int64);
137 iar(i_float);
138 iar(i_double);
139 iar(cereal::binary_data( i_vector.data(), static_cast<std::size_t>( i_vector.size() * sizeof(int32_t) ) ));
140 }
141
142 // Convert to big endian if we expect to read big and didn't start big
143 if( cereal::portable_binary_detail::is_little_endian() ^ inputLittleEndian ) // Convert to little endian if
144 {
145 CEREAL_TEST_SWAP_OUTPUT
146 for( auto & val : o_vector )
147 swapBytes(val);
148 }
149
150 CEREAL_TEST_CHECK_EQUAL
151
152 check_collection(i_vector, o_vector);
153 }
154 }
155
156 #endif // CEREAL_TEST_PORTABLE_BINARY_ARCHIVE_H_
157