]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/std/ranges/adaptors/take_while.cc
1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
22 #include <forward_list>
24 #include <testsuite_hooks.h>
25 #include <testsuite_iterators.h>
27 using __gnu_test::test_range
;
28 using __gnu_test::forward_iterator_wrapper
;
30 namespace ranges
= std::ranges
;
31 namespace views
= std::ranges::views
;
36 auto p
= [] (int i
) { return i
!= 16; };
37 auto v
= views::iota(10) | views::take_while(p
);
38 VERIFY( ranges::equal(v
, (int[]){10,11,12,13,14,15}) );
39 using R
= decltype(v
);
40 static_assert(ranges::view
<R
>);
41 static_assert(!ranges::common_range
<R
>);
42 static_assert(ranges::random_access_range
<R
>);
48 int x
[] = {1,2,3,4,5};
49 test_range
<int, forward_iterator_wrapper
> rx(x
);
50 auto v
= rx
| views::take_while([] (int i
) { return i
<4; });
51 VERIFY( ranges::equal(v
, (int[]){1,2,3}) );
52 using R
= decltype(v
);
53 static_assert(ranges::view
<R
>);
54 static_assert(!ranges::common_range
<R
>);
55 static_assert(ranges::forward_range
<R
>);
61 std::forward_list
<int> x
= {1,2,3,4,5};
63 = x
| views::transform(std::negate
{}) | views::take_while(std::identity
{});
65 // Verify that _Sentinel<false> is implicitly convertible to _Sentinel<true>.
66 static_assert(!ranges::common_range
<decltype(v
)>);
67 static_assert(!std::same_as
<decltype(ranges::end(v
)),
68 decltype(ranges::cend(v
))>);
69 auto b
= ranges::cend(v
);
77 auto v
= views::single(1) | views::take_while([](int& x
) { return true;});
78 static_assert(ranges::range
<decltype(v
)>);
79 static_assert(!ranges::range
<decltype(v
) const>);
82 template<auto take_while
= views::take_while
>
86 // Verify SFINAE behavior.
88 auto p
= [] (int*) { return true; };
89 static_assert(!requires
{ take_while(); });
90 static_assert(!requires
{ take_while(x
, p
, p
); });
91 static_assert(!requires
{ take_while(x
, p
); });
92 static_assert(!requires
{ take_while(p
)(x
); });
93 static_assert(!requires
{ x
| (take_while(p
) | views::all
); });
94 static_assert(!requires
{ (take_while(p
) | views::all
)(x
); });
95 static_assert(!requires
{ take_while
| views::all
; });
96 static_assert(!requires
{ views::all
| take_while
; });