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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
//===----------------------------------------------------------------------===//
//
// 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
// <algorithm>
// template<input_range R1, input_range R2, weakly_incrementable O,
// copy_constructible F, class Proj1 = identity, class Proj2 = identity>
// requires indirectly_writable<O, indirect_result_t<F&, projected<iterator_t<R1>, Proj1>,
// projected<iterator_t<R2>, Proj2>>>
// constexpr ranges::binary_transform_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>, O>
// ranges::transform(R1&& r1, R2&& r2, O result,
// F binary_op, Proj1 proj1 = {}, Proj2 proj2 = {});
// The iterator overloads are tested in ranges.transform.binary.iterator.pass.cpp.
#include <algorithm>
#include <array>
#include <cassert>
#include <functional>
#include <ranges>
#include "test_iterators.h"
#include "almost_satisfies_types.h"
struct BinaryFunc {
int operator()(int, int);
};
template <class Range>
concept HasTransformR = requires(Range r, int* out) { std::ranges::transform(r, r, out, BinaryFunc{}); };
static_assert(HasTransformR<std::array<int, 1>>);
static_assert(!HasTransformR<int>);
static_assert(!HasTransformR<InputRangeNotDerivedFrom>);
static_assert(!HasTransformR<InputRangeNotIndirectlyReadable>);
static_assert(!HasTransformR<InputRangeNotInputOrOutputIterator>);
static_assert(!HasTransformR<InputRangeNotSentinelSemiregular>);
static_assert(!HasTransformR<InputRangeNotSentinelEqualityComparableWith>);
template <class It>
concept HasTransformOut = requires(int* it, int* sent, It out, std::array<int, 2> range) {
std::ranges::transform(range, range, out, BinaryFunc{});
};
static_assert(HasTransformOut<int*>);
static_assert(!HasTransformOut<WeaklyIncrementableNotMovable>);
// check indirectly_readable
static_assert(HasTransformOut<char*>);
static_assert(!HasTransformOut<int**>);
struct MoveOnlyFunctor {
MoveOnlyFunctor(const MoveOnlyFunctor&) = delete;
MoveOnlyFunctor(MoveOnlyFunctor&&) = default;
int operator()(int, int);
};
template <class Func>
concept HasTransformFuncBinary = requires(int* it, int* sent, int* out, std::array<int, 2> range, Func func) {
std::ranges::transform(range, range, out, func);
};
static_assert(HasTransformFuncBinary<BinaryFunc>);
static_assert(!HasTransformFuncBinary<MoveOnlyFunctor>);
static_assert(std::is_same_v<std::ranges::binary_transform_result<int, long, char>,
std::ranges::in_in_out_result<int, long, char>>);
// clang-format off
template <class In1, class In2, class Out, class Sent1, class Sent2>
constexpr bool test_iterators() {
{ // simple
int a[] = {1, 2, 3, 4, 5};
int b[] = {5, 4, 3, 2, 1};
int c[5];
auto range1 = std::ranges::subrange(In1(a), Sent1(In1(a + 5)));
auto range2 = std::ranges::subrange(In2(b), Sent2(In2(b + 5)));
std::same_as<std::ranges::in_in_out_result<In1, In2, Out>> decltype(auto) ret = std::ranges::transform(
range1, range2, Out(c), [](int i, int j) { return i + j; });
assert((std::to_array(c) == std::array{6, 6, 6, 6, 6}));
assert(base(ret.in1) == a + 5);
assert(base(ret.in2) == b + 5);
assert(base(ret.out) == c + 5);
}
{ // first range empty
std::array<int, 0> a = {};
int b[] = {5, 4, 3, 2, 1};
int c[5];
auto range1 = std::ranges::subrange(In1(a.data()), Sent1(In1(a.data())));
auto range2 = std::ranges::subrange(In2(b), Sent2(In2(b + 5)));
auto ret = std::ranges::transform(range1, range2, Out(c), [](int i, int j) { return i + j; });
assert(base(ret.in1) == a.data());
assert(base(ret.in2) == b);
assert(base(ret.out) == c);
}
{ // second range empty
int a[] = {5, 4, 3, 2, 1};
std::array<int, 0> b = {};
int c[5];
auto range1 = std::ranges::subrange(In1(a), Sent1(In1(a + 5)));
auto range2 = std::ranges::subrange(In2(b.data()), Sent2(In2(b.data())));
auto ret = std::ranges::transform(range1, range2, Out(c), [](int i, int j) { return i + j; });
assert(base(ret.in1) == a);
assert(base(ret.in2) == b.data());
assert(base(ret.out) == c);
}
{ // both ranges empty
std::array<int, 0> a = {};
std::array<int, 0> b = {};
int c[5];
auto range1 = std::ranges::subrange(In1(a.data()), Sent1(In1(a.data())));
auto range2 = std::ranges::subrange(In2(b.data()), Sent2(In2(b.data())));
auto ret = std::ranges::transform(range1, range2, Out(c), [](int i, int j) { return i + j; });
assert(base(ret.in1) == a.data());
assert(base(ret.in2) == b.data());
assert(base(ret.out) == c);
}
{ // first range one element
int a[] = {2};
int b[] = {5, 4, 3, 2, 1};
int c[5];
auto range1 = std::ranges::subrange(In1(a), Sent1(In1(a + 1)));
auto range2 = std::ranges::subrange(In2(b), Sent2(In2(b + 5)));
auto ret = std::ranges::transform(range1, range2, Out(c), [](int i, int j) { return i + j; });
assert(c[0] == 7);
assert(base(ret.in1) == a + 1);
assert(base(ret.in2) == b + 1);
assert(base(ret.out) == c + 1);
}
{ // second range contains one element
int a[] = {5, 4, 3, 2, 1};
int b[] = {4};
int c[5];
auto range1 = std::ranges::subrange(In1(a), Sent1(In1(a + 5)));
auto range2 = std::ranges::subrange(In2(b), Sent2(In2(b + 1)));
auto ret = std::ranges::transform(range1, range2, Out(c), [](int i, int j) { return i + j; });
assert(c[0] == 9);
assert(base(ret.in1) == a + 1);
assert(base(ret.in2) == b + 1);
assert(base(ret.out) == c + 1);
}
{ // check that the transform function and projection call counts are correct
int predCount = 0;
int proj1Count = 0;
int proj2Count = 0;
auto pred = [&](int, int) { ++predCount; return 1; };
auto proj1 = [&](int) { ++proj1Count; return 0; };
auto proj2 = [&](int) { ++proj2Count; return 0; };
int a[] = {1, 2, 3, 4};
int b[] = {1, 2, 3, 4};
std::array<int, 4> c;
auto range1 = std::ranges::subrange(In1(a), Sent1(In1(a + 4)));
auto range2 = std::ranges::subrange(In2(b), Sent2(In2(b + 4)));
std::ranges::transform(range1, range2, Out(c.data()), pred, proj1, proj2);
assert(predCount == 4);
assert(proj1Count == 4);
assert(proj2Count == 4);
assert((c == std::array{1, 1, 1, 1}));
}
return true;
}
// clang-format on
template <class In2, class Out, class Sent2 = In2>
constexpr void test_iterator_in1() {
test_iterators<cpp17_input_iterator<int*>, In2, Out, sentinel_wrapper<cpp17_input_iterator<int*>>, Sent2>();
test_iterators<cpp20_input_iterator<int*>, In2, Out, sentinel_wrapper<cpp20_input_iterator<int*>>, Sent2>();
test_iterators<forward_iterator<int*>, In2, Out, forward_iterator<int*>, Sent2>();
test_iterators<bidirectional_iterator<int*>, In2, Out, bidirectional_iterator<int*>, Sent2>();
test_iterators<random_access_iterator<int*>, In2, Out, random_access_iterator<int*>, Sent2>();
test_iterators<contiguous_iterator<int*>, In2, Out, contiguous_iterator<int*>, Sent2>();
test_iterators<int*, In2, Out, int*, Sent2>();
// static_asserting here to avoid hitting the constant evaluation step limit
static_assert(test_iterators<cpp17_input_iterator<int*>, In2, Out, sentinel_wrapper<cpp17_input_iterator<int*>>, Sent2>());
static_assert(test_iterators<cpp20_input_iterator<int*>, In2, Out, sentinel_wrapper<cpp20_input_iterator<int*>>, Sent2>());
static_assert(test_iterators<forward_iterator<int*>, In2, Out, forward_iterator<int*>, Sent2>());
static_assert(test_iterators<bidirectional_iterator<int*>, In2, Out, bidirectional_iterator<int*>, Sent2>());
static_assert(test_iterators<random_access_iterator<int*>, In2, Out, random_access_iterator<int*>, Sent2>());
static_assert(test_iterators<contiguous_iterator<int*>, In2, Out, contiguous_iterator<int*>, Sent2>());
static_assert(test_iterators<int*, In2, Out, int*, Sent2>());
}
template <class Out>
constexpr void test_iterators_in1_in2() {
test_iterator_in1<cpp17_input_iterator<int*>, Out, sentinel_wrapper<cpp17_input_iterator<int*>>>();
test_iterator_in1<cpp20_input_iterator<int*>, Out, sentinel_wrapper<cpp20_input_iterator<int*>>>();
test_iterator_in1<forward_iterator<int*>, Out>();
test_iterator_in1<bidirectional_iterator<int*>, Out>();
test_iterator_in1<random_access_iterator<int*>, Out>();
test_iterator_in1<contiguous_iterator<int*>, Out>();
test_iterator_in1<int*, Out>();
}
constexpr bool test() {
test_iterators_in1_in2<cpp17_output_iterator<int*>>();
test_iterators_in1_in2<cpp20_output_iterator<int*>>();
test_iterators_in1_in2<forward_iterator<int*>>();
test_iterators_in1_in2<bidirectional_iterator<int*>>();
test_iterators_in1_in2<random_access_iterator<int*>>();
test_iterators_in1_in2<contiguous_iterator<int*>>();
test_iterators_in1_in2<int*>();
{ // check that std::ranges::dangling is returned properly
{
int b[] = {2, 5, 4, 3, 1};
std::array<int, 5> c;
std::same_as<std::ranges::in_in_out_result<std::ranges::dangling, int*, int*>> auto ret =
std::ranges::transform(std::array{1, 2, 3, 5, 4}, b, c.data(), [](int i, int j) { return i * j; });
assert((c == std::array{2, 10, 12, 15, 4}));
assert(ret.in2 == b + 5);
assert(ret.out == c.data() + c.size());
}
{
int a[] = {2, 5, 4, 3, 1, 4, 5, 6};
std::array<int, 8> c;
std::same_as<std::ranges::in_in_out_result<int*, std::ranges::dangling, int*>> auto ret =
std::ranges::transform(a, std::array{1, 2, 3, 5, 4, 5, 6, 7}, c.data(), [](int i, int j) { return i * j; });
assert((c == std::array{2, 10, 12, 15, 4, 20, 30, 42}));
assert(ret.in1 == a + 8);
assert(ret.out == c.data() + c.size());
}
{
std::array<int, 3> c;
std::same_as<std::ranges::in_in_out_result<std::ranges::dangling, std::ranges::dangling, int*>> auto ret =
std::ranges::transform(std::array{4, 4, 4}, std::array{4, 4, 4}, c.data(), [](int i, int j) { return i * j; });
assert((c == std::array{16, 16, 16}));
assert(ret.out == c.data() + c.size());
}
}
{ // check that returning another type from the projection works
struct S { int i; int other; };
S a[] = { S{0, 0}, S{1, 0}, S{3, 0}, S{10, 0} };
S b[] = { S{0, 10}, S{1, 20}, S{3, 30}, S{10, 40} };
std::array<int, 4> c;
std::ranges::transform(a, b, c.begin(), [](S s1, S s2) { return s1.i + s2.other; });
assert((c == std::array{10, 21, 33, 50}));
}
{ // check that std::invoke is used
struct S { int i; };
S a[] = { S{1}, S{3}, S{2} };
S b[] = { S{2}, S{5}, S{3} };
std::array<int, 3> c;
auto ret = std::ranges::transform(a, b, c.data(), [](int i, int j) { return i + j + 2; }, &S::i, &S::i);
assert((c == std::array{5, 10, 7}));
assert(ret.out == c.data() + 3);
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|