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