]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/std/ranges/adaptors/drop_while.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / std / ranges / adaptors / drop_while.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
26using __gnu_test::test_range;
27using __gnu_test::bidirectional_iterator_wrapper;
a1535015
PP
28using __gnu_test::forward_iterator_wrapper;
29using __gnu_test::random_access_iterator_wrapper;
cba9ef06
PP
30
31namespace ranges = std::ranges;
32namespace views = std::ranges::views;
33
34void
35test01()
36{
37 auto p = [] (int i) { return i != 16; };
38 auto v = views::iota(10) | views::drop_while(p);
39 VERIFY( ranges::equal(v | views::take(5), (int[]){16,17,18,19,20}) );
40 using R = decltype(v);
41 static_assert(ranges::view<R>);
42 static_assert(!ranges::common_range<R>);
43 static_assert(ranges::random_access_range<R>);
44}
45
46void
47test02()
48{
49 int x[] = {1,2,3,4,5};
50 test_range<int, bidirectional_iterator_wrapper> rx(x);
51 auto v = rx | views::drop_while([] (int i) { return i<4; });
52 VERIFY( ranges::equal(v, (int[]){4,5}) );
53 using R = decltype(v);
54 static_assert(ranges::view<R>);
55 static_assert(!ranges::common_range<R>);
56 static_assert(ranges::bidirectional_range<R>);
57}
58
a1535015
PP
59// The following tests that drop_while_view::begin caches its result.
60
61template<template<typename> typename wrapper>
62struct test_view : ranges::view_base
63{
64 bool begin_already_called = false;
65 static inline int x[] = {1,2,3,4,5};
66 test_range<int, wrapper> rx{x};
67
68 auto
69 begin()
70 {
71 if (begin_already_called)
72 x[0] = 10;
73 begin_already_called = true;
74 return rx.begin();
75 }
76
77 auto
78 end()
79 { return rx.end(); }
80};
81
82template<template<typename> typename wrapper>
83void
84test03()
85{
86 auto v
87 = test_view<wrapper>{} | views::drop_while([] (int i) { return i<3; });
88 VERIFY( ranges::equal(v, (int[]){3,4,5}) );
89 VERIFY( ranges::equal(v, (int[]){3,4,5}) );
90}
91
a25321ca
PP
92template<auto drop_while = views::drop_while>
93void
94test04()
95{
96 // Verify SFINAE behavior.
97 extern int x[5];
98 auto p = [] (int*) { return true; };
99 static_assert(!requires { drop_while(); });
100 static_assert(!requires { drop_while(x, p, p); });
101 static_assert(!requires { drop_while(x, p); });
102 static_assert(!requires { drop_while(p)(x); });
103 static_assert(!requires { x | (drop_while(p) | views::all); });
104 static_assert(!requires { (drop_while(p) | views::all)(x); });
105 static_assert(!requires { drop_while | views::all; });
106 static_assert(!requires { views::all | drop_while; });
107}
108
cba9ef06
PP
109int
110main()
111{
112 test01();
113 test02();
a1535015
PP
114 test03<forward_iterator_wrapper>();
115 test03<random_access_iterator_wrapper>();
a25321ca 116 test04();
cba9ef06 117}