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 | // iterator erase(const_iterator p) |
16 | |
17 | #include <unordered_map> |
18 | #include <string> |
19 | #include <set> |
20 | #include <cassert> |
21 | #include <cstddef> |
22 | |
23 | #include "test_macros.h" |
24 | #include "../../../check_consecutive.h" |
25 | #include "min_allocator.h" |
26 | |
27 | struct TemplateConstructor { |
28 | template <typename T> |
29 | TemplateConstructor(const T&) {} |
30 | }; |
31 | |
32 | bool operator==(const TemplateConstructor&, const TemplateConstructor&) { return false; } |
33 | struct Hash { |
34 | std::size_t operator()(const TemplateConstructor&) const { return 0; } |
35 | }; |
36 | |
37 | int main(int, char**) { |
38 | { |
39 | typedef std::unordered_multimap<int, std::string> C; |
40 | typedef std::pair<int, std::string> P; |
41 | P a[] = { |
42 | P(1, "one" ), |
43 | P(2, "two" ), |
44 | P(3, "three" ), |
45 | P(4, "four" ), |
46 | P(1, "four" ), |
47 | P(2, "four" ), |
48 | }; |
49 | C c(a, a + sizeof(a) / sizeof(a[0])); |
50 | C::const_iterator i = c.find(x: 2); |
51 | C::const_iterator i_next = i; |
52 | ++i_next; |
53 | std::string es = i->second; |
54 | C::iterator j = c.erase(position: i); |
55 | assert(j == i_next); |
56 | |
57 | assert(c.size() == 5); |
58 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
59 | Eq eq = c.equal_range(x: 1); |
60 | assert(std::distance(eq.first, eq.second) == 2); |
61 | std::multiset<std::string> s; |
62 | s.insert(x: "one" ); |
63 | s.insert(x: "four" ); |
64 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s); |
65 | eq = c.equal_range(x: 2); |
66 | assert(std::distance(eq.first, eq.second) == 1); |
67 | C::const_iterator k = eq.first; |
68 | assert(k->first == 2); |
69 | assert(k->second == (es == "two" ? "four" : "two" )); |
70 | eq = c.equal_range(x: 3); |
71 | assert(std::distance(eq.first, eq.second) == 1); |
72 | k = eq.first; |
73 | assert(k->first == 3); |
74 | assert(k->second == "three" ); |
75 | eq = c.equal_range(x: 4); |
76 | assert(std::distance(eq.first, eq.second) == 1); |
77 | k = eq.first; |
78 | assert(k->first == 4); |
79 | assert(k->second == "four" ); |
80 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
81 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
82 | } |
83 | #if TEST_STD_VER >= 11 |
84 | { |
85 | typedef std::unordered_multimap<int, |
86 | std::string, |
87 | std::hash<int>, |
88 | std::equal_to<int>, |
89 | min_allocator<std::pair<const int, std::string>>> |
90 | C; |
91 | typedef std::pair<int, std::string> P; |
92 | P a[] = { |
93 | P(1, "one" ), |
94 | P(2, "two" ), |
95 | P(3, "three" ), |
96 | P(4, "four" ), |
97 | P(1, "four" ), |
98 | P(2, "four" ), |
99 | }; |
100 | C c(a, a + sizeof(a) / sizeof(a[0])); |
101 | C::const_iterator i = c.find(2); |
102 | C::const_iterator i_next = i; |
103 | ++i_next; |
104 | std::string es = i->second; |
105 | C::iterator j = c.erase(i); |
106 | assert(j == i_next); |
107 | |
108 | assert(c.size() == 5); |
109 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
110 | Eq eq = c.equal_range(1); |
111 | assert(std::distance(eq.first, eq.second) == 2); |
112 | std::multiset<std::string> s; |
113 | s.insert("one" ); |
114 | s.insert("four" ); |
115 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
116 | eq = c.equal_range(2); |
117 | assert(std::distance(eq.first, eq.second) == 1); |
118 | C::const_iterator k = eq.first; |
119 | assert(k->first == 2); |
120 | assert(k->second == (es == "two" ? "four" : "two" )); |
121 | eq = c.equal_range(3); |
122 | assert(std::distance(eq.first, eq.second) == 1); |
123 | k = eq.first; |
124 | assert(k->first == 3); |
125 | assert(k->second == "three" ); |
126 | eq = c.equal_range(4); |
127 | assert(std::distance(eq.first, eq.second) == 1); |
128 | k = eq.first; |
129 | assert(k->first == 4); |
130 | assert(k->second == "four" ); |
131 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
132 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
133 | } |
134 | #endif |
135 | #if TEST_STD_VER >= 14 |
136 | { |
137 | // This is LWG #2059 |
138 | typedef TemplateConstructor T; |
139 | typedef std::unordered_multimap<T, int, Hash> C; |
140 | typedef C::iterator I; |
141 | |
142 | C m; |
143 | T a{0}; |
144 | I it = m.find(a); |
145 | if (it != m.end()) |
146 | m.erase(it); |
147 | } |
148 | #endif |
149 | |
150 | return 0; |
151 | } |
152 | |