From 39aed02e323e26e6dfb7687bbbed895ece6eebf7 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Mon, 7 May 2012 18:10:04 +0000 Subject: [PATCH] Unordered: Check that reserve works for both range and single element insert. [SVN r78369] --- include/boost/unordered/detail/unique.hpp | 2 +- test/unordered/rehash_tests.cpp | 58 ++++++++++++++++++++--- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/include/boost/unordered/detail/unique.hpp b/include/boost/unordered/detail/unique.hpp index 1f76e3b0..e27a0e7d 100644 --- a/include/boost/unordered/detail/unique.hpp +++ b/include/boost/unordered/detail/unique.hpp @@ -535,7 +535,7 @@ namespace boost { namespace unordered { namespace detail { a.construct_node(); a.construct_value2(*i); - if(this->size_ + 1 >= this->max_load_) + if(this->size_ + 1 > this->max_load_) this->reserve_for_insert(this->size_ + boost::unordered::detail::insert_size(i, j)); diff --git a/test/unordered/rehash_tests.cpp b/test/unordered/rehash_tests.cpp index 06c6a844..4f62ae9b 100644 --- a/test/unordered/rehash_tests.cpp +++ b/test/unordered/rehash_tests.cpp @@ -101,7 +101,44 @@ void rehash_test1(X* = 0, } template -void reserve_test(X* = 0, +void reserve_test1(X* = 0, + test::random_generator generator = test::default_generator) +{ + for (int random_mlf = 0; random_mlf < 2; ++random_mlf) + { + for (int i = 1; i < 2000; i += i < 50 ? 1 : 13) + { + test::random_values v(i, generator); + + test::ordered tracker; + tracker.insert_range(v.begin(), v.end()); + + X x; + x.max_load_factor(random_mlf ? + static_cast(std::rand() % 1000) / 500.0f + 0.5f : 1.0f); + + // For the current standard this should reserve i+1, I've + // submitted a defect report and will assume it's a defect + // for now. + x.reserve(i); + + // Insert an element before the range insert, otherwise there are + // no iterators to invalidate in the range insert, and it can + // rehash. + typename test::random_values::iterator it = v.begin(); + x.insert(*it); + ++it; + + std::size_t bucket_count = x.bucket_count(); + x.insert(it, v.end()); + BOOST_TEST(bucket_count == x.bucket_count()); + tracker.compare(x); + } + } +} + +template +void reserve_test2(X* = 0, test::random_generator generator = test::default_generator) { for (int random_mlf = 0; random_mlf < 2; ++random_mlf) @@ -115,13 +152,17 @@ void reserve_test(X* = 0, X x; x.max_load_factor(random_mlf ? - static_cast(std::rand() % 1000) / 500.0 + 0.5f : 1.0f); - // For the current standard this should reserve i+1, I've - // submitted a defect report and will assume it's a defect - // for now. + static_cast(std::rand() % 1000) / 500.0f + 0.5f : 1.0f); + x.reserve(i); std::size_t bucket_count = x.bucket_count(); - x.insert(v.begin(), v.end()); + + for (typename test::random_values::iterator it = v.begin(); + it != v.end(); ++it) + { + x.insert(*it); + } + BOOST_TEST(bucket_count == x.bucket_count()); tracker.compare(x); } @@ -145,7 +186,10 @@ UNORDERED_TEST(rehash_empty_test3, UNORDERED_TEST(rehash_test1, ((int_set_ptr)(int_multiset_ptr)(int_map_ptr)(int_multimap_ptr)) ) -UNORDERED_TEST(reserve_test, +UNORDERED_TEST(reserve_test1, + ((int_set_ptr)(int_multiset_ptr)(int_map_ptr)(int_multimap_ptr)) +) +UNORDERED_TEST(reserve_test2, ((int_set_ptr)(int_multiset_ptr)(int_map_ptr)(int_multimap_ptr)) )