xref: /llvm-project/libcxx/test/std/numerics/numeric.ops/reduce/reduce.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>
159a102b09SMarshall Clow //     typename iterator_traits<InputIterator>::value_type
169a102b09SMarshall Clow //     reduce(InputIterator first, InputIterator last);
179a102b09SMarshall Clow 
189a102b09SMarshall Clow #include <numeric>
199a102b09SMarshall Clow #include <cassert>
209a102b09SMarshall Clow 
217fc6a556SMarshall Clow #include "test_macros.h"
229a102b09SMarshall Clow #include "test_iterators.h"
239a102b09SMarshall Clow 
249a102b09SMarshall Clow template <class Iter, class T>
2567c88e47SMark de Wever TEST_CONSTEXPR_CXX20 void
test(Iter first,Iter last,T x)269a102b09SMarshall Clow test(Iter first, Iter last, T x)
279a102b09SMarshall Clow {
289a102b09SMarshall Clow     static_assert( std::is_same_v<typename std::iterator_traits<decltype(first)>::value_type,
299a102b09SMarshall Clow                                 decltype(std::reduce(first, last))> );
309a102b09SMarshall Clow     assert(std::reduce(first, last) == x);
319a102b09SMarshall Clow }
329a102b09SMarshall Clow 
339a102b09SMarshall Clow template <class Iter>
3467c88e47SMark de Wever TEST_CONSTEXPR_CXX20 void
test()359a102b09SMarshall Clow test()
369a102b09SMarshall Clow {
379a102b09SMarshall Clow     int ia[] = {1, 2, 3, 4, 5, 6};
389a102b09SMarshall Clow     unsigned sa = sizeof(ia) / sizeof(ia[0]);
399a102b09SMarshall Clow     test(Iter(ia), Iter(ia), 0);
409a102b09SMarshall Clow     test(Iter(ia), Iter(ia+1), 1);
419a102b09SMarshall Clow     test(Iter(ia), Iter(ia+2), 3);
429a102b09SMarshall Clow     test(Iter(ia), Iter(ia+sa), 21);
439a102b09SMarshall Clow }
449a102b09SMarshall Clow 
459a102b09SMarshall Clow template <typename T>
4667c88e47SMark de Wever TEST_CONSTEXPR_CXX20 void
test_return_type()4767c88e47SMark de Wever test_return_type()
489a102b09SMarshall Clow {
499a102b09SMarshall Clow     T *p = nullptr;
509a102b09SMarshall Clow     static_assert( std::is_same_v<T, decltype(std::reduce(p, p))> );
519a102b09SMarshall Clow }
529a102b09SMarshall Clow 
5367c88e47SMark de Wever TEST_CONSTEXPR_CXX20 bool
test()5467c88e47SMark de Wever test()
559a102b09SMarshall Clow {
569a102b09SMarshall Clow     test_return_type<char>();
579a102b09SMarshall Clow     test_return_type<int>();
589a102b09SMarshall Clow     test_return_type<unsigned long>();
599a102b09SMarshall Clow     test_return_type<float>();
609a102b09SMarshall Clow     test_return_type<double>();
619a102b09SMarshall Clow 
62773ae441SChristopher Di Bella     test<cpp17_input_iterator<const int*> >();
639a102b09SMarshall Clow     test<forward_iterator<const int*> >();
649a102b09SMarshall Clow     test<bidirectional_iterator<const int*> >();
659a102b09SMarshall Clow     test<random_access_iterator<const int*> >();
669a102b09SMarshall Clow     test<const int*>();
672df59c50SJF Bastien 
6867c88e47SMark de Wever     return true;
6967c88e47SMark de Wever }
7067c88e47SMark de Wever 
main(int,char **)7167c88e47SMark de Wever int main(int, char**)
7267c88e47SMark de Wever {
7367c88e47SMark de Wever     test();
7467c88e47SMark de Wever #if TEST_STD_VER > 17
7567c88e47SMark de Wever     static_assert(test());
7667c88e47SMark de Wever #endif
772df59c50SJF Bastien     return 0;
789a102b09SMarshall Clow }
79