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_UNORDERED_SET_H_
28 #define CEREAL_TEST_UNORDERED_SET_H_
29 #include "common.hpp"
30 
31 template <class IArchive, class OArchive> inline
test_unordered_set()32 void test_unordered_set()
33 {
34   std::random_device rd;
35   std::mt19937 gen(rd());
36 
37   for(int ii=0; ii<100; ++ii)
38   {
39     std::unordered_set<int> o_podunordered_set;
40     for(int j=0; j<100; ++j)
41       o_podunordered_set.insert(random_value<int>(gen));
42 
43     std::unordered_set<StructInternalSerialize, StructHash<StructInternalSerialize>> o_iserunordered_set;
44     for(int j=0; j<100; ++j)
45       o_iserunordered_set.insert({ random_value<int>(gen), random_value<int>(gen) });
46 
47     std::unordered_set<StructInternalSplit, StructHash<StructInternalSplit>> o_isplunordered_set;
48     for(int j=0; j<100; ++j)
49       o_isplunordered_set.insert({ random_value<int>(gen), random_value<int>(gen) });
50 
51     std::unordered_set<StructExternalSerialize, StructHash<StructExternalSerialize>> o_eserunordered_set;
52     for(int j=0; j<100; ++j)
53       o_eserunordered_set.insert({ random_value<int>(gen), random_value<int>(gen) });
54 
55     std::unordered_set<StructExternalSplit, StructHash<StructExternalSplit>> o_esplunordered_set;
56     for(int j=0; j<100; ++j)
57       o_esplunordered_set.insert({ random_value<int>(gen), random_value<int>(gen) });
58 
59     std::ostringstream os;
60     {
61       OArchive oar(os);
62 
63       oar(o_podunordered_set);
64       oar(o_iserunordered_set);
65       oar(o_isplunordered_set);
66       oar(o_eserunordered_set);
67       oar(o_esplunordered_set);
68     }
69 
70     std::unordered_set<int> i_podunordered_set;
71     std::unordered_set<StructInternalSerialize, StructHash<StructInternalSerialize>>   i_iserunordered_set;
72     std::unordered_set<StructInternalSplit, StructHash<StructInternalSplit>>           i_isplunordered_set;
73     std::unordered_set<StructExternalSerialize, StructHash<StructExternalSerialize>>   i_eserunordered_set;
74     std::unordered_set<StructExternalSplit, StructHash<StructExternalSplit>>           i_esplunordered_set;
75 
76     std::istringstream is(os.str());
77     {
78       IArchive iar(is);
79 
80       iar(i_podunordered_set);
81       iar(i_iserunordered_set);
82       iar(i_isplunordered_set);
83       iar(i_eserunordered_set);
84       iar(i_esplunordered_set);
85     }
86 
87     for(auto const & p : i_podunordered_set)
88     {
89       CHECK_EQ(o_podunordered_set.count(p), 1lu);
90     }
91 
92     for(auto const & p : i_iserunordered_set)
93     {
94       CHECK_EQ(o_iserunordered_set.count(p), 1lu);
95     }
96 
97     for(auto const & p : i_isplunordered_set)
98     {
99       CHECK_EQ(o_isplunordered_set.count(p), 1lu);
100     }
101 
102     for(auto const & p : i_eserunordered_set)
103     {
104       CHECK_EQ(o_eserunordered_set.count(p), 1lu);
105     }
106 
107     for(auto const & p : i_esplunordered_set)
108     {
109       CHECK_EQ(o_esplunordered_set.count(p), 1lu);
110     }
111   }
112 }
113 
114 #endif // CEREAL_TEST_UNORDERED_SET_H_
115