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/hash_map.hpp>
63 namespace BOOST_STD_EXTENSION_NAMESPACE
{
65 struct hash
<random_key
>{
66 std::size_t operator()(const random_key
& r
) const {
67 return static_cast<std::size_t>(r
);
70 } // namespace BOOST_STD_EXTENSION_NAMESPACE
74 const char * testfile
= boost::archive::tmpnam(NULL
);
75 BOOST_REQUIRE(NULL
!= testfile
);
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()));
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
);
87 BOOST_STD_EXTENSION_NAMESPACE::hash_map
<random_key
, A
> ahash_map1
;
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
);
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
);
101 std::remove(testfile
);
105 test_hash_multimap(){
106 const char * testfile
= boost::archive::tmpnam(NULL
);
107 BOOST_REQUIRE(NULL
!= testfile
);
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()));
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
);
118 BOOST_STD_EXTENSION_NAMESPACE::hash_multimap
<random_key
, A
> ahash_multimap1
;
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
);
124 std::vector
< std::pair
<random_key
, A
> > tvec
, tvec1
;
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
);
134 int test_main( int /* argc */, char* /* argv */[] )
137 test_hash_multimap();