Debian Codegraph @

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