xref: /llvm-project/libcxx/test/std/numerics/numeric.ops/reduce/reduce_init.pass.cpp (revision 5425106e493e26f7e0a87077c8eca657a9aff293)
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
10*5425106eSLouis Dionne 
11*5425106eSLouis Dionne // <numeric>
129a102b09SMarshall Clow 
1367c88e47SMark de Wever // Became constexpr in C++20
149a102b09SMarshall Clow // template<class InputIterator, class T>
159a102b09SMarshall Clow //   T reduce(InputIterator first, InputIterator last, T init);
169a102b09SMarshall Clow 
179a102b09SMarshall Clow #include <numeric>
189a102b09SMarshall Clow #include <cassert>
199a102b09SMarshall Clow 
207fc6a556SMarshall Clow #include "test_macros.h"
219a102b09SMarshall Clow #include "test_iterators.h"
229a102b09SMarshall Clow 
239a102b09SMarshall Clow template <class Iter, class T>
2467c88e47SMark de Wever TEST_CONSTEXPR_CXX20 void
test(Iter first,Iter last,T init,T x)259a102b09SMarshall Clow test(Iter first, Iter last, T init, T x)
269a102b09SMarshall Clow {
279a102b09SMarshall Clow     static_assert( std::is_same_v<T, decltype(std::reduce(first, last, init))> );
289a102b09SMarshall Clow     assert(std::reduce(first, last, init) == x);
299a102b09SMarshall Clow }
309a102b09SMarshall Clow 
319a102b09SMarshall Clow template <class Iter>
3267c88e47SMark de Wever TEST_CONSTEXPR_CXX20 void
test()339a102b09SMarshall Clow test()
349a102b09SMarshall Clow {
359a102b09SMarshall Clow     int ia[] = {1, 2, 3, 4, 5, 6};
369a102b09SMarshall Clow     unsigned sa = sizeof(ia) / sizeof(ia[0]);
379a102b09SMarshall Clow     test(Iter(ia), Iter(ia), 0, 0);
389a102b09SMarshall Clow     test(Iter(ia), Iter(ia), 1, 1);
399a102b09SMarshall Clow     test(Iter(ia), Iter(ia+1), 0, 1);
409a102b09SMarshall Clow     test(Iter(ia), Iter(ia+1), 2, 3);
419a102b09SMarshall Clow     test(Iter(ia), Iter(ia+2), 0, 3);
429a102b09SMarshall Clow     test(Iter(ia), Iter(ia+2), 3, 6);
439a102b09SMarshall Clow     test(Iter(ia), Iter(ia+sa), 0, 21);
449a102b09SMarshall Clow     test(Iter(ia), Iter(ia+sa), 4, 25);
459a102b09SMarshall Clow }
469a102b09SMarshall Clow 
479a102b09SMarshall Clow template <typename T, typename Init>
4867c88e47SMark de Wever TEST_CONSTEXPR_CXX20 void
test_return_type()4967c88e47SMark de Wever test_return_type()
509a102b09SMarshall Clow {
519a102b09SMarshall Clow     T *p = nullptr;
529a102b09SMarshall Clow     static_assert( std::is_same_v<Init, decltype(std::reduce(p, p, Init{}))> );
539a102b09SMarshall Clow }
549a102b09SMarshall Clow 
5567c88e47SMark de Wever TEST_CONSTEXPR_CXX20 bool
test()5667c88e47SMark de Wever test()
579a102b09SMarshall Clow {
589a102b09SMarshall Clow     test_return_type<char, int>();
599a102b09SMarshall Clow     test_return_type<int, int>();
609a102b09SMarshall Clow     test_return_type<int, unsigned long>();
619a102b09SMarshall Clow     test_return_type<float, int>();
629a102b09SMarshall Clow     test_return_type<short, float>();
639a102b09SMarshall Clow     test_return_type<double, char>();
649a102b09SMarshall Clow     test_return_type<char, double>();
659a102b09SMarshall Clow 
66773ae441SChristopher Di Bella     test<cpp17_input_iterator<const int*> >();
679a102b09SMarshall Clow     test<forward_iterator<const int*> >();
689a102b09SMarshall Clow     test<bidirectional_iterator<const int*> >();
699a102b09SMarshall Clow     test<random_access_iterator<const int*> >();
709a102b09SMarshall Clow     test<const int*>();
712df59c50SJF Bastien 
7267c88e47SMark de Wever     return true;
7367c88e47SMark de Wever }
7467c88e47SMark de Wever 
main(int,char **)7567c88e47SMark de Wever int main(int, char**)
7667c88e47SMark de Wever {
7767c88e47SMark de Wever     test();
7867c88e47SMark de Wever #if TEST_STD_VER > 17
7967c88e47SMark de Wever     static_assert(test());
8067c88e47SMark de Wever #endif
812df59c50SJF Bastien     return 0;
829a102b09SMarshall Clow }
83