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)
22namespace 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
39struct random_key {
40 friend class boost::serialization::access;
41 template<class Archive>
42 void serialize(
43 Archive & ar,
44 const unsigned int /* file_version */
45 ){
46 ar & boost::serialization::make_nvp(n: "random_key", v&: m_i);
47 }
48 int m_i;
49 random_key() : m_i(std::rand()){};
50 bool operator<(const random_key &rhs) const {
51 return m_i < rhs.m_i;
52 }
53 bool operator==(const random_key &rhs) const {
54 return m_i == rhs.m_i;
55 }
56 operator std::size_t () const { // required by hash_map
57 return m_i;
58 }
59};
60
61void
62test_map(){
63 const char * testfile = boost::archive::tmpnam(NULL);
64 BOOST_REQUIRE(NULL != testfile);
65
66 BOOST_MESSAGE("map");
67 // test map of objects
68 std::map<random_key, A> amap;
69 amap.insert(x: std::make_pair(x: random_key(), y: A()));
70 amap.insert(x: std::make_pair(x: random_key(), y: A()));
71 {
72 test_ostream os(testfile, TEST_STREAM_FLAGS);
73 test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
74 oa << boost::serialization::make_nvp(n: "amap", v&: amap);
75 }
76 std::map<random_key, A> amap1;
77 {
78 test_istream is(testfile, TEST_STREAM_FLAGS);
79 test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
80 ia >> boost::serialization::make_nvp(n: "amap", v&: amap1);
81 }
82 BOOST_CHECK(amap == amap1);
83 std::remove(filename: testfile);
84}
85
86void
87test_map_2(){
88 const char * testfile = boost::archive::tmpnam(NULL);
89 BOOST_REQUIRE(NULL != testfile);
90
91 BOOST_MESSAGE("map_2");
92 std::pair<int, int> a(11, 22);
93 std::map<int, int> b;
94 b[0] = 0;
95 b[-1] = -1;
96 b[1] = 1;
97 {
98 test_ostream os(testfile, TEST_STREAM_FLAGS);
99 std::pair<int, int> * const pa = &a;
100 std::map<int, int> * const pb = &b;
101 test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
102 oa << BOOST_SERIALIZATION_NVP(pb);
103 oa << BOOST_SERIALIZATION_NVP(pa);
104 }
105 {
106 test_istream is(testfile, TEST_STREAM_FLAGS);
107 std::pair<int, int> *pa = 0;
108 std::map<int, int> *pb = 0;
109 test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
110 ia >> BOOST_SERIALIZATION_NVP(pb);
111 ia >> BOOST_SERIALIZATION_NVP(pa);
112 delete pa;
113 delete pb;
114 }
115 std::remove(filename: testfile);
116}
117
118void
119test_multimap(){
120 const char * testfile = boost::archive::tmpnam(NULL);
121 BOOST_REQUIRE(NULL != testfile);
122
123 BOOST_MESSAGE("multimap");
124 std::multimap<random_key, A> amultimap;
125 amultimap.insert(x: std::make_pair(x: random_key(), y: A()));
126 amultimap.insert(x: std::make_pair(x: random_key(), y: A()));
127 {
128 test_ostream os(testfile, TEST_STREAM_FLAGS);
129 test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
130 oa << boost::serialization::make_nvp(n: "amultimap", v&: amultimap);
131 }
132 std::multimap<random_key, A> amultimap1;
133 {
134 test_istream is(testfile, TEST_STREAM_FLAGS);
135 test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
136 ia >> boost::serialization::make_nvp(n: "amultimap", v&: amultimap1);
137 }
138 BOOST_CHECK(amultimap == amultimap1);
139 std::remove(filename: testfile);
140}
141
142int test_main( int /* argc */, char* /* argv */[] )
143{
144 test_map();
145 test_map_2();
146 test_multimap();
147
148 return EXIT_SUCCESS;
149}
150

source code of boost/libs/serialization/test/test_map.cpp