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// size_type erase(const key_type& k);
16
17#include <unordered_set>
18#include <string>
19#include <cassert>
20
21#include "test_macros.h"
22#include "min_allocator.h"
23
24#if TEST_STD_VER >= 11
25template <typename Unordered>
26bool only_deletions ( const Unordered &whole, const Unordered &part ) {
27 typename Unordered::const_iterator w = whole.begin();
28 typename Unordered::const_iterator p = part.begin();
29
30 while ( w != whole.end () && p != part.end()) {
31 if ( *w == *p )
32 p++;
33 w++;
34 }
35
36 return p == part.end();
37}
38#endif
39
40int main(int, char**)
41{
42 {
43 typedef std::unordered_multiset<int> C;
44 typedef int P;
45 P a[] =
46 {
47 P(1),
48 P(2),
49 P(3),
50 P(4),
51 P(1),
52 P(2)
53 };
54 C c(a, a + sizeof(a)/sizeof(a[0]));
55 assert(c.erase(5) == 0);
56 assert(c.size() == 6);
57 assert(c.count(1) == 2);
58 assert(c.count(2) == 2);
59 assert(c.count(3) == 1);
60 assert(c.count(4) == 1);
61
62 assert(c.erase(2) == 2);
63 assert(c.size() == 4);
64 assert(c.count(1) == 2);
65 assert(c.count(3) == 1);
66 assert(c.count(4) == 1);
67
68 assert(c.erase(2) == 0);
69 assert(c.size() == 4);
70 assert(c.count(1) == 2);
71 assert(c.count(3) == 1);
72 assert(c.count(4) == 1);
73
74 assert(c.erase(4) == 1);
75 assert(c.size() == 3);
76 assert(c.count(1) == 2);
77 assert(c.count(3) == 1);
78
79 assert(c.erase(4) == 0);
80 assert(c.size() == 3);
81 assert(c.count(1) == 2);
82 assert(c.count(3) == 1);
83
84 assert(c.erase(1) == 2);
85 assert(c.size() == 1);
86 assert(c.count(3) == 1);
87
88 assert(c.erase(1) == 0);
89 assert(c.size() == 1);
90 assert(c.count(3) == 1);
91
92 assert(c.erase(3) == 1);
93 assert(c.size() == 0);
94
95 assert(c.erase(3) == 0);
96 assert(c.size() == 0);
97 }
98#if TEST_STD_VER >= 11
99 {
100 typedef std::unordered_multiset<int, std::hash<int>,
101 std::equal_to<int>, min_allocator<int>> C;
102 typedef int P;
103 P a[] =
104 {
105 P(1),
106 P(2),
107 P(3),
108 P(4),
109 P(1),
110 P(2)
111 };
112 C c(a, a + sizeof(a)/sizeof(a[0]));
113 assert(c.erase(5) == 0);
114 assert(c.size() == 6);
115 assert(c.count(1) == 2);
116 assert(c.count(2) == 2);
117 assert(c.count(3) == 1);
118 assert(c.count(4) == 1);
119
120 assert(c.erase(2) == 2);
121 assert(c.size() == 4);
122 assert(c.count(1) == 2);
123 assert(c.count(3) == 1);
124 assert(c.count(4) == 1);
125
126 assert(c.erase(2) == 0);
127 assert(c.size() == 4);
128 assert(c.count(1) == 2);
129 assert(c.count(3) == 1);
130 assert(c.count(4) == 1);
131
132 assert(c.erase(4) == 1);
133 assert(c.size() == 3);
134 assert(c.count(1) == 2);
135 assert(c.count(3) == 1);
136
137 assert(c.erase(4) == 0);
138 assert(c.size() == 3);
139 assert(c.count(1) == 2);
140 assert(c.count(3) == 1);
141
142 assert(c.erase(1) == 2);
143 assert(c.size() == 1);
144 assert(c.count(3) == 1);
145
146 assert(c.erase(1) == 0);
147 assert(c.size() == 1);
148 assert(c.count(3) == 1);
149
150 assert(c.erase(3) == 1);
151 assert(c.size() == 0);
152
153 assert(c.erase(3) == 0);
154 assert(c.size() == 0);
155 }
156 {
157 typedef std::unordered_multiset<int> C;
158 C m, m2;
159 for ( int i = 0; i < 10; ++i ) {
160 m.insert(i); m.insert(i);
161 m2.insert(i); m2.insert(i);
162 }
163
164 C::iterator i = m2.begin();
165 int ctr = 0;
166 while (i != m2.end()) {
167 if (ctr++ % 2 == 0)
168 m2.erase(i++);
169 else
170 ++i;
171 }
172
173 assert (only_deletions (m, m2));
174 }
175#endif
176
177 return 0;
178}
179

source code of libcxx/test/std/containers/unord/unord.multiset/erase_key.pass.cpp