Debian Codegraph @

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