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_set
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_set<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() == 4);
57 assert(c.count(1) == 1);
58 assert(c.count(2) == 1);
59 assert(c.count(3) == 1);
60 assert(c.count(4) == 1);
61
62 assert(c.erase(2) == 1);
63 assert(c.size() == 3);
64 assert(c.count(1) == 1);
65 assert(c.count(3) == 1);
66 assert(c.count(4) == 1);
67
68 assert(c.erase(2) == 0);
69 assert(c.size() == 3);
70 assert(c.count(1) == 1);
71 assert(c.count(3) == 1);
72 assert(c.count(4) == 1);
73
74 assert(c.erase(4) == 1);
75 assert(c.size() == 2);
76 assert(c.count(1) == 1);
77 assert(c.count(3) == 1);
78
79 assert(c.erase(4) == 0);
80 assert(c.size() == 2);
81 assert(c.count(1) == 1);
82 assert(c.count(3) == 1);
83
84 assert(c.erase(1) == 1);
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_set<int, std::hash<int>, std::equal_to<int>, min_allocator<int>> C;
101 typedef int P;
102 P a[] =
103 {
104 P(1),
105 P(2),
106 P(3),
107 P(4),
108 P(1),
109 P(2)
110 };
111 C c(a, a + sizeof(a)/sizeof(a[0]));
112 assert(c.erase(5) == 0);
113 assert(c.size() == 4);
114 assert(c.count(1) == 1);
115 assert(c.count(2) == 1);
116 assert(c.count(3) == 1);
117 assert(c.count(4) == 1);
118
119 assert(c.erase(2) == 1);
120 assert(c.size() == 3);
121 assert(c.count(1) == 1);
122 assert(c.count(3) == 1);
123 assert(c.count(4) == 1);
124
125 assert(c.erase(2) == 0);
126 assert(c.size() == 3);
127 assert(c.count(1) == 1);
128 assert(c.count(3) == 1);
129 assert(c.count(4) == 1);
130
131 assert(c.erase(4) == 1);
132 assert(c.size() == 2);
133 assert(c.count(1) == 1);
134 assert(c.count(3) == 1);
135
136 assert(c.erase(4) == 0);
137 assert(c.size() == 2);
138 assert(c.count(1) == 1);
139 assert(c.count(3) == 1);
140
141 assert(c.erase(1) == 1);
142 assert(c.size() == 1);
143 assert(c.count(3) == 1);
144
145 assert(c.erase(1) == 0);
146 assert(c.size() == 1);
147 assert(c.count(3) == 1);
148
149 assert(c.erase(3) == 1);
150 assert(c.size() == 0);
151
152 assert(c.erase(3) == 0);
153 assert(c.size() == 0);
154 }
155 {
156 typedef std::unordered_set<int> C;
157 C m, m2;
158 for ( int i = 0; i < 10; ++i ) {
159 m.insert(i);
160 m2.insert(i);
161 }
162
163 C::iterator i = m2.begin();
164 int ctr = 0;
165 while (i != m2.end()) {
166 if (ctr++ % 2 == 0)
167 m2.erase(i++);
168 else
169 ++i;
170 }
171
172 assert (only_deletions (m, m2));
173 }
174#endif
175
176 return 0;
177}
178

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