Debian Codegraph @

libstdc++-v3/testsuite/std/ranges/adaptors/drop_while.cc

raw
1 // Copyright (C) 2020-2025 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-do run { target c++20 } }
19
20 #include <algorithm>
21 #include <ranges>
22 #include <testsuite_hooks.h>
23 #include <testsuite_iterators.h>
24
25 using __gnu_test::test_range;
26 using __gnu_test::bidirectional_iterator_wrapper;
27 using __gnu_test::forward_iterator_wrapper;
28 using __gnu_test::random_access_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::drop_while(p);
38 VERIFY( ranges::equal(v | views::take(5), (int[]){16,17,18,19,20}) );
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, bidirectional_iterator_wrapper> rx(x);
50 auto v = rx | views::drop_while([] (int i) { return i<4; });
51 VERIFY( ranges::equal(v, (int[]){4,5}) );
52 using R = decltype(v);
53 static_assert(ranges::view<R>);
54 static_assert(!ranges::common_range<R>);
55 static_assert(ranges::bidirectional_range<R>);
56 }
57
58 // The following tests that drop_while_view::begin caches its result.
59
60 template<template<typename> typename wrapper>
61 struct test_view : ranges::view_base
62 {
63 bool begin_already_called = false;
64 static inline int x[] = {1,2,3,4,5};
65 test_range<int, wrapper> rx{x};
66
67 auto
68 begin()
69 {
70 if (begin_already_called)
71 x[0] = 10;
72 begin_already_called = true;
73 return rx.begin();
74 }
75
76 auto
77 end()
78 { return rx.end(); }
79 };
80
81 template<template<typename> typename wrapper>
82 void
83 test03()
84 {
85 auto v
86 = test_view<wrapper>{} | views::drop_while([] (int i) { return i<3; });
87 VERIFY( ranges::equal(v, (int[]){3,4,5}) );
88 VERIFY( ranges::equal(v, (int[]){3,4,5}) );
89 }
90
91 template<auto drop_while = views::drop_while>
92 void
93 test04()
94 {
95 // Verify SFINAE behavior.
96 extern int x[5];
97 auto p = [] (int*) { return true; };
98 static_assert(!requires { drop_while(); });
99 static_assert(!requires { drop_while(x, p, p); });
100 static_assert(!requires { drop_while(x, p); });
101 static_assert(!requires { drop_while(p)(x); });
102 static_assert(!requires { x | (drop_while(p) | views::all); });
103 static_assert(!requires { (drop_while(p) | views::all)(x); });
104 static_assert(!requires { drop_while | views::all; });
105 static_assert(!requires { views::all | drop_while; });
106 }
107
108 int
109 main()
110 {
111 test01();
112 test02();
113 test03<forward_iterator_wrapper>();
114 test03<random_access_iterator_wrapper>();
115 test04();
116 }