diff options
author | JF Bastien <jfbastien@apple.com> | 2018-12-19 17:45:32 +0000 |
---|---|---|
committer | JF Bastien <jfbastien@apple.com> | 2018-12-19 17:45:32 +0000 |
commit | e637637ae46a5b2fa1e9d10c16ae5b0922289f82 (patch) | |
tree | 1bd45f3e03d8bbb85a9dca6b46015769d521b186 /pstl/test/test_transform_binary.cpp | |
parent | 5d409b22781f5854f1bac3fd60c8499af0c865bf (diff) | |
download | bcm5719-llvm-e637637ae46a5b2fa1e9d10c16ae5b0922289f82.tar.gz bcm5719-llvm-e637637ae46a5b2fa1e9d10c16ae5b0922289f82.zip |
Initial PSTL commit
The initial commit of the Parallel STL upstream (under LLVM umbrella) based on
Parallel STL 20181204 open source release, which is available by
https://github.com/intel/parallelstl
Author: Mikhail Dvorskiy <mikhail.dvorskiy@intel.com>
Differential Revision: https://reviews.llvm.org/D55889
llvm-svn: 349653
Diffstat (limited to 'pstl/test/test_transform_binary.cpp')
-rw-r--r-- | pstl/test/test_transform_binary.cpp | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/pstl/test/test_transform_binary.cpp b/pstl/test/test_transform_binary.cpp new file mode 100644 index 00000000000..5c0c7e922cc --- /dev/null +++ b/pstl/test/test_transform_binary.cpp @@ -0,0 +1,117 @@ +// -*- C++ -*- +//===-- test_transform_binary.cpp -----------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "pstl/execution" +#include "pstl/algorithm" +#include "utils.h" + +using namespace TestUtils; + +template <typename In1, typename In2, typename Out> +class TheOperation +{ + Out val; + + public: + TheOperation(Out v) : val(v) {} + Out + operator()(const In1& x, const In2& y) const + { + return Out(val + x - y); + } +}; + +template <typename InputIterator1, typename InputIterator2, typename OutputIterator> +void +check_and_reset(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator out_first) +{ + typedef typename std::iterator_traits<OutputIterator>::value_type Out; + typename std::iterator_traits<OutputIterator>::difference_type k = 0; + for (; first1 != last1; ++first1, ++first2, ++out_first, ++k) + { + // check + Out expected = Out(1.5) + *first1 - *first2; + Out actual = *out_first; + if (std::is_floating_point<Out>::value) + { + EXPECT_TRUE((expected > actual ? expected - actual : actual - expected) < 1e7, + "wrong value in output sequence"); + } + else + { + EXPECT_EQ(expected, actual, "wrong value in output sequence"); + } + // reset + *out_first = k % 7 != 4 ? 7 * k - 5 : 0; + } +} + +struct test_one_policy +{ + template <typename Policy, typename InputIterator1, typename InputIterator2, typename OutputIterator, + typename BinaryOp> + void + operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, + OutputIterator out_first, OutputIterator out_last, BinaryOp op) + { + auto orrr = std::transform(exec, first1, last1, first2, out_first, op); + check_and_reset(first1, last1, first2, out_first); + } +}; + +template <typename In1, typename In2, typename Out, typename Predicate> +void +test(Predicate pred) +{ + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + Sequence<In1> in1(n, [](size_t k) { return k % 5 != 1 ? 3 * k - 7 : 0; }); + Sequence<In2> in2(n, [](size_t k) { return k % 7 != 2 ? 5 * k - 5 : 0; }); + + Sequence<Out> out(n, [](size_t k) { return -1; }); + + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.end(), in2.begin(), in2.end(), out.begin(), + out.end(), pred); + invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cend(), in2.cbegin(), in2.cend(), out.begin(), + out.end(), pred); + } +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename InputIterator, typename OutputInterator> + void + operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) + { + invoke_if(exec, [&]() { + InputIterator input_iter2 = input_iter; + transform(exec, input_iter, input_iter, input_iter2, out_iter, non_const(std::plus<T>())); + }); + } +}; + +int32_t +main() +{ + //const operator() + test<int32_t, int32_t, int32_t>(TheOperation<int32_t, int32_t, int32_t>(1)); + test<float32_t, float32_t, float32_t>(TheOperation<float32_t, float32_t, float32_t>(1.5)); + //non-const operator() + test<int32_t, float32_t, float32_t>(non_const(TheOperation<int32_t, float32_t, float32_t>(1.5))); + test<int64_t, float64_t, float32_t>(non_const(TheOperation<int64_t, float64_t, float32_t>(1.5))); + //lambda + test<int8_t, float64_t, int8_t>([](const int8_t& x, const float64_t& y) { return int8_t(int8_t(1.5) + x - y); }); + + test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} |