///////////////////////////////////////////////////////////////////////////// // // (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 #include "test_macros.hpp" using namespace boost::intrusive; static const std::size_t BucketSize = 11; template struct test_unordered_set { 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_rehash(std::vector& values); static void test_find(std::vector& values); static void test_impl(); static void test_clone(std::vector& values); }; template void test_unordered_set::test_all(std::vector& values) { test_sort(values); test_insert(values); test_swap(values); test_rehash(values); test_find(values); test_impl(); test_clone(values); } //test case due to an error in tree implementation: template void test_unordered_set::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 boost::intrusive::unordered_set ,std::equal_to ,ValueTraits::value_type::constant_time_size, std::size_t > unordered_set_type; typename unordered_set_type::bucket_type buckets [BucketSize]; unordered_set_type testset(buckets, BucketSize); 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_unordered_set::test_sort(std::vector& values) { typedef typename ValueTraits::value_type testvalue_t; typedef boost::intrusive::unordered_set ,std::equal_to ,ValueTraits::value_type::constant_time_size, std::size_t > unordered_set_type; typename unordered_set_type::bucket_type buckets [BucketSize]; unordered_set_type testset1(buckets, BucketSize, values.begin(), values.end()); BOOST_TEST (5 == std::distance(testset1.begin(), testset1.end())); { int init_values [] = { 1, 2, 3, 4, 5 }; TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } testset1.clear(); BOOST_TEST (testset1.empty()); } //test: insert, const_iterator, const_reverse_iterator, erase, iterator_to: template void test_unordered_set::test_insert(std::vector& values) { typedef typename ValueTraits::value_type testvalue_t; typedef boost::intrusive::unordered_set ,std::equal_to ,ValueTraits::value_type::constant_time_size, std::size_t > unordered_set_type; typename unordered_set_type::bucket_type buckets [BucketSize]; unordered_set_type testset(buckets, BucketSize); testset.insert(&values[0] + 2, &values[0] + 5); const unordered_set_type& const_testset = testset; { int init_values [] = { 1, 4, 5 }; TEST_INTRUSIVE_SEQUENCE( init_values, const_testset.begin() ); } typename unordered_set_type::iterator i = testset.begin(); BOOST_TEST (i->value_ == 1); i = testset.insert(values[0]).first; BOOST_TEST (&*i == &values[0]); 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, const_testset.begin() ); } } //test: insert (seq-version), swap, erase (seq-version), size: template void test_unordered_set::test_swap(std::vector& values) { typedef typename ValueTraits::value_type testvalue_t; typedef boost::intrusive::unordered_set ,std::equal_to ,ValueTraits::value_type::constant_time_size, std::size_t > unordered_set_type; typename unordered_set_type::bucket_type buckets1 [BucketSize]; unordered_set_type testset1(buckets1, BucketSize, &values[0], &values[0] + 2); typename unordered_set_type::bucket_type buckets2 [BucketSize]; unordered_set_type testset2(buckets2, BucketSize); 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.front() == &values[3]); BOOST_TEST (&*testset1.begin() == &values[3]); } //test: rehash: template void test_unordered_set::test_rehash(std::vector& values) { typedef typename ValueTraits::value_type testvalue_t; typedef boost::intrusive::unordered_set ,std::equal_to ,ValueTraits::value_type::constant_time_size, std::size_t > unordered_set_type; typename unordered_set_type::bucket_type buckets1 [BucketSize]; unordered_set_type testset1(buckets1, BucketSize, &values[0], &values[0] + 6); BOOST_TEST (testset1.size() == 5); { int init_values [] = { 1, 2, 3, 4, 5 }; TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } typename unordered_set_type::bucket_type buckets2 [2]; testset1.rehash(buckets2, 2); BOOST_TEST (testset1.size() == 5); { int init_values [] = { 4, 2, 5, 3, 1 }; TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } typename unordered_set_type::bucket_type buckets3 [BucketSize*2]; testset1.rehash(buckets3, BucketSize*2); BOOST_TEST (testset1.size() == 5); { int init_values [] = { 1, 2, 3, 4, 5 }; TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } //Now rehash reducing the buckets testset1.rehash(buckets3, 2); BOOST_TEST (testset1.size() == 5); { int init_values [] = { 4, 2, 5, 3, 1 }; TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } //Now rehash increasing the buckets testset1.rehash(buckets3, BucketSize*2); BOOST_TEST (testset1.size() == 5); { int init_values [] = { 1, 2, 3, 4, 5 }; TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } } //test: find, equal_range (lower_bound, upper_bound): template void test_unordered_set::test_find(std::vector& values) { typedef typename ValueTraits::value_type testvalue_t; typedef boost::intrusive::unordered_set ,std::equal_to ,ValueTraits::value_type::constant_time_size, std::size_t > unordered_set_type; typename unordered_set_type::bucket_type buckets [BucketSize]; unordered_set_type testset (buckets, BucketSize, values.begin(), values.end()); typedef typename unordered_set_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) == 1); cmp_val.value_ = 7; BOOST_TEST (testset.find (cmp_val) == testset.end()); } template void test_unordered_set ::test_clone(std::vector& values) { typedef boost::intrusive::unordered_set ,std::equal_to ,ValueTraits::value_type::constant_time_size, std::size_t > unordered_set_type; { //Test with equal bucket arrays typename unordered_set_type::bucket_type buckets1 [BucketSize]; typename unordered_set_type::bucket_type buckets2 [BucketSize]; unordered_set_type testset1 (buckets1, BucketSize, values.begin(), values.end()); unordered_set_type testset2 (buckets2, BucketSize); testset2.clone_from(testset1, test::new_cloner(), test::delete_disposer()); //Ordering is not guarantee in the cloning so insert data in a set and test std::set src(testset1.begin(), testset1.end()); std::set dst(testset2.begin(), testset2.end()); BOOST_TEST (src == dst ); testset2.clear_and_dispose(test::delete_disposer()); BOOST_TEST (testset2.empty()); } { //Test with bigger source bucket arrays typename unordered_set_type::bucket_type buckets1 [BucketSize*2]; typename unordered_set_type::bucket_type buckets2 [BucketSize]; unordered_set_type testset1 (buckets1, BucketSize*2, values.begin(), values.end()); unordered_set_type testset2 (buckets2, BucketSize); testset2.clone_from(testset1, test::new_cloner(), test::delete_disposer()); //Ordering is not guarantee in the cloning so insert data in a set and test std::set src(testset1.begin(), testset1.end()); std::set dst(testset2.begin(), testset2.end()); BOOST_TEST (src == dst ); testset2.clear_and_dispose(test::delete_disposer()); BOOST_TEST (testset2.empty()); } { //Test with smaller source bucket arrays typename unordered_set_type::bucket_type buckets1 [BucketSize]; typename unordered_set_type::bucket_type buckets2 [BucketSize*2]; unordered_set_type testset1 (buckets1, BucketSize, values.begin(), values.end()); unordered_set_type testset2 (buckets2, BucketSize*2); testset2.clone_from(testset1, test::new_cloner(), test::delete_disposer()); //Ordering is not guarantee in the cloning so insert data in a set and test std::set src(testset1.begin(), testset1.end()); std::set dst(testset2.begin(), testset2.end()); BOOST_TEST (src == dst ); testset2.clear_and_dispose(test::delete_disposer()); BOOST_TEST (testset2.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_unordered_set >::test_all(data); test_unordered_set >::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_unordered_set >::test_all(data); test_unordered_set >::test_all(data); test_unordered_set >::test_all(data); test_unordered_set >::test_all(data); return 0; } }; /* template class boost::intrusive::unordered_set < unordered_set_base_raw_t , boost::hash , std::equal_to, false>; template class boost::intrusive::unordered_set < unordered_set_member_raw_t , boost::hash , std::equal_to, false>; template class boost::intrusive::unordered_set < unordered_set_auto_base_raw , boost::hash , std::equal_to, false>; template class boost::intrusive::unordered_set < unordered_set_auto_member_raw , boost::hash , std::equal_to, false>; template class boost::intrusive::unordered_set < unordered_set_base_smart , boost::hash , std::equal_to, false>; template class boost::intrusive::unordered_set < unordered_set_member_smart , boost::hash , std::equal_to, false>; template class boost::intrusive::unordered_set < unordered_set_auto_base_smart , boost::hash , std::equal_to, false>; template class boost::intrusive::unordered_set < unordered_set_auto_member_smart , boost::hash , std::equal_to, false>; //Explicit instantiation of counted classes template class boost::intrusive::unordered_set < unordered_set_base_raw_t , boost::hash , std::equal_to, true>; template class boost::intrusive::unordered_set < unordered_set_member_raw_t , boost::hash , std::equal_to, true>; template class boost::intrusive::unordered_set < unordered_set_base_smart_t , boost::hash , std::equal_to, true>; template class boost::intrusive::unordered_set < unordered_set_member_smart_t , boost::hash , std::equal_to, true>; */ int main( int, char* [] ) { test_main_template()(); test_main_template, false>()(); test_main_template()(); test_main_template, true>()(); return boost::report_errors(); } #include