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