]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/std/ranges/adaptors/conditionally_borrowed.cc
1 // Copyright (C) 2020-2021 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 } }
25 #include <testsuite_hooks.h>
27 namespace ranges
= std::ranges
;
28 namespace views
= std::views
;
29 using namespace std::literals
;
31 // P2017R1 "Conditionally borrowed ranges"
32 auto trim(std::string
const& s
) {
33 auto isalpha
= [](unsigned char c
){ return std::isalpha(c
); };
34 auto b
= ranges::find_if(s
, isalpha
);
35 auto e
= ranges::find_if(s
| views::reverse
, isalpha
).base();
36 return ranges::subrange(b
, e
);
42 VERIFY( ranges::equal( trim(" abc "), "abc"sv
) );
44 std::string s
= "0123456789";
45 auto odd
= [](char c
){ return (c
- '0') % 2; };
46 using namespace std::views
;
47 auto pos
= ranges::find(s
| take(5) | drop(1) | drop_while(odd
), '2');
48 VERIFY( *pos
== '2' );
50 static_assert( ranges::borrowed_range
<decltype(s
| reverse
)> );
51 static_assert( ranges::borrowed_range
<decltype(s
| take(1))> );
52 static_assert( ranges::borrowed_range
<decltype(s
| drop(1))> );
53 static_assert( ranges::borrowed_range
<decltype(s
| drop_while(odd
))> );
55 ranges::subrange
r(s
.begin(), s
.cend());
56 static_assert( !ranges::common_range
<decltype(r
)> );
57 auto pos2
= ranges::find(r
| views::common
, '2');
58 VERIFY( *pos2
== '2' );
64 std::pair
<int, std::string_view
> a
[2]{ {1,"two"}, {3,"four"}};
65 auto pos
= ranges::find(a
| views::values
, "four");
66 VERIFY( *pos
== "four" );
68 static_assert( ranges::borrowed_range
<decltype(a
| views::keys
)> );