1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_map.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> // NULL, size_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 ::rand;
25 using ::remove;
26 using ::size_t;
27 }
28 #endif
29
30 #include "test_tools.hpp"
31
32 #include <boost/serialization/nvp.hpp>
33 #include <boost/serialization/hash_map.hpp>
34
35 #include "A.hpp"
36 #include "A.ipp"
37
38 #ifndef BOOST_HAS_HASH
39 #error "BOOST_HAS_HASH not defined!"
40 #endif
41
42 #include BOOST_HASH_MAP_HEADER
43
44 ///////////////////////////////////////////////////////
45 // a key value initialized with a random value for use
46 // in testing STL map serialization
47 struct random_key {
48 friend class boost::serialization::access;
49 template<class Archive>
serializerandom_key50 void serialize(
51 Archive & ar,
52 const unsigned int /* file_version */
53 ){
54 ar & boost::serialization::make_nvp("random_key", m_i);
55 }
56 int m_i;
random_keyrandom_key57 random_key() : m_i(std::rand()){};
operator <random_key58 bool operator<(const random_key &rhs) const {
59 return m_i < rhs.m_i;
60 }
operator ==random_key61 bool operator==(const random_key &rhs) const {
62 return m_i == rhs.m_i;
63 }
operator std::size_trandom_key64 operator std::size_t () const { // required by hash_map
65 return m_i;
66 }
67 };
68
69 namespace BOOST_STD_EXTENSION_NAMESPACE {
70 template<>
71 struct hash<random_key>{
operator ()BOOST_STD_EXTENSION_NAMESPACE::hash72 std::size_t operator()(const random_key& r) const {
73 return static_cast<std::size_t>(r);
74 }
75 };
76 } // namespace BOOST_STD_EXTENSION_NAMESPACE
77
78 void
test_hash_map()79 test_hash_map(){
80 const char * testfile = boost::archive::tmpnam(NULL);
81 BOOST_REQUIRE(NULL != testfile);
82
83 BOOST_CHECKPOINT("hash_map");
84 // test hash_map of objects
85 BOOST_STD_EXTENSION_NAMESPACE::hash_map<random_key, A> ahash_map;
86 ahash_map.insert(std::make_pair(random_key(), A()));
87 ahash_map.insert(std::make_pair(random_key(), A()));
88 {
89 test_ostream os(testfile, TEST_STREAM_FLAGS);
90 test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
91 oa << boost::serialization::make_nvp("ahashmap",ahash_map);
92 }
93 BOOST_STD_EXTENSION_NAMESPACE::hash_map<random_key, A> ahash_map1;
94 {
95 test_istream is(testfile, TEST_STREAM_FLAGS);
96 test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
97 ia >> boost::serialization::make_nvp("ahashmap",ahash_map1);
98 }
99
100 std::vector< std::pair<random_key, A> > tvec, tvec1;
101 std::copy(ahash_map.begin(), ahash_map.end(), std::back_inserter(tvec));
102 std::sort(tvec.begin(), tvec.end());
103 std::copy(ahash_map1.begin(), ahash_map1.end(), std::back_inserter(tvec1));
104 std::sort(tvec1.begin(), tvec1.end());
105 BOOST_CHECK(tvec == tvec1);
106
107 std::remove(testfile);
108 }
109
110 void
test_hash_multimap()111 test_hash_multimap(){
112 const char * testfile = boost::archive::tmpnam(NULL);
113 BOOST_REQUIRE(NULL != testfile);
114
115 BOOST_CHECKPOINT("hash_multimap");
116 BOOST_STD_EXTENSION_NAMESPACE::hash_multimap<random_key, A> ahash_multimap;
117 ahash_multimap.insert(std::make_pair(random_key(), A()));
118 ahash_multimap.insert(std::make_pair(random_key(), A()));
119 {
120 test_ostream os(testfile, TEST_STREAM_FLAGS);
121 test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
122 oa << boost::serialization::make_nvp("ahash_multimap", ahash_multimap);
123 }
124 BOOST_STD_EXTENSION_NAMESPACE::hash_multimap<random_key, A> ahash_multimap1;
125 {
126 test_istream is(testfile, TEST_STREAM_FLAGS);
127 test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
128 ia >> boost::serialization::make_nvp("ahash_multimap", ahash_multimap1);
129 }
130 std::vector< std::pair<random_key, A> > tvec, tvec1;
131 tvec.clear();
132 tvec1.clear();
133 std::copy(ahash_multimap.begin(), ahash_multimap.end(), std::back_inserter(tvec));
134 std::sort(tvec.begin(), tvec.end());
135 std::copy(ahash_multimap1.begin(), ahash_multimap1.end(), std::back_inserter(tvec1));
136 std::sort(tvec1.begin(), tvec1.end());
137 BOOST_CHECK(tvec == tvec1);
138 std::remove(testfile);
139 }
test_main(int,char * [])140 int test_main( int /* argc */, char* /* argv */[] )
141 {
142 test_hash_map();
143 test_hash_multimap();
144 return EXIT_SUCCESS;
145 }
146