1 // -*- C++ -*-
2 //===-- is_partitioned.pass.cpp -------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "support/pstl_test_config.h"
11 
12 #ifdef PSTL_STANDALONE_TESTS
13 #include "pstl/execution"
14 #include "pstl/algorithm"
15 #else
16 #include <execution>
17 #include <algorithm>
18 #endif // PSTL_STANDALONE_TESTS
19 
20 #include "support/utils.h"
21 
22 using namespace TestUtils;
23 
24 struct test_one_policy
25 {
26     //dummy specialization by policy type, in case of broken configuration
27 #if __PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || __PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN
28 
29     template <typename Iterator1, typename Predicate>
30     void
31     operator()(pstl::execution::unsequenced_policy, Iterator1 begin1, Iterator1 end1, Predicate pred)
32     {
33     }
34     template <typename Iterator1, typename Predicate>
35     void
36     operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 begin1, Iterator1 end1, Predicate pred)
37     {
38     }
39 #endif
40 
41     template <typename ExecutionPolicy, typename Iterator1, typename Predicate>
42     void
43     operator()(ExecutionPolicy&& exec, Iterator1 begin1, Iterator1 end1, Predicate pred)
44     {
45         const bool expected = std::is_partitioned(begin1, end1, pred);
46         const bool actual = std::is_partitioned(exec, begin1, end1, pred);
47         EXPECT_TRUE(actual == expected, "wrong return result from is_partitioned");
48     }
49 };
50 
51 template <typename T, typename Predicate>
52 void
53 test(Predicate pred)
54 {
55 
56     const std::size_t max_n = 1000000;
57     Sequence<T> in(max_n, [](std::size_t k) { return T(k); });
58 
59     for (std::size_t n1 = 0; n1 <= max_n; n1 = n1 <= 16 ? n1 + 1 : std::size_t(3.1415 * n1))
60     {
61         invoke_on_all_policies(test_one_policy(), in.begin(), in.begin() + n1, pred);
62         std::partition(in.begin(), in.begin() + n1, pred);
63         invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cbegin() + n1, pred);
64     }
65 }
66 
67 template <typename T>
68 struct LocalWrapper
69 {
70     explicit LocalWrapper(std::size_t k) : my_val(k) {}
71 
72   private:
73     T my_val;
74 };
75 
76 struct test_non_const
77 {
78     template <typename Policy, typename Iterator>
79     void
80     operator()(Policy&& exec, Iterator iter)
81     {
82         auto is_even = [&](float64_t v) {
83             uint32_t i = (uint32_t)v;
84             return i % 2 == 0;
85         };
86         invoke_if(exec, [&]() { is_partitioned(exec, iter, iter, non_const(is_even)); });
87     }
88 };
89 
90 int32_t
91 main()
92 {
93     test<float64_t>([](const float64_t x) { return x < 0; });
94     test<int32_t>([](const int32_t x) { return x > 1000; });
95     test<uint16_t>([](const uint16_t x) { return x % 5 < 3; });
96 #if !__PSTL_ICC_18_TEST_EARLY_EXIT_MONOTONIC_RELEASE_BROKEN && !__PSTL_ICC_19_TEST_IS_PARTITIONED_RELEASE_BROKEN
97     test<LocalWrapper<float64_t>>([](const LocalWrapper<float64_t>& x) { return true; });
98 #endif
99 
100     test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>());
101 
102     std::cout << done() << std::endl;
103     return 0;
104 }
105