unordered_set_test.cpp

来自「Boost provides free peer-reviewed portab」· C++ 代码 · 共 635 行 · 第 1/2 页

CPP
635
字号
   TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }   //This incremental rehash should success because the new size is twice the original   //and split_count is the same as the old bucket count   BOOST_TEST(testset1.incremental_rehash(bucket_traits(buckets2, BucketSize*2)) == true);   BOOST_TEST(testset1.split_count() == BucketSize);   {  int init_values [] = { 1, 2, 3, 4, 5 };   TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }   //This incremental rehash should also success because the new size is half the original   //and split_count is the same as the new bucket count   BOOST_TEST(testset1.incremental_rehash(bucket_traits(buckets1, BucketSize)) == true);   BOOST_TEST(testset1.split_count() == BucketSize);   {  int init_values [] = { 1, 2, 3, 4, 5 };   TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }   //Full shrink rehash   testset1.rehash(bucket_traits(buckets1, 4));   BOOST_TEST (testset1.size() == values.size()-1);   BOOST_TEST (testset1.incremental_rehash() == false);   {  int init_values [] = { 4, 5, 1, 2, 3 };      TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }   //Full shrink rehash again   testset1.rehash(bucket_traits(buckets1, 2));   BOOST_TEST (testset1.size() == values.size()-1);   BOOST_TEST (testset1.incremental_rehash() == false);   {  int init_values [] = { 2, 4, 3, 5, 1 };      TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }   //Full growing rehash   testset1.rehash(bucket_traits(buckets1, BucketSize));   BOOST_TEST (testset1.size() == values.size()-1);   BOOST_TEST (testset1.incremental_rehash() == false);   {  int init_values [] = { 1, 2, 3, 4, 5 };      TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }   //Incremental rehash shrinking   //First incremental rehashes should lead to the same sequence   for(std::size_t split_bucket = testset1.split_count(); split_bucket > 6; --split_bucket){      BOOST_TEST (testset1.incremental_rehash(false) == true);      BOOST_TEST(testset1.split_count() == (split_bucket-1));      {  int init_values [] = { 1, 2, 3, 4, 5 };      TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }   }   //Incremental rehash step   BOOST_TEST (testset1.incremental_rehash(false) == true);   BOOST_TEST(testset1.split_count() == (BucketSize/2+1));   {  int init_values [] = { 5, 1, 2, 3, 4 };      TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }   //Incremental rehash step 2   BOOST_TEST (testset1.incremental_rehash(false) == true);   BOOST_TEST(testset1.split_count() == (BucketSize/2));   {  int init_values [] = { 4, 5, 1, 2, 3 };      TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }   //This incremental rehash should fail because we've reached the half of the bucket array   BOOST_TEST(testset1.incremental_rehash(false) == false);   BOOST_TEST(testset1.split_count() == BucketSize/2);   {  int init_values [] = { 4, 5, 1, 2, 3 };   TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }}//test: rehash:template<class ValueTraits, bool CacheBegin, bool CompareHash, bool Incremental>void test_unordered_set<ValueTraits, CacheBegin, CompareHash, Incremental>::   test_rehash(std::vector<typename ValueTraits::value_type>& values, detail::false_){   typedef typename ValueTraits::value_type value_type;   typedef unordered_set      <value_type      , value_traits<ValueTraits>      , constant_time_size<value_type::constant_time_size>      , cache_begin<CacheBegin>      , compare_hash<CompareHash>      , incremental<Incremental>      > unordered_set_type;   typedef typename unordered_set_type::bucket_traits bucket_traits;   typename unordered_set_type::bucket_type buckets1 [BucketSize];   typename unordered_set_type::bucket_type buckets2 [2];   typename unordered_set_type::bucket_type buckets3 [BucketSize*2];   unordered_set_type testset1(&values[0], &values[0] + 6, bucket_traits(buckets1, BucketSize));   BOOST_TEST (testset1.size() == values.size()-1);   {  int init_values [] = { 1, 2, 3, 4, 5 };      TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }   testset1.rehash(bucket_traits(buckets2, 2));   BOOST_TEST (testset1.size() == values.size()-1);   {  int init_values [] = { 4, 2, 5, 3, 1 };      TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }   testset1.rehash(bucket_traits(buckets3, BucketSize*2));   BOOST_TEST (testset1.size() == values.size()-1);   {  int init_values [] = { 1, 2, 3, 4, 5 };      TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }   //Now rehash reducing the buckets   testset1.rehash(bucket_traits(buckets3, 2));   BOOST_TEST (testset1.size() == values.size()-1);   {  int init_values [] = { 4, 2, 5, 3, 1 };      TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }   //Now rehash increasing the buckets   testset1.rehash(bucket_traits(buckets3, BucketSize*2));   BOOST_TEST (testset1.size() == values.size()-1);   {  int init_values [] = { 1, 2, 3, 4, 5 };      TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() );  }}  //test: find, equal_range (lower_bound, upper_bound):template<class ValueTraits, bool CacheBegin, bool CompareHash, bool Incremental>void test_unordered_set<ValueTraits, CacheBegin, CompareHash, Incremental>::   test_find(std::vector<typename ValueTraits::value_type>& values){   typedef typename ValueTraits::value_type value_type;   typedef unordered_set      <value_type      , value_traits<ValueTraits>      , constant_time_size<value_type::constant_time_size>      , cache_begin<CacheBegin>      , compare_hash<CompareHash>      , incremental<Incremental>      > unordered_set_type;   typedef typename unordered_set_type::bucket_traits bucket_traits;   typename unordered_set_type::bucket_type buckets [BucketSize];   unordered_set_type testset (values.begin(), values.end(), bucket_traits(buckets, BucketSize));   typedef typename unordered_set_type::iterator iterator;   value_type cmp_val;   cmp_val.value_ = 2;   iterator i = testset.find (cmp_val);   BOOST_TEST (i->value_ == 2);   BOOST_TEST ((++i)->value_ != 2);   std::pair<iterator,iterator> 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<class ValueTraits, bool CacheBegin, bool CompareHash, bool Incremental>void test_unordered_set<ValueTraits, CacheBegin, CompareHash, Incremental>   ::test_clone(std::vector<typename ValueTraits::value_type>& values){   typedef typename ValueTraits::value_type value_type;   typedef unordered_set      <value_type      , value_traits<ValueTraits>      , constant_time_size<value_type::constant_time_size>      , cache_begin<CacheBegin>      , compare_hash<CompareHash>      , incremental<Incremental>      > unordered_set_type;   typedef typename unordered_set_type::bucket_traits bucket_traits;   {      //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 (values.begin(), values.end(), bucket_traits(buckets1, BucketSize));      unordered_set_type testset2 (bucket_traits(buckets2, BucketSize));      testset2.clone_from(testset1, test::new_cloner<value_type>(), test::delete_disposer<value_type>());      //Ordering is not guarantee in the cloning so insert data in a set and test      std::set<typename ValueTraits::value_type>         src(testset1.begin(), testset1.end());      std::set<typename ValueTraits::value_type>         dst(testset2.begin(), testset2.end());      BOOST_TEST (src.size() == dst.size() && std::equal(src.begin(), src.end(), dst.begin()));      testset2.clear_and_dispose(test::delete_disposer<value_type>());      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 (values.begin(), values.end(), bucket_traits(buckets1, BucketSize*2));      unordered_set_type testset2 (bucket_traits(buckets2, BucketSize));      testset2.clone_from(testset1, test::new_cloner<value_type>(), test::delete_disposer<value_type>());      //Ordering is not guaranteed in the cloning so insert data in a set and test      std::set<typename ValueTraits::value_type>         src(testset1.begin(), testset1.end());      std::set<typename ValueTraits::value_type>         dst(testset2.begin(), testset2.end());      BOOST_TEST (src.size() == dst.size() && std::equal(src.begin(), src.end(), dst.begin()));      testset2.clear_and_dispose(test::delete_disposer<value_type>());      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 (values.begin(), values.end(), bucket_traits(buckets1, BucketSize));      unordered_set_type testset2 (bucket_traits(buckets2, BucketSize*2));      testset2.clone_from(testset1, test::new_cloner<value_type>(), test::delete_disposer<value_type>());      //Ordering is not guarantee in the cloning so insert data in a set and test      std::set<typename ValueTraits::value_type>         src(testset1.begin(), testset1.end());      std::set<typename ValueTraits::value_type>         dst(testset2.begin(), testset2.end());      BOOST_TEST (src.size() == dst.size() && std::equal(src.begin(), src.end(), dst.begin()));      testset2.clear_and_dispose(test::delete_disposer<value_type>());      BOOST_TEST (testset2.empty());   }}template<class VoidPointer, bool constant_time_size, bool incremental>class test_main_template{   public:   int operator()()   {      typedef testvalue<VoidPointer, constant_time_size> value_type;      static const int random_init[6] = { 3, 2, 4, 1, 5, 2 };      std::vector<testvalue<VoidPointer, constant_time_size> > data (6);      for (int i = 0; i < 6; ++i)         data[i].value_ = random_init[i];       test_unordered_set < typename detail::get_base_value_traits                  < value_type                  , typename value_type::unordered_set_base_hook_t                  >::type                , true                , false                , incremental                >::test_all(data);      test_unordered_set < typename detail::get_member_value_traits                  < value_type                  , member_hook< value_type                               , typename value_type::unordered_set_member_hook_t                               , &value_type::unordered_set_node_                               >                  >::type                , false                , false                , incremental                >::test_all(data);      return 0;   }};template<class VoidPointer, bool incremental>class test_main_template<VoidPointer, false, incremental>{   public:   int operator()()   {      typedef testvalue<VoidPointer, false> value_type;      static const int random_init[6] = { 3, 2, 4, 1, 5, 2 };      std::vector<testvalue<VoidPointer, false> > data (6);      for (int i = 0; i < 6; ++i)         data[i].value_ = random_init[i];       test_unordered_set < typename detail::get_base_value_traits                  < value_type                  , typename value_type::unordered_set_base_hook_t                  >::type                , true                , false                , incremental                >::test_all(data);      test_unordered_set < typename detail::get_member_value_traits                  < value_type                  , member_hook< value_type                               , typename value_type::unordered_set_member_hook_t                               , &value_type::unordered_set_node_                               >                  >::type                , false                , false                , incremental                >::test_all(data);      test_unordered_set < typename detail::get_base_value_traits                  < value_type                  , typename value_type::unordered_set_auto_base_hook_t                  >::type                , true                , true                , incremental                >::test_all(data);      test_unordered_set < typename detail::get_member_value_traits                  < value_type                  , member_hook< value_type                               , typename value_type::unordered_set_auto_member_hook_t                               , &value_type::unordered_set_auto_node_                               >                  >::type                , false                , true                , incremental                >::test_all(data);      return 0;   }};int main( int, char* [] ) {   test_main_template<void*, false, true>()();   test_main_template<smart_ptr<void>, false, true>()();   test_main_template<void*, true, true>()();   test_main_template<smart_ptr<void>, true, true>()();   test_main_template<void*, false, false>()();   test_main_template<smart_ptr<void>, false, false>()();   test_main_template<void*, true, true>()();   test_main_template<smart_ptr<void>, true, false>()();   return boost::report_errors();}#include <boost/intrusive/detail/config_end.hpp>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?