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 //===----------------------------------------------------------------------===// 8 9 // <numeric> 10 // UNSUPPORTED: clang-8 11 12 // Became constexpr in C++20 13 // template <InputIterator InIter, 14 // OutputIterator<auto, const InIter::value_type&> OutIter> 15 // requires HasMinus<InIter::value_type, InIter::value_type> 16 // && Constructible<InIter::value_type, InIter::reference> 17 // && OutputIterator<OutIter, 18 // HasMinus<InIter::value_type, InIter::value_type>::result_type> 19 // && MoveAssignable<InIter::value_type> 20 // OutIter 21 // adjacent_difference(InIter first, InIter last, OutIter result); 22 23 #include <numeric> 24 #include <cassert> 25 26 #include "test_macros.h" 27 #include "test_iterators.h" 28 29 template <class InIter, class OutIter> 30 TEST_CONSTEXPR_CXX20 void 31 test() 32 { 33 int ia[] = {15, 10, 6, 3, 1}; 34 int ir[] = {15, -5, -4, -3, -2}; 35 const unsigned s = sizeof(ia) / sizeof(ia[0]); 36 int ib[s] = {0}; 37 OutIter r = std::adjacent_difference(InIter(ia), InIter(ia+s), OutIter(ib)); 38 assert(base(r) == ib + s); 39 for (unsigned i = 0; i < s; ++i) 40 assert(ib[i] == ir[i]); 41 } 42 43 #if TEST_STD_VER >= 11 44 45 class Y; 46 47 class X 48 { 49 int i_; 50 51 TEST_CONSTEXPR_CXX20 X& operator=(const X&); 52 public: 53 TEST_CONSTEXPR_CXX20 explicit X(int i) : i_(i) {} 54 TEST_CONSTEXPR_CXX20 X(const X& x) : i_(x.i_) {} 55 TEST_CONSTEXPR_CXX20 X& operator=(X&& x) 56 { 57 i_ = x.i_; 58 x.i_ = -1; 59 return *this; 60 } 61 62 TEST_CONSTEXPR_CXX20 friend X operator-(const X& x, const X& y) {return X(x.i_ - y.i_);} 63 64 friend class Y; 65 }; 66 67 class Y 68 { 69 int i_; 70 71 TEST_CONSTEXPR_CXX20 Y& operator=(const Y&); 72 public: 73 TEST_CONSTEXPR_CXX20 explicit Y(int i) : i_(i) {} 74 TEST_CONSTEXPR_CXX20 Y(const Y& y) : i_(y.i_) {} 75 TEST_CONSTEXPR_CXX20 void operator=(const X& x) {i_ = x.i_;} 76 }; 77 78 #endif 79 80 TEST_CONSTEXPR_CXX20 bool 81 test() 82 { 83 test<cpp17_input_iterator<const int*>, output_iterator<int*> >(); 84 test<cpp17_input_iterator<const int*>, forward_iterator<int*> >(); 85 test<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >(); 86 test<cpp17_input_iterator<const int*>, random_access_iterator<int*> >(); 87 test<cpp17_input_iterator<const int*>, int*>(); 88 89 test<forward_iterator<const int*>, output_iterator<int*> >(); 90 test<forward_iterator<const int*>, forward_iterator<int*> >(); 91 test<forward_iterator<const int*>, bidirectional_iterator<int*> >(); 92 test<forward_iterator<const int*>, random_access_iterator<int*> >(); 93 test<forward_iterator<const int*>, int*>(); 94 95 test<bidirectional_iterator<const int*>, output_iterator<int*> >(); 96 test<bidirectional_iterator<const int*>, forward_iterator<int*> >(); 97 test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >(); 98 test<bidirectional_iterator<const int*>, random_access_iterator<int*> >(); 99 test<bidirectional_iterator<const int*>, int*>(); 100 101 test<random_access_iterator<const int*>, output_iterator<int*> >(); 102 test<random_access_iterator<const int*>, forward_iterator<int*> >(); 103 test<random_access_iterator<const int*>, bidirectional_iterator<int*> >(); 104 test<random_access_iterator<const int*>, random_access_iterator<int*> >(); 105 test<random_access_iterator<const int*>, int*>(); 106 107 test<const int*, output_iterator<int*> >(); 108 test<const int*, forward_iterator<int*> >(); 109 test<const int*, bidirectional_iterator<int*> >(); 110 test<const int*, random_access_iterator<int*> >(); 111 test<const int*, int*>(); 112 113 #if TEST_STD_VER >= 11 114 X x[3] = {X(1), X(2), X(3)}; 115 Y y[3] = {Y(1), Y(2), Y(3)}; 116 std::adjacent_difference(x, x+3, y); 117 #endif 118 119 return true; 120 } 121 122 int main(int, char**) 123 { 124 test(); 125 #if TEST_STD_VER > 17 126 static_assert(test()); 127 #endif 128 return 0; 129 } 130