• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <algorithm> // std::copy
13 #include <vector>
14 #include <fstream>
15 #include <cstddef> // size_t, NULL
16 
17 #include <boost/config.hpp>
18 #include <boost/detail/workaround.hpp>
19 
20 #include <cstdio>
21 #if defined(BOOST_NO_STDC_NAMESPACE)
22 namespace std{
23     using ::rand;
24     using ::size_t;
25 }
26 #endif
27 
28 #include "test_tools.hpp"
29 
30 #include <boost/serialization/nvp.hpp>
31 #include <boost/serialization/map.hpp>
32 
33 #include "A.hpp"
34 #include "A.ipp"
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>
serializerandom_key42     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;
random_keyrandom_key49     random_key() : m_i(std::rand()){};
operator <random_key50     bool operator<(const random_key &rhs) const {
51         return m_i < rhs.m_i;
52     }
operator ==random_key53     bool operator==(const random_key &rhs) const {
54         return m_i == rhs.m_i;
55     }
operator std::size_trandom_key56     operator std::size_t () const {    // required by hash_map
57         return m_i;
58     }
59 };
60 
61 #include <boost/serialization/hash_map.hpp>
62 
63 namespace BOOST_STD_EXTENSION_NAMESPACE {
64     template<>
65     struct hash<random_key>{
operator ()BOOST_STD_EXTENSION_NAMESPACE::hash66         std::size_t operator()(const random_key& r) const {
67             return static_cast<std::size_t>(r);
68         }
69     };
70 } // namespace BOOST_STD_EXTENSION_NAMESPACE
71 
72 void
test_hash_map()73 test_hash_map(){
74     const char * testfile = boost::archive::tmpnam(NULL);
75     BOOST_REQUIRE(NULL != testfile);
76 
77     BOOST_CHECKPOINT("hash_map");
78     // test hash_map of objects
79     BOOST_STD_EXTENSION_NAMESPACE::hash_map<random_key, A> ahash_map;
80     ahash_map.insert(std::make_pair(random_key(), A()));
81     ahash_map.insert(std::make_pair(random_key(), A()));
82     {
83         test_ostream os(testfile, TEST_STREAM_FLAGS);
84         test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
85         oa << boost::serialization::make_nvp("ahashmap",ahash_map);
86     }
87     BOOST_STD_EXTENSION_NAMESPACE::hash_map<random_key, A> ahash_map1;
88     {
89         test_istream is(testfile, TEST_STREAM_FLAGS);
90         test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
91         ia >> boost::serialization::make_nvp("ahashmap",ahash_map1);
92     }
93 
94     std::vector< std::pair<random_key, A> > tvec, tvec1;
95     std::copy(ahash_map.begin(), ahash_map.end(), std::back_inserter(tvec));
96     std::sort(tvec.begin(), tvec.end());
97     std::copy(ahash_map1.begin(), ahash_map1.end(), std::back_inserter(tvec1));
98     std::sort(tvec1.begin(), tvec1.end());
99     BOOST_CHECK(tvec == tvec1);
100 
101     std::remove(testfile);
102 }
103 
104 void
test_hash_multimap()105 test_hash_multimap(){
106     const char * testfile = boost::archive::tmpnam(NULL);
107     BOOST_REQUIRE(NULL != testfile);
108 
109     BOOST_CHECKPOINT("hash_multimap");
110     BOOST_STD_EXTENSION_NAMESPACE::hash_multimap<random_key, A> ahash_multimap;
111     ahash_multimap.insert(std::make_pair(random_key(), A()));
112     ahash_multimap.insert(std::make_pair(random_key(), A()));
113     {
114         test_ostream os(testfile, TEST_STREAM_FLAGS);
115         test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
116         oa << boost::serialization::make_nvp("ahash_multimap", ahash_multimap);
117     }
118     BOOST_STD_EXTENSION_NAMESPACE::hash_multimap<random_key, A> ahash_multimap1;
119     {
120         test_istream is(testfile, TEST_STREAM_FLAGS);
121         test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
122         ia >> boost::serialization::make_nvp("ahash_multimap", ahash_multimap1);
123     }
124     std::vector< std::pair<random_key, A> > tvec, tvec1;
125     tvec.clear();
126     tvec1.clear();
127     std::copy(ahash_multimap.begin(), ahash_multimap.end(), std::back_inserter(tvec));
128     std::sort(tvec.begin(), tvec.end());
129     std::copy(ahash_multimap1.begin(), ahash_multimap1.end(), std::back_inserter(tvec1));
130     std::sort(tvec1.begin(), tvec1.end());
131     BOOST_CHECK(tvec == tvec1);
132     std::remove(testfile);
133 }
test_main(int,char * [])134 int test_main( int /* argc */, char* /* argv */[] )
135 {
136     test_hash_map();
137     test_hash_multimap();
138     return EXIT_SUCCESS;
139 }
140