| 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 <functional> |
| 22 |
#include <testsuite_hooks.h> |
| 23 |
#include <testsuite_iterators.h> |
| 24 |
|
| 25 |
using __gnu_test::test_container; |
| 26 |
using __gnu_test::test_range; |
| 27 |
using __gnu_test::input_iterator_wrapper; |
| 28 |
using __gnu_test::output_iterator_wrapper; |
| 29 |
using __gnu_test::forward_iterator_wrapper; |
| 30 |
|
| 31 |
namespace ranges = std::ranges; |
| 32 |
|
| 33 |
struct X { int i; }; |
| 34 |
|
| 35 |
void |
| 36 |
test01() |
| 37 |
{ |
| 38 |
{ |
| 39 |
int x[6] = { {2}, {4}, {6}, {8}, {10}, {11} }; |
| 40 |
int y[6] = { {3}, {5}, {7}, {9}, {11}, {12} }; |
| 41 |
auto [in, out] = ranges::transform(x, x, [] (int a) { return a+1; }); |
| 42 |
VERIFY( in == x+6 && out == x+6 ); |
| 43 |
VERIFY( ranges::equal(x, y) ); |
| 44 |
} |
| 45 |
|
| 46 |
{ |
| 47 |
X x[6] = { {2}, {4}, {6}, {8}, {10}, {11} }; |
| 48 |
int y[7] = { {3}, {5}, {7}, {9}, {11}, {12}, {0} }; |
| 49 |
int z[7] = { {0}, {0}, {0}, {0}, {0}, {0}, {0} }; |
| 50 |
test_container<X, forward_iterator_wrapper> cx(x); |
| 51 |
test_container<int, forward_iterator_wrapper> cy(y), cz(z); |
| 52 |
auto [in, out] |
| 53 |
= ranges::transform(cx, cz.begin(), [] (int a) { return a+1; }, &X::i); |
| 54 |
VERIFY( ranges::equal(cy, cz) ); |
| 55 |
VERIFY( in == cx.end() && ++out == cz.end() ); |
| 56 |
} |
| 57 |
|
| 58 |
{ |
| 59 |
X x[6] = { {2}, {4}, {6}, {8}, {10}, {11} }; |
| 60 |
X y[7] = { {3}, {5}, {7}, {9}, {11}, {12}, {0} }; |
| 61 |
int z[7] = { {0}, {0}, {0}, {0}, {0}, {0}, {0} }; |
| 62 |
test_range<X, input_iterator_wrapper> rx(x), ry(y); |
| 63 |
test_range<int, output_iterator_wrapper> rz(z); |
| 64 |
auto [in, out] |
| 65 |
= ranges::transform(rx, rz.begin(), [] (int a) { return a+1; }, &X::i); |
| 66 |
VERIFY( ranges::equal(ry, z, {}, &X::i) ); |
| 67 |
VERIFY( in == rx.end() && out.ptr == z+6 ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
struct Y { int i; int j; }; |
| 72 |
|
| 73 |
constexpr bool |
| 74 |
test02() |
| 75 |
{ |
| 76 |
int x[] = { 1, 2, 3 }; |
| 77 |
Y y[] = { {1,2}, {2,4}, {3,6} }; |
| 78 |
ranges::transform(y, y+3, x, [] (int a) { return -a; }, &Y::i); |
| 79 |
return x[0] == -1 && x[1] == -2 && x[2] == -3; |
| 80 |
} |
| 81 |
|
| 82 |
void |
| 83 |
test03() |
| 84 |
{ |
| 85 |
{ |
| 86 |
int x[6] = { {2}, {4}, {6}, {8}, {10}, {11} }; |
| 87 |
const int y[6] = { {3}, {5}, {7}, {9}, {11}, {12} }; |
| 88 |
int z[6] = { {5}, {9}, {13}, {17}, {21}, {23} }; |
| 89 |
auto [in1, in2, out] = ranges::transform(x, y, x, std::plus<>{}); |
| 90 |
VERIFY( in1 == x+6 && in2 == y+6 && out == x+6 ); |
| 91 |
VERIFY( ranges::equal(x, z) ); |
| 92 |
} |
| 93 |
|
| 94 |
{ |
| 95 |
int x[6] = { {2}, {4}, {6}, {8}, {10}, {11} }; |
| 96 |
const int y[6] = { {3}, {5}, {7}, {9}, {11}, {12} }; |
| 97 |
int z[6] = { {5}, {9}, {13}, {17}, {21}, {23} }; |
| 98 |
auto [in1, in2, out] = ranges::transform(y, x, x, std::plus<>{}); |
| 99 |
VERIFY( in1 == y+6 && in2 == x+6 && out == x+6 ); |
| 100 |
VERIFY( ranges::equal(x, z) ); |
| 101 |
} |
| 102 |
|
| 103 |
{ |
| 104 |
X x[6] = { {2}, {4}, {6}, {8}, {10}, {11} }; |
| 105 |
int y[7] = { {3}, {5}, {7}, {9}, {11}, {12}, {0} }; |
| 106 |
int z[6] = { {5}, {9}, {13}, {17}, {21}, {23} }; |
| 107 |
int w[6]; |
| 108 |
test_container<X, forward_iterator_wrapper> cx(x); |
| 109 |
test_container<int, forward_iterator_wrapper> cy(y), cz(z), cw(w); |
| 110 |
auto [in1, in2, out] |
| 111 |
= ranges::transform(cx, cy, cw.begin(), std::plus<>{}, &X::i); |
| 112 |
VERIFY( in1 == cx.end() && ++in2 == cy.end() && out == cw.end() ); |
| 113 |
VERIFY( ranges::equal(cw, cz) ); |
| 114 |
} |
| 115 |
|
| 116 |
{ |
| 117 |
X x[6] = { {2}, {4}, {6}, {8}, {10}, {11} }; |
| 118 |
int y[7] = { {3}, {5}, {7}, {9}, {11}, {12}, {0} }; |
| 119 |
int z[6] = { {5}, {9}, {13}, {17}, {21}, {23} }; |
| 120 |
int w[6]; |
| 121 |
test_range<X, input_iterator_wrapper> rx(x); |
| 122 |
test_range<int, input_iterator_wrapper> ry(y), rz(z); |
| 123 |
test_range<int, output_iterator_wrapper> rw(w); |
| 124 |
auto [in1, in2, out] |
| 125 |
= ranges::transform(rx, ry, rw.begin(), std::plus<>{}, &X::i); |
| 126 |
VERIFY( in1 == rx.end() && ++in2 == ry.end() && out.ptr == w+6 ); |
| 127 |
VERIFY( ranges::equal(w, rz) ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
constexpr bool |
| 132 |
test04() |
| 133 |
{ |
| 134 |
int x[3]; |
| 135 |
const Y y[3] = { {1,2}, {2,4}, {3,6} }; |
| 136 |
ranges::transform(y, y+3, y, y+3, x, std::plus<>{}, &Y::i, &Y::j); |
| 137 |
return x[0] == 3 && x[1] == 6 && x[2] == 9; |
| 138 |
} |
| 139 |
|
| 140 |
int |
| 141 |
main() |
| 142 |
{ |
| 143 |
test01(); |
| 144 |
static_assert(test02()); |
| 145 |
test03(); |
| 146 |
static_assert(test04()); |
| 147 |
} |
| 148 |
|