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 #include <boost/serialization/unordered_set.hpp>
44 #include <functional> // requires changeset [69520]; Ticket #5254
45
46 namespace std {
47 template<>
48 struct hash<A> {
operator ()std::hash49 std::size_t operator()(const A& a) const {
50 return static_cast<std::size_t>(a);
51 }
52 };
53 } // namespace std
54
55 void
test_unordered_set()56 test_unordered_set(){
57 const char * testfile = boost::archive::tmpnam(NULL);
58 BOOST_REQUIRE(NULL != testfile);
59
60 // test array of objects
61 std::unordered_set<A> anunordered_set;
62 A a, a1;
63 anunordered_set.insert(a);
64 anunordered_set.insert(a1);
65 {
66 test_ostream os(testfile, TEST_STREAM_FLAGS);
67 test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
68 oa << boost::serialization::make_nvp("anunordered_set", anunordered_set);
69 }
70 std::unordered_set<A> anunordered_set1;
71 {
72 test_istream is(testfile, TEST_STREAM_FLAGS);
73 test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
74 ia >> boost::serialization::make_nvp("anunordered_set", anunordered_set1);
75 }
76 std::vector<A> tvec, tvec1;
77 tvec.clear();
78 tvec1.clear();
79 std::copy(anunordered_set.begin(), anunordered_set.end(), std::back_inserter(tvec));
80 std::sort(tvec.begin(), tvec.end());
81 std::copy(anunordered_set1.begin(), anunordered_set1.end(), std::back_inserter(tvec1));
82 std::sort(tvec1.begin(), tvec1.end());
83 BOOST_CHECK(tvec == tvec1);
84 std::remove(testfile);
85 }
86
87 void
test_unordered_multiset()88 test_unordered_multiset(){
89 const char * testfile = boost::archive::tmpnam(NULL);
90 BOOST_REQUIRE(NULL != testfile);
91
92 std::unordered_multiset<A> anunordered_multiset;
93 anunordered_multiset.insert(A());
94 anunordered_multiset.insert(A());
95 {
96 test_ostream os(testfile, TEST_STREAM_FLAGS);
97 test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
98 oa << boost::serialization::make_nvp("anunordered_multiset", anunordered_multiset);
99 }
100 std::unordered_multiset<A> anunordered_multiset1;
101 {
102 test_istream is(testfile, TEST_STREAM_FLAGS);
103 test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
104 ia >> boost::serialization::make_nvp("anunordered_multiset", anunordered_multiset1);
105 }
106
107 std::vector<A> tvec, tvec1;
108 tvec.clear();
109 tvec1.clear();
110 std::copy(anunordered_multiset.begin(), anunordered_multiset.end(), std::back_inserter(tvec));
111 std::sort(tvec.begin(), tvec.end());
112 std::copy(anunordered_multiset1.begin(), anunordered_multiset1.end(), std::back_inserter(tvec1));
113 std::sort(tvec1.begin(), tvec1.end());
114 BOOST_CHECK(tvec == tvec1);
115
116 std::remove(testfile);
117 }
118
test_main(int,char * [])119 int test_main( int /* argc */, char* /* argv */[] ){
120 test_unordered_set();
121 test_unordered_multiset();
122 return EXIT_SUCCESS;
123 }
124