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

source code of libcxx/test/std/containers/unord/unord.map/rehash.pass.cpp