| 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 <forward_list> |
| 22 |
#include <ranges> |
| 23 |
#include <testsuite_hooks.h> |
| 24 |
#include <testsuite_iterators.h> |
| 25 |
|
| 26 |
using __gnu_test::test_range; |
| 27 |
using __gnu_test::bidirectional_iterator_wrapper; |
| 28 |
|
| 29 |
namespace ranges = std::ranges; |
| 30 |
namespace views = ranges::views; |
| 31 |
|
| 32 |
void |
| 33 |
test01() |
| 34 |
{ |
| 35 |
auto v = views::iota(0) | views::take(5); |
| 36 |
static_assert(ranges::view<decltype(v)>); |
| 37 |
static_assert(!ranges::sized_range<decltype(v)>); |
| 38 |
static_assert(!ranges::common_range<decltype(v)>); |
| 39 |
static_assert(ranges::random_access_range<decltype(v)>); |
| 40 |
static_assert(!ranges::contiguous_range<decltype(v)>); |
| 41 |
static_assert(ranges::range<const decltype(v)>); |
| 42 |
VERIFY( ranges::equal(v, (int[]){0,1,2,3,4}) ); |
| 43 |
} |
| 44 |
|
| 45 |
void |
| 46 |
test02() |
| 47 |
{ |
| 48 |
auto v = views::take(views::iota(0, 20), 5); |
| 49 |
static_assert(ranges::view<decltype(v)>); |
| 50 |
static_assert(ranges::sized_range<decltype(v)>); |
| 51 |
static_assert(ranges::common_range<decltype(v)>); |
| 52 |
static_assert(ranges::random_access_range<decltype(v)>); |
| 53 |
static_assert(!ranges::contiguous_range<decltype(v)>); |
| 54 |
static_assert(ranges::range<const decltype(v)>); |
| 55 |
VERIFY( ranges::equal(v, (int[]){0,1,2,3,4}) ); |
| 56 |
} |
| 57 |
|
| 58 |
void |
| 59 |
test03() |
| 60 |
{ |
| 61 |
int x[] = {0,1,2,3,4,5}; |
| 62 |
auto is_odd = [] (int i) { return i%2 == 1; }; |
| 63 |
auto v = x | views::filter(is_odd) | views::take(3); |
| 64 |
(void) ranges::begin(v); |
| 65 |
using R = decltype(v); |
| 66 |
static_assert(ranges::view<R>); |
| 67 |
static_assert(!ranges::sized_range<R>); |
| 68 |
static_assert(!ranges::common_range<R>); |
| 69 |
static_assert(ranges::forward_range<R>); |
| 70 |
static_assert(!ranges::random_access_range<R>); |
| 71 |
static_assert(!ranges::range<const R>); |
| 72 |
VERIFY( ranges::equal(v, (int[]){1,3,5}) ); |
| 73 |
} |
| 74 |
|
| 75 |
void |
| 76 |
test04() |
| 77 |
{ |
| 78 |
int x[] = {1,2,3,4,5}; |
| 79 |
test_range<int, bidirectional_iterator_wrapper> rx(x); |
| 80 |
auto v = ranges::take_view{rx, 3}; |
| 81 |
using R = decltype(v); |
| 82 |
static_assert(ranges::view<R>); |
| 83 |
static_assert(!ranges::sized_range<R>); |
| 84 |
static_assert(ranges::bidirectional_range<R>); |
| 85 |
VERIFY( ranges::equal(v | views::take(5), (int[]){1,2,3}) ); |
| 86 |
} |
| 87 |
|
| 88 |
void |
| 89 |
test05() |
| 90 |
{ |
| 91 |
std::forward_list<int> x = {1,2,3,4,5}; |
| 92 |
auto v = x | views::transform(std::negate{}) | views::take(4); |
| 93 |
|
| 94 |
// Verify that _Sentinel<false> is implicitly convertible to _Sentinel<true>. |
| 95 |
static_assert(!ranges::common_range<decltype(v)>); |
| 96 |
#if ! __cpp_lib_ranges_as_const |
| 97 |
static_assert(!std::same_as<decltype(ranges::end(v)), |
| 98 |
decltype(ranges::cend(v))>); |
| 99 |
#endif |
| 100 |
auto b = ranges::cend(v); |
| 101 |
b = ranges::end(v); |
| 102 |
} |
| 103 |
|
| 104 |
template<auto take = views::take> |
| 105 |
void |
| 106 |
test06() |
| 107 |
{ |
| 108 |
// Verify SFINAE behavior. |
| 109 |
extern int x[5]; |
| 110 |
int* n = 0; |
| 111 |
static_assert(!requires { take(); }); |
| 112 |
static_assert(!requires { take(x, n, n); }); |
| 113 |
static_assert(!requires { take(x, n); }); |
| 114 |
static_assert(!requires { take(n)(x); }); |
| 115 |
static_assert(!requires { x | (take(n) | views::all); }); |
| 116 |
static_assert(!requires { (take(n) | views::all)(x); }); |
| 117 |
static_assert(!requires { take | views::all; }); |
| 118 |
static_assert(!requires { views::all | take; }); |
| 119 |
} |
| 120 |
|
| 121 |
int |
| 122 |
main() |
| 123 |
{ |
| 124 |
test01(); |
| 125 |
test02(); |
| 126 |
test03(); |
| 127 |
test04(); |
| 128 |
test05(); |
| 129 |
test06(); |
| 130 |
} |