xref: /llvm-project/libcxx/test/std/numerics/numeric.ops/reduce/reduce_init_op.pass.cpp (revision 34f73804ed60e90276e2303398162c2fc05f47ec)
19a102b09SMarshall Clow //===----------------------------------------------------------------------===//
29a102b09SMarshall Clow //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69a102b09SMarshall Clow //
79a102b09SMarshall Clow //===----------------------------------------------------------------------===//
89a102b09SMarshall Clow 
931cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11, c++14
105425106eSLouis Dionne 
115425106eSLouis Dionne // <numeric>
129a102b09SMarshall Clow 
1367c88e47SMark de Wever // Became constexpr in C++20
149a102b09SMarshall Clow // template<class InputIterator, class T, class BinaryOperation>
159a102b09SMarshall Clow //   T reduce(InputIterator first, InputIterator last, T init, BinaryOperation op);
169a102b09SMarshall Clow 
179a102b09SMarshall Clow #include <numeric>
189a102b09SMarshall Clow #include <cassert>
19*34f73804SNikolas Klauser #include <functional>
209a102b09SMarshall Clow 
217fc6a556SMarshall Clow #include "test_macros.h"
229a102b09SMarshall Clow #include "test_iterators.h"
239a102b09SMarshall Clow 
249a102b09SMarshall Clow template <class Iter, class T, class Op>
2567c88e47SMark de Wever TEST_CONSTEXPR_CXX20 void
test(Iter first,Iter last,T init,Op op,T x)269a102b09SMarshall Clow test(Iter first, Iter last, T init, Op op, T x)
279a102b09SMarshall Clow {
289a102b09SMarshall Clow     static_assert( std::is_same_v<T, decltype(std::reduce(first, last, init, op))>, "" );
299a102b09SMarshall Clow     assert(std::reduce(first, last, init, op) == x);
309a102b09SMarshall Clow }
319a102b09SMarshall Clow 
329a102b09SMarshall Clow template <class Iter>
3367c88e47SMark de Wever TEST_CONSTEXPR_CXX20 void
test()349a102b09SMarshall Clow test()
359a102b09SMarshall Clow {
369a102b09SMarshall Clow     int ia[] = {1, 2, 3, 4, 5, 6};
379a102b09SMarshall Clow     unsigned sa = sizeof(ia) / sizeof(ia[0]);
389a102b09SMarshall Clow     test(Iter(ia), Iter(ia), 0, std::plus<>(), 0);
399a102b09SMarshall Clow     test(Iter(ia), Iter(ia), 1, std::multiplies<>(), 1);
409a102b09SMarshall Clow     test(Iter(ia), Iter(ia+1), 0, std::plus<>(), 1);
419a102b09SMarshall Clow     test(Iter(ia), Iter(ia+1), 2, std::multiplies<>(), 2);
429a102b09SMarshall Clow     test(Iter(ia), Iter(ia+2), 0, std::plus<>(), 3);
439a102b09SMarshall Clow     test(Iter(ia), Iter(ia+2), 3, std::multiplies<>(), 6);
449a102b09SMarshall Clow     test(Iter(ia), Iter(ia+sa), 0, std::plus<>(), 21);
459a102b09SMarshall Clow     test(Iter(ia), Iter(ia+sa), 4, std::multiplies<>(), 2880);
469a102b09SMarshall Clow }
479a102b09SMarshall Clow 
489a102b09SMarshall Clow template <typename T, typename Init>
4967c88e47SMark de Wever TEST_CONSTEXPR_CXX20 void
test_return_type()5067c88e47SMark de Wever test_return_type()
519a102b09SMarshall Clow {
529a102b09SMarshall Clow     T *p = nullptr;
539a102b09SMarshall Clow     static_assert( std::is_same_v<Init, decltype(std::reduce(p, p, Init{}, std::plus<>()))>, "" );
549a102b09SMarshall Clow }
559a102b09SMarshall Clow 
5667c88e47SMark de Wever TEST_CONSTEXPR_CXX20 bool
test()5767c88e47SMark de Wever test()
589a102b09SMarshall Clow {
599a102b09SMarshall Clow     test_return_type<char, int>();
609a102b09SMarshall Clow     test_return_type<int, int>();
619a102b09SMarshall Clow     test_return_type<int, unsigned long>();
629a102b09SMarshall Clow     test_return_type<float, int>();
639a102b09SMarshall Clow     test_return_type<short, float>();
649a102b09SMarshall Clow     test_return_type<double, char>();
659a102b09SMarshall Clow     test_return_type<char, double>();
669a102b09SMarshall Clow 
67773ae441SChristopher Di Bella     test<cpp17_input_iterator<const int*> >();
689a102b09SMarshall Clow     test<forward_iterator<const int*> >();
699a102b09SMarshall Clow     test<bidirectional_iterator<const int*> >();
709a102b09SMarshall Clow     test<random_access_iterator<const int*> >();
719a102b09SMarshall Clow     test<const int*>();
729a102b09SMarshall Clow 
739a102b09SMarshall Clow //  Make sure the math is done using the correct type
749a102b09SMarshall Clow     {
759a102b09SMarshall Clow     auto v = {1, 2, 3, 4, 5, 6, 7, 8};
769a102b09SMarshall Clow     unsigned res = std::reduce(v.begin(), v.end(), 1U, std::multiplies<>());
779a102b09SMarshall Clow     assert(res == 40320);       // 8! will not fit into a char
789a102b09SMarshall Clow     }
792df59c50SJF Bastien 
8067c88e47SMark de Wever     return true;
8167c88e47SMark de Wever }
8267c88e47SMark de Wever 
main(int,char **)8367c88e47SMark de Wever int main(int, char**)
8467c88e47SMark de Wever {
8567c88e47SMark de Wever     test();
8667c88e47SMark de Wever #if TEST_STD_VER > 17
8767c88e47SMark de Wever     static_assert(test());
8867c88e47SMark de Wever #endif
892df59c50SJF Bastien     return 0;
909a102b09SMarshall Clow }
91