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 | |
21 | #include <boost/detail/workaround.hpp> |
22 | #if defined(BOOST_NO_STDC_NAMESPACE) |
23 | namespace std{ |
24 | using ::remove; |
25 | using ::size_t; |
26 | } |
27 | #endif |
28 | |
29 | #include "test_tools.hpp" |
30 | |
31 | #include <boost/serialization/nvp.hpp> |
32 | #include <boost/serialization/hash_set.hpp> |
33 | |
34 | #include "A.hpp" |
35 | #include "A.ipp" |
36 | |
37 | #ifndef BOOST_HAS_HASH |
38 | #error "BOOST_HAS_HASH not defined!" |
39 | #endif |
40 | |
41 | #include BOOST_HASH_SET_HEADER |
42 | |
43 | namespace BOOST_STD_EXTENSION_NAMESPACE { |
44 | template<> |
45 | struct hash<A> { |
46 | std::size_t operator()(const A& a) const { |
47 | return static_cast<std::size_t>(a); |
48 | } |
49 | }; |
50 | } |
51 | |
52 | void |
53 | test_hash_set(){ |
54 | const char * testfile = boost::archive::tmpnam(NULL); |
55 | BOOST_REQUIRE(NULL != testfile); |
56 | |
57 | // test array of objects |
58 | BOOST_STD_EXTENSION_NAMESPACE::hash_set<A> ahash_set; |
59 | A a, a1; |
60 | ahash_set.insert(obj: a); |
61 | ahash_set.insert(obj: a1); |
62 | { |
63 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
64 | test_oarchive oa(os, TEST_ARCHIVE_FLAGS); |
65 | oa << boost::serialization::make_nvp(n: "ahash_set" , v&: ahash_set); |
66 | } |
67 | BOOST_STD_EXTENSION_NAMESPACE::hash_set<A> ahash_set1; |
68 | { |
69 | test_istream is(testfile, TEST_STREAM_FLAGS); |
70 | test_iarchive ia(is, TEST_ARCHIVE_FLAGS); |
71 | ia >> boost::serialization::make_nvp(n: "ahash_set" , v&: ahash_set1); |
72 | } |
73 | std::vector<A> tvec, tvec1; |
74 | tvec.clear(); |
75 | tvec1.clear(); |
76 | std::copy(first: ahash_set.begin(), last: ahash_set.end(), result: std::back_inserter(x&: tvec)); |
77 | std::sort(first: tvec.begin(), last: tvec.end()); |
78 | std::copy(first: ahash_set1.begin(), last: ahash_set1.end(), result: std::back_inserter(x&: tvec1)); |
79 | std::sort(first: tvec1.begin(), last: tvec1.end()); |
80 | BOOST_CHECK(tvec == tvec1); |
81 | std::remove(filename: testfile); |
82 | } |
83 | |
84 | void |
85 | test_hash_multiset(){ |
86 | const char * testfile = boost::archive::tmpnam(NULL); |
87 | BOOST_REQUIRE(NULL != testfile); |
88 | |
89 | BOOST_STD_EXTENSION_NAMESPACE::hash_multiset<A> ahash_multiset; |
90 | ahash_multiset.insert(obj: A()); |
91 | ahash_multiset.insert(obj: A()); |
92 | { |
93 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
94 | test_oarchive oa(os, TEST_ARCHIVE_FLAGS); |
95 | oa << boost::serialization::make_nvp(n: "ahash_multiset" , v&: ahash_multiset); |
96 | } |
97 | BOOST_STD_EXTENSION_NAMESPACE::hash_multiset<A> ahash_multiset1; |
98 | { |
99 | test_istream is(testfile, TEST_STREAM_FLAGS); |
100 | test_iarchive ia(is, TEST_ARCHIVE_FLAGS); |
101 | ia >> boost::serialization::make_nvp(n: "ahash_multiset" , v&: ahash_multiset1); |
102 | } |
103 | |
104 | std::vector<A> tvec, tvec1; |
105 | tvec.clear(); |
106 | tvec1.clear(); |
107 | std::copy(first: ahash_multiset.begin(), last: ahash_multiset.end(), result: std::back_inserter(x&: tvec)); |
108 | std::sort(first: tvec.begin(), last: tvec.end()); |
109 | std::copy(first: ahash_multiset1.begin(), last: ahash_multiset1.end(), result: std::back_inserter(x&: tvec1)); |
110 | std::sort(first: tvec1.begin(), last: tvec1.end()); |
111 | BOOST_CHECK(tvec == tvec1); |
112 | |
113 | std::remove(filename: testfile); |
114 | } |
115 | |
116 | int test_main( int /* argc */, char* /* argv */[] ){ |
117 | test_hash_set(); |
118 | test_hash_multiset(); |
119 | return EXIT_SUCCESS; |
120 | } |
121 | |