| 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> |
| 50 | void serialize( |
| 51 | Archive & ar, |
| 52 | const unsigned int /* file_version */ |
| 53 | ){ |
| 54 | ar & boost::serialization::make_nvp(n: "random_key" , v&: m_i); |
| 55 | } |
| 56 | int m_i; |
| 57 | random_key() : m_i(std::rand()){}; |
| 58 | bool operator<(const random_key &rhs) const { |
| 59 | return m_i < rhs.m_i; |
| 60 | } |
| 61 | bool operator==(const random_key &rhs) const { |
| 62 | return m_i == rhs.m_i; |
| 63 | } |
| 64 | 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>{ |
| 72 | 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 |
| 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(obj: std::make_pair(x: random_key(), y: A())); |
| 87 | ahash_map.insert(obj: std::make_pair(x: random_key(), y: A())); |
| 88 | { |
| 89 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
| 90 | test_oarchive oa(os, TEST_ARCHIVE_FLAGS); |
| 91 | oa << boost::serialization::make_nvp(n: "ahashmap" ,v&: 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(n: "ahashmap" ,v&: ahash_map1); |
| 98 | } |
| 99 | |
| 100 | std::vector< std::pair<random_key, A> > tvec, tvec1; |
| 101 | std::copy(first: ahash_map.begin(), last: ahash_map.end(), result: std::back_inserter(x&: tvec)); |
| 102 | std::sort(first: tvec.begin(), last: tvec.end()); |
| 103 | std::copy(first: ahash_map1.begin(), last: ahash_map1.end(), result: std::back_inserter(x&: tvec1)); |
| 104 | std::sort(first: tvec1.begin(), last: tvec1.end()); |
| 105 | BOOST_CHECK(tvec == tvec1); |
| 106 | |
| 107 | std::remove(filename: testfile); |
| 108 | } |
| 109 | |
| 110 | void |
| 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(obj: std::make_pair(x: random_key(), y: A())); |
| 118 | ahash_multimap.insert(obj: std::make_pair(x: random_key(), y: A())); |
| 119 | { |
| 120 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
| 121 | test_oarchive oa(os, TEST_ARCHIVE_FLAGS); |
| 122 | oa << boost::serialization::make_nvp(n: "ahash_multimap" , v&: 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(n: "ahash_multimap" , v&: ahash_multimap1); |
| 129 | } |
| 130 | std::vector< std::pair<random_key, A> > tvec, tvec1; |
| 131 | tvec.clear(); |
| 132 | tvec1.clear(); |
| 133 | std::copy(first: ahash_multimap.begin(), last: ahash_multimap.end(), result: std::back_inserter(x&: tvec)); |
| 134 | std::sort(first: tvec.begin(), last: tvec.end()); |
| 135 | std::copy(first: ahash_multimap1.begin(), last: ahash_multimap1.end(), result: std::back_inserter(x&: tvec1)); |
| 136 | std::sort(first: tvec1.begin(), last: tvec1.end()); |
| 137 | BOOST_CHECK(tvec == tvec1); |
| 138 | std::remove(filename: testfile); |
| 139 | } |
| 140 | int test_main( int /* argc */, char* /* argv */[] ) |
| 141 | { |
| 142 | test_hash_map(); |
| 143 | test_hash_multimap(); |
| 144 | return EXIT_SUCCESS; |
| 145 | } |
| 146 | |