1 | // -*- C++ -*- |
2 | //===-- is_sorted.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++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_is_sorted |
22 | { |
23 | template <typename Policy, typename Iterator> |
24 | void |
25 | operator()(Policy&& exec, Iterator first, Iterator last, bool exam) |
26 | { |
27 | using namespace std; |
28 | typedef typename std::iterator_traits<Iterator>::value_type T; |
29 | |
30 | //try random-access iterator |
31 | bool res = is_sorted(exec, first, last); |
32 | EXPECT_TRUE(exam == res, "is_sorted wrong result for random-access iterator" ); |
33 | auto iexam = is_sorted_until(first, last); |
34 | auto ires = is_sorted_until(exec, first, last); |
35 | EXPECT_TRUE(iexam == ires, "is_sorted_until wrong result for random-access iterator" ); |
36 | |
37 | //try random-access iterator with a predicate |
38 | res = is_sorted(exec, first, last, std::less<T>()); |
39 | EXPECT_TRUE(exam == res, "is_sorted wrong result for random-access iterator" ); |
40 | iexam = is_sorted_until(first, last, std::less<T>()); |
41 | ires = is_sorted_until(exec, first, last, std::less<T>()); |
42 | EXPECT_TRUE(iexam == ires, "is_sorted_until wrong result for random-access iterator" ); |
43 | } |
44 | }; |
45 | |
46 | template <typename T> |
47 | void |
48 | test_is_sorted_by_type() |
49 | { |
50 | |
51 | Sequence<T> in(99999, [](size_t v) -> T { return T(v); }); //fill 0..n |
52 | |
53 | invoke_on_all_policies(test_is_sorted(), in.begin(), in.end(), std::is_sorted(in.begin(), in.end())); |
54 | invoke_on_all_policies(test_is_sorted(), in.cbegin(), in.cend(), std::is_sorted(in.begin(), in.end())); |
55 | |
56 | in[in.size() / 2] = -1; |
57 | invoke_on_all_policies(test_is_sorted(), in.begin(), in.end(), std::is_sorted(in.begin(), in.end())); |
58 | invoke_on_all_policies(test_is_sorted(), in.cbegin(), in.cend(), std::is_sorted(in.begin(), in.end())); |
59 | |
60 | in[1] = -1; |
61 | invoke_on_all_policies(test_is_sorted(), in.begin(), in.end(), std::is_sorted(in.begin(), in.end())); |
62 | invoke_on_all_policies(test_is_sorted(), in.cbegin(), in.cend(), std::is_sorted(in.begin(), in.end())); |
63 | |
64 | //an empty container |
65 | Sequence<T> in0(0); |
66 | invoke_on_all_policies(test_is_sorted(), in0.begin(), in0.end(), std::is_sorted(in0.begin(), in0.end())); |
67 | invoke_on_all_policies(test_is_sorted(), in0.cbegin(), in0.cend(), std::is_sorted(in0.begin(), in0.end())); |
68 | |
69 | //non-descending order |
70 | Sequence<T> in1(9, [](size_t) -> T { return T(0); }); |
71 | invoke_on_all_policies(test_is_sorted(), in1.begin(), in1.end(), std::is_sorted(in1.begin(), in1.end())); |
72 | invoke_on_all_policies(test_is_sorted(), in1.cbegin(), in1.cend(), std::is_sorted(in1.begin(), in1.end())); |
73 | } |
74 | |
75 | template <typename T> |
76 | struct test_non_const |
77 | { |
78 | template <typename Policy, typename Iterator> |
79 | void |
80 | operator()(Policy&& exec, Iterator iter) |
81 | { |
82 | is_sorted(exec, iter, iter, std::less<T>()); |
83 | is_sorted_until(exec, iter, iter, std::less<T>()); |
84 | } |
85 | }; |
86 | |
87 | int |
88 | main() |
89 | { |
90 | |
91 | test_is_sorted_by_type<int32_t>(); |
92 | test_is_sorted_by_type<float64_t>(); |
93 | |
94 | test_is_sorted_by_type<Wrapper<int32_t>>(); |
95 | |
96 | test_algo_basic_single<int32_t>(f: run_for_rnd_fw<test_non_const<int32_t>>()); |
97 | |
98 | std::cout << done() << std::endl; |
99 | return 0; |
100 | } |
101 | |