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_multimap |
14 | |
15 | // void reserve(size_type n); |
16 | |
17 | #include <unordered_map> |
18 | #include <string> |
19 | #include <set> |
20 | #include <cassert> |
21 | |
22 | #include "test_macros.h" |
23 | #include "min_allocator.h" |
24 | |
25 | template <class C> |
26 | void test(const C& c) { |
27 | assert(c.size() == 6); |
28 | { |
29 | std::set<std::string> s; |
30 | s.insert(x: "one" ); |
31 | s.insert(x: "four" ); |
32 | assert(s.find(c.find(1)->second) != s.end()); |
33 | s.erase(s.find(c.find(1)->second)); |
34 | assert(s.find(std::next(c.find(1))->second) != s.end()); |
35 | } |
36 | { |
37 | std::set<std::string> s; |
38 | s.insert(x: "two" ); |
39 | s.insert(x: "four" ); |
40 | assert(s.find(c.find(2)->second) != s.end()); |
41 | s.erase(s.find(c.find(2)->second)); |
42 | assert(s.find(std::next(c.find(2))->second) != s.end()); |
43 | } |
44 | assert(c.find(3)->second == "three" ); |
45 | assert(c.find(4)->second == "four" ); |
46 | } |
47 | |
48 | void reserve_invariant(std::size_t n) // LWG #2156 |
49 | { |
50 | for (std::size_t i = 0; i < n; ++i) { |
51 | std::unordered_multimap<std::size_t, size_t> c; |
52 | c.reserve(n: n); |
53 | std::size_t buckets = c.bucket_count(); |
54 | for (std::size_t j = 0; j < i; ++j) { |
55 | c.insert(x: std::unordered_multimap<std::size_t, size_t>::value_type(i, i)); |
56 | assert(buckets == c.bucket_count()); |
57 | } |
58 | } |
59 | } |
60 | |
61 | int main(int, char**) { |
62 | { |
63 | typedef std::unordered_multimap<int, std::string> C; |
64 | typedef std::pair<int, std::string> P; |
65 | P a[] = { |
66 | P(1, "one" ), |
67 | P(2, "two" ), |
68 | P(3, "three" ), |
69 | P(4, "four" ), |
70 | P(1, "four" ), |
71 | P(2, "four" ), |
72 | }; |
73 | C c(a, a + sizeof(a) / sizeof(a[0])); |
74 | test(c); |
75 | assert(c.bucket_count() >= 7); |
76 | c.reserve(n: 3); |
77 | LIBCPP_ASSERT(c.bucket_count() == 7); |
78 | test(c); |
79 | c.max_load_factor(z: 2); |
80 | c.reserve(n: 3); |
81 | LIBCPP_ASSERT(c.bucket_count() == 3); |
82 | test(c); |
83 | c.reserve(n: 31); |
84 | assert(c.bucket_count() >= 16); |
85 | test(c); |
86 | } |
87 | #if TEST_STD_VER >= 11 |
88 | { |
89 | typedef std::unordered_multimap<int, |
90 | std::string, |
91 | std::hash<int>, |
92 | std::equal_to<int>, |
93 | min_allocator<std::pair<const int, std::string>>> |
94 | C; |
95 | typedef std::pair<int, std::string> P; |
96 | P a[] = { |
97 | P(1, "one" ), |
98 | P(2, "two" ), |
99 | P(3, "three" ), |
100 | P(4, "four" ), |
101 | P(1, "four" ), |
102 | P(2, "four" ), |
103 | }; |
104 | C c(a, a + sizeof(a) / sizeof(a[0])); |
105 | test(c); |
106 | assert(c.bucket_count() >= 7); |
107 | c.reserve(3); |
108 | LIBCPP_ASSERT(c.bucket_count() == 7); |
109 | test(c); |
110 | c.max_load_factor(2); |
111 | c.reserve(3); |
112 | LIBCPP_ASSERT(c.bucket_count() == 3); |
113 | test(c); |
114 | c.reserve(31); |
115 | assert(c.bucket_count() >= 16); |
116 | test(c); |
117 | } |
118 | #endif |
119 | reserve_invariant(n: 20); |
120 | |
121 | return 0; |
122 | } |
123 | |