Debian Codegraph @

libstdc++-v3/testsuite/23_containers/unordered_multimap/cons/from_range.cc

raw
1 // { dg-do run { target c++23 } }
2
3 #include <unordered_map>
4
5 #if __cpp_lib_containers_ranges != 202202L
6 # error "Feature-test macro __cpp_lib_containers_ranges has wrong value in <unordered_map>"
7 #endif
8
9 #include <algorithm>
10 #include <ranges>
11 #include <span>
12 #include <testsuite_allocator.h>
13 #include <testsuite_hooks.h>
14 #include <testsuite_iterators.h>
15
16 struct StateHash {
17 int state = 17;
18
19 template<typename T>
20 size_t operator()(T const& t) const {
21 return std::hash<T>()(t) + 43;
22 }
23 };
24
25 struct StateEq {
26 int state = 7;
27
28 template<typename T, typename U>
29 bool operator()(T const& l, U const & r) const {
30 return l == r;
31 }
32 };
33
34 void
35 test_deduction_guide()
36 {
37 __gnu_test::test_input_range<std::pair<long, float>> r(0, 0);
38 std::unordered_multimap m(std::from_range, r);
39 static_assert(std::is_same_v<
40 decltype(m),
41 std::unordered_multimap<long, float>>);
42
43 std::unordered_multimap m2(std::from_range, r, 0);
44 static_assert(std::is_same_v<
45 decltype(m2),
46 std::unordered_multimap<long, float>>);
47
48 StateHash hf;
49 std::unordered_multimap m3(std::from_range, r, 0, hf);
50 static_assert(std::is_same_v<
51 decltype(m3),
52 std::unordered_multimap<long, float, StateHash>>);
53
54 StateEq eq;
55 std::unordered_multimap m4(std::from_range, r, 0, hf, eq);
56 static_assert(std::is_same_v<
57 decltype(m4),
58 std::unordered_multimap<long, float, StateHash, StateEq>>);
59
60 using Alloc = __gnu_test::SimpleAllocator<std::pair<const long, float>>;
61 Alloc alloc;
62 std::unordered_multimap m5(std::from_range, r, alloc);
63 static_assert(std::is_same_v<
64 decltype(m5),
65 std::unordered_multimap<long, float,
66 std::hash<long>, std::equal_to<long>, Alloc>>);
67
68 std::unordered_multimap m6(std::from_range, r, 0, alloc);
69 static_assert(std::is_same_v<
70 decltype(m6),
71 std::unordered_multimap<long, float,
72 std::hash<long>, std::equal_to<long>, Alloc>>);
73
74 std::unordered_multimap m7(std::from_range, r, 0, hf, alloc);
75 static_assert(std::is_same_v<
76 decltype(m7),
77 std::unordered_multimap<long, float,
78 StateHash, std::equal_to<long>, Alloc>>);
79
80 std::unordered_multimap m8(std::from_range, r, 0, hf, eq, alloc);
81 static_assert(std::is_same_v<
82 decltype(m8),
83 std::unordered_multimap<long, float, StateHash, StateEq, Alloc>>);
84
85 __gnu_test::test_input_range<std::pair<const long, const float>> r2(0, 0);
86 std::unordered_multimap m9(std::from_range, r2);
87 static_assert(std::is_same_v<
88 decltype(m9),
89 std::unordered_multimap<long, const float>>);
90
91 // LWG4223: deduces map<const long&, float&>
92 // __gnu_test::test_input_range<std::pair<const long&, float&>> r3(0, 0);
93 // std::unordered_multimap m10(std::from_range, r3);
94
95 __gnu_test::test_input_range<std::tuple<long, float>> r4(0, 0);
96 std::unordered_multimap m11(std::from_range, r4);
97 static_assert(std::is_same_v<
98 decltype(m11),
99 std::unordered_multimap<long, float>>);
100 std::unordered_multimap it11(r4.begin(), r4.begin());
101 static_assert(std::is_same_v<
102 decltype(it11),
103 std::unordered_multimap<long, float>>);
104 }
105
106 template<typename T, typename U>
107 constexpr bool is_equal(std::hash<T>, std::hash<U>)
108 { return true; }
109
110 template<typename T, typename U>
111 constexpr bool is_equal(std::equal_to<T>, std::equal_to<U>)
112 { return true; }
113
114 constexpr bool is_equal(StateHash lhs, StateHash rhs)
115 { return lhs.state = rhs.state; }
116
117 constexpr bool is_equal(StateEq lhs, StateEq rhs)
118 { return lhs.state = rhs.state; }
119
120 template<typename Range, typename Alloc, typename Hash, typename Equal>
121 constexpr void
122 do_test(Alloc alloc, Hash hf, Equal eqf)
123 {
124 // The map's value_type, key_type and mapped_type.
125 using P = typename Alloc::value_type;
126 using K = typename P::first_type;
127 using V = typename P::second_type;
128
129 // The range's value_type.
130 using T = std::ranges::range_value_t<Range>;
131 T a[]{{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,0},
132 {1,1},{2,2},{3,3},{4,4},{5,5}};
133
134 auto eq = [&](std::unordered_multimap<K, V, Hash, Equal, Alloc> const& l,
135 std::span<T> r) {
136 if (l.size() != r.size())
137 return false;
138
139 return std::ranges::is_permutation(l, r);
140 };
141
142 std::unordered_multimap<K, V, Hash, Equal, Alloc>
143 m0(std::from_range, Range(a, a+0));
144 VERIFY( m0.empty() );
145 VERIFY( is_equal(m0.hash_function(), Hash()) );
146 VERIFY( is_equal(m0.key_eq(), Equal()) );
147 VERIFY( m0.get_allocator() == Alloc() );
148
149 std::unordered_multimap<K, V, Hash, Equal, Alloc>
150 m4(std::from_range, Range(a, a+4), 2);
151 VERIFY( eq(m4, {a, 4}) );
152 VERIFY( m4.bucket_count() >= 2 );
153 VERIFY( is_equal(m4.hash_function(), Hash()) );
154 VERIFY( is_equal(m4.key_eq(), Equal()) );
155 VERIFY( m4.get_allocator() == Alloc() );
156
157 std::unordered_multimap<K, V, Hash, Equal, Alloc>
158 m7(std::from_range, Range(a, a+7), 3, hf);
159 VERIFY( eq(m7, {a, 7}) );
160 VERIFY( m7.bucket_count() >= 3 );
161 VERIFY( is_equal(m7.hash_function(), hf) );
162 VERIFY( is_equal(m7.key_eq(), Equal()) );
163 VERIFY( m7.get_allocator() == Alloc() );
164
165 std::unordered_multimap<K, V, Hash, Equal, Alloc>
166 m9(std::from_range, Range(a, a+9), 5, hf, eqf);
167 VERIFY( eq(m9, {a, 9}) );
168 VERIFY( m9.bucket_count() >= 5 );
169 VERIFY( m9.get_allocator() == Alloc() );
170 VERIFY( is_equal(m9.hash_function(), hf) );
171 VERIFY( is_equal(m9.key_eq(), eqf) );
172
173 std::unordered_multimap<K, V, Hash, Equal, Alloc>
174 ma1(std::from_range, Range(a, a+14), alloc);
175 VERIFY( eq(ma1, {a, 14}) );
176 VERIFY( is_equal(ma1.hash_function(), Hash()) );
177 VERIFY( is_equal(ma1.key_eq(), Equal()) );
178 VERIFY( ma1.get_allocator() == alloc );
179
180 std::unordered_multimap<K, V, Hash, Equal, Alloc>
181 ma2(std::from_range, Range(a, a+14), 2, alloc);
182 VERIFY( eq(ma2, {a, 14}) );
183 VERIFY( ma2.bucket_count() >= 2 );
184 VERIFY( is_equal(ma2.hash_function(), Hash()) );
185 VERIFY( is_equal(ma2.key_eq(), Equal()) );
186 VERIFY( ma2.get_allocator() == alloc );
187
188 std::unordered_multimap<K, V, Hash, Equal, Alloc>
189 ma3(std::from_range, Range(a, a+14), 3, hf, alloc);
190 VERIFY( eq(ma3, {a, 14}) );
191 VERIFY( ma3.bucket_count() >= 3 );
192 VERIFY( is_equal(ma3.hash_function(), hf) );
193 VERIFY( is_equal(ma3.key_eq(), Equal()) );
194 VERIFY( ma3.get_allocator() == alloc );
195
196 std::unordered_multimap<K, V, Hash, Equal, Alloc>
197 ma4(std::from_range, Range(a, a+14), 5, hf, eqf, alloc);
198 VERIFY( eq(ma4, {a, 14}) );
199 VERIFY( ma4.bucket_count() >= 5 );
200 VERIFY( is_equal(ma4.hash_function(), hf) );
201 VERIFY( is_equal(ma4.key_eq(), eqf) );
202 VERIFY( ma4.get_allocator() == alloc );
203 }
204
205 template<typename Range>
206 void
207 do_test_ahe()
208 {
209 do_test<Range>(std::allocator<std::pair<const int, double>>(),
210 std::hash<int>(), std::equal_to<int>());
211 do_test<Range>(std::allocator<std::pair<const int, double>>(),
212 StateHash{27}, StateEq{17});
213 do_test<Range>(__gnu_test::uneq_allocator<std::pair<const int, double>>(42),
214 std::hash<int>(), std::equal_to<int>());
215 do_test<Range>(__gnu_test::uneq_allocator<std::pair<const int, double>>(42),
216 StateHash{27}, StateEq{17});
217 }
218
219 struct MyPair {
220 long x;
221 long y;
222
223 constexpr operator std::pair<int const, double>() const
224 { return {x, y}; }
225
226 friend bool operator==(MyPair, MyPair) = default;
227 constexpr friend bool operator==(MyPair lhs, std::pair<int const, double> rhs)
228 { return (lhs.x == rhs.first) && (lhs.y == rhs.second); }
229 };
230
231 bool
232 test_ranges()
233 {
234 using namespace __gnu_test;
235
236 do_test_ahe<test_forward_range<std::pair<int, double>>>();
237 do_test_ahe<test_range_nocopy<std::pair<int, double>, input_iterator_wrapper_nocopy>>();
238 do_test_ahe<test_forward_range<std::pair<short, float>>>();
239 do_test_ahe<test_forward_range<std::tuple<int, double>>>();
240 do_test_ahe<test_forward_range<MyPair>>();
241
242 return true;
243 }
244
245 void test_PR119358() {
246 #ifdef __SIZEOF_INT128__
247 auto r = std::views::iota(__int128(0))
248 | std::views::take(5);
249 auto z = std::views::zip(r, r);
250 std::unordered_multimap<__int128, __int128> m(std::from_range, z);
251 VERIFY( std::ranges::is_permutation(m, z) );
252 #endif
253 }
254
255 int main()
256 {
257 test_ranges();
258 test_PR119358();
259 }