[29] | 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 | |
---|
| 34 | #include "A.hpp" |
---|
| 35 | |
---|
| 36 | /////////////////////////////////////////////////////// |
---|
| 37 | // a key value initialized with a random value for use |
---|
| 38 | // in testing STL map serialization |
---|
| 39 | struct random_key { |
---|
| 40 | friend class boost::serialization::access; |
---|
| 41 | template<class Archive> |
---|
| 42 | void serialize( |
---|
| 43 | Archive & ar, |
---|
| 44 | const unsigned int /* file_version */ |
---|
| 45 | ){ |
---|
| 46 | ar & boost::serialization::make_nvp("random_key", m_i); |
---|
| 47 | } |
---|
| 48 | int m_i; |
---|
| 49 | random_key() : m_i(std::rand()){}; |
---|
| 50 | bool operator<(const random_key &rhs) const { |
---|
| 51 | return m_i < rhs.m_i; |
---|
| 52 | } |
---|
| 53 | bool operator==(const random_key &rhs) const { |
---|
| 54 | return m_i == rhs.m_i; |
---|
| 55 | } |
---|
| 56 | operator std::size_t () const { // required by hash_map |
---|
| 57 | return m_i; |
---|
| 58 | } |
---|
| 59 | }; |
---|
| 60 | |
---|
| 61 | BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(random_key) |
---|
| 62 | |
---|
| 63 | void |
---|
| 64 | test_map(){ |
---|
| 65 | const char * testfile = boost::archive::tmpnam(NULL); |
---|
| 66 | BOOST_REQUIRE(NULL != testfile); |
---|
| 67 | |
---|
| 68 | BOOST_CHECKPOINT("map"); |
---|
| 69 | // test map of objects |
---|
| 70 | std::map<random_key, A> amap; |
---|
| 71 | amap.insert(std::make_pair(random_key(), A())); |
---|
| 72 | amap.insert(std::make_pair(random_key(), A())); |
---|
| 73 | { |
---|
| 74 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
---|
| 75 | test_oarchive oa(os); |
---|
| 76 | oa << boost::serialization::make_nvp("amap", amap); |
---|
| 77 | } |
---|
| 78 | std::map<random_key, A> amap1; |
---|
| 79 | { |
---|
| 80 | test_istream is(testfile, TEST_STREAM_FLAGS); |
---|
| 81 | test_iarchive ia(is); |
---|
| 82 | ia >> boost::serialization::make_nvp("amap", amap1); |
---|
| 83 | } |
---|
| 84 | BOOST_CHECK(amap == amap1); |
---|
| 85 | std::remove(testfile); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | void |
---|
| 89 | test_map_2(){ |
---|
| 90 | const char * testfile = boost::archive::tmpnam(NULL); |
---|
| 91 | BOOST_REQUIRE(NULL != testfile); |
---|
| 92 | |
---|
| 93 | BOOST_CHECKPOINT("map_2"); |
---|
| 94 | std::pair<int, int> a(11, 22); |
---|
| 95 | std::map<int, int> b; |
---|
| 96 | b[0] = 0; |
---|
| 97 | b[-1] = -1; |
---|
| 98 | b[1] = 1; |
---|
| 99 | { |
---|
| 100 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
---|
| 101 | std::pair<int, int> * const pa = &a; |
---|
| 102 | std::map<int, int> * const pb = &b; |
---|
| 103 | test_oarchive oa(os); |
---|
| 104 | oa << BOOST_SERIALIZATION_NVP(pb); |
---|
| 105 | oa << BOOST_SERIALIZATION_NVP(pa); |
---|
| 106 | } |
---|
| 107 | { |
---|
| 108 | test_istream is(testfile, TEST_STREAM_FLAGS); |
---|
| 109 | std::pair<int, int> *pa = 0; |
---|
| 110 | std::map<int, int> *pb = 0; |
---|
| 111 | test_iarchive ia(is); |
---|
| 112 | ia >> BOOST_SERIALIZATION_NVP(pb); |
---|
| 113 | ia >> BOOST_SERIALIZATION_NVP(pa); |
---|
| 114 | delete pa; |
---|
| 115 | delete pb; |
---|
| 116 | } |
---|
| 117 | std::remove(testfile); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | void |
---|
| 121 | test_multimap(){ |
---|
| 122 | const char * testfile = boost::archive::tmpnam(NULL); |
---|
| 123 | BOOST_REQUIRE(NULL != testfile); |
---|
| 124 | |
---|
| 125 | BOOST_CHECKPOINT("multimap"); |
---|
| 126 | std::multimap<random_key, A> amultimap; |
---|
| 127 | amultimap.insert(std::make_pair(random_key(), A())); |
---|
| 128 | amultimap.insert(std::make_pair(random_key(), A())); |
---|
| 129 | { |
---|
| 130 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
---|
| 131 | test_oarchive oa(os); |
---|
| 132 | oa << boost::serialization::make_nvp("amultimap", amultimap); |
---|
| 133 | } |
---|
| 134 | std::multimap<random_key, A> amultimap1; |
---|
| 135 | { |
---|
| 136 | test_istream is(testfile, TEST_STREAM_FLAGS); |
---|
| 137 | test_iarchive ia(is); |
---|
| 138 | ia >> boost::serialization::make_nvp("amultimap", amultimap1); |
---|
| 139 | } |
---|
| 140 | BOOST_CHECK(amultimap == amultimap1); |
---|
| 141 | std::remove(testfile); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | #ifdef BOOST_HAS_HASH |
---|
| 145 | #include <boost/serialization/hash_map.hpp> |
---|
| 146 | |
---|
| 147 | /* |
---|
| 148 | #if defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION) |
---|
| 149 | #define STD _STLP_STD |
---|
| 150 | #else |
---|
| 151 | #define STD BOOST_STD_EXTENSION_NAMESPACE |
---|
| 152 | #endif |
---|
| 153 | */ |
---|
| 154 | namespace BOOST_STD_EXTENSION_NAMESPACE { |
---|
| 155 | template<> |
---|
| 156 | struct hash<random_key>{ |
---|
| 157 | std::size_t operator()(const random_key& r) const { |
---|
| 158 | return (std::size_t)r; |
---|
| 159 | } |
---|
| 160 | }; |
---|
| 161 | } // namespace BOOST_STD_EXTENSION_NAMESPACE |
---|
| 162 | |
---|
| 163 | void |
---|
| 164 | test_hash_map(){ |
---|
| 165 | const char * testfile = boost::archive::tmpnam(NULL); |
---|
| 166 | BOOST_REQUIRE(NULL != testfile); |
---|
| 167 | |
---|
| 168 | BOOST_CHECKPOINT("hash_map"); |
---|
| 169 | // test hash_map of objects |
---|
| 170 | BOOST_STD_EXTENSION_NAMESPACE::hash_map<random_key, A> ahash_map; |
---|
| 171 | ahash_map.insert(std::make_pair(random_key(), A())); |
---|
| 172 | ahash_map.insert(std::make_pair(random_key(), A())); |
---|
| 173 | { |
---|
| 174 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
---|
| 175 | test_oarchive oa(os); |
---|
| 176 | oa << boost::serialization::make_nvp("ahashmap",ahash_map); |
---|
| 177 | } |
---|
| 178 | BOOST_STD_EXTENSION_NAMESPACE::hash_map<random_key, A> ahash_map1; |
---|
| 179 | { |
---|
| 180 | test_istream is(testfile, TEST_STREAM_FLAGS); |
---|
| 181 | test_iarchive ia(is); |
---|
| 182 | ia >> boost::serialization::make_nvp("ahashmap",ahash_map1); |
---|
| 183 | } |
---|
| 184 | BOOST_CHECK(ahash_map == ahash_map1); |
---|
| 185 | std::remove(testfile); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | void |
---|
| 189 | test_hash_multimap(){ |
---|
| 190 | const char * testfile = boost::archive::tmpnam(NULL); |
---|
| 191 | BOOST_REQUIRE(NULL != testfile); |
---|
| 192 | |
---|
| 193 | BOOST_CHECKPOINT("hash_multimap"); |
---|
| 194 | BOOST_STD_EXTENSION_NAMESPACE::hash_multimap<random_key, A> ahash_multimap; |
---|
| 195 | ahash_multimap.insert(std::make_pair(random_key(), A())); |
---|
| 196 | ahash_multimap.insert(std::make_pair(random_key(), A())); |
---|
| 197 | { |
---|
| 198 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
---|
| 199 | test_oarchive oa(os); |
---|
| 200 | oa << boost::serialization::make_nvp("ahash_multimap", ahash_multimap); |
---|
| 201 | } |
---|
| 202 | BOOST_STD_EXTENSION_NAMESPACE::hash_multimap<random_key, A> ahash_multimap1; |
---|
| 203 | { |
---|
| 204 | test_istream is(testfile, TEST_STREAM_FLAGS); |
---|
| 205 | test_iarchive ia(is); |
---|
| 206 | ia >> boost::serialization::make_nvp("ahash_multimap", ahash_multimap1); |
---|
| 207 | } |
---|
| 208 | BOOST_CHECK(ahash_multimap == ahash_multimap1); |
---|
| 209 | std::remove(testfile); |
---|
| 210 | } |
---|
| 211 | #endif |
---|
| 212 | |
---|
| 213 | int test_main( int /* argc */, char* /* argv */[] ) |
---|
| 214 | { |
---|
| 215 | test_map(); |
---|
| 216 | test_map_2(); |
---|
| 217 | test_multimap(); |
---|
| 218 | #ifdef BOOST_HAS_HASH |
---|
| 219 | test_hash_map(); |
---|
| 220 | test_hash_multimap(); |
---|
| 221 | #endif |
---|
| 222 | return EXIT_SUCCESS; |
---|
| 223 | } |
---|