brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.0 KiB · 5654a6d Raw
129 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <numeric>10 11// Became constexpr in C++2012// 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//   OutIter20//   adjacent_difference(InIter first, InIter last, OutIter result);21 22#include <numeric>23#include <cassert>24 25#include "test_macros.h"26#include "test_iterators.h"27 28template <class InIter, class OutIter>29TEST_CONSTEXPR_CXX20 void30test()31{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]);40}41 42#if TEST_STD_VER >= 1143 44class Y;45 46class X47{48    int i_;49 50    TEST_CONSTEXPR_CXX20 X& operator=(const X&);51public: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)55    {56        i_ = x.i_;57        x.i_ = -1;58        return *this;59    }60 61    TEST_CONSTEXPR_CXX20 friend X operator-(const X& x, const X& y) {return X(x.i_ - y.i_);}62 63    friend class Y;64};65 66class Y67{68    int i_;69 70    TEST_CONSTEXPR_CXX20 Y& operator=(const Y&);71public: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_;}75};76 77#endif78 79TEST_CONSTEXPR_CXX20 bool80test()81{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*>();87 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*>();93 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*>();99 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*>();105 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*>();111 112#if TEST_STD_VER >= 11113    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#endif117 118    return true;119}120 121int main(int, char**)122{123    test();124#if TEST_STD_VER > 17125    static_assert(test());126#endif127    return 0;128}129