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