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 #include "common.hpp"
28 #include <boost/test/unit_test.hpp>
29
30 template <class IArchive, class OArchive>
test_multimap()31 void test_multimap()
32 {
33 std::random_device rd;
34 std::mt19937 gen(rd());
35
36 for(int ii=0; ii<100; ++ii)
37 {
38 std::multimap<std::string, int> o_podmultimap;
39 for(int j=0; j<100; ++j)
40 {
41 auto key = random_value<std::string>(gen);
42 o_podmultimap.insert({key, random_value<int>(gen)});
43 o_podmultimap.insert({key, random_value<int>(gen)});
44 }
45
46 std::multimap<uint8_t, StructInternalSerialize> o_isermultimap;
47 for(int j=0; j<100; ++j)
48 {
49 auto key = random_value<uint8_t>(gen);
50 o_isermultimap.insert({key, { random_value<int>(gen), random_value<int>(gen) }});
51 o_isermultimap.insert({key, { random_value<int>(gen), random_value<int>(gen) }});
52 }
53
54 std::multimap<int16_t, StructInternalSplit> o_isplmultimap;
55 for(int j=0; j<100; ++j)
56 {
57 auto key = random_value<int16_t>(gen);
58 o_isplmultimap.insert({key, { random_value<int>(gen), random_value<int>(gen) }});
59 o_isplmultimap.insert({key, { random_value<int>(gen), random_value<int>(gen) }});
60 }
61
62 std::multimap<uint32_t, StructExternalSerialize> o_esermultimap;
63 for(int j=0; j<100; ++j)
64 {
65 auto key = random_value<uint32_t>(gen);
66 o_esermultimap.insert({key, { random_value<int>(gen), random_value<int>(gen) }});
67 o_esermultimap.insert({key, { random_value<int>(gen), random_value<int>(gen) }});
68 }
69
70 std::multimap<int8_t, StructExternalSplit> o_esplmultimap;
71 for(int j=0; j<100; ++j)
72 {
73 auto key = random_value<char>(gen);
74 o_esplmultimap.insert({key, { random_value<int>(gen), random_value<int>(gen) }});
75 o_esplmultimap.insert({key, { random_value<int>(gen), random_value<int>(gen) }});
76 }
77
78 std::ostringstream os;
79 {
80 OArchive oar(os);
81
82 oar(o_podmultimap);
83 oar(o_isermultimap);
84 oar(o_isplmultimap);
85 oar(o_esermultimap);
86 oar(o_esplmultimap);
87 }
88
89 std::multimap<std::string, int> i_podmultimap;
90 std::multimap<uint8_t, StructInternalSerialize> i_isermultimap;
91 std::multimap<int16_t, StructInternalSplit> i_isplmultimap;
92 std::multimap<uint32_t, StructExternalSerialize> i_esermultimap;
93 std::multimap<int8_t, StructExternalSplit> i_esplmultimap;
94
95 std::istringstream is(os.str());
96 {
97 IArchive iar(is);
98
99 iar(i_podmultimap);
100 iar(i_isermultimap);
101 iar(i_isplmultimap);
102 iar(i_esermultimap);
103 iar(i_esplmultimap);
104 }
105
106 #define MULTIMAP_CHECK(InMap, OutMap) \
107 for( auto & pair : OutMap ) \
108 { \
109 auto const count = InMap.count( pair.first ); \
110 BOOST_CHECK_EQUAL( count, OutMap.count( pair.first ) ); \
111 auto find = InMap.find( pair.first ); \
112 bool found = false; \
113 for( size_t i = 0; i < count; ++i, ++find ) \
114 found |= find->second == pair.second; \
115 BOOST_CHECK( found ); \
116 }
117
118 MULTIMAP_CHECK( i_podmultimap, o_podmultimap );
119 MULTIMAP_CHECK( i_isermultimap, o_isermultimap );
120 MULTIMAP_CHECK( i_isplmultimap, o_isplmultimap );
121 MULTIMAP_CHECK( i_esermultimap, o_esermultimap );
122 MULTIMAP_CHECK( i_esplmultimap, o_esplmultimap );
123
124 #undef MULTIMAP_CHECK
125 }
126 }
127
BOOST_AUTO_TEST_CASE(binary_multimap)128 BOOST_AUTO_TEST_CASE( binary_multimap )
129 {
130 test_multimap<cereal::BinaryInputArchive, cereal::BinaryOutputArchive>();
131 }
132
BOOST_AUTO_TEST_CASE(portable_binary_multimap)133 BOOST_AUTO_TEST_CASE( portable_binary_multimap )
134 {
135 test_multimap<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>();
136 }
137
BOOST_AUTO_TEST_CASE(xml_multimap)138 BOOST_AUTO_TEST_CASE( xml_multimap )
139 {
140 test_multimap<cereal::XMLInputArchive, cereal::XMLOutputArchive>();
141 }
142
BOOST_AUTO_TEST_CASE(json_multimap)143 BOOST_AUTO_TEST_CASE( json_multimap )
144 {
145 test_multimap<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
146 }
147
148