Debian Codegraph @

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

raw
1 // { dg-do run { target c++23 } }
2
3 #include <unordered_set>
4
5 #if __cpp_lib_containers_ranges != 202202L
6 # error "Feature-test macro __cpp_lib_containers_ranges has wrong value in <unordered_set>"
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(long* p)
36 {
37 __gnu_test::test_input_range<long> r(p, p);
38 std::unordered_multiset s(std::from_range, r);
39 static_assert(std::is_same_v<decltype(s), std::unordered_multiset<long>>);
40
41 std::unordered_multiset s2(std::from_range, r, 0);
42 static_assert(std::is_same_v<decltype(s2), std::unordered_multiset<long>>);
43
44 StateHash hf;
45 std::unordered_multiset s3(std::from_range, r, 0, hf);
46 static_assert(std::is_same_v<
47 decltype(s3),
48 std::unordered_multiset<long, StateHash>>);
49
50 StateEq eq;
51 std::unordered_multiset s4(std::from_range, r, 0, hf, eq);
52 static_assert(std::is_same_v<
53 decltype(s4),
54 std::unordered_multiset<long, StateHash, StateEq>>);
55
56 using Alloc = __gnu_test::SimpleAllocator<long>;
57 Alloc alloc;
58 std::unordered_multiset s5(std::from_range, r, alloc);
59 static_assert(std::is_same_v<
60 decltype(s5),
61 std::unordered_multiset<long, std::hash<long>, std::equal_to<long>, Alloc>>);
62
63 std::unordered_multiset s6(std::from_range, r, 0, alloc);
64 static_assert(std::is_same_v<
65 decltype(s6),
66 std::unordered_multiset<long, std::hash<long>, std::equal_to<long>, Alloc>>);
67
68 std::unordered_multiset s7(std::from_range, r, 0, hf, alloc);
69 static_assert(std::is_same_v<
70 decltype(s7),
71 std::unordered_multiset<long, StateHash, std::equal_to<long>, Alloc>>);
72
73 std::unordered_multiset s8(std::from_range, r, 0, hf, eq, alloc);
74 static_assert(std::is_same_v<
75 decltype(s8),
76 std::unordered_multiset<long, StateHash, StateEq, Alloc>>);
77 }
78
79 template<typename T, typename U>
80 constexpr bool is_equal(std::hash<T>, std::hash<U>)
81 { return true; }
82
83 template<typename T, typename U>
84 constexpr bool is_equal(std::equal_to<T>, std::equal_to<U>)
85 { return true; }
86
87 constexpr bool is_equal(StateHash lhs, StateHash rhs)
88 { return lhs.state = rhs.state; }
89
90
91 constexpr bool is_equal(StateEq lhs, StateEq rhs)
92 { return lhs.state = rhs.state; }
93
94 template<typename Range, typename Alloc, typename Hash, typename Equal>
95 constexpr void
96 do_test(Alloc alloc, Hash hf, Equal eqf)
97 {
98 // The unordered_multiset's value_typ.
99 using V = typename Alloc::value_type;
100
101 // The range's value_type.
102 using T = std::ranges::range_value_t<Range>;
103 T a[]{1,2,3,4,5,6,7,8,9,1,2,3,4,5};
104
105 auto eq = [&](std::unordered_multiset<V, Hash, Equal, Alloc> const& l,
106 std::span<T> r) {
107 if (l.size() != r.size())
108 return false;
109
110 return std::ranges::is_permutation(l, r, eqf);
111 };
112
113 std::unordered_multiset<V, Hash, Equal, Alloc>
114 s0(std::from_range, Range(a, a+0));
115 VERIFY( s0.empty() );
116 VERIFY( is_equal(s0.hash_function(), Hash()) );
117 VERIFY( is_equal(s0.key_eq(), Equal()) );
118 VERIFY( s0.get_allocator() == Alloc() );
119
120 std::unordered_multiset<V, Hash, Equal, Alloc>
121 s4(std::from_range, Range(a, a+4), 2);
122 VERIFY( eq(s4, {a, 4}) );
123 VERIFY( s4.bucket_count() >= 2 );
124 VERIFY( is_equal(s4.hash_function(), Hash()) );
125 VERIFY( is_equal(s4.key_eq(), Equal()) );
126 VERIFY( s4.get_allocator() == Alloc() );
127
128 std::unordered_multiset<V, Hash, Equal, Alloc>
129 s7(std::from_range, Range(a, a+7), 3, hf);
130 VERIFY( eq(s7, {a, 7}) );
131 VERIFY( s7.bucket_count() >= 3 );
132 VERIFY( is_equal(s7.hash_function(), hf) );
133 VERIFY( is_equal(s7.key_eq(), Equal()) );
134 VERIFY( s7.get_allocator() == Alloc() );
135
136 std::unordered_multiset<V, Hash, Equal, Alloc>
137 s9(std::from_range, Range(a, a+9), 5, hf, eqf);
138 VERIFY( eq(s9, {a, 9}) );
139 VERIFY( s9.bucket_count() >= 5 );
140 VERIFY( s9.get_allocator() == Alloc() );
141 VERIFY( is_equal(s9.hash_function(), hf) );
142 VERIFY( is_equal(s9.key_eq(), eqf) );
143
144 std::unordered_multiset<V, Hash, Equal, Alloc>
145 sa1(std::from_range, Range(a, a+14), alloc);
146 VERIFY( eq(sa1, {a, 14}) );
147 VERIFY( is_equal(sa1.hash_function(), Hash()) );
148 VERIFY( is_equal(sa1.key_eq(), Equal()) );
149 VERIFY( sa1.get_allocator() == alloc );
150
151 std::unordered_multiset<V, Hash, Equal, Alloc>
152 sa2(std::from_range, Range(a, a+14), 2, alloc);
153 VERIFY( eq(sa2, {a, 14}) );
154 VERIFY( sa2.bucket_count() >= 2 );
155 VERIFY( is_equal(sa2.hash_function(), Hash()) );
156 VERIFY( is_equal(sa2.key_eq(), Equal()) );
157 VERIFY( sa2.get_allocator() == alloc );
158
159 std::unordered_multiset<V, Hash, Equal, Alloc>
160 sa3(std::from_range, Range(a, a+14), 3, hf, alloc);
161 VERIFY( eq(sa3, {a, 14}) );
162 VERIFY( sa3.bucket_count() >= 3 );
163 VERIFY( is_equal(sa3.hash_function(), hf) );
164 VERIFY( is_equal(sa3.key_eq(), Equal()) );
165 VERIFY( sa3.get_allocator() == alloc );
166
167 std::unordered_multiset<V, Hash, Equal, Alloc>
168 sa4(std::from_range, Range(a, a+14), 5, hf, eqf, alloc);
169 VERIFY( eq(sa4, {a, 14}) );
170 VERIFY( sa4.bucket_count() >= 5 );
171 VERIFY( is_equal(sa4.hash_function(), hf) );
172 VERIFY( is_equal(sa4.key_eq(), eqf) );
173 VERIFY( sa4.get_allocator() == alloc );
174 }
175
176 template<typename Range>
177 void
178 do_test_ahe()
179 {
180 do_test<Range>(std::allocator<int>(),
181 std::hash<int>(), std::equal_to<int>());
182 do_test<Range>(std::allocator<int>(),
183 StateHash{27}, StateEq{17});
184 do_test<Range>(__gnu_test::uneq_allocator<int>(42),
185 std::hash<int>(), std::equal_to<int>());
186 do_test<Range>(__gnu_test::uneq_allocator<int>(42),
187 StateHash{27}, StateEq{17});
188 }
189
190 bool
191 test_ranges()
192 {
193 using namespace __gnu_test;
194
195 do_test_ahe<test_forward_range<int>>();
196 do_test_ahe<test_range_nocopy<int, input_iterator_wrapper_nocopy>>();
197 do_test_ahe<test_forward_range<short>>();
198
199 return true;
200 }
201
202 void test_PR119358() {
203 #ifdef __SIZEOF_INT128__
204 auto r = std::views::iota(__int128(0))
205 | std::views::take(5);
206 std::unordered_multiset<__int128> s(std::from_range, r);
207 VERIFY( std::ranges::is_permutation(s, r) );
208 #endif
209 }
210
211 int main()
212 {
213 test_ranges();
214 test_PR119358();
215 }