File: test_update.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (281 lines) | stat: -rw-r--r-- 9,378 bytes parent folder | download | duplicates (15)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* Boost.MultiIndex test for replace(), modify() and modify_key().
 *
 * 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_update.hpp"

#include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
#include <algorithm>
#include <cstddef>
#include "pre_multi_index.hpp"
#include "employee.hpp"
#include "pair_of_ints.hpp"
#include <boost/detail/lightweight_test.hpp>
#include <boost/next_prior.hpp>

struct do_nothing
{
  template<typename T>
  void operator()(const T&)const{}
};

struct null_hash
{
  template<typename T>
  std::size_t operator()(const T&)const{return 0;}
};

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

  void operator()(int& x)const{x=n_;}

  int n_;
};

template<class MultiIndexContainer>
void test_stable_update()
{
  typedef typename MultiIndexContainer::iterator        iterator;
  typedef typename MultiIndexContainer::size_type       size_type;
  typedef typename MultiIndexContainer::difference_type difference_type;

  MultiIndexContainer c;
  c.insert(0);
  c.insert(1);c.insert(1);
  c.insert(2);c.insert(2);c.insert(2);c.insert(2);
  c.insert(3);
  c.insert(4);c.insert(4);c.insert(4);
  c.insert(5);c.insert(5);
  c.insert(6);
  c.insert(7);
  size_type num_elems=
    c.count(0)+c.count(1)+c.count(2)+c.count(3)+
    c.count(4)+c.count(5)+c.count(6)+c.count(7);

  for(size_type n=c.size();n--;){
    iterator it=boost::next(c.begin(),(difference_type)n);

    c.replace(it,*it);
    BOOST_TEST((size_type)std::distance(c.begin(),it)==n);

    c.modify(it,do_nothing());
    BOOST_TEST((size_type)std::distance(c.begin(),it)==n);

    c.modify(it,do_nothing(),do_nothing());
    BOOST_TEST((size_type)std::distance(c.begin(),it)==n);

    for(int i=0;i<=8;++i){
      MultiIndexContainer cpy(c);
      bool b=c.modify(it,assign_value(i),assign_value(*it));
      BOOST_TEST(b||(size_type)std::distance(c.begin(),it)==n);
      BOOST_TEST(c.count(0)+c.count(1)+c.count(2)+c.count(3)+c.count(4)+
                  c.count(5)+c.count(6)+c.count(7)+c.count(8)==num_elems);
      if(b){
        c=cpy;
        it=boost::next(c.begin(),(difference_type)n);
      }
    }
  }
}

using namespace boost::multi_index;

void test_update()
{
  employee_set              es;
  employee_set_as_inserted& i=get<as_inserted>(es);
  employee_set_randomly&    r=get<randomly>(es);

  es.insert(employee(0,"Joe",31,1123));
  es.insert(employee(1,"Robert",27,5601));
  es.insert(employee(2,"John",40,7889));
  es.insert(employee(3,"Olbert",20,9012));
  es.insert(employee(4,"John",57,1002));

  employee_set::iterator             it=es.find(employee(0,"Joe",31,1123));
  employee_set_as_inserted::iterator it1=
    project<as_inserted>(es,get<name>(es).find("Olbert"));
  employee_set_randomly::iterator    it2=
    project<randomly>(es,get<age>(es).find(57));

  BOOST_TEST(es.replace(it,*it));
  BOOST_TEST(i.replace(it1,*it1));
  BOOST_TEST(r.replace(it2,*it2));
  BOOST_TEST(!es.replace(it,employee(3,"Joe",31,1123))&&it->id==0);
  BOOST_TEST(es.replace(it,employee(0,"Joe",32,1123))&&it->age==32);
  BOOST_TEST(i.replace(it1,employee(3,"Albert",20,9012))&&it1->name==
                "Albert");
  BOOST_TEST(!r.replace(it2,employee(4,"John",57,5601)));

  {
    typedef multi_index_container<
      pair_of_ints,
      indexed_by<
        ordered_unique<BOOST_MULTI_INDEX_MEMBER(pair_of_ints,int,first)>,
        hashed_unique<BOOST_MULTI_INDEX_MEMBER(pair_of_ints,int,second)>,
        sequenced<> > >
    int_int_set;

    int_int_set iis;
    nth_index<int_int_set,1>::type& ii1=get<1>(iis);
    nth_index<int_int_set,2>::type& ii2=get<2>(iis);
    iis.insert(pair_of_ints(0,0));
    iis.insert(pair_of_ints(5,5));
    iis.insert(pair_of_ints(10,10));

    BOOST_TEST(!iis.replace(iis.begin(),pair_of_ints(5,0)));
    BOOST_TEST(!ii2.replace(ii2.begin(),pair_of_ints(0,5)));
    BOOST_TEST(!ii1.replace(project<1>(iis,iis.begin()),pair_of_ints(5,11)));
    BOOST_TEST(!iis.replace(iis.begin(),pair_of_ints(11,5)));
    BOOST_TEST(!iis.replace(boost::next(iis.begin()),pair_of_ints(10,5)));
    BOOST_TEST(!ii1.replace(
      project<1>(iis,boost::next(iis.begin())),pair_of_ints(5,10)));
    BOOST_TEST(!iis.replace(boost::prior(iis.end()),pair_of_ints(5,10)));
    BOOST_TEST(!ii2.replace(boost::prior(ii2.end()),pair_of_ints(10,5)));

    BOOST_TEST(iis.modify(iis.begin(),increment_first));
    BOOST_TEST(ii2.modify(ii2.begin(),increment_first));
    BOOST_TEST(ii1.modify(project<1>(iis,iis.begin()),increment_first));
    BOOST_TEST(ii2.modify(ii2.begin(),increment_first,decrement_first));

    BOOST_TEST(!iis.modify(iis.begin(),increment_first,decrement_first));
    BOOST_TEST(iis.size()==3);

    BOOST_TEST(!iis.modify(iis.begin(),increment_first));
    BOOST_TEST(iis.size()==2);

    iis.insert(pair_of_ints(0,0));
    BOOST_TEST(ii2.modify(boost::prior(ii2.end()),increment_second));
    BOOST_TEST(iis.modify(iis.begin(),increment_second));
    BOOST_TEST(ii2.modify(boost::prior(ii2.end()),increment_second));
    BOOST_TEST(iis.modify(iis.begin(),increment_second,decrement_second));

    BOOST_TEST(!ii2.modify(
      boost::prior(ii2.end()),increment_second,decrement_second));
    BOOST_TEST(ii2.size()==3);

    BOOST_TEST(!ii2.modify(boost::prior(ii2.end()),increment_second));
    BOOST_TEST(ii2.size()==2);

    iis.insert(pair_of_ints(0,0));
    BOOST_TEST(iis.modify_key(iis.begin(),increment_int));
    BOOST_TEST(iis.modify_key(iis.begin(),increment_int,decrement_int));
    BOOST_TEST(iis.modify_key(iis.begin(),increment_int));
    BOOST_TEST(iis.modify_key(iis.begin(),increment_int));

    BOOST_TEST(!iis.modify_key(iis.begin(),increment_int,decrement_int));
    BOOST_TEST(iis.size()==3);

    BOOST_TEST(!iis.modify_key(iis.begin(),increment_int));
    BOOST_TEST(iis.size()==2);

    nth_index_iterator<int_int_set,1>::type it_=ii1.find(5);
    BOOST_TEST(ii1.modify_key(it_,increment_int));
    BOOST_TEST(ii1.modify_key(it_,increment_int));
    BOOST_TEST(ii1.modify_key(it_,increment_int,decrement_int));
    BOOST_TEST(ii1.modify_key(it_,increment_int));

    BOOST_TEST(!ii1.modify_key(it_,increment_int,decrement_int));
    BOOST_TEST(ii1.size()==2);

    BOOST_TEST(!ii1.modify_key(it_,increment_int));
    BOOST_TEST(ii1.size()==1);
  }
  {
    typedef multi_index_container<
      pair_of_ints,
      indexed_by<
        hashed_unique<BOOST_MULTI_INDEX_MEMBER(pair_of_ints,int,first)>,
        random_access<>,
        ordered_unique<BOOST_MULTI_INDEX_MEMBER(pair_of_ints,int,second)> > >
    int_int_set;

    int_int_set iis;
    nth_index<int_int_set,1>::type& ii1=get<1>(iis);
    int_int_set::iterator p1=iis.insert(pair_of_ints(0,0)).first;
    int_int_set::iterator p2=iis.insert(pair_of_ints(5,5)).first;
    int_int_set::iterator p3=iis.insert(pair_of_ints(10,10)).first;

    BOOST_TEST(!iis.replace(p1,pair_of_ints(5,0)));
    BOOST_TEST(!ii1.replace(ii1.begin(),pair_of_ints(0,5)));
    BOOST_TEST(!iis.replace(p1,pair_of_ints(5,11)));
    BOOST_TEST(!iis.replace(p1,pair_of_ints(11,5)));
    BOOST_TEST(!iis.replace(p2,pair_of_ints(10,5)));
    BOOST_TEST(!iis.replace(p2,pair_of_ints(5,10)));
    BOOST_TEST(!iis.replace(p3,pair_of_ints(5,10)));
    BOOST_TEST(!ii1.replace(boost::prior(ii1.end()),pair_of_ints(10,5)));

    BOOST_TEST(iis.modify(p1,increment_first));
    BOOST_TEST(ii1.modify(ii1.begin(),increment_first));
    BOOST_TEST(iis.modify(p1,increment_first));
    BOOST_TEST(ii1.modify(ii1.begin(),increment_first,decrement_first));

    BOOST_TEST(!iis.modify(p1,increment_first,decrement_first));
    BOOST_TEST(iis.size()==3);

    BOOST_TEST(!iis.modify(p1,increment_first));
    BOOST_TEST(iis.size()==2);

    p1=iis.insert(pair_of_ints(0,0)).first;
    BOOST_TEST(ii1.modify(boost::prior(ii1.end()),increment_second));
    BOOST_TEST(iis.modify(p1,increment_second,decrement_second));
    BOOST_TEST(ii1.modify(boost::prior(ii1.end()),increment_second));
    BOOST_TEST(iis.modify(p1,increment_second));

    BOOST_TEST(!ii1.modify(
      boost::prior(ii1.end()),increment_second,decrement_second));
    BOOST_TEST(ii1.size()==3);

    BOOST_TEST(!ii1.modify(boost::prior(ii1.end()),increment_second));
    BOOST_TEST(ii1.size()==2);
  }
  {
    typedef multi_index_container<
      int,
      indexed_by<
        ordered_non_unique<identity<int> >
      >
    > int_multiset;
    test_stable_update<int_multiset>();

    typedef multi_index_container<
      int,
      indexed_by<
        hashed_unique<identity<int> >
      >
    > int_hashed_set;
    test_stable_update<int_hashed_set>();

    typedef multi_index_container<
      int,
      indexed_by<
        hashed_unique<identity<int> >
      >
    > int_hashed_multiset;
    test_stable_update<int_hashed_multiset>();

    typedef multi_index_container<
      int,
      indexed_by<
        hashed_unique<identity<int>,null_hash>
      >
    > degenerate_int_hashed_set;
    test_stable_update<degenerate_int_hashed_set>();

    typedef multi_index_container<
      int,
      indexed_by<
        hashed_non_unique<identity<int>,null_hash>
      >
    > degenerate_int_hashed_multiset;
    test_stable_update<degenerate_int_hashed_multiset>();
  }
}