| 1 | /* Boost.MultiIndex test for serialization, part 2. | 
|---|
| 2 |  * | 
|---|
| 3 |  * Copyright 2003-2006 Joaquín M López Muñoz. | 
|---|
| 4 |  * Distributed under the Boost Software License, Version 1.0. | 
|---|
| 5 |  * (See accompanying file LICENSE_1_0.txt or copy at | 
|---|
| 6 |  * http://www.boost.org/LICENSE_1_0.txt) | 
|---|
| 7 |  * | 
|---|
| 8 |  * See http://www.boost.org/libs/multi_index for library home page. | 
|---|
| 9 |  */ | 
|---|
| 10 |  | 
|---|
| 11 | #include "test_serialization.hpp" | 
|---|
| 12 | #include "test_serialization_template.hpp" | 
|---|
| 13 |  | 
|---|
| 14 | #include <boost/multi_index/hashed_index.hpp> | 
|---|
| 15 | #include <boost/multi_index/ordered_index.hpp> | 
|---|
| 16 | #include <boost/multi_index/sequenced_index.hpp> | 
|---|
| 17 | #include <boost/multi_index/random_access_index.hpp> | 
|---|
| 18 | #include <boost/multi_index/key_extractors.hpp> | 
|---|
| 19 | #include "pair_of_ints.hpp" | 
|---|
| 20 |  | 
|---|
| 21 | using namespace boost::multi_index; | 
|---|
| 22 |  | 
|---|
| 23 | void test_hashed_index_serialization() | 
|---|
| 24 | { | 
|---|
| 25 |   const int N=100; | 
|---|
| 26 |   const int SHUFFLE=10232; | 
|---|
| 27 |  | 
|---|
| 28 |   typedef multi_index_container< | 
|---|
| 29 |     int, | 
|---|
| 30 |     indexed_by< | 
|---|
| 31 |       hashed_unique<identity<int> >, | 
|---|
| 32 |       sequenced<> | 
|---|
| 33 |     > | 
|---|
| 34 |   > hashed_set; | 
|---|
| 35 |  | 
|---|
| 36 |   typedef hashed_set::iterator       iterator; | 
|---|
| 37 |   typedef hashed_set::local_iterator local_iterator; | 
|---|
| 38 |  | 
|---|
| 39 |   hashed_set hs; | 
|---|
| 40 |  | 
|---|
| 41 |   for(int i=0;i<N;++i){ | 
|---|
| 42 |     hs.insert(i*SHUFFLE); | 
|---|
| 43 |   } | 
|---|
| 44 |  | 
|---|
| 45 |   std::ostringstream oss; | 
|---|
| 46 |   { | 
|---|
| 47 |     boost::archive::text_oarchive oa(oss); | 
|---|
| 48 |     oa<<const_cast<const hashed_set&>(hs); | 
|---|
| 49 |  | 
|---|
| 50 |     std::vector<iterator> its(N); | 
|---|
| 51 |     for(int i=0;i<N;++i){ | 
|---|
| 52 |       iterator it=hs.find(i*SHUFFLE); | 
|---|
| 53 |       its.push_back(it); | 
|---|
| 54 |       oa<<const_cast<const iterator&>(its.back()); | 
|---|
| 55 |     } | 
|---|
| 56 |     iterator it=hs.end(); | 
|---|
| 57 |     oa<<const_cast<const iterator&>(it); | 
|---|
| 58 |  | 
|---|
| 59 |     std::vector<local_iterator> lits(2*N); | 
|---|
| 60 |     for(std::size_t buc=0;buc<hs.bucket_count();++buc){ | 
|---|
| 61 |       for(local_iterator lit=hs.begin(buc),lit_end=hs.end(buc); | 
|---|
| 62 |           lit!=lit_end;++lit){ | 
|---|
| 63 |         oa<<*lit; | 
|---|
| 64 |         lits.push_back(lit); | 
|---|
| 65 |         oa<<const_cast<const local_iterator&>(lits.back()); | 
|---|
| 66 |       } | 
|---|
| 67 |       local_iterator lit2=hs.end(buc); | 
|---|
| 68 |       lits.push_back(lit2); | 
|---|
| 69 |       oa<<const_cast<const local_iterator&>(lits.back()); | 
|---|
| 70 |     } | 
|---|
| 71 |   } | 
|---|
| 72 |  | 
|---|
| 73 |   hashed_set hs2; | 
|---|
| 74 |   std::istringstream iss(oss.str()); | 
|---|
| 75 |   boost::archive::text_iarchive ia(iss); | 
|---|
| 76 |   ia>>hs2; | 
|---|
| 77 |   BOOST_CHECK(get<1>(hs)==get<1>(hs2)); | 
|---|
| 78 |  | 
|---|
| 79 |   for(int j=0;j<N;++j){ | 
|---|
| 80 |     iterator it; | 
|---|
| 81 |     ia>>it; | 
|---|
| 82 |     BOOST_CHECK(*it==j*SHUFFLE); | 
|---|
| 83 |   } | 
|---|
| 84 |   iterator it; | 
|---|
| 85 |   ia>>it; | 
|---|
| 86 |   BOOST_CHECK(it==hs2.end()); | 
|---|
| 87 |  | 
|---|
| 88 |   for(std::size_t buc=0;buc<hs2.bucket_count();++buc){ | 
|---|
| 89 |     for(std::size_t k=0;k<hs2.bucket_size(buc);++k){ | 
|---|
| 90 |       int n; | 
|---|
| 91 |       local_iterator it; | 
|---|
| 92 |       ia>>n; | 
|---|
| 93 |       ia>>it; | 
|---|
| 94 |       BOOST_CHECK(*it==n); | 
|---|
| 95 |     } | 
|---|
| 96 |     local_iterator it2; | 
|---|
| 97 |     ia>>it2; | 
|---|
| 98 |     BOOST_CHECK(it2==hs2.end(buc)); | 
|---|
| 99 |   } | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | void test_serialization2() | 
|---|
| 103 | { | 
|---|
| 104 |   { | 
|---|
| 105 |     typedef multi_index_container< | 
|---|
| 106 |       pair_of_ints, | 
|---|
| 107 |       indexed_by< | 
|---|
| 108 |         ordered_unique< | 
|---|
| 109 |           BOOST_MULTI_INDEX_MEMBER(pair_of_ints,int,first) | 
|---|
| 110 |         >, | 
|---|
| 111 |         ordered_non_unique< | 
|---|
| 112 |           BOOST_MULTI_INDEX_MEMBER(pair_of_ints,int,second) | 
|---|
| 113 |         >, | 
|---|
| 114 |         random_access<>, | 
|---|
| 115 |         sequenced<> | 
|---|
| 116 |       > | 
|---|
| 117 |     > multi_index_t; | 
|---|
| 118 |  | 
|---|
| 119 |     multi_index_t m; | 
|---|
| 120 |     test_serialization(m); | 
|---|
| 121 |  | 
|---|
| 122 |     m.insert(pair_of_ints(4,0)); | 
|---|
| 123 |     test_serialization(m); | 
|---|
| 124 |  | 
|---|
| 125 |     m.insert(pair_of_ints(3,1)); | 
|---|
| 126 |     m.insert(pair_of_ints(2,1)); | 
|---|
| 127 |     test_serialization(m); | 
|---|
| 128 |  | 
|---|
| 129 |     m.insert(pair_of_ints(1,1)); | 
|---|
| 130 |     test_serialization(m); | 
|---|
| 131 |  | 
|---|
| 132 |     m.insert(pair_of_ints(0,0)); | 
|---|
| 133 |     test_serialization(m); | 
|---|
| 134 |  | 
|---|
| 135 |     m.insert(pair_of_ints(5,1)); | 
|---|
| 136 |     m.insert(pair_of_ints(7,1)); | 
|---|
| 137 |     m.insert(pair_of_ints(6,1)); | 
|---|
| 138 |     test_serialization(m); | 
|---|
| 139 |      | 
|---|
| 140 |     m.insert(pair_of_ints(8,1)); | 
|---|
| 141 |     m.insert(pair_of_ints(9,1)); | 
|---|
| 142 |     m.insert(pair_of_ints(12,1)); | 
|---|
| 143 |     m.insert(pair_of_ints(11,1)); | 
|---|
| 144 |     m.insert(pair_of_ints(10,1)); | 
|---|
| 145 |     test_serialization(m); | 
|---|
| 146 |  | 
|---|
| 147 |     /* testcase for bug reported at | 
|---|
| 148 |      * http://lists.boost.org/boost-users/2006/05/19362.php | 
|---|
| 149 |      */ | 
|---|
| 150 |  | 
|---|
| 151 |     m.clear(); | 
|---|
| 152 |     m.insert(pair_of_ints(0,0)); | 
|---|
| 153 |     m.insert(pair_of_ints(1,0)); | 
|---|
| 154 |     m.insert(pair_of_ints(2,1)); | 
|---|
| 155 |     m.insert(pair_of_ints(4,2)); | 
|---|
| 156 |     m.insert(pair_of_ints(3,2)); | 
|---|
| 157 |     test_serialization(m); | 
|---|
| 158 |   } | 
|---|
| 159 |   test_hashed_index_serialization(); | 
|---|
| 160 | } | 
|---|