xref: /llvm-project/libcxx/test/std/algorithms/alg.sorting/alg.partitions/pstl.is_partitioned.pass.cpp (revision 3a7876f6e2b01550c2178a7d295842b637d183c7)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14
10 
11 // UNSUPPORTED: libcpp-has-no-incomplete-pstl
12 
13 // template<class ExecutionPolicy, class ForwardIterator, class Predicate>
14 //   bool is_partitioned(ExecutionPolicy&& exec,
15 //                       ForwardIterator first, ForwardIterator last, Predicate pred);
16 
17 #include <algorithm>
18 #include <cassert>
19 #include <vector>
20 
21 #include "test_iterators.h"
22 #include "test_execution_policies.h"
23 
24 template <class Iter>
25 struct Test {
26   template <class Policy>
operator ()Test27   void operator()(Policy&& policy) {
28     { // simple test
29       int a[] = {1, 2, 3, 4, 5};
30       assert(std::is_partitioned(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i < 3; }));
31     }
32     { // check that the range is partitioned if the predicate returns true for all elements
33       int a[] = {1, 2, 3, 4, 5};
34       assert(std::is_partitioned(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int) { return true; }));
35     }
36     { // check that the range is partitioned if the predicate returns false for all elements
37       int a[] = {1, 2, 3, 4, 5};
38       assert(std::is_partitioned(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int) { return false; }));
39     }
40     { // check that false is returned if the range is not partitioned
41       int a[] = {1, 2, 3, 2, 5};
42       assert(!std::is_partitioned(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i < 3; }));
43     }
44     { // check that an empty range is partitioned
45       int a[] = {1, 2, 3, 2, 5};
46       assert(std::is_partitioned(policy, Iter(std::begin(a)), Iter(std::begin(a)), [](int i) { return i < 3; }));
47     }
48     { // check that a single element is partitioned
49       int a[] = {1};
50       assert(std::is_partitioned(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i < 3; }));
51     }
52     { // check that a range is partitioned when the partition point is the first element
53       int a[] = {1, 2, 2, 4, 5};
54       assert(std::is_partitioned(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i < 2; }));
55     }
56     { // check that a range is partitioned when the partition point is the last element
57       int a[] = {1, 2, 2, 4, 5};
58       assert(std::is_partitioned(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i < 2; }));
59     }
60     { // check that a large range works
61       std::vector<int> vec(150, 4);
62       vec[0] = 2;
63       vec[1] = 1;
64       assert(std::is_partitioned(policy, Iter(std::data(vec)), Iter(std::data(vec) + std::size(vec)), [](int i) {
65         return i < 3;
66       }));
67     }
68   }
69 };
70 
main(int,char **)71 int main(int, char**) {
72   types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{});
73 
74   return 0;
75 }
76