// -*- C++ -*- //===-- adjacent_difference.pass.cpp --------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // UNSUPPORTED: c++03, c++11, c++14 #include "support/pstl_test_config.h" #include #include #include #include "support/utils.h" using namespace TestUtils; template struct wrapper { T t; constexpr explicit wrapper(T t_) : t(t_) {} template constexpr wrapper(const wrapper& a) { t = a.t; } template constexpr void operator=(const wrapper& a) { t = a.t; } constexpr wrapper operator-(const wrapper& a) const { return wrapper(t - a.t); } }; template bool compare(const T& a, const T& b) { return a == b; } template bool compare(const wrapper& a, const wrapper& b) { return a.t == b.t; } template typename std::enable_if::value, bool>::type compute_and_check(Iterator1 first, Iterator1 last, Iterator2 d_first, T, Function f) { using T2 = typename std::iterator_traits::value_type; if (first == last) return true; { T2 temp(*first); if (!compare(temp, *d_first)) return false; } Iterator1 second = std::next(first); ++d_first; for (; second != last; ++first, ++second, ++d_first) { T2 temp(f(*second, *first)); if (!compare(temp, *d_first)) return false; } return true; } // we don't want to check equality here // because we can't be sure it will be strictly equal for floating point types template typename std::enable_if::value, bool>::type compute_and_check(Iterator1, Iterator1, Iterator2, T, Function) { return true; } struct test_one_policy { #if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) // dummy specialization by policy type, in case of broken configuration template typename std::enable_if::value, void>::type operator()(pstl::execution::unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e, T trash, Function f) { } template typename std::enable_if::value, void>::type operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e, T trash, Function f) { } #endif template void operator()(ExecutionPolicy&& exec, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e, T trash, Function f) { using namespace std; using T2 = typename std::iterator_traits::value_type; fill(actual_b, actual_e, trash); Iterator2 actual_return = adjacent_difference(exec, data_b, data_e, actual_b); EXPECT_TRUE(compute_and_check(data_b, data_e, actual_b, T2(0), std::minus()), "wrong effect of adjacent_difference"); EXPECT_TRUE(actual_return == actual_e, "wrong result of adjacent_difference"); fill(actual_b, actual_e, trash); actual_return = adjacent_difference(exec, data_b, data_e, actual_b, f); EXPECT_TRUE(compute_and_check(data_b, data_e, actual_b, T2(0), f), "wrong effect of adjacent_difference with functor"); EXPECT_TRUE(actual_return == actual_e, "wrong result of adjacent_difference with functor"); } }; template void test(Pred pred) { const std::size_t max_len = 100000; static constexpr T2 value = T2(77); static constexpr T1 trash = T1(31); Sequence actual(max_len, [](std::size_t i) { return T1(i); }); Sequence data(max_len, [](std::size_t i) { return i % 3 == 2 ? T2(i * i) : value; }); for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len)) { invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(), actual.begin() + len, trash, pred); invoke_on_all_policies(test_one_policy(), data.cbegin(), data.cbegin() + len, actual.begin(), actual.begin() + len, trash, pred); } } int main() { test([](uint32_t a, uint32_t b) { return a - b; }); test([](int64_t a, int64_t b) { return a / (b + 1); }); test([](float32_t a, float32_t b) { return (a + b) / 2; }); test, wrapper>( [](const wrapper& a, const wrapper& b) { return a - b; }); std::cout << done() << std::endl; return 0; }