| 1 | // -*- C++ -*- |
| 2 | // { dg-options "-ltbb" } |
| 3 | // { dg-do run { target c++17 } } |
| 4 | // { dg-timeout-factor 3 } |
| 5 | // { dg-require-effective-target tbb_backend } |
| 6 | |
| 7 | //===-- includes.pass.cpp -------------------------------------------------===// |
| 8 | // |
| 9 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 10 | // See https://llvm.org/LICENSE.txt for license information. |
| 11 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "pstl/pstl_test_config.h" |
| 16 | |
| 17 | #ifdef PSTL_STANDALONE_TESTS |
| 18 | |
| 19 | #include <cmath> |
| 20 | |
| 21 | #include "pstl/execution" |
| 22 | #include "pstl/algorithm" |
| 23 | #else |
| 24 | #include <execution> |
| 25 | #include <algorithm> |
| 26 | #endif // PSTL_STANDALONE_TESTS |
| 27 | |
| 28 | #include "pstl/test_utils.h" |
| 29 | |
| 30 | using namespace TestUtils; |
| 31 | |
| 32 | template <typename T> |
| 33 | struct Num |
| 34 | { |
| 35 | T val; |
| 36 | explicit Num(const T& v) : val(v) {} |
| 37 | |
| 38 | //for "includes" checks |
| 39 | template <typename T1> |
| 40 | bool |
| 41 | operator<(const Num<T1>& v1) const |
| 42 | { |
| 43 | return val < v1.val; |
| 44 | } |
| 45 | |
| 46 | //The types Type1 and Type2 must be such that an object of type InputIt can be dereferenced and then implicitly converted to both of them |
| 47 | template <typename T1> |
| 48 | operator Num<T1>() const |
| 49 | { |
| 50 | return Num<T1>((T1)val); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | struct test_one_policy |
| 55 | { |
| 56 | template <typename Policy, typename InputIterator1, typename InputIterator2, typename Compare> |
| 57 | typename std::enable_if<!TestUtils::isReverse<InputIterator1>::value, void>::type |
| 58 | operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, |
| 59 | Compare comp) |
| 60 | { |
| 61 | |
| 62 | auto expect_res = std::includes(first1, last1, first2, last2, comp); |
| 63 | auto res = std::includes(exec, first1, last1, first2, last2, comp); |
| 64 | |
| 65 | EXPECT_TRUE(expect_res == res, "wrong result for includes"); |
| 66 | } |
| 67 | |
| 68 | template <typename Policy, typename InputIterator1, typename InputIterator2, typename Compare> |
| 69 | typename std::enable_if<TestUtils::isReverse<InputIterator1>::value, void>::type |
| 70 | operator()(Policy&&, InputIterator1, InputIterator1, InputIterator2, InputIterator2, Compare) |
| 71 | { |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | template <typename T1, typename T2, typename Compare> |
| 76 | void |
| 77 | test_includes(Compare compare) |
| 78 | { |
| 79 | |
| 80 | #ifdef _GLIBCXX_DEBUG |
| 81 | const std::size_t n_max = 10000; |
| 82 | #else |
| 83 | const std::size_t n_max = 1000000; |
| 84 | #endif |
| 85 | |
| 86 | // The rand()%(2*n+1) encourages generation of some duplicates. |
| 87 | std::srand(42); |
| 88 | |
| 89 | for (std::size_t n = 0; n < n_max; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) |
| 90 | { |
| 91 | for (std::size_t m = 0; m < n_max; m = m <= 16 ? m + 1 : size_t(2.71828 * m)) |
| 92 | { |
| 93 | //prepare the input ranges |
| 94 | Sequence<T1> in1(n, [](std::size_t k) { return rand() % (2 * k + 1); }); |
| 95 | Sequence<T2> in2(m, [](std::size_t k) { return rand() % (k + 1); }); |
| 96 | |
| 97 | std::sort(in1.begin(), in1.end(), compare); |
| 98 | std::sort(in2.begin(), in2.end(), compare); |
| 99 | |
| 100 | invoke_on_all_policies(test_one_policy(), in1.begin(), in1.end(), in2.cbegin(), in2.cend(), compare); |
| 101 | |
| 102 | //test w/ non constant predicate |
| 103 | if (n < 5 && m < 5) |
| 104 | invoke_on_all_policies(test_one_policy(), in1.begin(), in1.end(), in2.cbegin(), in2.cend(), |
| 105 | non_const(compare)); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | int |
| 111 | main() |
| 112 | { |
| 113 | |
| 114 | test_includes<float64_t, float64_t>(std::less<>()); |
| 115 | test_includes<Num<int64_t>, Num<int32_t>>([](const Num<int64_t>& x, const Num<int32_t>& y) { return x < y; }); |
| 116 | std::cout << done() << std::endl; |
| 117 | |
| 118 | return 0; |
| 119 | } |