Adding cereal lib
[platform/upstream/iotivity.git] / extlibs / cereal / unittests / unordered_map.cpp
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>
31 void test_unordered_map()
32 {
33   std::random_device rd;
34   std::mt19937 gen(rd());
35
36   for(int ii=0; ii<100; ++ii)
37   {
38     std::unordered_map<std::string, int> o_podunordered_map;
39     for(int j=0; j<100; ++j)
40       o_podunordered_map.insert({random_value<std::string>(gen), random_value<int>(gen)});
41
42     std::unordered_map<uint16_t, StructInternalSerialize> o_iserunordered_map;
43     for(int j=0; j<100; ++j)
44       o_iserunordered_map.insert({random_value<uint16_t>(gen), { random_value<int>(gen), random_value<int>(gen) }});
45
46     std::unordered_map<uint16_t, StructInternalSplit> o_isplunordered_map;
47     for(int j=0; j<100; ++j)
48       o_isplunordered_map.insert({random_value<uint16_t>(gen), { random_value<int>(gen), random_value<int>(gen) }});
49
50     std::unordered_map<uint32_t, StructExternalSerialize> o_eserunordered_map;
51     for(int j=0; j<100; ++j)
52       o_eserunordered_map.insert({random_value<uint32_t>(gen), { random_value<int>(gen), random_value<int>(gen) }});
53
54     std::unordered_map<int8_t, StructExternalSplit> o_esplunordered_map;
55     for(int j=0; j<100; ++j)
56       o_esplunordered_map.insert({random_value<char>(gen),  { random_value<int>(gen), random_value<int>(gen) }});
57
58     std::ostringstream os;
59     {
60       OArchive oar(os);
61
62       oar(o_podunordered_map);
63       oar(o_iserunordered_map);
64       oar(o_isplunordered_map);
65       oar(o_eserunordered_map);
66       oar(o_esplunordered_map);
67     }
68
69     std::unordered_map<std::string, int> i_podunordered_map;
70     std::unordered_map<uint16_t, StructInternalSerialize>   i_iserunordered_map;
71     std::unordered_map<uint16_t, StructInternalSplit>        i_isplunordered_map;
72     std::unordered_map<uint32_t, StructExternalSerialize> i_eserunordered_map;
73     std::unordered_map<int8_t, StructExternalSplit>       i_esplunordered_map;
74
75     std::istringstream is(os.str());
76     {
77       IArchive iar(is);
78
79       iar(i_podunordered_map);
80       iar(i_iserunordered_map);
81       iar(i_isplunordered_map);
82       iar(i_eserunordered_map);
83       iar(i_esplunordered_map);
84     }
85
86     for(auto const & p : i_podunordered_map)
87     {
88       auto v = o_podunordered_map.find(p.first);
89       BOOST_CHECK(v != o_podunordered_map.end());
90       BOOST_CHECK_EQUAL(p.second, v->second);
91     }
92
93     for(auto const & p : i_iserunordered_map)
94     {
95       auto v = o_iserunordered_map.find(p.first);
96       BOOST_CHECK(v != o_iserunordered_map.end());
97       BOOST_CHECK_EQUAL(p.second, v->second);
98     }
99
100     for(auto const & p : i_isplunordered_map)
101     {
102       auto v = o_isplunordered_map.find(p.first);
103       BOOST_CHECK(v != o_isplunordered_map.end());
104       BOOST_CHECK_EQUAL(p.second, v->second);
105     }
106
107     for(auto const & p : i_eserunordered_map)
108     {
109       auto v = o_eserunordered_map.find(p.first);
110       BOOST_CHECK(v != o_eserunordered_map.end());
111       BOOST_CHECK_EQUAL(p.second, v->second);
112     }
113
114     for(auto const & p : i_esplunordered_map)
115     {
116       auto v = o_esplunordered_map.find(p.first);
117       BOOST_CHECK(v != o_esplunordered_map.end());
118       BOOST_CHECK_EQUAL(p.second, v->second);
119     }
120   }
121 }
122
123 BOOST_AUTO_TEST_CASE( binary_unordered_map )
124 {
125   test_unordered_map<cereal::BinaryInputArchive, cereal::BinaryOutputArchive>();
126 }
127
128 BOOST_AUTO_TEST_CASE( portable_binary_unordered_map )
129 {
130   test_unordered_map<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>();
131 }
132
133 BOOST_AUTO_TEST_CASE( xml_unordered_map )
134 {
135   test_unordered_map<cereal::XMLInputArchive, cereal::XMLOutputArchive>();
136 }
137
138 BOOST_AUTO_TEST_CASE( json_unordered_map )
139 {
140   test_unordered_map<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
141 }
142