• 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/boost_unordered_map.hpp>
62 #include <functional> // requires changeset [69520]; Ticket #5254
63 
64 namespace boost {
65     template<>
66     struct hash<random_key>{
operator ()boost::hash67         std::size_t operator()(const random_key& r) const {
68             return static_cast<std::size_t>(r);
69         }
70     };
71 } // namespace std
72 
73 void
test_unordered_map()74 test_unordered_map(){
75     const char * testfile = boost::archive::tmpnam(NULL);
76     BOOST_REQUIRE(NULL != testfile);
77 
78     BOOST_CHECKPOINT("unordered_map");
79     // test unordered_map of objects
80     boost::unordered_map<random_key, A> anunordered_map;
81     anunordered_map.insert(std::make_pair(random_key(), A()));
82     anunordered_map.insert(std::make_pair(random_key(), A()));
83     {
84         test_ostream os(testfile, TEST_STREAM_FLAGS);
85         test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
86         oa << boost::serialization::make_nvp("anunorderedmap",anunordered_map);
87     }
88     boost::unordered_map<random_key, A> anunordered_map1;
89     {
90         test_istream is(testfile, TEST_STREAM_FLAGS);
91         test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
92         ia >> boost::serialization::make_nvp("anunorderedmap",anunordered_map1);
93     }
94 
95     std::vector< std::pair<random_key, A> > tvec, tvec1;
96     std::copy(anunordered_map.begin(), anunordered_map.end(), std::back_inserter(tvec));
97     std::sort(tvec.begin(), tvec.end());
98     std::copy(anunordered_map1.begin(), anunordered_map1.end(), std::back_inserter(tvec1));
99     std::sort(tvec1.begin(), tvec1.end());
100     BOOST_CHECK(tvec == tvec1);
101 
102     std::remove(testfile);
103 }
104 
105 void
test_unordered_multimap()106 test_unordered_multimap(){
107     const char * testfile = boost::archive::tmpnam(NULL);
108     BOOST_REQUIRE(NULL != testfile);
109 
110     BOOST_CHECKPOINT("unordered_multimap");
111     boost::unordered_multimap<random_key, A> anunordered_multimap;
112     anunordered_multimap.insert(std::make_pair(random_key(), A()));
113     anunordered_multimap.insert(std::make_pair(random_key(), A()));
114     {
115         test_ostream os(testfile, TEST_STREAM_FLAGS);
116         test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
117         oa << boost::serialization::make_nvp("anunordered_multimap", anunordered_multimap);
118     }
119     boost::unordered_multimap<random_key, A> anunordered_multimap1;
120     {
121         test_istream is(testfile, TEST_STREAM_FLAGS);
122         test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
123         ia >> boost::serialization::make_nvp("anunordered_multimap", anunordered_multimap1);
124     }
125     std::vector< std::pair<random_key, A> > tvec, tvec1;
126     tvec.clear();
127     tvec1.clear();
128     std::copy(anunordered_multimap.begin(), anunordered_multimap.end(), std::back_inserter(tvec));
129     std::sort(tvec.begin(), tvec.end());
130     std::copy(anunordered_multimap1.begin(), anunordered_multimap1.end(), std::back_inserter(tvec1));
131     std::sort(tvec1.begin(), tvec1.end());
132     BOOST_CHECK(tvec == tvec1);
133     std::remove(testfile);
134 }
135 
test_main(int,char * [])136 int test_main( int /* argc */, char* /* argv */[] )
137 {
138     test_unordered_map();
139     test_unordered_multimap();
140 
141     return EXIT_SUCCESS;
142 }
143