1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // <unordered_map> |
10 | |
11 | // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>, |
12 | // class Alloc = allocator<pair<const Key, T>>> |
13 | // class unordered_map |
14 | |
15 | // void rehash(size_type n); |
16 | |
17 | #include <unordered_map> |
18 | #include <string> |
19 | #include <cassert> |
20 | |
21 | #include "test_macros.h" |
22 | #include "min_allocator.h" |
23 | |
24 | template <class C> |
25 | void rehash_postcondition(const C& c, std::size_t n) |
26 | { |
27 | assert(c.bucket_count() >= c.size() / c.max_load_factor() && c.bucket_count() >= n); |
28 | } |
29 | |
30 | template <class C> |
31 | void test(const C& c) |
32 | { |
33 | assert(c.size() == 4); |
34 | assert(c.at(1) == "one" ); |
35 | assert(c.at(2) == "two" ); |
36 | assert(c.at(3) == "three" ); |
37 | assert(c.at(4) == "four" ); |
38 | } |
39 | |
40 | int main(int, char**) |
41 | { |
42 | { |
43 | typedef std::unordered_map<int, std::string> C; |
44 | typedef std::pair<int, std::string> P; |
45 | P a[] = |
46 | { |
47 | P(1, "one" ), |
48 | P(2, "two" ), |
49 | P(3, "three" ), |
50 | P(4, "four" ), |
51 | P(1, "four" ), |
52 | P(2, "four" ), |
53 | }; |
54 | C c(a, a + sizeof(a)/sizeof(a[0])); |
55 | test(c); |
56 | assert(c.bucket_count() >= 5); |
57 | c.rehash(n: 3); |
58 | rehash_postcondition(c, n: 3); |
59 | LIBCPP_ASSERT(c.bucket_count() == 5); |
60 | test(c); |
61 | c.max_load_factor(z: 2); |
62 | c.rehash(n: 3); |
63 | rehash_postcondition(c, n: 3); |
64 | LIBCPP_ASSERT(c.bucket_count() == 3); |
65 | test(c); |
66 | c.rehash(n: 31); |
67 | rehash_postcondition(c, n: 31); |
68 | LIBCPP_ASSERT(c.bucket_count() == 31); |
69 | test(c); |
70 | } |
71 | #if TEST_STD_VER >= 11 |
72 | { |
73 | typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>, |
74 | min_allocator<std::pair<const int, std::string>>> C; |
75 | typedef std::pair<int, std::string> P; |
76 | P a[] = |
77 | { |
78 | P(1, "one" ), |
79 | P(2, "two" ), |
80 | P(3, "three" ), |
81 | P(4, "four" ), |
82 | P(1, "four" ), |
83 | P(2, "four" ), |
84 | }; |
85 | C c(a, a + sizeof(a)/sizeof(a[0])); |
86 | test(c); |
87 | assert(c.bucket_count() >= 5); |
88 | c.rehash(3); |
89 | rehash_postcondition(c, 3); |
90 | LIBCPP_ASSERT(c.bucket_count() == 5); |
91 | test(c); |
92 | c.max_load_factor(2); |
93 | c.rehash(3); |
94 | rehash_postcondition(c, 3); |
95 | LIBCPP_ASSERT(c.bucket_count() == 3); |
96 | test(c); |
97 | c.rehash(31); |
98 | rehash_postcondition(c, 31); |
99 | LIBCPP_ASSERT(c.bucket_count() == 31); |
100 | test(c); |
101 | } |
102 | #endif |
103 | |
104 | return 0; |
105 | } |
106 | |