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 first, const_iterator last)
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 
main(int,char **)27 int main(int, char**)
28 {
29     {
30         typedef std::unordered_multimap<int, std::string> C;
31         typedef std::pair<int, std::string> P;
32         P a[] =
33         {
34             P(1, "one"),
35             P(2, "two"),
36             P(3, "three"),
37             P(4, "four"),
38             P(1, "four"),
39             P(2, "four"),
40         };
41         C c(a, a + sizeof(a)/sizeof(a[0]));
42         C::const_iterator i = c.find(2);
43         C::const_iterator j = std::next(i, 2);
44         C::iterator k = c.erase(i, i);
45         assert(k == i);
46         assert(c.size() == 6);
47         typedef std::pair<C::iterator, C::iterator> Eq;
48         Eq eq = c.equal_range(1);
49         assert(std::distance(eq.first, eq.second) == 2);
50         std::multiset<std::string> s;
51         s.insert("one");
52         s.insert("four");
53         CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
54         eq = c.equal_range(2);
55         assert(std::distance(eq.first, eq.second) == 2);
56         s.insert("two");
57         s.insert("four");
58         CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s);
59         eq = c.equal_range(3);
60         assert(std::distance(eq.first, eq.second) == 1);
61         k = eq.first;
62         assert(k->first == 3);
63         assert(k->second == "three");
64         eq = c.equal_range(4);
65         assert(std::distance(eq.first, eq.second) == 1);
66         k = eq.first;
67         assert(k->first == 4);
68         assert(k->second == "four");
69         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
70         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
71 
72         k = c.erase(i, j);
73         assert(c.size() == 4);
74         eq = c.equal_range(1);
75         assert(std::distance(eq.first, eq.second) == 2);
76         s.insert("one");
77         s.insert("four");
78         CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
79         eq = c.equal_range(3);
80         assert(std::distance(eq.first, eq.second) == 1);
81         k = eq.first;
82         assert(k->first == 3);
83         assert(k->second == "three");
84         eq = c.equal_range(4);
85         assert(std::distance(eq.first, eq.second) == 1);
86         k = eq.first;
87         assert(k->first == 4);
88         assert(k->second == "four");
89         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
90         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
91 
92         k = c.erase(c.cbegin(), c.cend());
93         assert(c.size() == 0);
94         assert(k == c.end());
95     }
96 #if TEST_STD_VER >= 11
97     {
98         typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>,
99                             min_allocator<std::pair<const int, std::string>>> C;
100         typedef std::pair<int, std::string> P;
101         P a[] =
102         {
103             P(1, "one"),
104             P(2, "two"),
105             P(3, "three"),
106             P(4, "four"),
107             P(1, "four"),
108             P(2, "four"),
109         };
110         C c(a, a + sizeof(a)/sizeof(a[0]));
111         C::const_iterator i = c.find(2);
112         C::const_iterator j = std::next(i, 2);
113         C::iterator k = c.erase(i, i);
114         assert(k == i);
115         assert(c.size() == 6);
116         typedef std::pair<C::iterator, C::iterator> Eq;
117         Eq eq = c.equal_range(1);
118         assert(std::distance(eq.first, eq.second) == 2);
119         std::multiset<std::string> s;
120         s.insert("one");
121         s.insert("four");
122         CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
123         eq = c.equal_range(2);
124         assert(std::distance(eq.first, eq.second) == 2);
125         s.insert("two");
126         s.insert("four");
127         CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s);
128         eq = c.equal_range(3);
129         assert(std::distance(eq.first, eq.second) == 1);
130         k = eq.first;
131         assert(k->first == 3);
132         assert(k->second == "three");
133         eq = c.equal_range(4);
134         assert(std::distance(eq.first, eq.second) == 1);
135         k = eq.first;
136         assert(k->first == 4);
137         assert(k->second == "four");
138         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
139         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
140 
141         k = c.erase(i, j);
142         assert(c.size() == 4);
143         eq = c.equal_range(1);
144         assert(std::distance(eq.first, eq.second) == 2);
145         s.insert("one");
146         s.insert("four");
147         CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
148         eq = c.equal_range(3);
149         assert(std::distance(eq.first, eq.second) == 1);
150         k = eq.first;
151         assert(k->first == 3);
152         assert(k->second == "three");
153         eq = c.equal_range(4);
154         assert(std::distance(eq.first, eq.second) == 1);
155         k = eq.first;
156         assert(k->first == 4);
157         assert(k->second == "four");
158         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
159         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
160 
161         k = c.erase(c.cbegin(), c.cend());
162         assert(c.size() == 0);
163         assert(k == c.end());
164     }
165 #endif
166 
167   return 0;
168 }
169