Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / numerics / numeric.ops / reduce / reduce_init_op.pass.cpp
blob347d3755a07cf06e5169cd0a127b54bd72261a07
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
11 // <numeric>
13 // Became constexpr in C++20
14 // template<class InputIterator, class T, class BinaryOperation>
15 // T reduce(InputIterator first, InputIterator last, T init, BinaryOperation op);
17 #include <numeric>
18 #include <cassert>
19 #include <functional>
21 #include "test_macros.h"
22 #include "test_iterators.h"
24 template <class Iter, class T, class Op>
25 TEST_CONSTEXPR_CXX20 void
26 test(Iter first, Iter last, T init, Op op, T x)
28 static_assert( std::is_same_v<T, decltype(std::reduce(first, last, init, op))>, "" );
29 assert(std::reduce(first, last, init, op) == x);
32 template <class Iter>
33 TEST_CONSTEXPR_CXX20 void
34 test()
36 int ia[] = {1, 2, 3, 4, 5, 6};
37 unsigned sa = sizeof(ia) / sizeof(ia[0]);
38 test(Iter(ia), Iter(ia), 0, std::plus<>(), 0);
39 test(Iter(ia), Iter(ia), 1, std::multiplies<>(), 1);
40 test(Iter(ia), Iter(ia+1), 0, std::plus<>(), 1);
41 test(Iter(ia), Iter(ia+1), 2, std::multiplies<>(), 2);
42 test(Iter(ia), Iter(ia+2), 0, std::plus<>(), 3);
43 test(Iter(ia), Iter(ia+2), 3, std::multiplies<>(), 6);
44 test(Iter(ia), Iter(ia+sa), 0, std::plus<>(), 21);
45 test(Iter(ia), Iter(ia+sa), 4, std::multiplies<>(), 2880);
48 template <typename T, typename Init>
49 TEST_CONSTEXPR_CXX20 void
50 test_return_type()
52 T *p = nullptr;
53 static_assert( std::is_same_v<Init, decltype(std::reduce(p, p, Init{}, std::plus<>()))>, "" );
56 TEST_CONSTEXPR_CXX20 bool
57 test()
59 test_return_type<char, int>();
60 test_return_type<int, int>();
61 test_return_type<int, unsigned long>();
62 test_return_type<float, int>();
63 test_return_type<short, float>();
64 test_return_type<double, char>();
65 test_return_type<char, double>();
67 test<cpp17_input_iterator<const int*> >();
68 test<forward_iterator<const int*> >();
69 test<bidirectional_iterator<const int*> >();
70 test<random_access_iterator<const int*> >();
71 test<const int*>();
73 // Make sure the math is done using the correct type
75 auto v = {1, 2, 3, 4, 5, 6, 7, 8};
76 unsigned res = std::reduce(v.begin(), v.end(), 1U, std::multiplies<>());
77 assert(res == 40320); // 8! will not fit into a char
80 return true;
83 int main(int, char**)
85 test();
86 #if TEST_STD_VER > 17
87 static_assert(test());
88 #endif
89 return 0;