• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Olaf Krzikalla 2004-2006.
4 // (C) Copyright Ion Gaztanaga  2006-2013.
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 //    (See accompanying file LICENSE_1_0.txt or copy at
8 //          http://www.boost.org/LICENSE_1_0.txt)
9 //
10 // See http://www.boost.org/libs/intrusive for documentation.
11 //
12 /////////////////////////////////////////////////////////////////////////////
13 #include <boost/container/vector.hpp>
14 #include <boost/intrusive/detail/config_begin.hpp>
15 #include "common_functors.hpp"
16 #include <boost/detail/lightweight_test.hpp>
17 #include <boost/intrusive/options.hpp>
18 #include <boost/intrusive/detail/iterator.hpp>
19 #include <boost/intrusive/detail/mpl.hpp>
20 #include "test_macros.hpp"
21 #include "test_container.hpp"
22 #include "generic_assoc_test.hpp"
23 #include <typeinfo>
24 #include <boost/intrusive/priority_compare.hpp>
25 
26 namespace boost{
27 namespace intrusive{
28 namespace test{
29 
30 template<class ContainerDefiner>
31 struct test_generic_set
32 {
33    static void test_all();
34    private:
35    typedef typename ContainerDefiner::value_cont_type    value_cont_type;
36    static void test_sort(value_cont_type&);
37    static void test_insert(value_cont_type&);
38    static void test_insert_advanced(value_cont_type&, detail::true_type);
39    static void test_insert_advanced(value_cont_type&, detail::false_type);
40    static void test_swap(value_cont_type&);
41    static void test_merge(value_cont_type&);
42    static void test_find(value_cont_type&);
43    static void test_impl();
44 };
45 
46 
47 template<class ContainerDefiner>
test_all()48 void test_generic_set<ContainerDefiner>::test_all()
49 {
50    typedef typename ContainerDefiner::template container
51       <>::type set_type;
52    {
53       static const int random_init[6] = { 3, 2, 4, 1, 5, 2 };
54       value_cont_type values(6);
55       for (int i = 0; i < 6; ++i)
56          (&values[i])->value_ = random_init[i];
57 
58       {
59          set_type testset(values.begin(), values.end());
60          test::test_container(testset);
61          testset.clear();
62          testset.insert(values.begin(), values.end());
63          test::test_common_unordered_and_associative_container(testset, values);
64          testset.clear();
65          testset.insert(values.begin(), values.end());
66          test::test_associative_container(testset, values);
67          testset.clear();
68          testset.insert(values.begin(), values.end());
69          test::test_unique_container(testset, values);
70       }
71 
72       test_sort(values);
73       test_insert(values);
74       test_insert_advanced(values, detail::bool_< is_treap< set_type >::value >());
75       test_swap(values);
76       test_merge(values);
77       test_find(values);
78       test_impl();
79       test_generic_assoc<ContainerDefiner>::test_all(values);
80    }
81    {
82       value_cont_type values(6);
83       for (int i = 0; i < 6; ++i)
84          (&values[i])->value_ = i+1;
85       set_type testset(values.begin(), values.end());
86       test::test_iterator_bidirectional(testset);
87    }
88 }
89 
90 //test case due to an error in tree implementation:
91 template<class ContainerDefiner>
test_impl()92 void test_generic_set<ContainerDefiner>::test_impl()
93 {
94    value_cont_type values (5);
95    for (int i = 0; i < 5; ++i)
96       (&values[i])->value_ = i;
97 
98    typedef typename ContainerDefiner::template container
99       <>::type set_type;
100    set_type testset;
101    for (int i = 0; i < 5; ++i)
102       testset.insert (values[i]);
103 
104    testset.erase (testset.iterator_to (values[0]));
105    testset.erase (testset.iterator_to (values[1]));
106    testset.insert (values[1]);
107 
108    testset.erase (testset.iterator_to (values[2]));
109    testset.erase (testset.iterator_to (values[3]));
110 }
111 
112 //test: constructor, iterator, clear, reverse_iterator, front, back, size:
113 template<class ContainerDefiner>
test_sort(value_cont_type & values)114 void test_generic_set<ContainerDefiner>::test_sort(value_cont_type& values)
115 {
116    typedef typename ContainerDefiner::template container
117       <>::type set_type;
118 
119    set_type testset1 (values.begin(), values.end());
120    {  int init_values [] = { 1, 2, 3, 4, 5 };
121       TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }
122 
123    testset1.clear();
124    BOOST_TEST (testset1.empty());
125 
126    typedef typename ContainerDefiner::template container
127       < compare<even_odd> >::type set_type2;
128 
129    set_type2 testset2 (values.begin(), values.begin() + 6);
130    {  int init_values [] = { 5, 3, 1, 4, 2 };
131       TEST_INTRUSIVE_SEQUENCE( init_values, testset2.rbegin() );  }
132    BOOST_TEST (testset2.begin()->value_ == 2);
133    BOOST_TEST (testset2.rbegin()->value_ == 5);
134 }
135 
136 //test: insert, const_iterator, const_reverse_iterator, erase, s_iterator_to:
137 template<class ContainerDefiner>
test_insert(value_cont_type & values)138 void test_generic_set<ContainerDefiner>::test_insert(value_cont_type& values)
139 {
140    typedef typename ContainerDefiner::template container
141       <>::type set_type;
142    {
143       set_type testset;
144       testset.insert(values.begin() + 2, values.begin() + 5);
145       testset.check();
146 
147       const set_type& const_testset = testset;
148       {  int init_values [] = { 1, 4, 5 };
149          TEST_INTRUSIVE_SEQUENCE( init_values, const_testset.begin() );  }
150 
151       typename set_type::iterator i = testset.begin();
152       BOOST_TEST (i->value_ == 1);
153 
154       i = testset.insert (i, values[0]);
155       testset.check();
156       BOOST_TEST (&*i == &values[0]);
157 
158       {  int init_values [] = { 5, 4, 3, 1 };
159          TEST_INTRUSIVE_SEQUENCE( init_values, testset.rbegin() );  }
160 
161       i = testset.iterator_to (values[2]);
162       BOOST_TEST (&*i == &values[2]);
163 
164       i = set_type::s_iterator_to(values[2]);
165       BOOST_TEST (&*i == &values[2]);
166 
167       typedef typename value_cont_type::const_reference const_reference;
168       typename set_type::const_iterator ic;
169       ic = testset.iterator_to (static_cast< const_reference >(values[2]));
170       BOOST_TEST (&*ic == &values[2]);
171       ic = set_type::s_iterator_to (static_cast< const_reference >(values[2]));
172       BOOST_TEST (&*ic == &values[2]);
173 
174       testset.erase (i);
175       testset.check();
176       {  int init_values [] = { 1, 3, 5 };
177          TEST_INTRUSIVE_SEQUENCE( init_values, testset.begin() );  }
178    }
179 }
180 
181 // treap version
182 template<class ValueType, class KeyType>
183 struct prio_comp
184    : priority_compare<int>
185 {
operator ()boost::intrusive::test::prio_comp186    bool operator()(const ValueType &v, const KeyType &k) const
187    {  return this->priority_compare<int>::operator()(v.int_value(), k.int_value());  }
188 
operator ()boost::intrusive::test::prio_comp189    bool operator()(const KeyType &k, const ValueType &v) const
190    {  return this->priority_compare<int>::operator()(k.int_value(), v.int_value());  }
191 };
192 
193 template<class ContainerDefiner>
test_insert_advanced(value_cont_type & values,detail::true_type)194 void test_generic_set<ContainerDefiner>::test_insert_advanced
195 (value_cont_type& values, detail::true_type)
196 {
197    typedef typename ContainerDefiner::template container
198       <>::type set_type;
199    typedef typename set_type::key_of_value key_of_value;
200    typedef typename set_type::priority_of_value priority_of_value;
201    typedef typename set_type::value_type  value_type;
202    typedef priority_compare<> prio_comp_t;
203    {
204       set_type testset;
205       testset.insert(values.begin(), values.begin() + values.size());
206       testset.check();
207       value_type v(1);
208       typename set_type::insert_commit_data data;
209       BOOST_TEST ((!testset.insert_check(1, any_less(), 1, prio_comp_t(), data).second));
210       BOOST_TEST ((!testset.insert_check(testset.begin(), 1, any_less(), 1, prio_comp_t(), data).second));
211       BOOST_TEST ((!testset.insert_check(key_of_value()(v), priority_of_value()(v), data).second));
212       BOOST_TEST ((!testset.insert_check(testset.begin(), key_of_value()(v), priority_of_value()(v), data).second));
213    }
214 }
215 
216 //test: insert, const_iterator, const_reverse_iterator, erase, s_iterator_to:
217 template<class ContainerDefiner>
test_insert_advanced(value_cont_type & values,detail::false_type)218 void test_generic_set<ContainerDefiner>::test_insert_advanced
219 (value_cont_type& values, detail::false_type)
220 {
221    typedef typename ContainerDefiner::template container
222       <>::type set_type;
223    typedef typename set_type::key_of_value   key_of_value;
224    typedef typename set_type::value_type     value_type;
225    {
226       set_type testset;
227       testset.insert(values.begin(), values.begin() + values.size());
228       testset.check();
229       value_type v(1);
230       typename set_type::insert_commit_data data;
231       BOOST_TEST ((!testset.insert_check(1, any_less(), data).second));
232       BOOST_TEST ((!testset.insert_check(key_of_value()(v), data).second));
233       BOOST_TEST ((!testset.insert_check(testset.begin(), 1, any_less(), data).second));
234       BOOST_TEST ((!testset.insert_check(testset.begin(), key_of_value()(v), data).second));
235    }
236 }
237 
238 //test: insert (seq-version), swap, erase (seq-version), size:
239 template<class ContainerDefiner>
test_swap(value_cont_type & values)240 void test_generic_set<ContainerDefiner>::test_swap(value_cont_type& values)
241 {
242    typedef typename ContainerDefiner::template container
243       <>::type set_type;
244    set_type testset1 (values.begin(), values.begin() + 2);
245    set_type testset2;
246    testset2.insert (values.begin() + 2, values.begin() + 6);
247    testset1.swap (testset2);
248 
249    {  int init_values [] = { 1, 2, 4, 5 };
250       TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }
251 
252    {  int init_values [] = { 2, 3 };
253       TEST_INTRUSIVE_SEQUENCE( init_values, testset2.begin() );  }
254 
255    testset1.erase (testset1.iterator_to(values[5]), testset1.end());
256    BOOST_TEST (testset1.size() == 1);
257    //  BOOST_TEST (&testset1.front() == &values[3]);
258    BOOST_TEST (&*testset1.begin() == &values[3]);
259 }
260 
261 template<class ContainerDefiner>
test_merge(value_cont_type & values)262 void test_generic_set<ContainerDefiner>::test_merge(value_cont_type& values)
263 {
264    typedef typename ContainerDefiner::template container
265       <>::type set_type;
266    typedef typename set_type::key_type key_type;
267 
268    typedef typename ContainerDefiner::template container
269       < compare< std::greater<key_type> > >::type set_greater_type;
270 
271    //2,3
272    set_type testset1 (values.begin(), values.begin() + 2);
273    //5, 4, 2, 1
274    set_greater_type testset2;
275    testset2.insert (values.begin() + 2, values.begin() + 6);
276 
277    testset2.merge(testset1);
278    testset1.check();
279    testset2.check();
280 
281    BOOST_TEST (testset1.size() == 1);
282    {  int init_values [] = { 2 };
283       TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }
284    BOOST_TEST (&*testset1.begin() == &values[1]);
285 
286    BOOST_TEST (testset2.size() == 5);
287    {  int init_values [] = { 5, 4, 3, 2, 1 };
288       TEST_INTRUSIVE_SEQUENCE( init_values, testset2.begin() );  }
289 
290    testset1.merge(testset2);
291    testset1.check();
292    testset2.check();
293 
294    BOOST_TEST (testset1.size() == 5);
295    {  int init_values [] = { 1, 2, 3, 4, 5 };
296       TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }
297 
298    BOOST_TEST (testset2.size() == 1);
299    {  int init_values [] = { 2 };
300       TEST_INTRUSIVE_SEQUENCE( init_values, testset2.begin() );  }
301    BOOST_TEST (&*testset2.begin() == &values[5]);
302 }
303 
304 //test: find, equal_range (lower_bound, upper_bound), bounded_range:
305 template<class ContainerDefiner>
test_find(value_cont_type & values)306 void test_generic_set<ContainerDefiner>::test_find(value_cont_type& values)
307 {
308    typedef typename ContainerDefiner::template container
309       <>::type set_type;
310    set_type testset (values.begin(), values.end());
311    typedef typename set_type::iterator       iterator;
312    typedef typename set_type::const_iterator const_iterator;
313    typedef typename set_type::key_of_value   key_of_value;
314    typedef typename value_cont_type::reference reference;
315 
316    {
317       //value_type cmp_val;
318       value_cont_type cmp_val_cont(1);
319       reference cmp_val = cmp_val_cont.front();
320       (&cmp_val)->value_ = 2;
321       iterator i = testset.find(key_of_value()(cmp_val));
322       BOOST_TEST (i == testset.find(2, any_less()));
323       BOOST_TEST (i->value_ == 2);
324       BOOST_TEST ((++i)->value_ != 2);
325 
326       std::pair<iterator,iterator> range = testset.equal_range (key_of_value()(cmp_val));
327       BOOST_TEST(range == testset.equal_range (2, any_less()));
328 
329       BOOST_TEST (range.first->value_ == 2);
330       BOOST_TEST (range.second->value_ == 3);
331       BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 1);
332 
333       (&cmp_val)->value_ = 7;
334       BOOST_TEST (testset.find (key_of_value()(cmp_val)) == testset.end());
335       BOOST_TEST (testset.find (7, any_less()) == testset.end());
336    }
337 
338    {
339       const set_type &const_testset = testset;
340       std::pair<iterator,iterator> range;
341       std::pair<const_iterator, const_iterator> const_range;
342       //value_type cmp_val_lower, cmp_val_upper;
343       value_cont_type cmp_val_cont(2);
344       reference cmp_val_lower = cmp_val_cont.front();
345       reference cmp_val_upper = cmp_val_cont.back();
346       {
347       (&cmp_val_lower)->value_ = 1;
348       (&cmp_val_upper)->value_ = 2;
349       //left-closed, right-closed
350       range = testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, true);
351       BOOST_TEST (range == testset.bounded_range (1, 2, any_less(), true, true));
352       BOOST_TEST (range.first->value_ == 1);
353       BOOST_TEST (range.second->value_ == 3);
354       BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 2);
355       }
356       {
357       (&cmp_val_lower)->value_ = 1;
358       (&cmp_val_upper)->value_ = 2;
359       const_range = const_testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, false);
360       BOOST_TEST (const_range == const_testset.bounded_range (1, 2, any_less(), true, false));
361       BOOST_TEST (const_range.first->value_ == 1);
362       BOOST_TEST (const_range.second->value_ == 2);
363       BOOST_TEST (boost::intrusive::iterator_distance (const_range.first, const_range.second) == 1);
364 
365       (&cmp_val_lower)->value_ = 1;
366       (&cmp_val_upper)->value_ = 3;
367       range = testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, false);
368       BOOST_TEST (range == testset.bounded_range (1, 3, any_less(), true, false));
369       BOOST_TEST (range.first->value_ == 1);
370       BOOST_TEST (range.second->value_ == 3);
371       BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 2);
372       }
373       {
374       (&cmp_val_lower)->value_ = 1;
375       (&cmp_val_upper)->value_ = 2;
376       const_range = const_testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), false, true);
377       BOOST_TEST (const_range == const_testset.bounded_range (1, 2, any_less(), false, true));
378       BOOST_TEST (const_range.first->value_ == 2);
379       BOOST_TEST (const_range.second->value_ == 3);
380       BOOST_TEST (boost::intrusive::iterator_distance (const_range.first, const_range.second) == 1);
381       }
382       {
383       (&cmp_val_lower)->value_ = 1;
384       (&cmp_val_upper)->value_ = 2;
385       range = testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), false, false);
386       BOOST_TEST (range == testset.bounded_range (1, 2, any_less(), false, false));
387       BOOST_TEST (range.first->value_ == 2);
388       BOOST_TEST (range.second->value_ == 2);
389       BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 0);
390       }
391       {
392       (&cmp_val_lower)->value_ = 5;
393       (&cmp_val_upper)->value_ = 6;
394       const_range = const_testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, false);
395       BOOST_TEST (const_range == const_testset.bounded_range (5, 6, any_less(), true, false));
396       BOOST_TEST (const_range.first->value_ == 5);
397       BOOST_TEST (const_range.second == const_testset.end());
398       BOOST_TEST (boost::intrusive::iterator_distance (const_range.first, const_range.second) == 1);
399       }
400    }
401 }
402 
403 }}}   //namespace boost::intrusive::test
404 
405 #include <boost/intrusive/detail/config_end.hpp>
406