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
|
//===----------------------------------------------------------------------===//
//
// 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::transform
#include <ranges>
#include <cassert>
#include <concepts>
#include <type_traits>
#include <utility>
#include "test_macros.h"
#include "types.h"
template <class View, class T>
concept CanBePiped = requires (View&& view, T&& t) {
{ std::forward<View>(view) | std::forward<T>(t) };
};
struct NonCopyableFunction {
NonCopyableFunction(NonCopyableFunction const&) = delete;
template <class T>
constexpr T operator()(T x) const { return x; }
};
constexpr bool test() {
int buff[8] = {0, 1, 2, 3, 4, 5, 6, 7};
// Test `views::transform(f)(v)`
{
{
using Result = std::ranges::transform_view<MoveOnlyView, PlusOne>;
std::same_as<Result> auto result = std::views::transform(PlusOne{})(MoveOnlyView{buff});
assert(result.begin().base() == buff);
assert(result[0] == 1);
assert(result[1] == 2);
assert(result[2] == 3);
}
{
auto const partial = std::views::transform(PlusOne{});
using Result = std::ranges::transform_view<MoveOnlyView, PlusOne>;
std::same_as<Result> auto result = partial(MoveOnlyView{buff});
assert(result.begin().base() == buff);
assert(result[0] == 1);
assert(result[1] == 2);
assert(result[2] == 3);
}
}
// Test `v | views::transform(f)`
{
{
using Result = std::ranges::transform_view<MoveOnlyView, PlusOne>;
std::same_as<Result> auto result = MoveOnlyView{buff} | std::views::transform(PlusOne{});
assert(result.begin().base() == buff);
assert(result[0] == 1);
assert(result[1] == 2);
assert(result[2] == 3);
}
{
auto const partial = std::views::transform(PlusOne{});
using Result = std::ranges::transform_view<MoveOnlyView, PlusOne>;
std::same_as<Result> auto result = MoveOnlyView{buff} | partial;
assert(result.begin().base() == buff);
assert(result[0] == 1);
assert(result[1] == 2);
assert(result[2] == 3);
}
}
// Test `views::transform(v, f)`
{
using Result = std::ranges::transform_view<MoveOnlyView, PlusOne>;
std::same_as<Result> auto result = std::views::transform(MoveOnlyView{buff}, PlusOne{});
assert(result.begin().base() == buff);
assert(result[0] == 1);
assert(result[1] == 2);
assert(result[2] == 3);
}
// Test that one can call std::views::transform with arbitrary stuff, as long as we
// don't try to actually complete the call by passing it a range.
//
// That makes no sense and we can't do anything with the result, but it's valid.
{
struct X { };
auto partial = std::views::transform(X{});
(void)partial;
}
// Test `adaptor | views::transform(f)`
{
{
using Result = std::ranges::transform_view<std::ranges::transform_view<MoveOnlyView, PlusOne>, TimesTwo>;
std::same_as<Result> auto result = MoveOnlyView{buff} | std::views::transform(PlusOne{}) | std::views::transform(TimesTwo{});
assert(result.begin().base().base() == buff);
assert(result[0] == 2);
assert(result[1] == 4);
assert(result[2] == 6);
}
{
auto const partial = std::views::transform(PlusOne{}) | std::views::transform(TimesTwo{});
using Result = std::ranges::transform_view<std::ranges::transform_view<MoveOnlyView, PlusOne>, TimesTwo>;
std::same_as<Result> auto result = MoveOnlyView{buff} | partial;
assert(result.begin().base().base() == buff);
assert(result[0] == 2);
assert(result[1] == 4);
assert(result[2] == 6);
}
}
// Test SFINAE friendliness
{
struct NotAView { };
struct NotInvocable { };
static_assert(!CanBePiped<MoveOnlyView, decltype(std::views::transform)>);
static_assert( CanBePiped<MoveOnlyView, decltype(std::views::transform(PlusOne{}))>);
static_assert(!CanBePiped<NotAView, decltype(std::views::transform(PlusOne{}))>);
static_assert(!CanBePiped<MoveOnlyView, decltype(std::views::transform(NotInvocable{}))>);
static_assert(!std::is_invocable_v<decltype(std::views::transform)>);
static_assert(!std::is_invocable_v<decltype(std::views::transform), PlusOne, MoveOnlyView>);
static_assert( std::is_invocable_v<decltype(std::views::transform), MoveOnlyView, PlusOne>);
static_assert(!std::is_invocable_v<decltype(std::views::transform), MoveOnlyView, PlusOne, PlusOne>);
static_assert(!std::is_invocable_v<decltype(std::views::transform), NonCopyableFunction>);
}
{
static_assert(std::is_same_v<decltype(std::ranges::views::transform), decltype(std::views::transform)>);
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|