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_multiset
14
15// void reserve(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 test(const C& c)
25{
26 assert(c.size() == 6);
27 assert(c.count(1) == 2);
28 assert(c.count(2) == 2);
29 assert(c.count(3) == 1);
30 assert(c.count(4) == 1);
31}
32
33void reserve_invariant(std::size_t n) // LWG #2156
34{
35 for (std::size_t i = 0; i < n; ++i)
36 {
37 std::unordered_multiset<std::size_t> c;
38 c.reserve(n: n);
39 std::size_t buckets = c.bucket_count();
40 for (std::size_t j = 0; j < i; ++j)
41 {
42 c.insert(x: i);
43 assert(buckets == c.bucket_count());
44 }
45 }
46}
47
48int main(int, char**)
49{
50 {
51 typedef std::unordered_multiset<int> C;
52 typedef int P;
53 P a[] =
54 {
55 P(1),
56 P(2),
57 P(3),
58 P(4),
59 P(1),
60 P(2)
61 };
62 C c(a, a + sizeof(a)/sizeof(a[0]));
63 test(c);
64 assert(c.bucket_count() >= 7);
65 c.reserve(n: 3);
66 LIBCPP_ASSERT(c.bucket_count() == 7);
67 test(c);
68 c.max_load_factor(z: 2);
69 c.reserve(n: 3);
70 LIBCPP_ASSERT(c.bucket_count() == 3);
71 test(c);
72 c.reserve(n: 31);
73 assert(c.bucket_count() >= 16);
74 test(c);
75 }
76#if TEST_STD_VER >= 11
77 {
78 typedef std::unordered_multiset<int, std::hash<int>,
79 std::equal_to<int>, min_allocator<int>> C;
80 typedef int P;
81 P a[] =
82 {
83 P(1),
84 P(2),
85 P(3),
86 P(4),
87 P(1),
88 P(2)
89 };
90 C c(a, a + sizeof(a)/sizeof(a[0]));
91 test(c);
92 assert(c.bucket_count() >= 7);
93 c.reserve(3);
94 LIBCPP_ASSERT(c.bucket_count() == 7);
95 test(c);
96 c.max_load_factor(2);
97 c.reserve(3);
98 LIBCPP_ASSERT(c.bucket_count() == 3);
99 test(c);
100 c.reserve(31);
101 assert(c.bucket_count() >= 16);
102 test(c);
103 }
104#endif
105 reserve_invariant(n: 20);
106
107 return 0;
108}
109

source code of libcxx/test/std/containers/unord/unord.multiset/reserve.pass.cpp