| 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 | // Use, modification and distribution is subject to the Boost Software |
|---|
| 6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
|---|
| 7 | // http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 8 | |
|---|
| 9 | // should pass compilation and execution |
|---|
| 10 | |
|---|
| 11 | #include <algorithm> |
|---|
| 12 | #include <vector> |
|---|
| 13 | #include <fstream> |
|---|
| 14 | #include <cstddef> // size_t |
|---|
| 15 | |
|---|
| 16 | #include <boost/config.hpp> |
|---|
| 17 | #include <boost/detail/workaround.hpp> |
|---|
| 18 | |
|---|
| 19 | #include <cstdio> |
|---|
| 20 | #if defined(BOOST_NO_STDC_NAMESPACE) |
|---|
| 21 | namespace std{ |
|---|
| 22 | using ::rand; |
|---|
| 23 | using ::size_t; |
|---|
| 24 | } |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 27 | #include "test_tools.hpp" |
|---|
| 28 | #include <boost/preprocessor/stringize.hpp> |
|---|
| 29 | #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST) |
|---|
| 30 | |
|---|
| 31 | #include <boost/serialization/nvp.hpp> |
|---|
| 32 | #include <boost/serialization/map.hpp> |
|---|
| 33 | #ifdef BOOST_HAS_HASH |
|---|
| 34 | #include <boost/serialization/hash_map.hpp> |
|---|
| 35 | #endif |
|---|
| 36 | |
|---|
| 37 | #include "A.hpp" |
|---|
| 38 | |
|---|
| 39 | /////////////////////////////////////////////////////// |
|---|
| 40 | // a key value initialized with a random value for use |
|---|
| 41 | // in testing STL map serialization |
|---|
| 42 | struct random_key { |
|---|
| 43 | friend class boost::serialization::access; |
|---|
| 44 | template<class Archive> |
|---|
| 45 | void serialize( |
|---|
| 46 | Archive & ar, |
|---|
| 47 | const unsigned int /* file_version */ |
|---|
| 48 | ){ |
|---|
| 49 | ar & boost::serialization::make_nvp("random_key", m_i); |
|---|
| 50 | } |
|---|
| 51 | int m_i; |
|---|
| 52 | random_key() : m_i(std::rand()){}; |
|---|
| 53 | bool operator<(const random_key &rhs) const { |
|---|
| 54 | return m_i < rhs.m_i; |
|---|
| 55 | } |
|---|
| 56 | bool operator==(const random_key &rhs) const { |
|---|
| 57 | return m_i == rhs.m_i; |
|---|
| 58 | } |
|---|
| 59 | operator std::size_t () const { // required by hash_map |
|---|
| 60 | return m_i; |
|---|
| 61 | } |
|---|
| 62 | }; |
|---|
| 63 | |
|---|
| 64 | BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(random_key) |
|---|
| 65 | |
|---|
| 66 | #if defined(__LIBCOMO__) || (defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION)) |
|---|
| 67 | |
|---|
| 68 | namespace std { |
|---|
| 69 | template<> |
|---|
| 70 | struct equal_to<random_key> { |
|---|
| 71 | bool operator()(const random_key& lhs, const random_key& rhs) const { |
|---|
| 72 | return lhs.operator==(rhs); |
|---|
| 73 | } |
|---|
| 74 | }; |
|---|
| 75 | } // namespace std |
|---|
| 76 | |
|---|
| 77 | namespace BOOST_STD_EXTENSION_NAMESPACE { |
|---|
| 78 | template<> |
|---|
| 79 | struct hash<random_key> { |
|---|
| 80 | std::size_t operator()(const random_key& r) const { |
|---|
| 81 | return (std::size_t)r; |
|---|
| 82 | } |
|---|
| 83 | }; |
|---|
| 84 | } // namespace BOOST_STD_EXTENSION_NAMESPACE |
|---|
| 85 | |
|---|
| 86 | #endif |
|---|
| 87 | |
|---|
| 88 | void |
|---|
| 89 | test_map(){ |
|---|
| 90 | const char * testfile = boost::archive::tmpnam(NULL); |
|---|
| 91 | BOOST_REQUIRE(NULL != testfile); |
|---|
| 92 | |
|---|
| 93 | BOOST_CHECKPOINT("map"); |
|---|
| 94 | // test map of objects |
|---|
| 95 | std::map<random_key, A> amap; |
|---|
| 96 | amap.insert(std::make_pair(random_key(), A())); |
|---|
| 97 | amap.insert(std::make_pair(random_key(), A())); |
|---|
| 98 | { |
|---|
| 99 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
|---|
| 100 | test_oarchive oa(os); |
|---|
| 101 | oa << boost::serialization::make_nvp("amap", amap); |
|---|
| 102 | } |
|---|
| 103 | std::map<random_key, A> amap1; |
|---|
| 104 | { |
|---|
| 105 | test_istream is(testfile, TEST_STREAM_FLAGS); |
|---|
| 106 | test_iarchive ia(is); |
|---|
| 107 | ia >> boost::serialization::make_nvp("amap", amap1); |
|---|
| 108 | } |
|---|
| 109 | BOOST_CHECK(amap == amap1); |
|---|
| 110 | std::remove(testfile); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | void |
|---|
| 114 | test_map_2(){ |
|---|
| 115 | const char * testfile = boost::archive::tmpnam(NULL); |
|---|
| 116 | BOOST_REQUIRE(NULL != testfile); |
|---|
| 117 | |
|---|
| 118 | BOOST_CHECKPOINT("map_2"); |
|---|
| 119 | std::pair<int, int> a(11, 22); |
|---|
| 120 | std::map<int, int> b; |
|---|
| 121 | b[0] = 0; |
|---|
| 122 | b[-1] = -1; |
|---|
| 123 | b[1] = 1; |
|---|
| 124 | { |
|---|
| 125 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
|---|
| 126 | std::pair<int, int> * const pa = &a; |
|---|
| 127 | std::map<int, int> * const pb = &b; |
|---|
| 128 | test_oarchive oa(os); |
|---|
| 129 | oa << BOOST_SERIALIZATION_NVP(pb); |
|---|
| 130 | oa << BOOST_SERIALIZATION_NVP(pa); |
|---|
| 131 | } |
|---|
| 132 | { |
|---|
| 133 | test_istream is(testfile, TEST_STREAM_FLAGS); |
|---|
| 134 | std::pair<int, int> *pa = 0; |
|---|
| 135 | std::map<int, int> *pb = 0; |
|---|
| 136 | test_iarchive ia(is); |
|---|
| 137 | ia >> BOOST_SERIALIZATION_NVP(pb); |
|---|
| 138 | ia >> BOOST_SERIALIZATION_NVP(pa); |
|---|
| 139 | delete pa; |
|---|
| 140 | delete pb; |
|---|
| 141 | } |
|---|
| 142 | std::remove(testfile); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | void |
|---|
| 146 | test_multimap(){ |
|---|
| 147 | const char * testfile = boost::archive::tmpnam(NULL); |
|---|
| 148 | BOOST_REQUIRE(NULL != testfile); |
|---|
| 149 | |
|---|
| 150 | BOOST_CHECKPOINT("multimap"); |
|---|
| 151 | std::multimap<random_key, A> amultimap; |
|---|
| 152 | amultimap.insert(std::make_pair(random_key(), A())); |
|---|
| 153 | amultimap.insert(std::make_pair(random_key(), A())); |
|---|
| 154 | { |
|---|
| 155 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
|---|
| 156 | test_oarchive oa(os); |
|---|
| 157 | oa << boost::serialization::make_nvp("amultimap", amultimap); |
|---|
| 158 | } |
|---|
| 159 | std::multimap<random_key, A> amultimap1; |
|---|
| 160 | { |
|---|
| 161 | test_istream is(testfile, TEST_STREAM_FLAGS); |
|---|
| 162 | test_iarchive ia(is); |
|---|
| 163 | ia >> boost::serialization::make_nvp("amultimap", amultimap1); |
|---|
| 164 | } |
|---|
| 165 | BOOST_CHECK(amultimap == amultimap1); |
|---|
| 166 | std::remove(testfile); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | void |
|---|
| 170 | test_hash_map(){ |
|---|
| 171 | #ifdef BOOST_HAS_HASH |
|---|
| 172 | const char * testfile = boost::archive::tmpnam(NULL); |
|---|
| 173 | BOOST_REQUIRE(NULL != testfile); |
|---|
| 174 | |
|---|
| 175 | BOOST_CHECKPOINT("hash_map"); |
|---|
| 176 | // test hash_map of objects |
|---|
| 177 | BOOST_STD_EXTENSION_NAMESPACE::hash_map<random_key, A> ahash_map; |
|---|
| 178 | ahash_map.insert(std::make_pair(random_key(), A())); |
|---|
| 179 | ahash_map.insert(std::make_pair(random_key(), A())); |
|---|
| 180 | { |
|---|
| 181 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
|---|
| 182 | test_oarchive oa(os); |
|---|
| 183 | oa << boost::serialization::make_nvp("ahashmap",ahash_map); |
|---|
| 184 | } |
|---|
| 185 | BOOST_STD_EXTENSION_NAMESPACE::hash_map<random_key, A> ahash_map1; |
|---|
| 186 | { |
|---|
| 187 | test_istream is(testfile, TEST_STREAM_FLAGS); |
|---|
| 188 | test_iarchive ia(is); |
|---|
| 189 | ia >> boost::serialization::make_nvp("ahashmap",ahash_map1); |
|---|
| 190 | } |
|---|
| 191 | // at least one library - MSL notes: it doesn't make much sense |
|---|
| 192 | // to implement the == operator for hash collections - but goes ahead |
|---|
| 193 | // does it anyway even though it doesn't seem to work. So sort into |
|---|
| 194 | // vectors and then compare. |
|---|
| 195 | std::vector< std::pair<random_key, A> > tvec, tvec1; |
|---|
| 196 | std::copy(ahash_map.begin(), ahash_map.end(), std::back_inserter(tvec)); |
|---|
| 197 | std::sort(tvec.begin(), tvec.end()); |
|---|
| 198 | std::copy(ahash_map1.begin(), ahash_map1.end(), std::back_inserter(tvec1)); |
|---|
| 199 | std::sort(tvec1.begin(), tvec1.end()); |
|---|
| 200 | BOOST_CHECK(tvec == tvec1); |
|---|
| 201 | std::remove(testfile); |
|---|
| 202 | #endif |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | void |
|---|
| 206 | test_hash_multimap(){ |
|---|
| 207 | #ifdef BOOST_HAS_HASH |
|---|
| 208 | const char * testfile = boost::archive::tmpnam(NULL); |
|---|
| 209 | BOOST_REQUIRE(NULL != testfile); |
|---|
| 210 | |
|---|
| 211 | BOOST_CHECKPOINT("hash_multimap"); |
|---|
| 212 | BOOST_STD_EXTENSION_NAMESPACE::hash_multimap<random_key, A> ahash_multimap; |
|---|
| 213 | ahash_multimap.insert(std::make_pair(random_key(), A())); |
|---|
| 214 | ahash_multimap.insert(std::make_pair(random_key(), A())); |
|---|
| 215 | { |
|---|
| 216 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
|---|
| 217 | test_oarchive oa(os); |
|---|
| 218 | oa << boost::serialization::make_nvp("ahash_multimap", ahash_multimap); |
|---|
| 219 | } |
|---|
| 220 | BOOST_STD_EXTENSION_NAMESPACE::hash_multimap<random_key, A> ahash_multimap1; |
|---|
| 221 | { |
|---|
| 222 | test_istream is(testfile, TEST_STREAM_FLAGS); |
|---|
| 223 | test_iarchive ia(is); |
|---|
| 224 | ia >> boost::serialization::make_nvp("ahash_multimap", ahash_multimap1); |
|---|
| 225 | } |
|---|
| 226 | // at least one library - MSL notes: it doesn't make much sense |
|---|
| 227 | // to implement the == operator for hash collections - but goes ahead |
|---|
| 228 | // does it anyway even though it doesn't seem to work. So sort into |
|---|
| 229 | // vectors and then compare. |
|---|
| 230 | std::vector< std::pair<random_key, A> > tvec, tvec1; |
|---|
| 231 | tvec.clear(); |
|---|
| 232 | tvec1.clear(); |
|---|
| 233 | std::copy(ahash_multimap.begin(), ahash_multimap.end(), std::back_inserter(tvec)); |
|---|
| 234 | std::sort(tvec.begin(), tvec.end()); |
|---|
| 235 | std::copy(ahash_multimap1.begin(), ahash_multimap1.end(), std::back_inserter(tvec1)); |
|---|
| 236 | std::sort(tvec1.begin(), tvec1.end()); |
|---|
| 237 | BOOST_CHECK(tvec == tvec1); |
|---|
| 238 | std::remove(testfile); |
|---|
| 239 | #endif |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | int test_main( int /* argc */, char* /* argv */[] ) |
|---|
| 243 | { |
|---|
| 244 | test_map(); |
|---|
| 245 | test_map_2(); |
|---|
| 246 | test_multimap(); |
|---|
| 247 | test_hash_map(); |
|---|
| 248 | test_hash_multimap(); |
|---|
| 249 | return EXIT_SUCCESS; |
|---|
| 250 | } |
|---|