rehash.pass.cpp (4245B)
1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <unordered_map> 11 12 // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>, 13 // class Alloc = allocator<pair<const Key, T>>> 14 // class unordered_multimap 15 16 // void rehash(size_type n); 17 18 #include <unordered_map> 19 #include <string> 20 #include <set> 21 #include <cassert> 22 #include <cfloat> 23 #include <cmath> 24 #include <cstddef> 25 26 #include "test_macros.h" 27 #include "min_allocator.h" 28 29 template <class C> 30 void rehash_postcondition(const C& c, size_t n) 31 { 32 assert(c.bucket_count() >= c.size() / c.max_load_factor() && c.bucket_count() >= n); 33 } 34 35 template <class C> 36 void test(const C& c) 37 { 38 assert(c.size() == 6); 39 typedef std::pair<typename C::const_iterator, typename C::const_iterator> Eq; 40 Eq eq = c.equal_range(1); 41 assert(std::distance(eq.first, eq.second) == 2); 42 typename C::const_iterator i = eq.first; 43 { 44 std::set<std::string> s; 45 s.insert("one"); 46 s.insert("four"); 47 for ( int n = 0; n < 2; ++n ) 48 { 49 assert(i->first == 1); 50 assert(s.find(i->second) != s.end()); 51 s.erase(s.find(i->second)); 52 ++i; 53 } 54 } 55 eq = c.equal_range(2); 56 assert(std::distance(eq.first, eq.second) == 2); 57 i = eq.first; 58 { 59 std::set<std::string> s; 60 s.insert("two"); 61 s.insert("four"); 62 for ( int n = 0; n < 2; ++n ) 63 { 64 assert(i->first == 2); 65 assert(s.find(i->second) != s.end()); 66 s.erase(s.find(i->second)); 67 ++i; 68 } 69 } 70 eq = c.equal_range(3); 71 assert(std::distance(eq.first, eq.second) == 1); 72 i = eq.first; 73 assert(i->first == 3); 74 assert(i->second == "three"); 75 eq = c.equal_range(4); 76 assert(std::distance(eq.first, eq.second) == 1); 77 i = eq.first; 78 assert(i->first == 4); 79 assert(i->second == "four"); 80 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); 81 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); 82 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); 83 } 84 85 int main() 86 { 87 { 88 typedef std::unordered_multimap<int, std::string> C; 89 typedef std::pair<int, std::string> P; 90 P a[] = 91 { 92 P(1, "one"), 93 P(2, "two"), 94 P(3, "three"), 95 P(4, "four"), 96 P(1, "four"), 97 P(2, "four"), 98 }; 99 C c(a, a + sizeof(a)/sizeof(a[0])); 100 test(c); 101 assert(c.bucket_count() >= 7); 102 c.rehash(3); 103 rehash_postcondition(c, 3); 104 LIBCPP_ASSERT(c.bucket_count() == 7); 105 test(c); 106 c.max_load_factor(2); 107 c.rehash(3); 108 rehash_postcondition(c, 3); 109 LIBCPP_ASSERT(c.bucket_count() == 3); 110 test(c); 111 c.rehash(31); 112 rehash_postcondition(c, 31); 113 LIBCPP_ASSERT(c.bucket_count() == 31); 114 test(c); 115 } 116 #if TEST_STD_VER >= 11 117 { 118 typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>, 119 min_allocator<std::pair<const int, std::string>>> C; 120 typedef std::pair<int, std::string> P; 121 P a[] = 122 { 123 P(1, "one"), 124 P(2, "two"), 125 P(3, "three"), 126 P(4, "four"), 127 P(1, "four"), 128 P(2, "four"), 129 }; 130 C c(a, a + sizeof(a)/sizeof(a[0])); 131 test(c); 132 assert(c.bucket_count() >= 7); 133 c.rehash(3); 134 rehash_postcondition(c, 3); 135 LIBCPP_ASSERT(c.bucket_count() == 7); 136 test(c); 137 c.max_load_factor(2); 138 c.rehash(3); 139 rehash_postcondition(c, 3); 140 LIBCPP_ASSERT(c.bucket_count() == 3); 141 test(c); 142 c.rehash(31); 143 rehash_postcondition(c, 31); 144 LIBCPP_ASSERT(c.bucket_count() == 31); 145 test(c); 146 } 147 #endif 148 }