|
| 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 | +// UNSUPPORTED: c++03, c++11, c++14 |
| 10 | + |
| 11 | +// UNSUPPORTED: libcpp-has-no-incomplete-pstl |
| 12 | + |
| 13 | +// <numeric> |
| 14 | + |
| 15 | +// template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2> |
| 16 | +// ForwardIterator2 |
| 17 | +// adjacent_difference(ExecutionPolicy&& exec, |
| 18 | +// ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result); |
| 19 | +// |
| 20 | +// template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, |
| 21 | +// class BinaryOperation> |
| 22 | +// ForwardIterator2 |
| 23 | +// adjacent_difference(ExecutionPolicy&& exec, |
| 24 | +// ForwardIterator1 first, ForwardIterator1 last, |
| 25 | +// ForwardIterator2 result, BinaryOperation binary_op); |
| 26 | + |
| 27 | +#include <algorithm> |
| 28 | +#include <array> |
| 29 | +#include <numeric> |
| 30 | +#include <vector> |
| 31 | + |
| 32 | +#include "test_execution_policies.h" |
| 33 | +#include "test_iterators.h" |
| 34 | + |
| 35 | +template <class Iter1, class Iter2> |
| 36 | +struct Test { |
| 37 | + template <int N, int OutN = N - 1, class Policy> |
| 38 | + void test(Policy&& policy, |
| 39 | + std::array<int, N> input, |
| 40 | + std::array<int, OutN> plus_expected, |
| 41 | + std::array<int, OutN> minus_expected) { |
| 42 | + { |
| 43 | + std::array<int, OutN> out; |
| 44 | + auto ret = |
| 45 | + std::adjacent_difference(policy, Iter1(input.data()), Iter1(input.data() + input.size()), Iter2(out.data())); |
| 46 | + assert(base(ret) == out.data() + out.size()); |
| 47 | + assert(out == minus_expected); |
| 48 | + } |
| 49 | + { |
| 50 | + std::array<int, OutN> out; |
| 51 | + auto ret = std::adjacent_difference( |
| 52 | + policy, Iter1(input.data()), Iter1(input.data() + input.size()), Iter2(out.data()), std::plus{}); |
| 53 | + assert(base(ret) == out.data() + out.size()); |
| 54 | + assert(out == plus_expected); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + template <class Policy> |
| 59 | + void operator()(Policy&& policy) { |
| 60 | + // simple test |
| 61 | + test<4>(policy, {1, 2, 3, 4}, {3, 5, 7}, {1, 1, 1}); |
| 62 | + // empty range |
| 63 | + test<0, 0>(policy, {}, {}, {}); |
| 64 | + // single element range |
| 65 | + test<1>(policy, {1}, {}, {}); |
| 66 | + // two element range |
| 67 | + test<2>(policy, {10, 5}, {15}, {-5}); |
| 68 | + |
| 69 | + // Large inputs with generated data |
| 70 | + for (auto e : {100, 322, 497, 2048}) { |
| 71 | + std::vector<int> input(e); |
| 72 | + std::iota(input.begin(), input.end(), 0); |
| 73 | + std::vector<int> expected(e - 1); |
| 74 | + auto binop = [](int lhs, int rhs) { return lhs + rhs * 3; }; |
| 75 | + std::adjacent_difference(input.begin(), input.end(), expected.begin(), binop); |
| 76 | + std::vector<int> output(e - 1); |
| 77 | + std::adjacent_difference(input.begin(), input.end(), output.begin(), binop); |
| 78 | + assert(output == expected); |
| 79 | + } |
| 80 | + |
| 81 | + { // ensure that all values are used exactly once |
| 82 | + std::array input = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 83 | + std::array<bool, input.size() - 1> called{}; |
| 84 | + std::array<int, input.size() - 1> output; |
| 85 | + std::adjacent_difference(input.data(), input.data() + input.size(), output.data(), [&](int lhs, int rhs) { |
| 86 | + assert(!called[rhs]); |
| 87 | + called[rhs] = true; |
| 88 | + return rhs - lhs; |
| 89 | + }); |
| 90 | + assert(std::all_of(called.begin(), called.end(), [](bool b) { return b; })); |
| 91 | + } |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +int main(int, char**) { |
| 96 | + types::for_each(types::forward_iterator_list<int*>{}, types::apply_type_identity{[](auto v1) { |
| 97 | + using Iter1 = typename decltype(v1)::type; |
| 98 | + types::for_each( |
| 99 | + types::forward_iterator_list<int*>{}, |
| 100 | + TestIteratorWithPolicies<types::partial_instantiation<Test, Iter1>::template apply>{}); |
| 101 | + }}); |
| 102 | + |
| 103 | + return 0; |
| 104 | +} |
0 commit comments