| 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.5 [transform.reduce] |
| 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, 8, 9, 10}; |
| 28 |
double b[] = {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5}; |
| 29 |
|
| 30 |
using __gnu_test::test_container; |
| 31 |
using __gnu_test::input_iterator_wrapper; |
| 32 |
|
| 33 |
/* |
| 34 |
template<class InputIterator1, class InputIterator2, class T> |
| 35 |
T transform_reduce(InputIterator1, InputIterator1, InputIterator2, T); |
| 36 |
*/ |
| 37 |
void |
| 38 |
test01() |
| 39 |
{ |
| 40 |
auto res = std::transform_reduce(std::begin(a), std::end(a), std::begin(b), |
| 41 |
1.0f); |
| 42 |
static_assert(std::is_same_v<decltype(res), float>); |
| 43 |
VERIFY( res == (float)(1 + 0.5 + 1 + 1.5 + 2 + 2.5 + 3 + 3.5 + 4 + 4.5 + 5) ); |
| 44 |
|
| 45 |
test_container<int, input_iterator_wrapper> ca(a); |
| 46 |
test_container<double, input_iterator_wrapper> cb(b); |
| 47 |
|
| 48 |
auto res2 = std::transform_reduce(ca.begin(), ca.end(), cb.begin(), |
| 49 |
1.0f); |
| 50 |
static_assert(std::is_same_v<decltype(res2), float>); |
| 51 |
VERIFY( res2 == res ); |
| 52 |
} |
| 53 |
|
| 54 |
/* |
| 55 |
template<class InputIterator1, class InputIterator2, class T, |
| 56 |
class BinaryOperation1, class BinaryOperation2> |
| 57 |
T transform_reduce(InputIterator1, InputIterator1, InputIterator2, T, |
| 58 |
BinaryOperation1, BinaryOperation2); |
| 59 |
*/ |
| 60 |
void |
| 61 |
test02() |
| 62 |
{ |
| 63 |
auto res = std::transform_reduce(std::begin(a), std::end(a), std::begin(b), |
| 64 |
1L, std::multiplies<>(), std::plus<int>()); |
| 65 |
static_assert(std::is_same_v<decltype(res), long>); |
| 66 |
VERIFY( res == (1L * 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10) ); |
| 67 |
|
| 68 |
test_container<int, input_iterator_wrapper> ca(a); |
| 69 |
test_container<double, input_iterator_wrapper> cb(b); |
| 70 |
|
| 71 |
auto res2 = std::transform_reduce(ca.begin(), ca.end(), cb.begin(), |
| 72 |
1L, std::multiplies<>(), std::plus<int>()); |
| 73 |
static_assert(std::is_same_v<decltype(res2), long>); |
| 74 |
VERIFY( res2 == res ); |
| 75 |
} |
| 76 |
|
| 77 |
/* |
| 78 |
template<class InputIterator, class T, class BinaryOperation, |
| 79 |
class UnaryOperation> |
| 80 |
T transform_reduce(InputIterator, InputIterator, T, |
| 81 |
BinaryOperation, UnaryOperation); |
| 82 |
*/ |
| 83 |
void |
| 84 |
test03() |
| 85 |
{ |
| 86 |
auto res = std::transform_reduce(std::begin(a), std::end(a), 10.0, |
| 87 |
std::plus<>(), |
| 88 |
[](int i) { return i * i; }); |
| 89 |
static_assert(std::is_same_v<decltype(res), double>); |
| 90 |
VERIFY( res == (10.0 + 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100) ); |
| 91 |
|
| 92 |
test_container<int, input_iterator_wrapper> ca(a); |
| 93 |
test_container<double, input_iterator_wrapper> cb(b); |
| 94 |
|
| 95 |
auto res2 = std::transform_reduce(ca.begin(), ca.end(), 10.0, |
| 96 |
std::plus<>(), |
| 97 |
[](int i) { return i * i; }); |
| 98 |
static_assert(std::is_same_v<decltype(res2), double>); |
| 99 |
VERIFY( res2 == (10.0 + 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100) ); |
| 100 |
} |
| 101 |
|
| 102 |
int |
| 103 |
main() |
| 104 |
{ |
| 105 |
test01(); |
| 106 |
test02(); |
| 107 |
test03(); |
| 108 |
} |