Debian Codegraph @

libstdc++-v3/testsuite/23_containers/unordered_map/operations/1.cc

raw
1 // Copyright (C) 2021-2025 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.
8
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.
13
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/>.
17
18 // { dg-do run { target c++20 } }
19 // { dg-add-options no_pch }
20
21 #include <unordered_map>
22
23 #ifndef __cpp_lib_generic_unordered_lookup
24 # error "Feature-test macro for generic lookup missing in <unordered_map>"
25 #elif __cpp_lib_generic_unordered_lookup < 201811L
26 # error "Feature-test macro for generic lookup has wrong value in <unordered_map>"
27 #endif
28
29 #include <testsuite_hooks.h>
30
31 struct Equal
32 {
33 typedef void is_transparent;
34
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; }
38
39 static int count;
40 };
41
42 int Equal::count = 0;
43
44 struct Hash
45 {
46 typedef void is_transparent;
47
48 std::size_t operator()(int i) const { ++count; return i; }
49 std::size_t operator()(long l) const { return l; }
50
51 static int count;
52 };
53
54 int Hash::count = 0;
55
56 using test_type = std::unordered_map<int, char, Hash, Equal>;
57
58 test_type x{ { 1, '2' }, { 3, '4' } };
59 const test_type& cx = x;
60
61 void
62 test01()
63 {
64 Hash::count = 0;
65 Equal::count = 0;
66
67 VERIFY( x.contains(1L) );
68
69 auto it = x.find(1L);
70 VERIFY( it != x.end() && it->second == '2' );
71 it = x.find(2L);
72 VERIFY( it == x.end() );
73
74 auto cit = cx.find(3L);
75 VERIFY( cit != cx.end() && cit->second == '4' );
76 cit = cx.find(2L);
77 VERIFY( cit == cx.end() );
78
79 VERIFY( Hash::count == 0 );
80 VERIFY( Equal::count == 0 );
81
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");
86 }
87
88 void
89 test02()
90 {
91 Hash::count = 0;
92 Equal::count = 0;
93
94 auto n = x.count(1L);
95 VERIFY( n == 1 );
96 n = x.count(2L);
97 VERIFY( n == 0 );
98
99 auto cn = cx.count(3L);
100 VERIFY( cn == 1 );
101 cn = cx.count(2L);
102 VERIFY( cn == 0 );
103
104 VERIFY( Hash::count == 0 );
105 VERIFY( Equal::count == 0 );
106 }
107
108 void
109 test03()
110 {
111 Hash::count = 0;
112 Equal::count = 0;
113
114 auto it = x.equal_range(1L);
115 VERIFY( it.first != it.second && it.first->second == '2' );
116 it = x.equal_range(2L);
117 VERIFY( it.first == it.second && it.first == x.end() );
118
119 auto cit = cx.equal_range(1L);
120 VERIFY( cit.first != cit.second && cit.first->second == '2' );
121 cit = cx.equal_range(2L);
122 VERIFY( cit.first == cit.second && cit.first == cx.end() );
123
124 VERIFY( Hash::count == 0 );
125 VERIFY( Equal::count == 0 );
126
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>");
133 }
134
135 void
136 test04()
137 {
138 struct E
139 {
140 bool operator()(int l, int r) const { return l == r; }
141
142 struct Partition { };
143
144 bool operator()(int l, Partition) const { return l < 6; }
145 bool operator()(Partition, int r) const { return 3 < r; }
146
147 using is_transparent = void;
148 };
149
150 struct H
151 {
152 size_t
153 operator()(int x) const
154 { return 0; }
155
156 size_t
157 operator()(E::Partition) const
158 { return 0; }
159
160 using is_transparent = void;
161 };
162
163 std::unordered_map<int, int, H, E> m{ {1,0}, {2,0}, {3,0}, {4, 0}, {5, 0} };
164
165 auto n = m.count(E::Partition{});
166 VERIFY( n == 2 );
167 }
168
169 int
170 main()
171 {
172 test01();
173 test02();
174 test03();
175 test04();
176 }