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_set>
10
11// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
12// class Alloc = allocator<Value>>
13// class unordered_set
14
15// void rehash(size_type n);
16
17#include <unordered_set>
18#include <cassert>
19
20#include "test_macros.h"
21#include "min_allocator.h"
22
23template <class C>
24void rehash_postcondition(const C& c, std::size_t n)
25{
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{
32 assert(c.size() == 4);
33 assert(c.count(1) == 1);
34 assert(c.count(2) == 1);
35 assert(c.count(3) == 1);
36 assert(c.count(4) == 1);
37}
38
39int main(int, char**)
40{
41 {
42 typedef std::unordered_set<int> C;
43 typedef int P;
44 P a[] =
45 {
46 P(1),
47 P(2),
48 P(3),
49 P(4),
50 P(1),
51 P(2)
52 };
53 C c(a, a + sizeof(a)/sizeof(a[0]));
54 test(c);
55 assert(c.bucket_count() >= 5);
56 c.rehash(n: 3);
57 rehash_postcondition(c, n: 3);
58 LIBCPP_ASSERT(c.bucket_count() == 5);
59 test(c);
60 c.max_load_factor(z: 2);
61 c.rehash(n: 3);
62 rehash_postcondition(c, n: 3);
63 LIBCPP_ASSERT(c.bucket_count() == 3);
64 test(c);
65 c.rehash(n: 31);
66 rehash_postcondition(c, n: 31);
67 LIBCPP_ASSERT(c.bucket_count() == 31);
68 test(c);
69 }
70#if TEST_STD_VER >= 11
71 {
72 typedef std::unordered_set<int, std::hash<int>,
73 std::equal_to<int>, min_allocator<int>> C;
74 typedef int P;
75 P a[] =
76 {
77 P(1),
78 P(2),
79 P(3),
80 P(4),
81 P(1),
82 P(2)
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.set/rehash.pass.cpp