Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / algorithms / ranges_robust_against_nonbool_predicates.pass.cpp
blob04773f0f5bc802e15e44fc52b292fe067cbcc563
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
11 // <algorithm>
13 // Range algorithms that take predicates should support predicates that return a non-boolean value as long as the
14 // returned type is implicitly convertible to bool.
16 #include <algorithm>
18 #include <array>
19 #include <concepts>
20 #include <initializer_list>
21 #include <iterator>
22 #include <ranges>
24 #include "boolean_testable.h"
25 #include "test_macros.h"
27 constexpr auto unary_pred = [](int i) { return BooleanTestable(i > 0); };
28 static_assert(!std::same_as<decltype(unary_pred(1)), bool>);
29 static_assert(std::convertible_to<decltype(unary_pred(1)), bool>);
31 constexpr auto binary_pred = [](int i, int j) { return BooleanTestable(i < j); };
32 static_assert(!std::same_as<decltype(binary_pred(1, 2)), bool>);
33 static_assert(std::convertible_to<decltype(binary_pred(1, 2)), bool>);
35 // Invokes both the (iterator, sentinel, ...) and the (range, ...) overloads of the given niebloid.
37 // (in, ...)
38 template <class Func, std::ranges::range Input, class... Args>
39 constexpr void test(Func&& func, Input& in, Args&&... args) {
40 (void)func(in.begin(), in.end(), std::forward<Args>(args)...);
41 (void)func(in, std::forward<Args>(args)...);
44 // (in1, in2, ...)
45 template <class Func, std::ranges::range Input, class... Args>
46 constexpr void test(Func&& func, Input& in1, Input& in2, Args&&... args) {
47 (void)func(in1.begin(), in1.end(), in2.begin(), in2.end(), std::forward<Args>(args)...);
48 (void)func(in1, in2, std::forward<Args>(args)...);
51 // (in, mid, ...)
52 template <class Func, std::ranges::range Input, class... Args>
53 constexpr void test_mid(Func&& func, Input& in, std::ranges::iterator_t<Input> mid, Args&&... args) {
54 (void)func(in.begin(), mid, in.end(), std::forward<Args>(args)...);
55 (void)func(in, mid, std::forward<Args>(args)...);
58 constexpr bool test_all() {
59 std::array in = {1, 2, 3};
60 std::array in2 = {4, 5, 6};
61 auto mid = in.begin() + 1;
63 std::array output = {7, 8, 9, 10, 11, 12};
64 auto out = output.begin();
65 auto out2 = output.begin() + 1;
67 int x = 2;
68 int count = 1;
70 test(std::ranges::any_of, in, unary_pred);
71 test(std::ranges::all_of, in, unary_pred);
72 #if TEST_STD_VER >= 23
73 test(std::ranges::ends_with, in, in2, binary_pred);
74 #endif
75 test(std::ranges::none_of, in, unary_pred);
76 test(std::ranges::find_if, in, unary_pred);
77 test(std::ranges::find_if_not, in, unary_pred);
78 test(std::ranges::find_first_of, in, in2, binary_pred);
79 test(std::ranges::adjacent_find, in, binary_pred);
80 test(std::ranges::mismatch, in, in2, binary_pred);
81 test(std::ranges::equal, in, in2, binary_pred);
82 test(std::ranges::lexicographical_compare, in, in2, binary_pred);
83 test(std::ranges::partition_point, in, unary_pred);
84 test(std::ranges::lower_bound, in, x, binary_pred);
85 test(std::ranges::upper_bound, in, x, binary_pred);
86 test(std::ranges::equal_range, in, x, binary_pred);
87 test(std::ranges::binary_search, in, x, binary_pred);
89 // min
90 (void)std::ranges::min(1, 2, binary_pred);
91 (void)std::ranges::min(std::initializer_list<int>{1, 2}, binary_pred);
92 (void)std::ranges::min(in, binary_pred);
93 // max
94 (void)std::ranges::max(1, 2, binary_pred);
95 (void)std::ranges::max(std::initializer_list<int>{1, 2}, binary_pred);
96 (void)std::ranges::max(in, binary_pred);
97 // minmax
98 (void)std::ranges::minmax(1, 2, binary_pred);
99 (void)std::ranges::minmax(std::initializer_list<int>{1, 2}, binary_pred);
100 (void)std::ranges::minmax(in, binary_pred);
102 test(std::ranges::min_element, in, binary_pred);
103 test(std::ranges::max_element, in, binary_pred);
104 test(std::ranges::minmax_element, in, binary_pred);
105 test(std::ranges::count_if, in, unary_pred);
106 test(std::ranges::search, in, in2, binary_pred);
107 test(std::ranges::search_n, in, count, x, binary_pred);
108 test(std::ranges::find_end, in, in2, binary_pred);
109 test(std::ranges::is_partitioned, in, unary_pred);
110 test(std::ranges::is_sorted, in, binary_pred);
111 test(std::ranges::is_sorted_until, in, binary_pred);
112 test(std::ranges::includes, in, in2, binary_pred);
113 test(std::ranges::is_heap, in, binary_pred);
114 test(std::ranges::is_heap_until, in, binary_pred);
115 (void)std::ranges::clamp(2, 1, 3, binary_pred);
116 test(std::ranges::is_permutation, in, in2, binary_pred);
117 test(std::ranges::copy_if, in, out, unary_pred);
118 test(std::ranges::remove_copy_if, in, out, unary_pred);
119 test(std::ranges::replace_if, in, unary_pred, x);
120 test(std::ranges::replace_copy_if, in, out, unary_pred, x);
121 test(std::ranges::unique_copy, in, out, binary_pred);
122 test(std::ranges::partition_copy, in, out, out2, unary_pred);
123 test(std::ranges::partial_sort_copy, in, in2, binary_pred);
124 test(std::ranges::merge, in, in2, out, binary_pred);
125 #if TEST_STD_VER > 20
126 test(std::ranges::starts_with, in, in2, binary_pred);
127 #endif
128 test(std::ranges::set_difference, in, in2, out, binary_pred);
129 test(std::ranges::set_intersection, in, in2, out, binary_pred);
130 test(std::ranges::set_symmetric_difference, in, in2, out, binary_pred);
131 test(std::ranges::set_union, in, in2, out, binary_pred);
132 test(std::ranges::remove_if, in, unary_pred);
133 test(std::ranges::unique, in, binary_pred);
134 test(std::ranges::partition, in, unary_pred);
135 if (!std::is_constant_evaluated())
136 test(std::ranges::stable_partition, in, unary_pred);
137 test(std::ranges::sort, in, binary_pred);
138 if (!std::is_constant_evaluated())
139 test(std::ranges::stable_sort, in, binary_pred);
140 test_mid(std::ranges::partial_sort, in, mid, binary_pred);
141 test_mid(std::ranges::nth_element, in, mid, binary_pred);
142 if (!std::is_constant_evaluated())
143 test_mid(std::ranges::inplace_merge, in, mid, binary_pred);
144 test(std::ranges::make_heap, in, binary_pred);
145 test(std::ranges::push_heap, in, binary_pred);
146 test(std::ranges::pop_heap, in, binary_pred);
147 test(std::ranges::sort_heap, in, binary_pred);
148 test(std::ranges::prev_permutation, in, binary_pred);
149 test(std::ranges::next_permutation, in, binary_pred);
151 return true;
154 int main(int, char**) {
155 test_all();
156 static_assert(test_all());
158 return 0;