• 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 
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> {
operator ()BOOST_STD_EXTENSION_NAMESPACE::hash46         std::size_t operator()(const A& a) const {
47             return static_cast<std::size_t>(a);
48         }
49     };
50 }
51 
52 void
test_hash_set()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(a);
61     ahash_set.insert(a1);
62     {
63         test_ostream os(testfile, TEST_STREAM_FLAGS);
64         test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
65         oa << boost::serialization::make_nvp("ahash_set", 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("ahash_set", ahash_set1);
72     }
73     std::vector<A> tvec, tvec1;
74     tvec.clear();
75     tvec1.clear();
76     std::copy(ahash_set.begin(), ahash_set.end(), std::back_inserter(tvec));
77     std::sort(tvec.begin(), tvec.end());
78     std::copy(ahash_set1.begin(), ahash_set1.end(), std::back_inserter(tvec1));
79     std::sort(tvec1.begin(), tvec1.end());
80     BOOST_CHECK(tvec == tvec1);
81     std::remove(testfile);
82 }
83 
84 void
test_hash_multiset()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(A());
91     ahash_multiset.insert(A());
92     {
93         test_ostream os(testfile, TEST_STREAM_FLAGS);
94         test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
95         oa << boost::serialization::make_nvp("ahash_multiset", 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("ahash_multiset", ahash_multiset1);
102     }
103 
104     std::vector<A> tvec, tvec1;
105     tvec.clear();
106     tvec1.clear();
107     std::copy(ahash_multiset.begin(), ahash_multiset.end(), std::back_inserter(tvec));
108     std::sort(tvec.begin(), tvec.end());
109     std::copy(ahash_multiset1.begin(), ahash_multiset1.end(), std::back_inserter(tvec1));
110     std::sort(tvec1.begin(), tvec1.end());
111     BOOST_CHECK(tvec == tvec1);
112 
113     std::remove(testfile);
114 }
115 
test_main(int,char * [])116 int test_main( int /* argc */, char* /* argv */[] ){
117     test_hash_set();
118     test_hash_multiset();
119     return EXIT_SUCCESS;
120 }
121