///////////////////////////////////////////////////////////////////////////// // // (C) Copyright Olaf Krzikalla 2004-2006. // (C) Copyright Ion Gaztanaga 2006-2007. // // 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 #include #include #include "itestvalue.hpp" #include "smart_ptr.hpp" #include "common_functors.hpp" #include #include #include "test_macros.hpp" #include "test_container.hpp" using namespace boost::intrusive; template struct test_multiset { typedef typename ValueTraits::value_type value_type; static void test_all (std::vector& values); static void test_sort(std::vector& values); static void test_insert(std::vector& values); static void test_swap(std::vector& values); static void test_find(std::vector& values); static void test_impl(); static void test_clone(std::vector& values); }; template void test_multiset::test_all (std::vector& values) { typedef multiset ,ValueTraits::value_type::constant_time_size, std::size_t > 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_find(values); test_impl(); test_clone(values); } //test case due to an error in tree implementation: template void test_multiset::test_impl() { typedef typename ValueTraits::value_type testvalue_t; std::vector values (5); for (int i = 0; i < 5; ++i) values[i].value_ = i; typedef multiset ,ValueTraits::value_type::constant_time_size, std::size_t > 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 void test_multiset::test_sort(std::vector& values) { typedef typename ValueTraits::value_type testvalue_t; typedef multiset ,ValueTraits::value_type::constant_time_size, std::size_t > 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 multiset multiset_type2; multiset_type2 testset2 (&values[0], &values[0] + 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 void test_multiset::test_insert(std::vector& values) { typedef typename ValueTraits::value_type testvalue_t; typedef multiset ,ValueTraits::value_type::constant_time_size, std::size_t > multiset_type; multiset_type testset; testset.insert(&values[0] + 2, &values[0] + 5); { 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]); 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]); testset.erase(i); { int init_values [] = { 1, 3, 5 }; TEST_INTRUSIVE_SEQUENCE( init_values, testset.begin() ); } } //test: insert (seq-version), swap, erase (seq-version), size: template void test_multiset::test_swap(std::vector& values) { typedef typename ValueTraits::value_type testvalue_t; typedef multiset ,ValueTraits::value_type::constant_time_size, std::size_t > multiset_type; multiset_type testset1 (&values[0], &values[0] + 2); multiset_type testset2; testset2.insert (&values[0] + 2, &values[0] + 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]); } //test: find, equal_range (lower_bound, upper_bound): template void test_multiset::test_find(std::vector& values) { typedef typename ValueTraits::value_type testvalue_t; typedef multiset ,ValueTraits::value_type::constant_time_size, std::size_t > multiset_type; multiset_type testset (values.begin(), values.end()); typedef typename multiset_type::iterator iterator; testvalue_t cmp_val; cmp_val.value_ = 2; iterator i = testset.find (cmp_val); BOOST_TEST (i->value_ == 2); BOOST_TEST ((++i)->value_ == 2); std::pair range = testset.equal_range (cmp_val); BOOST_TEST (range.first->value_ == 2); BOOST_TEST (range.second->value_ == 3); BOOST_TEST (std::distance (range.first, range.second) == 2); cmp_val.value_ = 7; BOOST_TEST (testset.find (cmp_val) == testset.end()); } template void test_multiset ::test_clone(std::vector& values) { typedef typename ValueTraits::value_type testvalue_t; typedef multiset ,ValueTraits::value_type::constant_time_size, std::size_t > multiset_type; multiset_type testmultiset1 (&values[0], &values[0] + values.size()); multiset_type testmultiset2; testmultiset2.clone_from(testmultiset1, test::new_cloner(), test::delete_disposer()); BOOST_TEST (testmultiset2 == testmultiset1); testmultiset2.clear_and_dispose(test::delete_disposer()); BOOST_TEST (testmultiset2.empty()); } template class test_main_template { public: int operator()() { typedef testvalue testvalue_t; static const int random_init[6] = { 3, 2, 4, 1, 5, 2 }; std::vector > data (6); for (int i = 0; i < 6; ++i) data[i].value_ = random_init[i]; test_multiset >::test_all(data); test_multiset >::test_all(data); return 0; } }; template class test_main_template { public: int operator()() { typedef testvalue testvalue_t; static const int random_init[6] = { 3, 2, 4, 1, 5, 2 }; std::vector > data (6); for (int i = 0; i < 6; ++i) data[i].value_ = random_init[i]; test_multiset >::test_all(data); test_multiset >::test_all(data); test_multiset >::test_all(data); test_multiset >::test_all(data); return 0; } }; /* //Explicit instantiations of non-counted classes template class multiset , false>; template class multiset , false>; template class multiset , false>; template class multiset , false>; template class multiset , false>; template class multiset , false>; template class multiset , false>; template class multiset , false>; //Explicit instantiation of counted classes template class multiset , true>; template class multiset , true>; template class multiset , true>; template class multiset , true>; */ int main( int, char* [] ) { test_main_template()(); test_main_template, false>()(); test_main_template()(); test_main_template, true>()(); return boost::report_errors(); } #include