libstdc++: Fix missing/misplaced { dg-options "-std=gnu++20" } in tests
[official-gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_set / operations / 1.cc
blobdea21df7bcbbe3ea7d8a6275223fbee311f229b2
1 // Copyright (C) 2021-2023 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-options "-std=gnu++20" }
19 // { dg-do run { target c++20 } }
21 #include <unordered_set>
23 #ifndef __cpp_lib_generic_unordered_lookup
24 # error "Feature-test macro for generic lookup missing in <unordered_set>"
25 #elif __cpp_lib_generic_unordered_lookup < 201811L
26 # error "Feature-test macro for generic lookup has wrong value in <unordered_set>"
27 #endif
29 #include <testsuite_hooks.h>
31 struct Equal
33 typedef void is_transparent;
35 bool operator()(int i, long l) const { return i == l; }
36 bool operator()(long l, int i) const { return l == i; }
37 bool operator()(int i, int j) const { ++count; return i == j; }
39 static int count;
42 int Equal::count = 0;
44 struct Hash
46 typedef void is_transparent;
48 std::size_t operator()(int i) const { ++count; return i; }
49 std::size_t operator()(long l) const { return l; }
51 static int count;
54 int Hash::count = 0;
56 using test_type = std::unordered_set<int, Hash, Equal>;
58 test_type x{ 1, 3, 5 };
59 const test_type& cx = x;
61 void
62 test01()
64 Hash::count = 0;
65 Equal::count = 0;
67 VERIFY( x.contains(1L) );
69 auto it = x.find(1L);
70 VERIFY( it != x.end() && *it == 1 );
71 it = x.find(2L);
72 VERIFY( it == x.end() );
74 auto cit = cx.find(3L);
75 VERIFY( cit != cx.end() && *cit == 3 );
76 cit = cx.find(2L);
77 VERIFY( cit == cx.end() );
79 VERIFY( Hash::count == 0 );
80 VERIFY( Equal::count == 0 );
82 static_assert(std::is_same<decltype(it), test_type::iterator>::value,
83 "find returns iterator");
84 static_assert(std::is_same<decltype(cit), test_type::const_iterator>::value,
85 "const find returns const_iterator");
88 void
89 test02()
91 Hash::count = 0;
92 Equal::count = 0;
94 auto n = x.count(1L);
95 VERIFY( n == 1 );
96 n = x.count(2L);
97 VERIFY( n == 0 );
99 auto cn = cx.count(3L);
100 VERIFY( cn == 1 );
101 cn = cx.count(2L);
102 VERIFY( cn == 0 );
104 VERIFY( Hash::count == 0 );
105 VERIFY( Equal::count == 0 );
108 void
109 test03()
111 Hash::count = 0;
112 Equal::count = 0;
114 auto it = x.equal_range(1L);
115 VERIFY( it.first != it.second && *it.first == 1 );
116 it = x.equal_range(2L);
117 VERIFY( it.first == it.second && it.first == x.end() );
119 auto cit = cx.equal_range(1L);
120 VERIFY( cit.first != cit.second && *cit.first == 1 );
121 cit = cx.equal_range(2L);
122 VERIFY( cit.first == cit.second && cit.first == cx.end() );
124 VERIFY( Hash::count == 0 );
125 VERIFY( Equal::count == 0 );
127 using pair = std::pair<test_type::iterator, test_type::iterator>;
128 static_assert(std::is_same<decltype(it), pair>::value,
129 "equal_range returns pair<iterator, iterator>");
130 using cpair = std::pair<test_type::const_iterator, test_type::const_iterator>;
131 static_assert(std::is_same<decltype(cit), cpair>::value,
132 "const equal_range returns pair<const_iterator, const_iterator>");
135 void
136 test04()
138 // https://gcc.gnu.org/ml/libstdc++/2015-01/msg00176.html
139 // Verify the new function template overloads do not cause problems
140 // when the comparison function is not transparent.
141 struct I
143 int i;
144 operator int() const { return i; }
147 std::unordered_set<int> s;
148 I i = { };
149 s.find(i);
152 void
153 test05()
155 struct E
157 bool operator()(int l, int r) const { return l == r; }
159 struct Partition { };
161 bool operator()(int l, Partition) const { return l < 6; }
162 bool operator()(Partition, int r) const { return r > 3; }
164 using is_transparent = void;
167 struct H
169 size_t
170 operator()(int x) const
171 { return (size_t)(x / 10); }
173 size_t
174 operator()(E::Partition) const
175 { return 0; }
177 using is_transparent = void;
180 std::unordered_set<int, H, E> s{ 1, 2, 3, 4, 5 };
182 auto n = s.count(E::Partition{});
183 VERIFY( n == 2 );
187 main()
189 test01();
190 test02();
191 test03();
192 test04();
193 test05();