]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/std/ranges/adaptors/all.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / std / ranges / adaptors / all.cc
CommitLineData
7adcbafe 1// Copyright (C) 2020-2022 Free Software Foundation, Inc.
cba9ef06
PP
2//
3// This file is part of the GNU ISO C++ Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
17
18// { dg-options "-std=gnu++2a" }
19// { dg-do run { target c++2a } }
20
21#include <algorithm>
22#include <ranges>
23#include <testsuite_hooks.h>
24#include <testsuite_iterators.h>
25
26namespace ranges = std::ranges;
27namespace views = std::ranges::views;
28
29void
30test01()
31{
32 int x[] = {1,2,3,4,5};
33 auto v = views::all(x);
34
35 static_assert(ranges::view<decltype(v)>);
36 static_assert(ranges::random_access_range<decltype(v)>);
37
38 VERIFY( ranges::size(v) == 5 );
39 VERIFY( ranges::size(x | views::all) == 5 );
40 VERIFY( ranges::size(v | views::all | views::all) == 5 );
41 VERIFY( ranges::size(v | (views::all | views::all)) == 5 );
42
43 ranges::reverse(v);
44 VERIFY( ranges::equal(v, (int[]){5,4,3,2,1}) );
45}
46
47void
48test02()
49{
50 int x[5] = { 0 };
51 int k = 0;
52 for (auto&& i : ranges::ref_view{x})
53 i += ++k;
54 VERIFY( ranges::equal(x, (int[]){1,2,3,4,5}) );
55}
56
57constexpr bool
58test03()
59{
60 std::array ints{0,1,2,3,4,5};
61 auto even = [] (int i) { return i%2==0; };
62 auto odd = [] (int i) { return i%2==1; };
63 auto square = [] (int i) { return i*i; };
64 int sum = 0;
65 for (auto v : (ints
66 | (views::all
67 | (views::filter(even)
68 | (views::filter(odd) | views::all)))
69 | views::transform(square)))
70 sum += v;
71 return sum == 0;
72}
73
74constexpr bool
75test04()
76{
77 auto odd = [] (int i) { return i%2==1; };
78 auto square = [] (int i) { return i*i; };
79 auto increment = [] (int i) { return i+1; };
80 auto small = [] (int i) { return i<30; };
81 auto non_negative = [] (int i) { return i>=0; };
82 auto negative = [] (int i) { return i<0; };
83 return ranges::equal(views::iota(-5)
84 | views::drop_while(negative)
85 | views::take_while(non_negative)
86 | views::transform(increment)
87 | views::filter(odd)
88 | views::take(3)
89 | views::all
90 | views::transform(square),
91 views::iota(-5)
92 | views::drop_while(negative)
93 | views::drop(1)
94 | views::filter(odd)
95 | views::transform(square)
96 | views::take_while(small)
97 | views::take_while(small));
98}
99
100static_assert(std::is_empty_v<decltype(views::common
101 | views::join
102 | views::all
103 | views::common
104 | views::keys
105 | views::reverse)>);
a25321ca
PP
106#if 0
107// Adding empty range adaptor closure objects to a pipeline used to not
108// increase the size of the pipeline, but now that our range adaptor closure
109// objects derive from a common empty base class, [[no_unique_address]] can no
110// longer make two empty adjacent range adaptor closure objects occupy the same
111// data member address.
cba9ef06
PP
112static_assert(sizeof(decltype(views::take(5) | views::drop(5)))
113 == sizeof(decltype(views::take(5)
114 | views::join
115 | views::common
116 | views::all
117 | views::keys
118 | views::drop(5)
119 | views::reverse)));
a25321ca
PP
120#endif
121
122template<auto all = views::all>
123void
124test05()
125{
126 // Verify SFINAE behavior.
127 static_assert(!requires { all(); });
128 static_assert(!requires { all(0, 0); });
129 static_assert(!requires { all(0); });
130 static_assert(!requires { 0 | all; });
131}
cba9ef06 132
9245b0e8
JW
133template<bool B1, bool B2>
134struct BorrowedRange
135{
136 int* ptr = nullptr;
137
138 BorrowedRange(int (&arr)[3]) noexcept : ptr(arr) { }
139
140 int* begin() const noexcept(B1) { return ptr; }
141 int* end() const noexcept(B2) { return ptr + 3; }
142};
143
144template<bool B1, bool B2>
145const bool std::ranges::enable_borrowed_range<BorrowedRange<B1, B2>> = true;
146
147void
148test06()
149{
150 int x[] { 1, 2, 3 };
151
152 // Using ref_view:
153 static_assert(noexcept(views::all(x)));
154
155 // Using subrange:
156 static_assert(noexcept(views::all(BorrowedRange<true, true>(x))));
157 static_assert(!noexcept(views::all(BorrowedRange<true, false>(x))));
158 static_assert(!noexcept(views::all(BorrowedRange<false, true>(x))));
159 static_assert(!noexcept(views::all(BorrowedRange<false, false>(x))));
160}
161
a2c2dcc6
PP
162void
163test07()
164{
165 // LWG 3481
166 struct view_t : ranges::empty_view<int> { // move-only view
167 view_t(const view_t&) = delete;
168 view_t(view_t&&) = default;
169 view_t& operator=(const view_t&) = delete;
170 view_t& operator=(view_t&&) = default;
171 };
172 static_assert(std::movable<view_t> && !std::copyable<view_t>);
173 static_assert(!ranges::viewable_range<view_t&>);
174}
175
cba9ef06
PP
176int
177main()
178{
179 test01();
180 test02();
181 static_assert(test03());
182 static_assert(test04());
a25321ca 183 test05();
9245b0e8 184 test06();
a2c2dcc6 185 test07();
cba9ef06 186}