Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / containers / unord / unord.set / erase_key.pass.cpp
bloba50f56b0a3232e527e756787407209f3e0f03242
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 //===----------------------------------------------------------------------===//
9 // <unordered_set>
11 // template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
12 // class Alloc = allocator<Value>>
13 // class unordered_set
15 // size_type erase(const key_type& k);
17 #include <unordered_set>
18 #include <string>
19 #include <cassert>
21 #include "test_macros.h"
22 #include "min_allocator.h"
24 #if TEST_STD_VER >= 11
25 template <typename Unordered>
26 bool only_deletions ( const Unordered &whole, const Unordered &part ) {
27 typename Unordered::const_iterator w = whole.begin();
28 typename Unordered::const_iterator p = part.begin();
30 while ( w != whole.end () && p != part.end()) {
31 if ( *w == *p )
32 p++;
33 w++;
36 return p == part.end();
38 #endif
40 int main(int, char**)
43 typedef std::unordered_set<int> C;
44 typedef int P;
45 P a[] =
47 P(1),
48 P(2),
49 P(3),
50 P(4),
51 P(1),
52 P(2)
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);
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);
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);
74 assert(c.erase(4) == 1);
75 assert(c.size() == 2);
76 assert(c.count(1) == 1);
77 assert(c.count(3) == 1);
79 assert(c.erase(4) == 0);
80 assert(c.size() == 2);
81 assert(c.count(1) == 1);
82 assert(c.count(3) == 1);
84 assert(c.erase(1) == 1);
85 assert(c.size() == 1);
86 assert(c.count(3) == 1);
88 assert(c.erase(1) == 0);
89 assert(c.size() == 1);
90 assert(c.count(3) == 1);
92 assert(c.erase(3) == 1);
93 assert(c.size() == 0);
95 assert(c.erase(3) == 0);
96 assert(c.size() == 0);
98 #if TEST_STD_VER >= 11
100 typedef std::unordered_set<int, std::hash<int>, std::equal_to<int>, min_allocator<int>> C;
101 typedef int P;
102 P a[] =
104 P(1),
105 P(2),
106 P(3),
107 P(4),
108 P(1),
109 P(2)
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);
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);
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);
131 assert(c.erase(4) == 1);
132 assert(c.size() == 2);
133 assert(c.count(1) == 1);
134 assert(c.count(3) == 1);
136 assert(c.erase(4) == 0);
137 assert(c.size() == 2);
138 assert(c.count(1) == 1);
139 assert(c.count(3) == 1);
141 assert(c.erase(1) == 1);
142 assert(c.size() == 1);
143 assert(c.count(3) == 1);
145 assert(c.erase(1) == 0);
146 assert(c.size() == 1);
147 assert(c.count(3) == 1);
149 assert(c.erase(3) == 1);
150 assert(c.size() == 0);
152 assert(c.erase(3) == 0);
153 assert(c.size() == 0);
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);
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;
172 assert (only_deletions (m, m2));
174 #endif
176 return 0;