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 //===-- is_partitioned.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 #include "pstl/execution"
19 #include "pstl/algorithm"
20 #else
21 #include <execution>
22 #include <algorithm>
23 #endif // PSTL_STANDALONE_TESTS
24 
25 #include "pstl/test_utils.h"
26 
27 using namespace TestUtils;
28 
29 struct test_one_policy
30 {
31     //dummy specialization by policy type, in case of broken configuration
32 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN
33 
34     template <typename Iterator1, typename Predicate>
35     void
operator ()test_one_policy36     operator()(pstl::execution::unsequenced_policy, Iterator1 begin1, Iterator1 end1, Predicate pred)
37     {
38     }
39     template <typename Iterator1, typename Predicate>
40     void
operator ()test_one_policy41     operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 begin1, Iterator1 end1, Predicate pred)
42     {
43     }
44 #endif
45 
46     template <typename ExecutionPolicy, typename Iterator1, typename Predicate>
47     void
operator ()test_one_policy48     operator()(ExecutionPolicy&& exec, Iterator1 begin1, Iterator1 end1, Predicate pred)
49     {
50         const bool expected = std::is_partitioned(begin1, end1, pred);
51         const bool actual = std::is_partitioned(exec, begin1, end1, pred);
52         EXPECT_TRUE(actual == expected, "wrong return result from is_partitioned");
53     }
54 };
55 
56 template <typename T, typename Predicate>
57 void
test(Predicate pred)58 test(Predicate pred)
59 {
60 
61     const std::size_t max_n = 1000000;
62     Sequence<T> in(max_n, [](std::size_t k) { return T(k); });
63 
64     for (std::size_t n1 = 0; n1 <= max_n; n1 = n1 <= 16 ? n1 + 1 : std::size_t(3.1415 * n1))
65     {
66         invoke_on_all_policies(test_one_policy(), in.begin(), in.begin() + n1, pred);
67         std::partition(in.begin(), in.begin() + n1, pred);
68         invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cbegin() + n1, pred);
69     }
70 }
71 
72 template <typename T>
73 struct LocalWrapper
74 {
LocalWrapperLocalWrapper75     explicit LocalWrapper(std::size_t k) : my_val(k) {}
76 
77   private:
78     T my_val;
79 };
80 
81 struct test_non_const
82 {
83     template <typename Policy, typename Iterator>
84     void
operator ()test_non_const85     operator()(Policy&& exec, Iterator iter)
86     {
87         auto is_even = [&](float64_t v) {
88             uint32_t i = (uint32_t)v;
89             return i % 2 == 0;
90         };
91         invoke_if(exec, [&]() { is_partitioned(exec, iter, iter, non_const(is_even)); });
92     }
93 };
94 
95 int32_t
main()96 main()
97 {
98     test<float64_t>([](const float64_t x) { return x < 0; });
99     test<int32_t>([](const int32_t x) { return x > 1000; });
100     test<uint16_t>([](const uint16_t x) { return x % 5 < 3; });
101 #if !_PSTL_ICC_18_TEST_EARLY_EXIT_MONOTONIC_RELEASE_BROKEN && !_PSTL_ICC_19_TEST_IS_PARTITIONED_RELEASE_BROKEN
102     test<LocalWrapper<float64_t>>([](const LocalWrapper<float64_t>& x) { return true; });
103 #endif
104 
105     test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>());
106 
107     std::cout << done() << std::endl;
108     return 0;
109 }
110