]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/serialization/test/test_map_unordered.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
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)
10 // should pass compilation and execution
12 #include <algorithm> // std::copy
15 #include <cstddef> // size_t, NULL
17 #include <boost/config.hpp>
18 #include <boost/detail/workaround.hpp>
21 #if defined(BOOST_NO_STDC_NAMESPACE)
28 #include "test_tools.hpp"
30 #include <boost/serialization/nvp.hpp>
31 #include <boost/serialization/map.hpp>
36 ///////////////////////////////////////////////////////
37 // a key value initialized with a random value for use
38 // in testing STL map serialization
40 friend class boost::serialization::access
;
41 template<class Archive
>
44 const unsigned int /* file_version */
46 ar
& boost::serialization::make_nvp("random_key", m_i
);
49 random_key() : m_i(std::rand()){};
50 bool operator<(const random_key
&rhs
) const {
53 bool operator==(const random_key
&rhs
) const {
54 return m_i
== rhs
.m_i
;
56 operator std::size_t () const { // required by hash_map
61 #include <boost/serialization/unordered_map.hpp>
62 #include <functional> // requires changeset [69520]; Ticket #5254
66 struct hash
<random_key
>{
67 std::size_t operator()(const random_key
& r
) const {
68 return static_cast<std::size_t>(r
);
75 const char * testfile
= boost::archive::tmpnam(NULL
);
76 BOOST_REQUIRE(NULL
!= testfile
);
78 BOOST_CHECKPOINT("unordered_map");
79 // test unordered_map of objects
80 std::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()));
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
);
88 std::unordered_map
<random_key
, A
> anunordered_map1
;
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
);
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
);
102 std::remove(testfile
);
106 test_unordered_multimap(){
107 const char * testfile
= boost::archive::tmpnam(NULL
);
108 BOOST_REQUIRE(NULL
!= testfile
);
110 BOOST_CHECKPOINT("unordered_multimap");
111 std::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()));
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
);
119 std::unordered_multimap
<random_key
, A
> anunordered_multimap1
;
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
);
125 std::vector
< std::pair
<random_key
, A
> > tvec
, tvec1
;
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
);
136 int test_main( int /* argc */, char* /* argv */[] )
138 test_unordered_map();
139 test_unordered_multimap();