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, c++17
10 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
11 // REQUIRES: stdlib=libc++
12 
13 // [algorithms.requirements]/2
14 // [range.iter.ops.general]/2
15 
16 #include <algorithm>
17 #include <concepts>
18 #include <iterator>
19 #include <memory>
20 #include <random>
21 #include <ranges>
22 #include <type_traits>
23 #include <utility>
24 
25 // Niebloids, unlike CPOs, are *not* required to be semiregular or even to have
26 // a declared type at all; they are specified as "magic" overload sets whose
27 // names are not found by argument-dependent lookup and which inhibit
28 // argument-dependent lookup if they are found via a `using`-declaration.
29 //
30 // libc++ implements them using the same function-object technique we use for CPOs;
31 // therefore this file should stay in sync with ./cpo.compile.pass.cpp.
32 
33 template <class CPO, class... Args>
test(CPO & o,Args &&...)34 constexpr bool test(CPO& o, Args&&...) {
35   static_assert(std::is_const_v<CPO>);
36   static_assert(std::is_class_v<CPO>);
37   static_assert(std::is_trivial_v<CPO>);
38 
39   auto p = o;
40   using T = decltype(p);
41 
42   // The type of a customization point object, ignoring cv-qualifiers, shall model semiregular.
43   static_assert(std::semiregular<T>);
44 
45   // The type T of a customization point object, ignoring cv-qualifiers, shall model...
46   static_assert(std::invocable<T&, Args...>);
47   static_assert(std::invocable<const T&, Args...>);
48   static_assert(std::invocable<T, Args...>);
49   static_assert(std::invocable<const T, Args...>);
50 
51   return true;
52 }
53 
54 int *p;
55 int a[10];
__anond6f594150102(int x) 56 auto odd = [](int x) { return x % 2 != 0; };
__anond6f594150202(int x) 57 auto triple = [](int x) { return 3*x; };
__anond6f594150302null58 auto gen = [] { return 42; };
59 std::mt19937 g;
60 
61 // [algorithm.syn]
62 
63 static_assert(test(std::ranges::adjacent_find, a));
64 static_assert(test(std::ranges::all_of, a, odd));
65 static_assert(test(std::ranges::any_of, a, odd));
66 static_assert(test(std::ranges::binary_search, a, 42));
67 static_assert(test(std::ranges::clamp, 42, 42, 42));
68 static_assert(test(std::ranges::copy, a, a));
69 static_assert(test(std::ranges::copy_backward, a, a));
70 static_assert(test(std::ranges::copy_if, a, a, odd));
71 static_assert(test(std::ranges::copy_n, a, 10, a));
72 static_assert(test(std::ranges::count, a, 42));
73 static_assert(test(std::ranges::count_if, a, odd));
74 //static_assert(test(std::ranges::ends_with, a, a));
75 static_assert(test(std::ranges::equal, a, a));
76 static_assert(test(std::ranges::equal_range, a, 42));
77 static_assert(test(std::ranges::fill, a, 42));
78 static_assert(test(std::ranges::fill_n, a, 10, 42));
79 static_assert(test(std::ranges::find, a, 42));
80 static_assert(test(std::ranges::find_end, a, a));
81 static_assert(test(std::ranges::find_first_of, a, a));
82 static_assert(test(std::ranges::find_if, a, odd));
83 static_assert(test(std::ranges::find_if_not, a, odd));
84 static_assert(test(std::ranges::for_each, a, odd));
85 static_assert(test(std::ranges::for_each_n, a, 10, odd));
86 static_assert(test(std::ranges::generate, a, gen));
87 static_assert(test(std::ranges::generate_n, a, 10, gen));
88 static_assert(test(std::ranges::includes, a, a));
89 static_assert(test(std::ranges::inplace_merge, a, a+5));
90 static_assert(test(std::ranges::is_heap, a));
91 static_assert(test(std::ranges::is_heap_until, a));
92 static_assert(test(std::ranges::is_partitioned, a, odd));
93 static_assert(test(std::ranges::is_permutation, a, a));
94 static_assert(test(std::ranges::is_sorted, a));
95 static_assert(test(std::ranges::is_sorted_until, a));
96 static_assert(test(std::ranges::lexicographical_compare, a, a));
97 static_assert(test(std::ranges::lower_bound, a, 42));
98 static_assert(test(std::ranges::make_heap, a));
99 static_assert(test(std::ranges::max, a));
100 static_assert(test(std::ranges::max_element, a));
101 static_assert(test(std::ranges::merge, a, a, a));
102 static_assert(test(std::ranges::min, a));
103 static_assert(test(std::ranges::min_element, a));
104 static_assert(test(std::ranges::minmax, a));
105 static_assert(test(std::ranges::minmax_element, a));
106 static_assert(test(std::ranges::mismatch, a, a));
107 static_assert(test(std::ranges::move, a, a));
108 static_assert(test(std::ranges::move_backward, a, a));
109 static_assert(test(std::ranges::next_permutation, a));
110 static_assert(test(std::ranges::none_of, a, odd));
111 static_assert(test(std::ranges::nth_element, a, a+5));
112 static_assert(test(std::ranges::partial_sort, a, a+5));
113 static_assert(test(std::ranges::partial_sort_copy, a, a));
114 static_assert(test(std::ranges::partition, a, odd));
115 static_assert(test(std::ranges::partition_copy, a, a, a, odd));
116 static_assert(test(std::ranges::partition_point, a, odd));
117 static_assert(test(std::ranges::pop_heap, a));
118 static_assert(test(std::ranges::prev_permutation, a));
119 static_assert(test(std::ranges::push_heap, a));
120 static_assert(test(std::ranges::remove, a, 42));
121 static_assert(test(std::ranges::remove_copy, a, a, 42));
122 static_assert(test(std::ranges::remove_copy_if, a, a, odd));
123 static_assert(test(std::ranges::remove_if, a, odd));
124 static_assert(test(std::ranges::replace, a, 42, 43));
125 static_assert(test(std::ranges::replace_copy, a, a, 42, 43));
126 static_assert(test(std::ranges::replace_copy_if, a, a, odd, 43));
127 static_assert(test(std::ranges::replace_if, a, odd, 43));
128 static_assert(test(std::ranges::reverse, a));
129 static_assert(test(std::ranges::reverse_copy, a, a));
130 static_assert(test(std::ranges::rotate, a, a+5));
131 static_assert(test(std::ranges::rotate_copy, a, a+5, a));
132 static_assert(test(std::ranges::sample, a, a, 5, g));
133 static_assert(test(std::ranges::search, a, a));
134 static_assert(test(std::ranges::search_n, a, 10, 42));
135 static_assert(test(std::ranges::set_difference, a, a, a));
136 static_assert(test(std::ranges::set_intersection, a, a, a));
137 static_assert(test(std::ranges::set_symmetric_difference, a, a, a));
138 static_assert(test(std::ranges::set_union, a, a, a));
139 static_assert(test(std::ranges::shuffle, a, g));
140 static_assert(test(std::ranges::sort, a));
141 static_assert(test(std::ranges::sort_heap, a));
142 static_assert(test(std::ranges::stable_partition, a, odd));
143 static_assert(test(std::ranges::stable_sort, a));
144 //static_assert(test(std::ranges::starts_with, a, a));
145 static_assert(test(std::ranges::swap_ranges, a, a));
146 static_assert(test(std::ranges::transform, a, a, triple));
147 static_assert(test(std::ranges::unique, a));
148 static_assert(test(std::ranges::unique_copy, a, a));
149 static_assert(test(std::ranges::upper_bound, a, 42));
150 
151 // [memory.syn]
152 
153 static_assert(test(std::ranges::construct_at, a, 42));
154 static_assert(test(std::ranges::destroy, a));
155 static_assert(test(std::ranges::destroy, a, a+10));
156 static_assert(test(std::ranges::destroy_at, a));
157 static_assert(test(std::ranges::destroy_n, a, 10));
158 static_assert(test(std::ranges::uninitialized_copy, a, a));
159 static_assert(test(std::ranges::uninitialized_copy, a, a+10, a, a+10));
160 static_assert(test(std::ranges::uninitialized_copy_n, a, 10, a, a+10));
161 static_assert(test(std::ranges::uninitialized_default_construct, a));
162 static_assert(test(std::ranges::uninitialized_default_construct, a, a+10));
163 static_assert(test(std::ranges::uninitialized_default_construct_n, a, 10));
164 static_assert(test(std::ranges::uninitialized_fill, a, 42));
165 static_assert(test(std::ranges::uninitialized_fill, a, a+10, 42));
166 static_assert(test(std::ranges::uninitialized_fill_n, a, 10, 42));
167 static_assert(test(std::ranges::uninitialized_move, a, a));
168 static_assert(test(std::ranges::uninitialized_move, a, a+10, a, a+10));
169 static_assert(test(std::ranges::uninitialized_move_n, a, 10, a, a+10));
170 static_assert(test(std::ranges::uninitialized_value_construct, a));
171 static_assert(test(std::ranges::uninitialized_value_construct, a, a+10));
172 static_assert(test(std::ranges::uninitialized_value_construct_n, a, 10));
173 
174 // [numeric.ops.overview] currently has no ranges algorithms. See P1813, P2214
175 
176 // [range.iter.ops]
177 
178 static_assert(test(std::ranges::advance, p, 5));
179 static_assert(test(std::ranges::advance, p, 5, a+10));
180 static_assert(test(std::ranges::advance, p, a+10));
181 static_assert(test(std::ranges::distance, a));
182 static_assert(test(std::ranges::distance, a, a+10));
183 static_assert(test(std::ranges::next, a));
184 static_assert(test(std::ranges::next, a, 5));
185 static_assert(test(std::ranges::next, a, 5, a+10));
186 static_assert(test(std::ranges::next, a, a+10));
187 static_assert(test(std::ranges::prev, a+10));
188 static_assert(test(std::ranges::prev, a+10, 5));
189 static_assert(test(std::ranges::prev, a+10, 5, a));
190