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