Debian Codegraph @

libstdc++-v3/testsuite/std/ranges/conv/1.cc

raw
1 // { dg-do run { target c++23 } }
2
3 // C++23 26.5.7 Range conversions [range.utility.conv]
4
5 #include <ranges>
6 #include <vector>
7 #include <string>
8 #include <deque>
9 #include <list>
10 #include <forward_list>
11 #include <map>
12 #include <testsuite_hooks.h>
13 #include <testsuite_allocator.h>
14 #include <testsuite_iterators.h>
15 #include <unordered_map>
16
17 void
18 test_p1206r7_examples()
19 {
20 using Alloc = __gnu_test::uneq_allocator<int>;
21 const Alloc alloc(303);
22 const std::map<int, const char*> m{{1, "one"}, {2, "two"}, {3, "three"}};
23 namespace ranges = std::ranges;
24
25 auto l = std::views::iota(1, 10);
26 // create a vector with the elements of l
27 auto vec = ranges::to<std::vector<int>>(l); // or vector{std::from_range, l};
28 //Specify an allocator
29 auto b = ranges::to<std::vector<int, Alloc>>(l, alloc); // or vector{std::from_range, l, alloc};
30 //deducing value_type
31 auto c = ranges::to<std::vector>(l);
32 // explicit conversion int -> long
33 auto d = ranges::to<std::vector<long>>(l);
34 //Supports converting associative container to sequence containers
35 auto f = ranges::to<std::vector>(m);
36 //Supports converting sequence containers to associative ones
37 auto g = ranges::to<std::map>(f);
38 //Pipe syntax
39 auto g2 = l | ranges::views::take(42) | ranges::to<std::vector>();
40 //Pipe syntax with allocator
41 auto h = l | ranges::views::take(42) | ranges::to<std::vector>(alloc);
42 //The pipe syntax also support specifying the type and conversions
43 auto i = l | ranges::views::take(42) | ranges::to<std::vector<long>>();
44 // Nested ranges
45 std::list<std::forward_list<int>> lst = {{0, 1, 2, 3}, {4, 5, 6, 7}};
46 auto vec1 = ranges::to<std::vector<std::vector<int>>>(lst);
47 auto vec2 = ranges::to<std::vector<std::deque<double>>>(lst);
48
49 VERIFY( vec == std::vector<int>(std::ranges::begin(l), std::ranges::end(l)) );
50 static_assert(std::is_same_v<decltype(b), std::vector<int, Alloc>>);
51 VERIFY( b == (std::vector<int, Alloc>(vec.begin(), vec.end())) );
52 VERIFY( b.get_allocator() == alloc );
53 static_assert(std::is_same_v<decltype(c), std::vector<int>>);
54 VERIFY( c == vec );
55 static_assert(std::is_same_v<decltype(d), std::vector<long>>);
56 VERIFY( d == std::vector<long>(vec.begin(), vec.end()) );
57 VERIFY( g == m );
58 static_assert(std::is_same_v<decltype(g2), std::vector<int>>);
59 VERIFY( g2 == vec );
60 static_assert(std::is_same_v<decltype(h), std::vector<int, Alloc>>);
61 VERIFY( h == b );
62 VERIFY( h.get_allocator() == alloc );
63 VERIFY( i == d );
64 static_assert(std::is_same_v<decltype(vec1), std::vector<std::vector<int>>>);
65 VERIFY( vec1[1][1] == 5 );
66 static_assert(std::is_same_v<decltype(vec2), std::vector<std::deque<double>>>);
67 VERIFY( vec2[1][2] == 6.0 );
68 }
69
70 void
71 test_example_1()
72 {
73 using namespace std;
74 using ranges::to;
75
76 // Example 1 from C++23 [range.utility.conv.general]
77 string_view str = "the quick brown fox";
78 auto words = views::split(str, ' ') | to<vector<string>>();
79
80 VERIFY( (is_same_v<decltype(words), vector<string>>) );
81 VERIFY( words == vector<string>({"the", "quick", "brown", "fox"}) );
82 }
83
84 template<typename C>
85 struct Cont1
86 {
87 template<typename R, typename... Args>
88 requires std::constructible_from<C, R&, Args&...>
89 Cont1(R&& r, Args&&... args)
90 : c(r, args...)
91 { }
92
93 C c;
94 };
95
96 void
97 test_2_1_1()
98 {
99 // (2.1.1) constructible_from<C, R, Args...>
100
101 std::vector<int> v{1, 2, 3, 4};
102 auto v2 = std::ranges::to<std::vector<int>>(v);
103 static_assert(std::is_same_v<decltype(v2), decltype(v)>);
104 VERIFY( v2 == v );
105
106 std::initializer_list<int> il{5, 6, 7, 8};
107 v2 = std::ranges::to<std::vector<int>>(il);
108 VERIFY( v2 == std::vector<int>(il) );
109
110 v2 = std::ranges::to<std::vector<int>>(il, std::allocator<int>{});
111 VERIFY( v2 == std::vector<int>(il) );
112
113 using Alloc = __gnu_test::uneq_allocator<int>;
114 using V = std::vector<int, Alloc>;
115
116 V v3({10, 11, 12, 13}, Alloc(14));
117 auto v4 = std::ranges::to<V>(v3);
118 static_assert(std::is_same_v<decltype(v4), V>);
119 VERIFY( v4 == v3 );
120 VERIFY( v4.get_allocator() == v3.get_allocator() );
121
122 auto v5 = std::ranges::to<V>(v3, Alloc(33));
123 VERIFY( v5 == v3 );
124 VERIFY( v5.get_allocator() == Alloc(33) );
125
126 auto v6 = std::ranges::to<V>(il, Alloc(44));
127 VERIFY( v6 == V(il) );
128 VERIFY( v6.get_allocator() == Alloc(44) );
129
130 auto c = std::ranges::to<Cont1<V>>(V{1, 2, 3});
131 static_assert(std::is_same_v<decltype(c), Cont1<V>>);
132 VERIFY( c.c == V({1, 2, 3}) );
133
134 auto c2 = std::ranges::to<Cont1<V>>(V{4, 5, 6}, Alloc(55));
135 static_assert(std::is_same_v<decltype(c2), Cont1<V>>);
136 VERIFY( c2.c == V({4, 5, 6}) );
137 VERIFY( c2.c.get_allocator() == Alloc(55) );
138
139 auto c3 = std::ranges::to<Cont1<V>>(il, Alloc(66));
140 static_assert(std::is_same_v<decltype(c2), Cont1<V>>);
141 VERIFY( c3.c == V(v2.begin(), v2.end()) );
142 VERIFY( c3.c.get_allocator() == Alloc(66) );
143 }
144
145 template<typename C>
146 struct Cont2
147 {
148 template<typename R, typename... Args>
149 requires std::constructible_from<C, R&, Args&...>
150 Cont2(std::from_range_t, R&& r, Args&&... args)
151 : c(r, args...)
152 { }
153
154 C c;
155 };
156
157 void
158 test_2_1_2()
159 {
160 // (2.1.2) constructible_from<C, from_range_t, R, Args...>
161
162 using Alloc = __gnu_test::uneq_allocator<int>;
163 using V = std::vector<int, Alloc>;
164 auto c = std::ranges::to<Cont2<V>>(V{1, 2, 3});
165 static_assert(std::is_same_v<decltype(c), Cont2<V>>);
166 VERIFY( c.c == V({1, 2, 3}) );
167
168 auto c2 = std::ranges::to<Cont2<V>>(V{4, 5, 6}, Alloc(7));
169 static_assert(std::is_same_v<decltype(c2), Cont2<V>>);
170 VERIFY( c2.c == V({4, 5, 6}) );
171 VERIFY( c2.c.get_allocator() == Alloc(7) );
172 }
173
174 template<typename C>
175 struct Cont3
176 {
177 template<typename It, typename Sent, typename... Args>
178 requires std::same_as<It, Sent>
179 && std::constructible_from<C, It&, Sent&, Args&...>
180 Cont3(It first, Sent last, Args&&... args)
181 : c(first, last, args...)
182 { }
183
184 C c;
185 };
186
187 void
188 test_2_1_3()
189 {
190 // (2.1.3) constructible_from<C, iterator_t<R>, sentinel_t<R<, Args...>
191
192 using Alloc = __gnu_test::uneq_allocator<int>;
193 using V = std::vector<int, Alloc>;
194
195 std::list<unsigned> l{1u, 2u, 3u};
196 auto c = std::ranges::to<Cont3<V>>(l);
197 static_assert(std::is_same_v<decltype(c), Cont3<V>>);
198 VERIFY( c.c == V(l.begin(), l.end()) );
199
200 std::list<long> l2{4l, 5l, 6l};
201 auto c2 = std::ranges::to<Cont3<V>>(l2, Alloc(78));
202 static_assert(std::is_same_v<decltype(c2), Cont3<V>>);
203 VERIFY( c2.c == V(l2.begin(), l2.end()) );
204 VERIFY( c2.c.get_allocator() == Alloc(78) );
205 }
206
207 enum AppendKind { None, EmplaceBack, PushBack, Emplace, Insert };
208
209 template<typename C, AppendKind Kind>
210 struct Cont4
211 {
212 // Only support construction with no args or an allocator.
213 // This forces the use of either emplace_back, push_back, emplace or insert.
214 Cont4() { }
215 Cont4(typename C::allocator_type a) : c(a) { }
216
217 template<typename T>
218 requires (Kind <= EmplaceBack)
219 && requires(C& c, T&& t) { c.emplace_back(std::forward<T>(t)); }
220 void
221 emplace_back(T&& t)
222 {
223 kind = EmplaceBack;
224 c.emplace_back(std::forward<T>(t));
225 }
226
227 template<typename T>
228 requires (Kind <= PushBack)
229 && requires(C& c, T&& t) { c.push_back(std::forward<T>(t)); }
230 void
231 push_back(T&& t)
232 {
233 kind = PushBack;
234 c.push_back(std::forward<T>(t));
235 }
236
237 template<typename T>
238 requires (Kind <= Emplace)
239 && requires(C& c, T&& t) { c.emplace(c.end(), std::forward<T>(t)); }
240 void
241 emplace(typename C::iterator pos, T&& t)
242 {
243 kind = Emplace;
244 c.emplace(pos, std::forward<T>(t));
245 }
246
247 template<typename T>
248 void
249 insert(typename C::iterator pos, T&& t)
250 {
251 kind = Insert;
252 c.insert(pos, std::forward<T>(t));
253 }
254
255 // Required to satisfy reservable-container
256 void
257 reserve(typename C::size_type n) requires requires(C& c) { c.reserve(n); }
258 {
259 c.reserve(n);
260 used_reserve = true;
261 }
262
263 // Satisfying sized_range is required to satisfy reservable-container
264 typename C::iterator begin() { return c.begin(); }
265 typename C::iterator end() { return c.end(); }
266 auto size() const { return c.size(); }
267
268 // Required to satisfy reservable-container
269 auto capacity() const requires requires(C& c) { c.capacity(); }
270 { return c.capacity(); }
271
272 // Required to satisfy reservable-container
273 auto max_size() const { return c.max_size(); }
274
275 C c;
276 AppendKind kind{};
277 bool used_reserve = false;
278 };
279
280 void
281 test_2_1_4()
282 {
283 // (2.1.4) constructible_from<C, Args...> and
284 // container-insertable<C, range_reference_t<R>>
285
286 using Alloc = __gnu_test::uneq_allocator<int>;
287 using Alloc2 = __gnu_test::uneq_allocator<short>;
288 using V = std::vector<int, Alloc>;
289 using List = std::list<short, Alloc2>;
290
291 std::list<unsigned> l{1u, 2u, 3u};
292 std::list<long> l2{4l, 5l, 6l};
293
294 // use vector::emplace_back and vector::reserve
295 auto c = std::ranges::to<Cont4<V, EmplaceBack>>(l);
296 static_assert(std::is_same_v<decltype(c), Cont4<V, EmplaceBack>>);
297 VERIFY( c.c == V(l.begin(), l.end()) );
298 VERIFY( c.kind == EmplaceBack );
299 VERIFY( c.used_reserve );
300
301 // use vector::emplace_back and vector::reserve
302 auto c2 = std::ranges::to<Cont4<V, EmplaceBack>>(l2, Alloc(78));
303 static_assert(std::is_same_v<decltype(c2), Cont4<V, EmplaceBack>>);
304 VERIFY( c2.c == V(l2.begin(), l2.end()) );
305 VERIFY( c2.c.get_allocator() == Alloc(78) );
306 VERIFY( c2.kind == EmplaceBack );
307 VERIFY( c2.used_reserve );
308
309 // use list::emplace_back
310 auto c3 = std::ranges::to<Cont4<List, EmplaceBack>>(c.c, Alloc2(99));
311 static_assert(std::is_same_v<decltype(c3), Cont4<List, EmplaceBack>>);
312 VERIFY( c3.c == List(l.begin(), l.end()) );
313 VERIFY( c3.c.get_allocator() == Alloc(99) );
314 VERIFY( c3.kind == EmplaceBack );
315 VERIFY( ! c3.used_reserve );
316
317 // use vector::push_back and vector::reserve
318 auto c4 = std::ranges::to<Cont4<V, PushBack>>(l);
319 static_assert(std::is_same_v<decltype(c4), Cont4<V, PushBack>>);
320 VERIFY( c4.c == V(l.begin(), l.end()) );
321 VERIFY( c4.kind == PushBack );
322 VERIFY( c4.used_reserve );
323
324 // use vector::push_back and vector::reserve
325 auto c5 = std::ranges::to<Cont4<V, PushBack>>(l2, Alloc(78));
326 static_assert(std::is_same_v<decltype(c5), Cont4<V, PushBack>>);
327 VERIFY( c5.c == V(l2.begin(), l2.end()) );
328 VERIFY( c5.c.get_allocator() == Alloc(78) );
329 VERIFY( c5.kind == PushBack );
330 VERIFY( c5.used_reserve );
331
332 // use list::push_back
333 auto c6 = std::ranges::to<Cont4<List, PushBack>>(c.c, Alloc2(99));
334 static_assert(std::is_same_v<decltype(c6), Cont4<List, PushBack>>);
335 VERIFY( c6.c == List(l.begin(), l.end()) );
336 VERIFY( c6.c.get_allocator() == Alloc(99) );
337 VERIFY( c6.kind == PushBack );
338 VERIFY( ! c6.used_reserve );
339
340 // use vector::emplace and vector::reserve
341 auto c7 = std::ranges::to<Cont4<V, Emplace>>(l);
342 static_assert(std::is_same_v<decltype(c7), Cont4<V, Emplace>>);
343 VERIFY( c7.c == V(l.begin(), l.end()) );
344 VERIFY( c7.kind == Emplace );
345 VERIFY( c7.used_reserve );
346
347 // use vector::emplace and vector::reserve
348 auto c8 = std::ranges::to<Cont4<V, Emplace>>(l2, Alloc(78));
349 static_assert(std::is_same_v<decltype(c8), Cont4<V, Emplace>>);
350 VERIFY( c8.c == V(l2.begin(), l2.end()) );
351 VERIFY( c8.c.get_allocator() == Alloc(78) );
352 VERIFY( c8.kind == Emplace );
353 VERIFY( c8.used_reserve );
354
355 // use list::emplace
356 auto c9 = std::ranges::to<Cont4<List, Emplace>>(c.c, Alloc2(99));
357 static_assert(std::is_same_v<decltype(c9), Cont4<List, Emplace>>);
358 VERIFY( c9.c == List(l.begin(), l.end()) );
359 VERIFY( c9.c.get_allocator() == Alloc(99) );
360 VERIFY( c9.kind == Emplace );
361 VERIFY( ! c9.used_reserve );
362
363 // use vector::insert and vector::reserve
364 auto c10 = std::ranges::to<Cont4<V, Insert>>(l);
365 static_assert(std::is_same_v<decltype(c10), Cont4<V, Insert>>);
366 VERIFY( c10.c == V(l.begin(), l.end()) );
367 VERIFY( c10.kind == Insert );
368 VERIFY( c10.used_reserve );
369
370 // use vector::insert and vector::reserve
371 auto c11 = std::ranges::to<Cont4<V, Insert>>(l2, Alloc(78));
372 static_assert(std::is_same_v<decltype(c11), Cont4<V, Insert>>);
373 VERIFY( c11.c == V(l2.begin(), l2.end()) );
374 VERIFY( c11.c.get_allocator() == Alloc(78) );
375 VERIFY( c11.kind == Insert );
376 VERIFY( c11.used_reserve );
377
378 // use list::insert
379 auto c12 = std::ranges::to<Cont4<List, Insert>>(c.c, Alloc2(99));
380 static_assert(std::is_same_v<decltype(c12), Cont4<List, Insert>>);
381 VERIFY( c12.c == List(l.begin(), l.end()) );
382 VERIFY( c12.c.get_allocator() == Alloc(99) );
383 VERIFY( c12.kind == Insert );
384 VERIFY( ! c12.used_reserve );
385
386 struct NoCopyPls
387 {
388 NoCopyPls(int) { }
389 NoCopyPls(const NoCopyPls&) { throw; }
390 };
391
392 // Uses list::emplace_back(const int&) not list::push_back(NoCopyPls&&).
393 (void) std::ranges::to<std::list<NoCopyPls>>(l);
394 }
395
396 void
397 test_2_2()
398 {
399 // (2.2) input_range<range_reference_t<R>>
400
401 std::string s1[]{ "one", "two", "three", "four" };
402 std::string s2[]{ "V", "VI", "VII", "VIII" };
403 std::string s3[]{ "0x09", "0x0a", "0x0b", "0x0c" };
404 using R = __gnu_test::test_input_range<std::string>;
405 R input_ranges[]{R(s1), R(s2), R(s3)};
406 __gnu_test::test_input_range<R> rr(input_ranges);
407 namespace pmr = std::pmr;
408 __gnu_test::memory_resource res;
409 #if _GLIBCXX_USE_CXX11_ABI
410 auto vvs = std::ranges::to<pmr::vector<pmr::vector<pmr::string>>>(rr, &res);
411 auto str_alloc = pmr::polymorphic_allocator<char>(&res);
412 #else
413 auto vvs = std::ranges::to<pmr::vector<pmr::vector<std::string>>>(rr, &res);
414 auto str_alloc = std::allocator<char>();
415 #endif
416 VERIFY( vvs[1][1] == "VI" );
417 VERIFY( vvs[2][2] == "0x0b" );
418 VERIFY( vvs[0].get_allocator().resource() == &res );
419 VERIFY( vvs[2][2].get_allocator() == str_alloc );
420 }
421
422 void
423 test_lwg3984()
424 {
425 std::vector<std::vector<int>> v;
426 auto r = std::views::all(std::move(v));
427 auto l = std::ranges::to<std::list<std::list<int>>>(r);
428 VERIFY(l.empty());
429 }
430
431 void
432 test_nodiscard()
433 {
434 std::vector<int> v;
435 std::ranges::to<std::vector<long>>(v); // { dg-warning "ignoring return" }
436 std::ranges::to<std::vector>(v); // { dg-warning "ignoring return" }
437 std::ranges::to<std::vector<long>>(); // { dg-warning "ignoring return" }
438 std::ranges::to<std::vector>(); // { dg-warning "ignoring return" }
439 }
440
441 void
442 test_constexpr()
443 {
444 constexpr int x = [](int i) {
445 auto c1 = std::views::iota(1, i) | std::ranges::to<std::vector<int>>();
446 auto c2 = std::views::iota(i, 10) | std::ranges::to<std::vector>();
447 return c1[0] + c2[0];
448 }(5);
449 static_assert(x == 6);
450 }
451
452 void
453 test_sfinae()
454 {
455 // PR libstdc++/112802
456 [](auto x) {
457 static_assert(!requires { std::ranges::to<std::vector<int>>()(x); });
458 static_assert(!requires { std::ranges::to<std::vector>()(x); });
459 }(0);
460 }
461
462 void
463 test_composition()
464 {
465 // PR libstdc++/113068
466 auto adaptor = std::ranges::to<std::string>() | std::ranges::to<std::string>();
467 auto str = adaptor(" ");
468 }
469
470 constexpr bool
471 test_pr119282()
472 {
473 // PR libstdc++/119282
474 auto v = std::array{1, 2, 3}
475 | std::views::transform([](auto x) { return std::array{x}; })
476 | std::views::join
477 | std::ranges::to<std::vector>();
478 VERIFY( std::ranges::size(v) == 3 );
479 return true;
480 }
481
482 void
483 test_lwg2713()
484 {
485 using Alloc = __gnu_test::uneq_allocator<std::pair<const int, const char*>>;
486 const Alloc alloc(303);
487 const std::map<int, const char*> m{{1, "one"}, {2, "two"}, {3, "three"}};
488 namespace ranges = std::ranges;
489
490 // Call constructors with bucket count
491 auto m1 = m | ranges::to<std::unordered_map>(0, alloc);
492 VERIFY( m1.get_allocator() == alloc );
493 auto m2 = m | ranges::to<std::unordered_multimap>(0, alloc);
494 VERIFY( m2.get_allocator() == alloc );
495 // These call constructors added in lwg2713
496 auto m3 = m | ranges::to<std::unordered_map>(alloc);
497 VERIFY( m3.get_allocator() == alloc );
498 auto m4 = m | ranges::to<std::unordered_multimap>(alloc);
499 VERIFY( m4.get_allocator() == alloc );
500 }
501
502 int main()
503 {
504 test_p1206r7_examples();
505 test_example_1();
506 test_2_1_1();
507 test_2_1_2();
508 test_2_1_3();
509 test_2_1_4();
510 test_2_2();
511 test_lwg2713();
512 test_lwg3984();
513 test_nodiscard();
514 test_constexpr();
515 test_sfinae();
516 test_composition();
517 static_assert(test_pr119282());
518 }