]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/std/ranges/adaptors/take_while.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / std / ranges / adaptors / take_while.cc
1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
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 <forward_list>
23 #include <ranges>
24 #include <testsuite_hooks.h>
25 #include <testsuite_iterators.h>
26
27 using __gnu_test::test_range;
28 using __gnu_test::forward_iterator_wrapper;
29
30 namespace ranges = std::ranges;
31 namespace views = std::ranges::views;
32
33 void
34 test01()
35 {
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>);
43 }
44
45 void
46 test02()
47 {
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>);
56 }
57
58 void
59 test03()
60 {
61 std::forward_list<int> x = {1,2,3,4,5};
62 auto v
63 = x | views::transform(std::negate{}) | views::take_while(std::identity{});
64
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);
70 b = ranges::end(v);
71 }
72
73 void
74 test04()
75 {
76 // LWG 3450
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>);
80 }
81
82 template<auto take_while = views::take_while>
83 void
84 test05()
85 {
86 // Verify SFINAE behavior.
87 extern int x[5];
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; });
97 }
98
99 int
100 main()
101 {
102 test01();
103 test02();
104 test03();
105 test04();
106 test05();
107 }