| 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.3 [reduce] |
| 21 | |
| 22 | #include <numeric> |
| 23 | #include <iterator> |
| 24 | #include <testsuite_hooks.h> |
| 25 | #include <testsuite_iterators.h> |
| 26 | |
| 27 | /* |
| 28 | template<class InputIterator> |
| 29 | iterator_traits<InputIterator>::value_type |
| 30 | reduce(InputIterator, InputIterator); |
| 31 | */ |
| 32 | void |
| 33 | test01() |
| 34 | { |
| 35 | using __gnu_test::test_container; |
| 36 | using __gnu_test::input_iterator_wrapper; |
| 37 | int array[5] = { 1, 2, 3, 4, 5 }; |
| 38 | test_container<int, input_iterator_wrapper> con(array); |
| 39 | int res = std::reduce(con.begin(), con.end()); |
| 40 | VERIFY( res == 15 ); |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | template<class InputIterator, class T> |
| 45 | T reduce(InputIterator, InputIterator, T); |
| 46 | */ |
| 47 | void |
| 48 | test02() |
| 49 | { |
| 50 | bool b[] = {true, false, true, true, false, true, false, true, true, false}; |
| 51 | int res = std::reduce(std::begin(b), std::end(b), 100); |
| 52 | VERIFY( res == 106 ); |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | template<class InputIterator, class T> |
| 57 | T reduce(InputIterator, InputIterator, T); |
| 58 | template<class InputIterator, class T, class BinaryOperation> |
| 59 | T reduce(InputIterator, InputIterator, T, BinaryOperation); |
| 60 | */ |
| 61 | void |
| 62 | test03() |
| 63 | { |
| 64 | int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| 65 | |
| 66 | auto res = std::reduce(std::begin(a), std::end(a), (short)11); |
| 67 | static_assert(std::is_same_v<decltype(res), short>); |
| 68 | VERIFY( res == 66 ); |
| 69 | |
| 70 | auto res2 = std::reduce(std::begin(a), std::end(a), -1l, std::multiplies<>()); |
| 71 | static_assert(std::is_same_v<decltype(res2), long>); |
| 72 | VERIFY( res2 == -3628800 ); |
| 73 | } |
| 74 | |
| 75 | int |
| 76 | main() |
| 77 | { |
| 78 | test01(); |
| 79 | test02(); |
| 80 | test03(); |
| 81 | } |