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_map 15 16 // void rehash(size_type n); 17 18 #include <unordered_map> 19 #include <string> 20 #include <cassert> 21 22 #include "test_macros.h" 23 #include "min_allocator.h" 24 25 template <class C> 26 void rehash_postcondition(const C& c, size_t n) 27 { 28 assert(c.bucket_count() >= c.size() / c.max_load_factor() && c.bucket_count() >= n); 29 } 30 31 template <class C> 32 void test(const C& c) 33 { 34 assert(c.size() == 4); 35 assert(c.at(1) == "one"); 36 assert(c.at(2) == "two"); 37 assert(c.at(3) == "three"); 38 assert(c.at(4) == "four"); 39 } 40 41 int main() 42 { 43 { 44 typedef std::unordered_map<int, std::string> C; 45 typedef std::pair<int, std::string> P; 46 P a[] = 47 { 48 P(1, "one"), 49 P(2, "two"), 50 P(3, "three"), 51 P(4, "four"), 52 P(1, "four"), 53 P(2, "four"), 54 }; 55 C c(a, a + sizeof(a)/sizeof(a[0])); 56 test(c); 57 assert(c.bucket_count() >= 5); 58 c.rehash(3); 59 rehash_postcondition(c, 3); 60 LIBCPP_ASSERT(c.bucket_count() == 5); 61 test(c); 62 c.max_load_factor(2); 63 c.rehash(3); 64 rehash_postcondition(c, 3); 65 LIBCPP_ASSERT(c.bucket_count() == 3); 66 test(c); 67 c.rehash(31); 68 rehash_postcondition(c, 31); 69 LIBCPP_ASSERT(c.bucket_count() == 31); 70 test(c); 71 } 72 #if TEST_STD_VER >= 11 73 { 74 typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>, 75 min_allocator<std::pair<const int, std::string>>> C; 76 typedef std::pair<int, std::string> P; 77 P a[] = 78 { 79 P(1, "one"), 80 P(2, "two"), 81 P(3, "three"), 82 P(4, "four"), 83 P(1, "four"), 84 P(2, "four"), 85 }; 86 C c(a, a + sizeof(a)/sizeof(a[0])); 87 test(c); 88 assert(c.bucket_count() >= 5); 89 c.rehash(3); 90 rehash_postcondition(c, 3); 91 LIBCPP_ASSERT(c.bucket_count() == 5); 92 test(c); 93 c.max_load_factor(2); 94 c.rehash(3); 95 rehash_postcondition(c, 3); 96 LIBCPP_ASSERT(c.bucket_count() == 3); 97 test(c); 98 c.rehash(31); 99 rehash_postcondition(c, 31); 100 LIBCPP_ASSERT(c.bucket_count() == 31); 101 test(c); 102 } 103 #endif 104 } 105