File: test_serialization3.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (216 lines) | stat: -rw-r--r-- 4,925 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* Boost.MultiIndex test for serialization, part 3.
 *
 * Copyright 2003-2018 Joaquin M Lopez Munoz.
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or copy at
 * http://www.boost.org/LICENSE_1_0.txt)
 *
 * See http://www.boost.org/libs/multi_index for library home page.
 */

#include "test_serialization3.hpp"
#include "test_serialization_template.hpp"

#include <boost/move/core.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/key_extractors.hpp>
#include "non_std_allocator.hpp"

struct non_default_ctble
{
  non_default_ctble(int n_):n(n_){}

  bool operator==(const non_default_ctble& x)const{return n==x.n;}

  int n;
};

#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
namespace boost{
namespace serialization{
#endif

template<class Archive>
void save_construct_data(
  Archive& ar,const non_default_ctble* p,const unsigned int version)
{
  if(version<3)return;

  ar<<boost::serialization::make_nvp("n",p->n);
}

template<class Archive>
void load_construct_data(
  Archive& ar,non_default_ctble* p,const unsigned int version)
{
  if(version<3)return;

  int n=0;
  ar>>boost::serialization::make_nvp("n",n);
  ::new(p)non_default_ctble(n);
}

template<class Archive>
void serialize(Archive&,non_default_ctble&,const unsigned int)
{
}

#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
} /* namespace serialization */
} /* namespace boost*/
#endif

namespace boost{
namespace serialization{
template<> struct version<non_default_ctble>
{
  BOOST_STATIC_CONSTANT(int,value=3);
};
} /* namespace serialization */
} /* namespace boost*/

struct non_copyable
{
  non_copyable(int n_=0):n(n_){}
  non_copyable(BOOST_RV_REF(non_copyable) x):n(x.n){}

  bool operator==(const non_copyable& x)const{return n==x.n;}
  bool operator<(const non_copyable& x)const{return n<x.n;}

  int n;

private:
  BOOST_MOVABLE_BUT_NOT_COPYABLE(non_copyable)
};

#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
namespace boost{
namespace serialization{
#endif

template<class Archive>
void serialize(Archive& ar,non_copyable& x,const unsigned int)
{
  ar&boost::serialization::make_nvp("n",x.n);
}

#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
} /* namespace serialization */
} /* namespace boost*/
#endif

using namespace boost::multi_index;

void test_serialization3()
{
  const int N=100;
  const int SHUFFLE=10232;

  typedef multi_index_container<
    int,
    indexed_by<
      hashed_unique<identity<int> >,
      sequenced<>
    >,
    non_std_allocator<int>
  > hashed_set;

  typedef hashed_set::iterator       iterator;
  typedef hashed_set::local_iterator local_iterator;

  hashed_set hs;

  for(int i=0;i<N;++i){
    hs.insert(i*SHUFFLE);
  }

  std::ostringstream oss;
  {
    boost::archive::text_oarchive oa(oss);
    oa<<const_cast<const hashed_set&>(hs);

    std::vector<iterator> its(N);
    for(int i=0;i<N;++i){
      iterator it=hs.find(i*SHUFFLE);
      its.push_back(it);
      oa<<const_cast<const iterator&>(its.back());
    }
    iterator it=hs.end();
    oa<<const_cast<const iterator&>(it);

    std::vector<local_iterator> lits(2*N);
    for(std::size_t buc=0;buc<hs.bucket_count();++buc){
      for(local_iterator lit=hs.begin(buc),lit_end=hs.end(buc);
          lit!=lit_end;++lit){
        oa<<*lit;
        lits.push_back(lit);
        oa<<const_cast<const local_iterator&>(lits.back());
      }
      local_iterator lit2=hs.end(buc);
      lits.push_back(lit2);
      oa<<const_cast<const local_iterator&>(lits.back());
    }
  }

  hashed_set hs2;
  std::istringstream iss(oss.str());
  boost::archive::text_iarchive ia(iss);
  ia>>hs2;
  BOOST_TEST(boost::multi_index::get<1>(hs)==boost::multi_index::get<1>(hs2));

  for(int j=0;j<N;++j){
    iterator it;
    ia>>it;
    BOOST_TEST(*it==j*SHUFFLE);
  }
  iterator it;
  ia>>it;
  BOOST_TEST(it==hs2.end());

  for(std::size_t buc=0;buc<hs2.bucket_count();++buc){
    for(std::size_t k=0;k<hs2.bucket_size(buc);++k){
      int n;
      local_iterator it_;
      ia>>n;
      ia>>it_;
      BOOST_TEST(*it_==n);
    }
    local_iterator it2;
    ia>>it2;
    BOOST_TEST(it2==hs2.end(buc));
  }

  {
    typedef multi_index_container<
      non_default_ctble,
      indexed_by<
        ordered_unique<
          BOOST_MULTI_INDEX_MEMBER(non_default_ctble,int,n)
        >
      >
    > multi_index_t;

    multi_index_t m;
    for(int i=0;i<100;++i)m.insert(non_default_ctble(i));
    test_serialization(m);
  }

  {
    /* testcase for https://svn.boost.org/trac10/ticket/13478 */

    typedef multi_index_container<
      non_copyable,
      indexed_by<
        ordered_unique<
          BOOST_MULTI_INDEX_MEMBER(non_copyable,int,n)
        >
      >
    > multi_index_t;

    multi_index_t m;
    for(int i=0;i<100;++i)m.emplace(i);
    test_serialization(m);
  }
}