1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // TODO: make `join_view` non-experimental once D2770 is implemented.
11 // UNSUPPORTED: !c++experimental
21 #include "test_iterators.h"
22 #include "type_algorithms.h"
24 template <class InContainer
, class OutContainer
>
25 constexpr void test_containers() {
26 using InIter
= typename
InContainer::iterator
;
27 using OutIter
= typename
OutContainer::iterator
;
30 InContainer in
{1, 2, 3, 4};
33 std::same_as
<std::ranges::in_out_result
<InIter
, OutIter
>> auto ret
=
34 std::ranges::move(in
.begin(), in
.end(), out
.begin());
35 assert(std::ranges::equal(in
, out
));
36 assert(ret
.in
== in
.end());
37 assert(ret
.out
== out
.end());
40 InContainer in
{1, 2, 3, 4};
42 std::same_as
<std::ranges::in_out_result
<InIter
, OutIter
>> auto ret
= std::ranges::move(in
, out
.begin());
43 assert(std::ranges::equal(in
, out
));
44 assert(ret
.in
== in
.end());
45 assert(ret
.out
== out
.end());
49 template <class Iter
, class Sent
>
50 constexpr void test_join_view() {
51 auto to_subranges
= std::views::transform([](auto& vec
) {
52 return std::ranges::subrange(Iter(vec
.data()), Sent(Iter(vec
.data() + vec
.size())));
55 { // segmented -> contiguous
56 std::vector
<std::vector
<int>> vectors
= {};
57 auto range
= vectors
| to_subranges
;
58 std::vector
<std::ranges::subrange
<Iter
, Sent
>> subrange_vector(range
.begin(), range
.end());
59 std::array
<int, 0> arr
;
61 std::ranges::move(subrange_vector
| std::views::join
, arr
.begin());
62 assert(std::ranges::equal(arr
, std::array
<int, 0>{}));
64 { // segmented -> contiguous
65 std::vector
<std::vector
<int>> vectors
= {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10}, {}};
66 auto range
= vectors
| to_subranges
;
67 std::vector
<std::ranges::subrange
<Iter
, Sent
>> subrange_vector(range
.begin(), range
.end());
68 std::array
<int, 10> arr
;
70 std::ranges::move(subrange_vector
| std::views::join
, arr
.begin());
71 assert(std::ranges::equal(arr
, std::array
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}));
73 { // contiguous -> segmented
74 std::vector
<std::vector
<int>> vectors
= {{0, 0, 0, 0}, {0, 0}, {0, 0, 0, 0}, {}};
75 auto range
= vectors
| to_subranges
;
76 std::vector
<std::ranges::subrange
<Iter
, Sent
>> subrange_vector(range
.begin(), range
.end());
77 std::array arr
= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
79 std::ranges::move(arr
, (subrange_vector
| std::views::join
).begin());
80 assert(std::ranges::equal(subrange_vector
| std::views::join
, std::array
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}));
82 { // segmented -> segmented
83 std::vector
<std::vector
<int>> vectors
= {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10}, {}};
84 auto range1
= vectors
| to_subranges
;
85 std::vector
<std::ranges::subrange
<Iter
, Sent
>> subrange_vector(range1
.begin(), range1
.end());
86 std::vector
<std::vector
<int>> to_vectors
= {{0, 0, 0, 0}, {0, 0, 0, 0}, {}, {0, 0}};
87 auto range2
= to_vectors
| to_subranges
;
88 std::vector
<std::ranges::subrange
<Iter
, Sent
>> to_subrange_vector(range2
.begin(), range2
.end());
90 std::ranges::move(subrange_vector
| std::views::join
, (to_subrange_vector
| std::views::join
).begin());
91 assert(std::ranges::equal(to_subrange_vector
| std::views::join
, std::array
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}));
95 constexpr bool test_constexpr() {
96 test_containers
<std::vector
<int>, std::vector
<int>>();
98 types::for_each(types::forward_iterator_list
<int*>{}, []<class Iter
> {
99 test_join_view
<Iter
, Iter
>();
100 test_join_view
<Iter
, sentinel_wrapper
<Iter
>>();
101 test_join_view
<Iter
, sized_sentinel
<Iter
>>();
106 int main(int, char**) {
107 test_containers
<std::deque
<int>, std::deque
<int>>();
108 test_containers
<std::deque
<int>, std::vector
<int>>();
109 test_containers
<std::vector
<int>, std::deque
<int>>();
112 static_assert(test_constexpr());