xref: /llvm-project/libcxx/test/std/containers/unord/unord.set/erase_const_iter.pass.cpp (revision fb855eb941b6d740cc6560297d0b4d3201dcaf9f)
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 // iterator erase(const_iterator p)
16 
17 #include <unordered_set>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 #include "min_allocator.h"
22 
23 struct TemplateConstructor
24 {
25     template<typename T>
TemplateConstructorTemplateConstructor26     TemplateConstructor (const T&) {}
27 };
28 
operator ==(const TemplateConstructor &,const TemplateConstructor &)29 bool operator==(const TemplateConstructor&, const TemplateConstructor&) { return false; }
operator ()Hash30 struct Hash { std::size_t operator() (const TemplateConstructor &) const { return 0; } };
31 
main(int,char **)32 int main(int, char**)
33 {
34     {
35         typedef std::unordered_set<int> C;
36         typedef int P;
37         P a[] =
38         {
39             P(1),
40             P(2),
41             P(3),
42             P(4),
43             P(1),
44             P(2)
45         };
46         C c(a, a + sizeof(a)/sizeof(a[0]));
47         C::const_iterator i = c.find(2);
48         C::const_iterator i_next = i;
49         ++i_next;
50         C::iterator j = c.erase(i);
51         assert(j == i_next);
52 
53         assert(c.size() == 3);
54         assert(c.count(1) == 1);
55         assert(c.count(3) == 1);
56         assert(c.count(4) == 1);
57     }
58 #if TEST_STD_VER >= 11
59     {
60         typedef std::unordered_set<int, std::hash<int>, std::equal_to<int>, min_allocator<int>> C;
61         typedef int P;
62         P a[] =
63         {
64             P(1),
65             P(2),
66             P(3),
67             P(4),
68             P(1),
69             P(2)
70         };
71         C c(a, a + sizeof(a)/sizeof(a[0]));
72         C::const_iterator i = c.find(2);
73         C::const_iterator i_next = i;
74         ++i_next;
75         C::iterator j = c.erase(i);
76         assert(j == i_next);
77 
78         assert(c.size() == 3);
79         assert(c.count(1) == 1);
80         assert(c.count(3) == 1);
81         assert(c.count(4) == 1);
82     }
83 #endif
84 #if TEST_STD_VER >= 14
85     {
86     //  This is LWG #2059
87         typedef TemplateConstructor T;
88         typedef std::unordered_set<T, Hash> C;
89         typedef C::iterator I;
90 
91         C m;
92         T a{0};
93         I it = m.find(a);
94         if (it != m.end())
95             m.erase(it);
96     }
97 #endif
98 
99   return 0;
100 }
101