Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / numerics / numeric.ops / adjacent.difference / adjacent_difference.pass.cpp
blob5654a6d6055726d4628d4009dfaaadacea846364
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 // <numeric>
11 // Became constexpr in C++20
12 // template <InputIterator InIter,
13 // OutputIterator<auto, const InIter::value_type&> OutIter>
14 // requires HasMinus<InIter::value_type, InIter::value_type>
15 // && Constructible<InIter::value_type, InIter::reference>
16 // && OutputIterator<OutIter,
17 // HasMinus<InIter::value_type, InIter::value_type>::result_type>
18 // && MoveAssignable<InIter::value_type>
19 // OutIter
20 // adjacent_difference(InIter first, InIter last, OutIter result);
22 #include <numeric>
23 #include <cassert>
25 #include "test_macros.h"
26 #include "test_iterators.h"
28 template <class InIter, class OutIter>
29 TEST_CONSTEXPR_CXX20 void
30 test()
32 int ia[] = {15, 10, 6, 3, 1};
33 int ir[] = {15, -5, -4, -3, -2};
34 const unsigned s = sizeof(ia) / sizeof(ia[0]);
35 int ib[s] = {0};
36 OutIter r = std::adjacent_difference(InIter(ia), InIter(ia+s), OutIter(ib));
37 assert(base(r) == ib + s);
38 for (unsigned i = 0; i < s; ++i)
39 assert(ib[i] == ir[i]);
42 #if TEST_STD_VER >= 11
44 class Y;
46 class X
48 int i_;
50 TEST_CONSTEXPR_CXX20 X& operator=(const X&);
51 public:
52 TEST_CONSTEXPR_CXX20 explicit X(int i) : i_(i) {}
53 TEST_CONSTEXPR_CXX20 X(const X& x) : i_(x.i_) {}
54 TEST_CONSTEXPR_CXX20 X& operator=(X&& x)
56 i_ = x.i_;
57 x.i_ = -1;
58 return *this;
61 TEST_CONSTEXPR_CXX20 friend X operator-(const X& x, const X& y) {return X(x.i_ - y.i_);}
63 friend class Y;
66 class Y
68 int i_;
70 TEST_CONSTEXPR_CXX20 Y& operator=(const Y&);
71 public:
72 TEST_CONSTEXPR_CXX20 explicit Y(int i) : i_(i) {}
73 TEST_CONSTEXPR_CXX20 Y(const Y& y) : i_(y.i_) {}
74 TEST_CONSTEXPR_CXX20 void operator=(const X& x) {i_ = x.i_;}
77 #endif
79 TEST_CONSTEXPR_CXX20 bool
80 test()
82 test<cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();
83 test<cpp17_input_iterator<const int*>, forward_iterator<int*> >();
84 test<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
85 test<cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
86 test<cpp17_input_iterator<const int*>, int*>();
88 test<forward_iterator<const int*>, cpp17_output_iterator<int*> >();
89 test<forward_iterator<const int*>, forward_iterator<int*> >();
90 test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
91 test<forward_iterator<const int*>, random_access_iterator<int*> >();
92 test<forward_iterator<const int*>, int*>();
94 test<bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();
95 test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
96 test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
97 test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
98 test<bidirectional_iterator<const int*>, int*>();
100 test<random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
101 test<random_access_iterator<const int*>, forward_iterator<int*> >();
102 test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
103 test<random_access_iterator<const int*>, random_access_iterator<int*> >();
104 test<random_access_iterator<const int*>, int*>();
106 test<const int*, cpp17_output_iterator<int*> >();
107 test<const int*, forward_iterator<int*> >();
108 test<const int*, bidirectional_iterator<int*> >();
109 test<const int*, random_access_iterator<int*> >();
110 test<const int*, int*>();
112 #if TEST_STD_VER >= 11
113 X x[3] = {X(1), X(2), X(3)};
114 Y y[3] = {Y(1), Y(2), Y(3)};
115 std::adjacent_difference(x, x+3, y);
116 #endif
118 return true;
121 int main(int, char**)
123 test();
124 #if TEST_STD_VER > 17
125 static_assert(test());
126 #endif
127 return 0;