1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// std::views::take
#include <ranges>
#include <cassert>
#include <concepts>
#include <span>
#include <string_view>
#include <utility>
#include "test_iterators.h"
template <class View, class T>
concept CanBePiped = requires (View&& view, T&& t) {
{ std::forward<View>(view) | std::forward<T>(t) };
};
struct SizedView : std::ranges::view_base {
int* begin_ = nullptr;
int* end_ = nullptr;
constexpr SizedView(int* begin, int* end) : begin_(begin), end_(end) {}
constexpr auto begin() const { return forward_iterator<int*>(begin_); }
constexpr auto end() const { return sized_sentinel<forward_iterator<int*>>(forward_iterator<int*>(end_)); }
};
static_assert(std::ranges::forward_range<SizedView>);
static_assert(std::ranges::sized_range<SizedView>);
static_assert(std::ranges::view<SizedView>);
template <class T>
constexpr void test_small_range(const T& input) {
constexpr int N = 100;
auto size = std::ranges::size(input);
auto result = input | std::views::take(N);
assert(size < N);
assert(result.size() == size);
}
constexpr bool test() {
constexpr int N = 8;
int buf[N] = {1, 2, 3, 4, 5, 6, 7, 8};
// Test that `std::views::take` is a range adaptor.
{
using SomeView = SizedView;
// Test `view | views::take`
{
SomeView view(buf, buf + N);
std::same_as<std::ranges::take_view<SomeView>> decltype(auto) result = view | std::views::take(3);
assert(result.base().begin_ == buf);
assert(result.base().end_ == buf + N);
assert(result.size() == 3);
}
// Test `adaptor | views::take`
{
SomeView view(buf, buf + N);
auto f = [](int i) { return i; };
auto const partial = std::views::transform(f) | std::views::take(3);
using Result = std::ranges::take_view<std::ranges::transform_view<SomeView, decltype(f)>>;
std::same_as<Result> decltype(auto) result = partial(view);
assert(result.base().base().begin_ == buf);
assert(result.base().base().end_ == buf + N);
assert(result.size() == 3);
}
// Test `views::take | adaptor`
{
SomeView view(buf, buf + N);
auto f = [](int i) { return i; };
auto const partial = std::views::take(3) | std::views::transform(f);
using Result = std::ranges::transform_view<std::ranges::take_view<SomeView>, decltype(f)>;
std::same_as<Result> decltype(auto) result = partial(view);
assert(result.base().base().begin_ == buf);
assert(result.base().base().end_ == buf + N);
assert(result.size() == 3);
}
// Check SFINAE friendliness
{
struct NotAView { };
static_assert(!std::is_invocable_v<decltype(std::views::take)>);
static_assert(!std::is_invocable_v<decltype(std::views::take), NotAView, int>);
static_assert( CanBePiped<SomeView&, decltype(std::views::take(3))>);
static_assert( CanBePiped<int(&)[10], decltype(std::views::take(3))>);
static_assert(!CanBePiped<int(&&)[10], decltype(std::views::take(3))>);
static_assert(!CanBePiped<NotAView, decltype(std::views::take(3))>);
static_assert(!CanBePiped<SomeView&, decltype(std::views::take(/*n=*/NotAView{}))>);
}
}
{
static_assert(std::same_as<decltype(std::views::take), decltype(std::ranges::views::take)>);
}
// `views::take(empty_view, n)` returns an `empty_view`.
{
using Result = std::ranges::empty_view<int>;
[[maybe_unused]] std::same_as<Result> decltype(auto) result = std::views::empty<int> | std::views::take(3);
}
// `views::take(span, n)` returns a `span`.
{
std::span<int> s(buf);
std::same_as<decltype(s)> decltype(auto) result = s | std::views::take(3);
assert(result.size() == 3);
}
// `views::take(span, n)` returns a `span` with a dynamic extent, regardless of the input `span`.
{
std::span<int, 8> s(buf);
std::same_as<std::span<int, std::dynamic_extent>> decltype(auto) result = s | std::views::take(3);
assert(result.size() == 3);
}
// `views::take(string_view, n)` returns a `string_view`.
{
{
std::string_view sv = "abcdef";
std::same_as<decltype(sv)> decltype(auto) result = sv | std::views::take(3);
assert(result.size() == 3);
}
{
std::u32string_view sv = U"abcdef";
std::same_as<decltype(sv)> decltype(auto) result = sv | std::views::take(3);
assert(result.size() == 3);
}
}
// `views::take(subrange, n)` returns a `subrange`.
{
auto subrange = std::ranges::subrange(buf, buf + N);
using Result = std::ranges::subrange<int*>;
std::same_as<Result> decltype(auto) result = subrange | std::views::take(3);
assert(result.size() == 3);
}
// `views::take(subrange, n)` doesn't return a `subrange` if it's not a random access range.
{
SizedView v(buf, buf + N);
auto subrange = std::ranges::subrange(v.begin(), v.end());
using Result = std::ranges::take_view<std::ranges::subrange<forward_iterator<int*>,
sized_sentinel<forward_iterator<int*>>>>;
std::same_as<Result> decltype(auto) result = subrange | std::views::take(3);
assert(result.size() == 3);
}
// `views::take(subrange, n)` returns a `subrange` with all default template arguments.
{
std::ranges::subrange<int*, sized_sentinel<int*>, std::ranges::subrange_kind::sized> subrange;
using Result = std::ranges::subrange<int*, int*, std::ranges::subrange_kind::sized>;
[[maybe_unused]] std::same_as<Result> decltype(auto) result = subrange | std::views::take(3);
}
// `views::take(iota_view, n)` returns an `iota_view`.
{
auto iota = std::views::iota(1, 8);
// The second template argument of the resulting `iota_view` is different because it has to be able to hold
// the `range_difference_t` of the input `iota_view`.
using Result = std::ranges::iota_view<int, std::ranges::range_difference_t<decltype(iota)>>;
std::same_as<Result> decltype(auto) result = iota | std::views::take(3);
assert(result.size() == 3);
}
// When the size of the input range `s` is shorter than `n`, only `s` elements are taken.
{
test_small_range(std::span(buf));
test_small_range(std::string_view("abcdef"));
test_small_range(std::ranges::subrange(buf, buf + N));
test_small_range(std::views::iota(1, 8));
}
// Test that it's possible to call `std::views::take` with any single argument as long as the resulting closure is
// never invoked. There is no good use case for it, but it's valid.
{
struct X { };
[[maybe_unused]] auto partial = std::views::take(X{});
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|