• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_set.cpp
3 
4 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
5 // (C) Copyright 2014 Jim Bell
6 // Use, modification and distribution is subject to the Boost Software
7 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 // should pass compilation and execution
11 
12 #include <cstddef> // NULLsize_t
13 #include <cstdio> // remove
14 #include <fstream>
15 
16 #include <algorithm> // std::copy
17 #include <vector>
18 
19 #include <boost/config.hpp>
20 #if defined(BOOST_NO_STDC_NAMESPACE)
21 namespace std{
22     using ::size_t;
23 }
24 #endif
25 
26 #include <boost/detail/workaround.hpp>
27 #if defined(BOOST_NO_STDC_NAMESPACE)
28 namespace std{
29     using ::remove;
30 }
31 #endif
32 
33 #include <boost/archive/archive_exception.hpp>
34 
35 #include "test_tools.hpp"
36 
37 #include <boost/serialization/nvp.hpp>
38 #include <boost/serialization/set.hpp>
39 
40 #include "A.hpp"
41 #include "A.ipp"
42 
43 void
test_set()44 test_set(){
45     const char * testfile = boost::archive::tmpnam(NULL);
46     BOOST_REQUIRE(NULL != testfile);
47 
48     // test array of objects
49     std::set<A> aset;
50     aset.insert(A());
51     aset.insert(A());
52     {
53         test_ostream os(testfile, TEST_STREAM_FLAGS);
54         test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
55         oa << boost::serialization::make_nvp("aset", aset);
56     }
57     std::set<A> aset1;
58     {
59         test_istream is(testfile, TEST_STREAM_FLAGS);
60         test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
61         ia >> boost::serialization::make_nvp("aset", aset1);
62     }
63     BOOST_CHECK(aset == aset1);
64     std::remove(testfile);
65 }
66 
67 void
test_multiset()68 test_multiset(){
69     const char * testfile = boost::archive::tmpnam(NULL);
70     BOOST_REQUIRE(NULL != testfile);
71 
72     std::multiset<A> amultiset;
73     amultiset.insert(A());
74     amultiset.insert(A());
75     {
76         test_ostream os(testfile, TEST_STREAM_FLAGS);
77         test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
78         oa << boost::serialization::make_nvp("amultiset", amultiset);
79     }
80     std::multiset<A> amultiset1;
81     {
82         test_istream is(testfile, TEST_STREAM_FLAGS);
83         test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
84         ia >> boost::serialization::make_nvp("amultiset", amultiset1);
85     }
86     BOOST_CHECK(amultiset == amultiset1);
87     std::remove(testfile);
88 }
89 
90 #ifdef BOOST_HAS_HASH
91 
92 #include <boost/serialization/hash_set.hpp>
93 
94 namespace BOOST_STD_EXTENSION_NAMESPACE {
95     template<>
96     struct hash<A> {
operator ()BOOST_STD_EXTENSION_NAMESPACE::hash97         std::size_t operator()(const A& a) const {
98             return static_cast<std::size_t>(a);
99         }
100     };
101 }
102 
103 void
test_hash_set()104 test_hash_set(){
105     const char * testfile = boost::archive::tmpnam(NULL);
106     BOOST_REQUIRE(NULL != testfile);
107 
108     // test array of objects
109     BOOST_STD_EXTENSION_NAMESPACE::hash_set<A> ahash_set;
110     A a, a1;
111     ahash_set.insert(a);
112     ahash_set.insert(a1);
113     {
114         test_ostream os(testfile, TEST_STREAM_FLAGS);
115         test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
116         oa << boost::serialization::make_nvp("ahash_set", ahash_set);
117     }
118     BOOST_STD_EXTENSION_NAMESPACE::hash_set<A> ahash_set1;
119     {
120         test_istream is(testfile, TEST_STREAM_FLAGS);
121         test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
122         ia >> boost::serialization::make_nvp("ahash_set", ahash_set1);
123     }
124     std::vector<A> tvec, tvec1;
125     tvec.clear();
126     tvec1.clear();
127     std::copy(ahash_set.begin(), ahash_set.end(), std::back_inserter(tvec));
128     std::sort(tvec.begin(), tvec.end());
129     std::copy(ahash_set1.begin(), ahash_set1.end(), std::back_inserter(tvec1));
130     std::sort(tvec1.begin(), tvec1.end());
131     BOOST_CHECK(tvec == tvec1);
132     std::remove(testfile);
133 }
134 
135 void
test_hash_multiset()136 test_hash_multiset(){
137     const char * testfile = boost::archive::tmpnam(NULL);
138     BOOST_REQUIRE(NULL != testfile);
139 
140     BOOST_STD_EXTENSION_NAMESPACE::hash_multiset<A> ahash_multiset;
141     ahash_multiset.insert(A());
142     ahash_multiset.insert(A());
143     {
144         test_ostream os(testfile, TEST_STREAM_FLAGS);
145         test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
146         oa << boost::serialization::make_nvp("ahash_multiset", ahash_multiset);
147     }
148     BOOST_STD_EXTENSION_NAMESPACE::hash_multiset<A> ahash_multiset1;
149     {
150         test_istream is(testfile, TEST_STREAM_FLAGS);
151         test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
152         ia >> boost::serialization::make_nvp("ahash_multiset", ahash_multiset1);
153     }
154 
155     std::vector<A> tvec, tvec1;
156     tvec.clear();
157     tvec1.clear();
158     std::copy(ahash_multiset.begin(), ahash_multiset.end(), std::back_inserter(tvec));
159     std::sort(tvec.begin(), tvec.end());
160     std::copy(ahash_multiset1.begin(), ahash_multiset1.end(), std::back_inserter(tvec1));
161     std::sort(tvec1.begin(), tvec1.end());
162     BOOST_CHECK(tvec == tvec1);
163 
164     std::remove(testfile);
165 }
166 #endif
167 
168 #ifndef BOOST_NO_CXX11_HDR_UNORDERED_SET
169 
170 #include <boost/serialization/unordered_set.hpp>
171 #include <functional> // requires changeset [69520]; Ticket #5254
172 
173 namespace std {
174     template<>
175     struct hash<A> {
operator ()std::hash176         std::size_t operator()(const A& a) const {
177             return static_cast<std::size_t>(a);
178         }
179     };
180 } // namespace std
181 
182 void
test_unordered_set()183 test_unordered_set(){
184     const char * testfile = boost::archive::tmpnam(NULL);
185     BOOST_REQUIRE(NULL != testfile);
186 
187     // test array of objects
188     std::unordered_set<A> anunordered_set;
189     A a, a1;
190     anunordered_set.insert(a);
191     anunordered_set.insert(a1);
192     {
193         test_ostream os(testfile, TEST_STREAM_FLAGS);
194         test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
195         oa << boost::serialization::make_nvp("anunordered_set", anunordered_set);
196     }
197     std::unordered_set<A> anunordered_set1;
198     {
199         test_istream is(testfile, TEST_STREAM_FLAGS);
200         test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
201         ia >> boost::serialization::make_nvp("anunordered_set", anunordered_set1);
202     }
203     std::vector<A> tvec, tvec1;
204     tvec.clear();
205     tvec1.clear();
206     std::copy(anunordered_set.begin(), anunordered_set.end(), std::back_inserter(tvec));
207     std::sort(tvec.begin(), tvec.end());
208     std::copy(anunordered_set1.begin(), anunordered_set1.end(), std::back_inserter(tvec1));
209     std::sort(tvec1.begin(), tvec1.end());
210     BOOST_CHECK(tvec == tvec1);
211     std::remove(testfile);
212 }
213 
214 void
test_unordered_multiset()215 test_unordered_multiset(){
216     const char * testfile = boost::archive::tmpnam(NULL);
217     BOOST_REQUIRE(NULL != testfile);
218 
219     std::unordered_multiset<A> anunordered_multiset;
220     anunordered_multiset.insert(A());
221     anunordered_multiset.insert(A());
222     {
223         test_ostream os(testfile, TEST_STREAM_FLAGS);
224         test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
225         oa << boost::serialization::make_nvp("anunordered_multiset", anunordered_multiset);
226     }
227     std::unordered_multiset<A> anunordered_multiset1;
228     {
229         test_istream is(testfile, TEST_STREAM_FLAGS);
230         test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
231         ia >> boost::serialization::make_nvp("anunordered_multiset", anunordered_multiset1);
232     }
233 
234     std::vector<A> tvec, tvec1;
235     tvec.clear();
236     tvec1.clear();
237     std::copy(anunordered_multiset.begin(), anunordered_multiset.end(), std::back_inserter(tvec));
238     std::sort(tvec.begin(), tvec.end());
239     std::copy(anunordered_multiset1.begin(), anunordered_multiset1.end(), std::back_inserter(tvec1));
240     std::sort(tvec1.begin(), tvec1.end());
241     BOOST_CHECK(tvec == tvec1);
242 
243     std::remove(testfile);
244 }
245 #endif
246 
test_main(int,char * [])247 int test_main( int /* argc */, char* /* argv */[] ){
248     test_set();
249     test_multiset();
250 
251     #ifdef BOOST_HAS_HASH
252     test_hash_set();
253     test_hash_multiset();
254     #endif
255 
256     #ifndef BOOST_NO_CXX11_HDR_UNORDERED_SET
257 	test_unordered_set();
258 	test_unordered_multiset();
259     #endif
260 
261     return EXIT_SUCCESS;
262 }
263