| 1 |
// { dg-do run { target c++17 } } |
| 2 |
|
| 3 |
// Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// You should have received a copy of the GNU General Public License along |
| 17 |
// with this library; see the file COPYING3. If not see |
| 18 |
// <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
// C++17 29.8.10 [transform.inclusive.scan] |
| 21 |
|
| 22 |
#include <numeric> |
| 23 |
#include <iterator> |
| 24 |
#include <testsuite_hooks.h> |
| 25 |
#include <testsuite_iterators.h> |
| 26 |
|
| 27 |
int a[] = {1, 2, 3, 4, 5, 6, 7}; |
| 28 |
|
| 29 |
using __gnu_test::test_container; |
| 30 |
using __gnu_test::input_iterator_wrapper; |
| 31 |
using __gnu_test::output_iterator_wrapper; |
| 32 |
|
| 33 |
/* |
| 34 |
template<class InputIterator, class OutputIterator, class BinaryOperation, |
| 35 |
class UnaryOperation> |
| 36 |
OutputIterator |
| 37 |
transform_inclusive_scan(InputIterator, InputIterator, OutputIterator, |
| 38 |
BinaryOperation, UnaryOperation); |
| 39 |
*/ |
| 40 |
void |
| 41 |
test01() |
| 42 |
{ |
| 43 |
int out[7]; |
| 44 |
test_container<int, output_iterator_wrapper> co(out); |
| 45 |
test_container<int, input_iterator_wrapper> ca(a); |
| 46 |
auto end = std::transform_inclusive_scan(ca.begin(), ca.end(), co.begin(), |
| 47 |
std::multiplies<>(), |
| 48 |
[](int i) { return i+1; }); |
| 49 |
static_assert(std::is_same_v<decltype(end), decltype(co.begin())>); |
| 50 |
VERIFY( end.ptr == out+7 ); |
| 51 |
VERIFY( out[0] == 2 ); |
| 52 |
VERIFY( out[1] == (2*3) ); |
| 53 |
VERIFY( out[2] == (2*3*4) ); |
| 54 |
VERIFY( out[3] == (2*3*4*5) ); |
| 55 |
VERIFY( out[4] == (2*3*4*5*6) ); |
| 56 |
VERIFY( out[5] == (2*3*4*5*6*7) ); |
| 57 |
VERIFY( out[6] == (2*3*4*5*6*7*8) ); |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
template<class InputIterator, class OutputIterator, class BinaryOperation, |
| 62 |
class UnaryOperation, class T> |
| 63 |
OutputIterator |
| 64 |
transform_inclusive_scan(InputIterator, InputIterator, OutputIterator, |
| 65 |
BinaryOperation, UnaryOperation, T); |
| 66 |
*/ |
| 67 |
void |
| 68 |
test02() |
| 69 |
{ |
| 70 |
int out[7]; |
| 71 |
test_container<int, output_iterator_wrapper> co(out); |
| 72 |
test_container<int, input_iterator_wrapper> ca(a); |
| 73 |
auto end = std::transform_inclusive_scan(ca.begin(), ca.end(), co.begin(), |
| 74 |
std::multiplies<>(), |
| 75 |
[](int i) { return i+1; }, |
| 76 |
3); |
| 77 |
static_assert(std::is_same_v<decltype(end), decltype(co.begin())>); |
| 78 |
VERIFY( end.ptr == out+7 ); |
| 79 |
VERIFY( out[0] == 3*2 ); |
| 80 |
VERIFY( out[1] == (3*2*3) ); |
| 81 |
VERIFY( out[2] == (3*2*3*4) ); |
| 82 |
VERIFY( out[3] == (3*2*3*4*5) ); |
| 83 |
VERIFY( out[4] == (3*2*3*4*5*6) ); |
| 84 |
VERIFY( out[5] == (3*2*3*4*5*6*7) ); |
| 85 |
VERIFY( out[6] == (3*2*3*4*5*6*7*8) ); |
| 86 |
} |
| 87 |
|
| 88 |
int |
| 89 |
main() |
| 90 |
{ |
| 91 |
test01(); |
| 92 |
test02(); |
| 93 |
} |