add cereal library
[platform/upstream/iotivity.git] / extlibs / cereal / cereal / unittests / unordered_set.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_set()
32 {
33   std::random_device rd;
34   std::mt19937 gen(rd());
35
36   for(int ii=0; ii<100; ++ii)
37   {
38     std::unordered_set<int> o_podunordered_set;
39     for(int j=0; j<100; ++j)
40       o_podunordered_set.insert(random_value<int>(gen));
41
42     std::unordered_set<StructInternalSerialize, StructHash<StructInternalSerialize>> o_iserunordered_set;
43     for(int j=0; j<100; ++j)
44       o_iserunordered_set.insert({ random_value<int>(gen), random_value<int>(gen) });
45
46     std::unordered_set<StructInternalSplit, StructHash<StructInternalSplit>> o_isplunordered_set;
47     for(int j=0; j<100; ++j)
48       o_isplunordered_set.insert({ random_value<int>(gen), random_value<int>(gen) });
49
50     std::unordered_set<StructExternalSerialize, StructHash<StructExternalSerialize>> o_eserunordered_set;
51     for(int j=0; j<100; ++j)
52       o_eserunordered_set.insert({ random_value<int>(gen), random_value<int>(gen) });
53
54     std::unordered_set<StructExternalSplit, StructHash<StructExternalSplit>> o_esplunordered_set;
55     for(int j=0; j<100; ++j)
56       o_esplunordered_set.insert({ random_value<int>(gen), random_value<int>(gen) });
57
58     std::ostringstream os;
59     {
60       OArchive oar(os);
61
62       oar(o_podunordered_set);
63       oar(o_iserunordered_set);
64       oar(o_isplunordered_set);
65       oar(o_eserunordered_set);
66       oar(o_esplunordered_set);
67     }
68
69     std::unordered_set<int> i_podunordered_set;
70     std::unordered_set<StructInternalSerialize, StructHash<StructInternalSerialize>>   i_iserunordered_set;
71     std::unordered_set<StructInternalSplit, StructHash<StructInternalSplit>>           i_isplunordered_set;
72     std::unordered_set<StructExternalSerialize, StructHash<StructExternalSerialize>>   i_eserunordered_set;
73     std::unordered_set<StructExternalSplit, StructHash<StructExternalSplit>>           i_esplunordered_set;
74
75     std::istringstream is(os.str());
76     {
77       IArchive iar(is);
78
79       iar(i_podunordered_set);
80       iar(i_iserunordered_set);
81       iar(i_isplunordered_set);
82       iar(i_eserunordered_set);
83       iar(i_esplunordered_set);
84     }
85
86     for(auto const & p : i_podunordered_set)
87     {
88       BOOST_CHECK_EQUAL(o_podunordered_set.count(p), 1);
89     }
90
91     for(auto const & p : i_iserunordered_set)
92     {
93       BOOST_CHECK_EQUAL(o_iserunordered_set.count(p), 1);
94     }
95
96     for(auto const & p : i_isplunordered_set)
97     {
98       BOOST_CHECK_EQUAL(o_isplunordered_set.count(p), 1);
99     }
100
101     for(auto const & p : i_eserunordered_set)
102     {
103       BOOST_CHECK_EQUAL(o_eserunordered_set.count(p), 1);
104     }
105
106     for(auto const & p : i_esplunordered_set)
107     {
108       BOOST_CHECK_EQUAL(o_esplunordered_set.count(p), 1);
109     }
110   }
111 }
112
113 BOOST_AUTO_TEST_CASE( binary_unordered_set )
114 {
115   test_unordered_set<cereal::BinaryInputArchive, cereal::BinaryOutputArchive>();
116 }
117
118 BOOST_AUTO_TEST_CASE( portable_binary_unordered_set )
119 {
120   test_unordered_set<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>();
121 }
122
123 BOOST_AUTO_TEST_CASE( xml_unordered_set )
124 {
125   test_unordered_set<cereal::XMLInputArchive, cereal::XMLOutputArchive>();
126 }
127
128 BOOST_AUTO_TEST_CASE( json_unordered_set )
129 {
130   test_unordered_set<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
131 }
132
133