123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- /////////////////////////////////////////////////////////////////////////////
- //
- // (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>
- #include <boost/intrusive/detail/config_begin.hpp>
- #include "common_functors.hpp"
- #include <boost/detail/lightweight_test.hpp>
- #include <boost/intrusive/options.hpp>
- #include <boost/intrusive/detail/iterator.hpp>
- #include "test_macros.hpp"
- #include "test_container.hpp"
- #include "generic_assoc_test.hpp"
- #include <typeinfo>
- namespace boost{
- namespace intrusive{
- namespace test{
- template<class ContainerDefiner>
- struct test_generic_multiset
- {
- typedef typename ContainerDefiner::value_cont_type value_cont_type;
- static void test_all();
- static void test_sort(value_cont_type&);
- static void test_insert(value_cont_type&);
- static void test_swap(value_cont_type&);
- static void test_merge(value_cont_type&);
- static void test_find(value_cont_type&);
- static void test_impl();
- };
- template<class ContainerDefiner>
- void test_generic_multiset<ContainerDefiner>::test_all ()
- {
- static const int random_init[6] = { 3, 2, 4, 1, 5, 2 };
- value_cont_type values (6);
- for (int i = 0; i < 6; ++i)
- (&values[i])->value_ = random_init[i];
- typedef typename ContainerDefiner::template container
- <>::type multiset_type;
- {
- multiset_type testset(values.begin(), values.end());
- test::test_container(testset);
- testset.clear();
- testset.insert(values.begin(), values.end());
- test::test_common_unordered_and_associative_container(testset, values);
- testset.clear();
- testset.insert(values.begin(), values.end());
- test::test_associative_container(testset, values);
- testset.clear();
- testset.insert(values.begin(), values.end());
- test::test_non_unique_container(testset, values);
- }
- test_sort(values);
- test_insert(values);
- test_swap(values);
- test_merge(values);
- test_find(values);
- test_impl();
- test_generic_assoc<ContainerDefiner>::test_all(values);
- }
- //test case due to an error in tree implementation:
- template<class ContainerDefiner>
- void test_generic_multiset<ContainerDefiner>::test_impl()
- {
- value_cont_type values (5);
- for (int i = 0; i < 5; ++i)
- (&values[i])->value_ = i;
- typedef typename ContainerDefiner::template container
- <>::type multiset_type;
- multiset_type testset;
- for (int i = 0; i < 5; ++i)
- testset.insert (values[i]);
- testset.erase (testset.iterator_to (values[0]));
- testset.erase (testset.iterator_to (values[1]));
- testset.insert (values[1]);
- testset.erase (testset.iterator_to (values[2]));
- testset.erase (testset.iterator_to (values[3]));
- }
- //test: constructor, iterator, clear, reverse_iterator, front, back, size:
- template<class ContainerDefiner>
- void test_generic_multiset<ContainerDefiner>::test_sort(value_cont_type& values)
- {
- typedef typename ContainerDefiner::template container
- <>::type multiset_type;
- multiset_type testset1 (values.begin(), values.end());
- { int init_values [] = { 1, 2, 2, 3, 4, 5 };
- TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); }
- testset1.clear();
- BOOST_TEST (testset1.empty());
- typedef typename ContainerDefiner::template container
- <compare<even_odd>
- >::type multiset_type2;
- multiset_type2 testset2 (values.begin(), values.begin() + 6);
- { int init_values [] = { 5, 3, 1, 4, 2, 2 };
- TEST_INTRUSIVE_SEQUENCE( init_values, testset2.rbegin() ); }
- BOOST_TEST (testset2.begin()->value_ == 2);
- BOOST_TEST (testset2.rbegin()->value_ == 5);
- }
- //test: insert, const_iterator, const_reverse_iterator, erase, iterator_to:
- template<class ContainerDefiner>
- void test_generic_multiset<ContainerDefiner>::test_insert(value_cont_type& values)
- {
- typedef typename ContainerDefiner::template container
- <>::type multiset_type;
- multiset_type testset;
- testset.insert(values.begin() + 2, values.begin() + 5);
- testset.check();
- { int init_values [] = { 1, 4, 5 };
- TEST_INTRUSIVE_SEQUENCE( init_values, testset.begin() ); }
- typename multiset_type::iterator i = testset.begin();
- BOOST_TEST (i->value_ == 1);
- i = testset.insert (i, values[0]);
- testset.check();
- BOOST_TEST (&*i == &values[0]);
- { int init_values [] = { 5, 4, 3, 1 };
- TEST_INTRUSIVE_SEQUENCE( init_values, testset.rbegin() ); }
- i = testset.iterator_to (values[2]);
- BOOST_TEST (&*i == &values[2]);
- i = multiset_type::s_iterator_to (values[2]);
- BOOST_TEST (&*i == &values[2]);
- testset.erase(i);
- testset.check();
- { int init_values [] = { 1, 3, 5 };
- TEST_INTRUSIVE_SEQUENCE( init_values, testset.begin() ); }
- }
- //test: insert (seq-version), swap, erase (seq-version), size:
- template<class ContainerDefiner>
- void test_generic_multiset<ContainerDefiner>::test_swap(value_cont_type& values)
- {
- typedef typename ContainerDefiner::template container
- <>::type multiset_type;
- multiset_type testset1 (values.begin(), values.begin() + 2);
- multiset_type testset2;
- testset2.insert (values.begin() + 2, values.begin() + 6);
- testset1.swap (testset2);
- { int init_values [] = { 1, 2, 4, 5 };
- TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); }
- { int init_values [] = { 2, 3 };
- TEST_INTRUSIVE_SEQUENCE( init_values, testset2.begin() ); }
- testset1.erase (testset1.iterator_to(values[5]), testset1.end());
- BOOST_TEST (testset1.size() == 1);
- BOOST_TEST (&*testset1.begin() == &values[3]);
- }
- template<class ContainerDefiner>
- void test_generic_multiset<ContainerDefiner>::test_merge(value_cont_type& values)
- {
- typedef typename ContainerDefiner::template container
- <>::type multiset_type;
- typedef typename multiset_type::key_of_value key_of_value;
- typedef typename multiset_type::key_type key_type;
- typedef typename ContainerDefiner::template container
- < compare< std::greater<key_type> > >::type multiset_greater_type;
- //original vector: 3, 2, 4, 1, 5, 2
- //2,3
- multiset_type testset1 (values.begin(), values.begin() + 2);
- //5, 4, 2, 1
- multiset_greater_type testset2;
- testset2.insert (values.begin() + 2, values.begin() + 6);
- testset2.merge(testset1);
- testset1.check();
- testset2.check();
- BOOST_TEST (testset1.empty());
- BOOST_TEST (testset2.size() == 6);
- { int init_values [] = { 5, 4, 3, 2, 2, 1 };
- TEST_INTRUSIVE_SEQUENCE( init_values, testset2.begin() ); }
- value_cont_type cmp_val_cont(1);
- typename value_cont_type::reference cmp_val = cmp_val_cont.front();
- (&cmp_val)->value_ = 2;
- BOOST_TEST (*testset2.find(key_of_value()(cmp_val)) == values[5]);
- BOOST_TEST (*testset2.find(2, any_greater()) == values[5]);
- BOOST_TEST (&*(++testset2.find(key_of_value()(cmp_val))) == &values[1]);
- testset1.merge(testset2);
- testset1.check();
- testset2.check();
- BOOST_TEST (testset1.size() == 6);
- { int init_values [] = { 1, 2, 2, 3, 4, 5 };
- TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); }
- BOOST_TEST (*testset1.find(key_of_value()(cmp_val)) == values[5]);
- BOOST_TEST (*testset1.find(2, any_less()) == values[5]);
- BOOST_TEST (&*(++testset1.find(key_of_value()(cmp_val))) == &values[1]);
- BOOST_TEST (testset2.empty());
- }
- //test: find, equal_range (lower_bound, upper_bound):
- template<class ContainerDefiner>
- void test_generic_multiset<ContainerDefiner>::test_find(value_cont_type& values)
- {
- typedef typename ContainerDefiner::template container
- <>::type multiset_type;
- typedef typename multiset_type::key_of_value key_of_value;
- multiset_type testset (values.begin(), values.end());
- typedef typename multiset_type::iterator iterator;
- typedef typename multiset_type::const_iterator const_iterator;
- {
- value_cont_type cmp_val_cont(1);
- typename value_cont_type::reference cmp_val = cmp_val_cont.front();
- (&cmp_val)->value_ = 2;
- iterator i = testset.find (key_of_value()(cmp_val));
- BOOST_TEST (i == testset.find (2, any_less()));
- BOOST_TEST (i->value_ == 2);
- BOOST_TEST ((++i)->value_ == 2);
- std::pair<iterator,iterator> range = testset.equal_range (key_of_value()(cmp_val));
- BOOST_TEST(range == testset.equal_range (2, any_less()));
- BOOST_TEST (range.first->value_ == 2);
- BOOST_TEST (range.second->value_ == 3);
- BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 2);
- (&cmp_val)->value_ = 7;
- BOOST_TEST (testset.find(key_of_value()(cmp_val)) == testset.end());
- BOOST_TEST (testset.find (7, any_less()) == testset.end());
- }
- { //1, 2, 2, 3, 4, 5
- const multiset_type &const_testset = testset;
- std::pair<iterator,iterator> range;
- std::pair<const_iterator, const_iterator> const_range;
- value_cont_type cmp_val_cont(2);
- typename value_cont_type::reference cmp_val_lower = cmp_val_cont.front();
- typename value_cont_type::reference cmp_val_upper = cmp_val_cont.back();
- {
- (&cmp_val_lower)->value_ = 1;
- (&cmp_val_upper)->value_ = 2;
- //left-closed, right-closed
- range = testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, true);
- BOOST_TEST (range == testset.bounded_range (1, 2, any_less(), true, true));
- BOOST_TEST (range.first->value_ == 1);
- BOOST_TEST (range.second->value_ == 3);
- BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 3);
- }
- {
- (&cmp_val_lower)->value_ = 1;
- (&cmp_val_upper)->value_ = 2;
- const_range = const_testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, false);
- BOOST_TEST (const_range == const_testset.bounded_range (1, 2, any_less(), true, false));
- BOOST_TEST (const_range.first->value_ == 1);
- BOOST_TEST (const_range.second->value_ == 2);
- BOOST_TEST (boost::intrusive::iterator_distance (const_range.first, const_range.second) == 1);
- (&cmp_val_lower)->value_ = 1;
- (&cmp_val_upper)->value_ = 3;
- range = testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, false);
- BOOST_TEST (range == testset.bounded_range (1, 3, any_less(), true, false));
- BOOST_TEST (range.first->value_ == 1);
- BOOST_TEST (range.second->value_ == 3);
- BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 3);
- }
- {
- (&cmp_val_lower)->value_ = 1;
- (&cmp_val_upper)->value_ = 2;
- const_range = const_testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), false, true);
- BOOST_TEST (const_range == const_testset.bounded_range (1, 2, any_less(), false, true));
- BOOST_TEST (const_range.first->value_ == 2);
- BOOST_TEST (const_range.second->value_ == 3);
- BOOST_TEST (boost::intrusive::iterator_distance (const_range.first, const_range.second) == 2);
- }
- {
- (&cmp_val_lower)->value_ = 1;
- (&cmp_val_upper)->value_ = 2;
- range = testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), false, false);
- BOOST_TEST (range == testset.bounded_range (1, 2, any_less(), false, false));
- BOOST_TEST (range.first->value_ == 2);
- BOOST_TEST (range.second->value_ == 2);
- BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 0);
- }
- {
- (&cmp_val_lower)->value_ = 5;
- (&cmp_val_upper)->value_ = 6;
- const_range = const_testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, false);
- BOOST_TEST (const_range == const_testset.bounded_range (5, 6, any_less(), true, false));
- BOOST_TEST (const_range.first->value_ == 5);
- BOOST_TEST (const_range.second == const_testset.end());
- BOOST_TEST (boost::intrusive::iterator_distance (const_range.first, const_range.second) == 1);
- }
- }
- }
- }}} //namespace boost::intrusive::test
- #include <boost/intrusive/detail/config_end.hpp>
|