summaryrefslogtreecommitdiff
path: root/libs/intrusive/test/generic_assoc_test.hpp
blob: 0107afe33e4e3e3adfaff9db97102765af544e67 (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Olaf Krzikalla 2004-2006.
// (C) Copyright Ion Gaztanaga  2006-2013.
//
// 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/intrusive for documentation.
//
/////////////////////////////////////////////////////////////////////////////
#include <boost/container/vector.hpp> //vector
#include <algorithm> //sort, random_shuffle
#include <boost/intrusive/detail/config_begin.hpp>
#include "common_functors.hpp"
#include <boost/intrusive/options.hpp>
#include <boost/detail/lightweight_test.hpp>
#include "test_macros.hpp"
#include "test_container.hpp"

namespace boost{
namespace intrusive{
namespace test{

template<class T>
struct has_splay
{
   static const bool value = false;
};

template<class T>
struct has_rebalance
{
   static const bool value = false;
};

template<class T>
struct has_insert_before
{
   static const bool value = false;
};

template<class T>
struct has_const_searches
{
   static const bool value = true;
};

template<class T, bool = has_const_searches<T>::value>
struct search_const_iterator
{
   typedef typename T::const_iterator type;
};

template<class T>
struct search_const_iterator<T, false>
{
   typedef typename T::iterator type;
};

template<class T, bool = has_const_searches<T>::value>
struct search_const_container
{
   typedef const T type;
};

template<class T>
struct search_const_container<T, false>
{
   typedef T type;
};

template<class ValueTraits, template <class = void, class = void, class = void, class = void> class ContainerDefiner>
struct test_generic_assoc
{
   typedef typename ValueTraits::value_type value_type;
   typedef typename Value_Container< value_type >::type value_cont_type;
   typedef typename ValueTraits::reference reference;
   typedef typename ValueTraits::const_reference const_reference;
   static void test_all(value_cont_type&);
   static void test_clone(value_cont_type&);
   static void test_insert_erase_burst();
   static void test_container_from_end(value_cont_type&, detail::true_type);
   static void test_container_from_end(value_cont_type&, detail::false_type) {}
   static void test_splay_up(value_cont_type&, detail::true_type);
   static void test_splay_up(value_cont_type&, detail::false_type) {}
   static void test_splay_down(value_cont_type&, detail::true_type);
   static void test_splay_down(value_cont_type&, detail::false_type) {}
   static void test_rebalance(value_cont_type&, detail::true_type);
   static void test_rebalance(value_cont_type&, detail::false_type) {}
   static void test_insert_before(value_cont_type&, detail::true_type);
   static void test_insert_before(value_cont_type&, detail::false_type) {}
   static void test_container_from_iterator(value_cont_type&, detail::true_type);
   static void test_container_from_iterator(value_cont_type&, detail::false_type) {}
};

template<class ValueTraits, template <class = void, class = void, class = void, class = void> class ContainerDefiner>
void test_generic_assoc<ValueTraits, ContainerDefiner>::
   test_container_from_iterator(value_cont_type& values, detail::true_type)
{
   typedef ContainerDefiner
      < value_type
      , value_traits<ValueTraits>
      , constant_time_size<value_type::constant_time_size>
      > definer_function;
   typedef typename definer_function::type assoc_type;
   assoc_type testset(values.begin(), values.end());
   typedef typename assoc_type::iterator        it_type;
   typedef typename assoc_type::const_iterator  cit_type;
   typedef typename assoc_type::size_type       sz_type;
   sz_type sz = testset.size();
   for(it_type b(testset.begin()), e(testset.end()); b != e; ++b)
   {
      assoc_type &s = assoc_type::container_from_iterator(b);
      const assoc_type &cs = assoc_type::container_from_iterator(cit_type(b));
      BOOST_TEST(&s == &cs);
      BOOST_TEST(&s == &testset);
      s.erase(b);
      BOOST_TEST(testset.size() == (sz-1));
      s.insert(*b);
      BOOST_TEST(testset.size() == sz);
   }
}

template<class ValueTraits, template <class = void, class = void, class = void, class = void> class ContainerDefiner>
void test_generic_assoc<ValueTraits, ContainerDefiner>::test_insert_erase_burst()
{
   typedef typename ValueTraits::value_type value_type;

   //value_cont_type values;
   const std::size_t MaxValues = 200;
   value_cont_type values(MaxValues);
   for(std::size_t i = 0; i != MaxValues; ++i){
      (&values[i])->value_ = i;
   }

   typedef ContainerDefiner
      < value_type
      , value_traits<ValueTraits>
      , constant_time_size<value_type::constant_time_size>
      > definer_function;
   typedef typename definer_function::type assoc_type;
   typedef typename assoc_type::iterator iterator;

   {  //Ordered insertion + erasure
      assoc_type testset (values.begin(), values.begin() + values.size());
      TEST_INTRUSIVE_SEQUENCE_EXPECTED(testset, testset.begin());
      testset.check();
      iterator it(testset.begin()), itend(testset.end());
      for(std::size_t i = 0; it != itend; ++i){
         BOOST_TEST(&*it == &values[i]);
         it = testset.erase(it);
         testset.check();
      }
      BOOST_TEST(testset.empty());
   }

   {  //Now random insertions + erasure
      assoc_type testset;
      typedef typename value_cont_type::iterator vec_iterator;
      boost::container::vector<vec_iterator> it_vector;
      //Random insertion
      for(vec_iterator it(values.begin()), itend(values.end())
         ; it != itend
         ; ++it){
         it_vector.push_back(it);
      }
      for(std::size_t i = 0; i != MaxValues; ++i){
         testset.insert(*it_vector[i]);
         testset.check();
      }
      TEST_INTRUSIVE_SEQUENCE_EXPECTED(testset, testset.begin());
      //Random erasure
      std::random_shuffle(it_vector.begin(), it_vector.end());
      for(std::size_t i = 0; i != MaxValues; ++i){
         testset.erase(testset.iterator_to(*it_vector[i]));
         testset.check();
      }
      BOOST_TEST(testset.empty());
   }
}

template<class ValueTraits, template <class = void, class = void, class = void, class = void> class ContainerDefiner>
void test_generic_assoc<ValueTraits, ContainerDefiner>::test_all(value_cont_type& values)
{
   typedef ContainerDefiner
      < value_type
      , value_traits<ValueTraits>
      , constant_time_size<value_type::constant_time_size>
      > definer_function;
   typedef typename definer_function::type assoc_type;

   test_clone(values);
   test_container_from_end(values, detail::bool_< assoc_type::has_container_from_iterator >());
   test_splay_up(values, detail::bool_< has_splay< assoc_type >::value >());
   test_splay_down(values, detail::bool_< has_splay< assoc_type >::value >());
   test_rebalance(values, detail::bool_< has_rebalance< assoc_type >::value >());
   test_insert_before(values, detail::bool_< has_insert_before< assoc_type >::value >());
   test_insert_erase_burst();
   test_container_from_iterator(values, detail::bool_< assoc_type::has_container_from_iterator >());
}

template<class ValueTraits, template <class = void, class = void, class = void, class = void> class ContainerDefiner>
void test_generic_assoc<ValueTraits, ContainerDefiner>
   ::test_clone(value_cont_type& values)
{
   typedef ContainerDefiner
      < value_type
      , value_traits<ValueTraits>
      , constant_time_size<value_type::constant_time_size>
      > definer_function;
   typedef typename definer_function::type assoc_type;
   assoc_type testset1 (values.begin(), values.begin() + values.size());
   assoc_type testset2;

   testset2.clone_from(testset1, test::new_cloner<value_type>(), test::delete_disposer<value_type>());
   BOOST_TEST (testset2 == testset1);
   testset2.clear_and_dispose(test::delete_disposer<value_type>());
   BOOST_TEST (testset2.empty());
}

template<class ValueTraits, template <class = void, class = void, class = void, class = void> class ContainerDefiner>
void test_generic_assoc<ValueTraits, ContainerDefiner>
   ::test_container_from_end(value_cont_type& values, detail::true_type)
{
   typedef typename ValueTraits::value_type value_type;
   typedef ContainerDefiner
      < value_type
      , value_traits<ValueTraits>
      , constant_time_size<value_type::constant_time_size>
      > definer_function;
   typedef typename definer_function::type assoc_type;
   assoc_type testset (values.begin(), values.begin() + values.size());
   BOOST_TEST (testset == assoc_type::container_from_end_iterator(testset.end()));
   BOOST_TEST (testset == assoc_type::container_from_end_iterator(testset.cend()));
}

template<class ValueTraits, template <class = void, class = void, class = void, class = void> class ContainerDefiner>
void test_generic_assoc<ValueTraits, ContainerDefiner>::test_splay_up
(value_cont_type& values, detail::true_type)
{
   typedef typename ValueTraits::value_type value_type;
   typedef ContainerDefiner
      < value_type
      , value_traits<ValueTraits>
      , constant_time_size<value_type::constant_time_size>
      > definer_function;
   typedef typename definer_function::type assoc_type;

   typedef typename assoc_type::iterator iterator;
   typedef value_cont_type orig_set_t;
   std::size_t num_values;
   orig_set_t original_testset;
   {
      assoc_type testset (values.begin(), values.end());
      num_values = testset.size();
      original_testset = value_cont_type(testset.begin(), testset.end());
   }

   for(std::size_t i = 0; i != num_values; ++i){
      assoc_type testset (values.begin(), values.end());
      {
         iterator it = testset.begin();
         for(std::size_t j = 0; j != i; ++j, ++it){}
         testset.splay_up(it);
      }
      BOOST_TEST (testset.size() == num_values);
      iterator it = testset.begin();
      for( typename orig_set_t::const_iterator origit    = original_testset.begin()
         , origitend = original_testset.end()
         ; origit != origitend
         ; ++origit, ++it){
         BOOST_TEST(*origit == *it);
      }
   }
}

template<class ValueTraits, template <class = void, class = void, class = void, class = void> class ContainerDefiner>
void test_generic_assoc<ValueTraits, ContainerDefiner>::test_splay_down
(value_cont_type& values, detail::true_type)
{
   typedef typename ValueTraits::value_type value_type;
   typedef ContainerDefiner
      < value_type
      , value_traits<ValueTraits>
      , constant_time_size<value_type::constant_time_size>
      > definer_function;
   typedef typename definer_function::type assoc_type;

   typedef typename assoc_type::iterator iterator;
   typedef value_cont_type orig_set_t;
   std::size_t num_values;
   orig_set_t original_testset;
   {
      assoc_type testset (values.begin(), values.end());
      num_values = testset.size();
      original_testset = value_cont_type(testset.begin(), testset.end());
   }

   for(std::size_t i = 0; i != num_values; ++i){
      assoc_type testset (values.begin(), values.end());
      BOOST_TEST(testset.size() == num_values);
      {
         iterator it = testset.begin();
         for(std::size_t j = 0; j != i; ++j, ++it){}
         BOOST_TEST(*it == *testset.splay_down(*it));
      }
      BOOST_TEST (testset.size() == num_values);
      iterator it = testset.begin();
      for( typename orig_set_t::const_iterator origit    = original_testset.begin()
         , origitend = original_testset.end()
         ; origit != origitend
         ; ++origit, ++it){
         BOOST_TEST(*origit == *it);
      }
   }
}

template<class ValueTraits, template <class = void, class = void, class = void, class = void> class ContainerDefiner>
void test_generic_assoc<ValueTraits, ContainerDefiner>::test_rebalance
(value_cont_type& values, detail::true_type)
{
   typedef typename ValueTraits::value_type value_type;
   typedef ContainerDefiner
      < value_type
      , value_traits<ValueTraits>
      , constant_time_size<value_type::constant_time_size>
      > definer_function;
   typedef typename definer_function::type assoc_type;
   typedef value_cont_type orig_set_t;
   orig_set_t original_testset;
   {
      assoc_type testset (values.begin(), values.end());
      //original_testset.insert(original_testset.end(), testset.begin(), testset.end());
      original_testset = value_cont_type(testset.begin(), testset.end());
   }
   {
      assoc_type testset(values.begin(), values.end());
      testset.rebalance();
      TEST_INTRUSIVE_SEQUENCE_EXPECTED(original_testset, testset.begin());
   }

   {
      std::size_t numdata;
      {
         assoc_type testset(values.begin(), values.end());
         numdata = testset.size();
      }

      for(int i = 0; i != (int)numdata; ++i){
         assoc_type testset(values.begin(), values.end());
         typename assoc_type::iterator it = testset.begin();
         for(int j = 0; j  != i; ++j)  ++it;
         testset.rebalance_subtree(it);
         TEST_INTRUSIVE_SEQUENCE_EXPECTED(original_testset, testset.begin());
      }
   }
}

template<class ValueTraits, template <class = void, class = void, class = void, class = void> class ContainerDefiner>
void test_generic_assoc<ValueTraits, ContainerDefiner>::test_insert_before
(value_cont_type& values, detail::true_type)
{
   typedef typename ValueTraits::value_type value_type;
   typedef ContainerDefiner
      < value_type
      , value_traits<ValueTraits>
      , constant_time_size<value_type::constant_time_size>
      > definer_function;
   typedef typename definer_function::type assoc_type;
   {
      assoc_type testset;
      typedef typename value_cont_type::iterator vec_iterator;
      for(vec_iterator it(values.begin()), itend(values.end())
         ; it != itend
         ; ++it){
         testset.push_back(*it);
      }
      BOOST_TEST(testset.size() == values.size());
      TEST_INTRUSIVE_SEQUENCE_EXPECTED(values, testset.begin());
   }
   {
      assoc_type testset;
      typedef typename value_cont_type::iterator vec_iterator;

      for(vec_iterator it(--values.end()); true; --it){
         testset.push_front(*it);
       if(it == values.begin()){
            break;
       }
      }
      BOOST_TEST(testset.size() == values.size());
      TEST_INTRUSIVE_SEQUENCE_EXPECTED(values, testset.begin());
   }
   {
      assoc_type testset;
      typedef typename value_cont_type::iterator vec_iterator;
      typename assoc_type::iterator it_pos =
         testset.insert_before(testset.end(), *values.rbegin());
      testset.insert_before(testset.begin(), *values.begin());
      for(vec_iterator it(++values.begin()), itend(--values.end())
         ; it != itend
         ; ++it){
         testset.insert_before(it_pos, *it);
      }
      BOOST_TEST(testset.size() == values.size());
      TEST_INTRUSIVE_SEQUENCE_EXPECTED(values, testset.begin());
   }
}

}}}   //namespace boost::intrusive::test

#include <boost/intrusive/detail/config_end.hpp>