xref: /llvm-project/libcxx/test/libcxx/algorithms/pstl.iterator-requirements.verify.cpp (revision 9540950a45eee79a3ae4e9e0fa92d72b703d14dd)
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 // REQUIRES: stdlib=libc++
11 // UNSUPPORTED: libcpp-has-no-incomplete-pstl
12 
13 // <algorithm>
14 // <numeric>
15 
16 // Make sure that all PSTL algorithms contain checks for iterator requirements.
17 // This is not a requirement from the Standard, but we strive to catch misuse in
18 // the PSTL both because we can, and because iterator category mistakes in the
19 // PSTL can lead to subtle bugs.
20 
21 // Ignore spurious errors after the initial static_assert failure.
22 // ADDITIONAL_COMPILE_FLAGS: -Xclang -verify-ignore-unexpected=error
23 
24 // We only diagnose this in C++20 and above because we implement the checks with concepts.
25 // UNSUPPORTED: c++17
26 
27 #include <algorithm>
28 #include <cstddef>
29 #include <execution>
30 #include <numeric>
31 
32 #include "test_iterators.h"
33 
34 using non_forward_iterator = cpp17_input_iterator<int*>;
35 struct non_output_iterator : forward_iterator<int*> {
36   constexpr int const& operator*() const; // prevent it from being an output iterator
37 };
38 
f(non_forward_iterator non_fwd,non_output_iterator non_output,std::execution::sequenced_policy pol)39 void f(non_forward_iterator non_fwd, non_output_iterator non_output, std::execution::sequenced_policy pol) {
40   auto pred     = [](auto&&...) -> bool { return true; };
41   auto func     = [](auto&&...) -> int { return 1; };
42   int* it       = nullptr;
43   int* out      = nullptr;
44   std::size_t n = 0;
45   int val       = 0;
46 
47   {
48     (void)std::any_of(pol, non_fwd, non_fwd, pred);  // expected-error@*:* {{static assertion failed: any_of}}
49     (void)std::all_of(pol, non_fwd, non_fwd, pred);  // expected-error@*:* {{static assertion failed: all_of}}
50     (void)std::none_of(pol, non_fwd, non_fwd, pred); // expected-error@*:* {{static assertion failed: none_of}}
51   }
52 
53   {
54     (void)std::copy(pol, non_fwd, non_fwd, it); // expected-error@*:* {{static assertion failed: copy}}
55     (void)std::copy(pol, it, it, non_fwd);      // expected-error@*:* {{static assertion failed: copy}}
56     (void)std::copy(pol, it, it, non_output);   // expected-error@*:* {{static assertion failed: copy}}
57   }
58   {
59     (void)std::copy_n(pol, non_fwd, n, it);    // expected-error@*:* {{static assertion failed: copy_n}}
60     (void)std::copy_n(pol, it, n, non_fwd);    // expected-error@*:* {{static assertion failed: copy_n}}
61     (void)std::copy_n(pol, it, n, non_output); // expected-error@*:* {{static assertion failed: copy_n}}
62   }
63 
64   {
65     (void)std::count(pol, non_fwd, non_fwd, val);     // expected-error@*:* {{static assertion failed: count}}
66     (void)std::count_if(pol, non_fwd, non_fwd, pred); // expected-error@*:* {{static assertion failed: count_if}}
67   }
68 
69   {
70     (void)std::equal(pol, non_fwd, non_fwd, it);       // expected-error@*:* {{static assertion failed: equal}}
71     (void)std::equal(pol, it, it, non_fwd);            // expected-error@*:* {{static assertion failed: equal}}
72     (void)std::equal(pol, non_fwd, non_fwd, it, pred); // expected-error@*:* {{static assertion failed: equal}}
73     (void)std::equal(pol, it, it, non_fwd, pred);      // expected-error@*:* {{static assertion failed: equal}}
74 
75     (void)std::equal(pol, non_fwd, non_fwd, it, it);       // expected-error@*:* {{static assertion failed: equal}}
76     (void)std::equal(pol, it, it, non_fwd, non_fwd);       // expected-error@*:* {{static assertion failed: equal}}
77     (void)std::equal(pol, non_fwd, non_fwd, it, it, pred); // expected-error@*:* {{static assertion failed: equal}}
78     (void)std::equal(pol, it, it, non_fwd, non_fwd, pred); // expected-error@*:* {{static assertion failed: equal}}
79   }
80 
81   {
82     (void)std::fill(pol, non_fwd, non_fwd, val); // expected-error@*:* {{static assertion failed: fill}}
83     (void)std::fill_n(pol, non_fwd, n, val);     // expected-error@*:* {{static assertion failed: fill_n}}
84   }
85 
86   {
87     (void)std::find(pol, non_fwd, non_fwd, val);         // expected-error@*:* {{static assertion failed: find}}
88     (void)std::find_if(pol, non_fwd, non_fwd, pred);     // expected-error@*:* {{static assertion failed: find_if}}
89     (void)std::find_if_not(pol, non_fwd, non_fwd, pred); // expected-error@*:* {{static assertion failed: find_if_not}}
90   }
91 
92   {
93     (void)std::for_each(pol, non_fwd, non_fwd, func); // expected-error@*:* {{static assertion failed: for_each}}
94     (void)std::for_each_n(pol, non_fwd, n, func);     // expected-error@*:* {{static assertion failed: for_each_n}}
95   }
96 
97   {
98     (void)std::generate(pol, non_fwd, non_fwd, func); // expected-error@*:* {{static assertion failed: generate}}
99     (void)std::generate_n(pol, non_fwd, n, func);     // expected-error@*:* {{static assertion failed: generate_n}}
100   }
101 
102   {
103     (void)std::is_partitioned(
104         pol, non_fwd, non_fwd, pred); // expected-error@*:* {{static assertion failed: is_partitioned}}
105   }
106 
107   {
108     (void)std::merge(pol, non_fwd, non_fwd, it, it, out); // expected-error@*:* {{static assertion failed: merge}}
109     (void)std::merge(pol, it, it, non_fwd, non_fwd, out); // expected-error@*:* {{static assertion failed: merge}}
110     (void)std::merge(pol, it, it, it, it, non_output);    // expected-error@*:* {{static assertion failed: merge}}
111 
112     (void)std::merge(pol, non_fwd, non_fwd, it, it, out, pred); // expected-error@*:* {{static assertion failed: merge}}
113     (void)std::merge(pol, it, it, non_fwd, non_fwd, out, pred); // expected-error@*:* {{static assertion failed: merge}}
114     (void)std::merge(pol, it, it, it, it, non_output, pred);    // expected-error@*:* {{static assertion failed: merge}}
115   }
116 
117   {
118     (void)std::move(pol, non_fwd, non_fwd, out); // expected-error@*:* {{static assertion failed: move}}
119     (void)std::move(pol, it, it, non_fwd);       // expected-error@*:* {{static assertion failed: move}}
120     (void)std::move(pol, it, it, non_output);    // expected-error@*:* {{static assertion failed: move}}
121   }
122 
123   {
124     (void)std::replace_if(
125         pol, non_fwd, non_fwd, pred, val);               // expected-error@*:* {{static assertion failed: replace_if}}
126     (void)std::replace(pol, non_fwd, non_fwd, val, val); // expected-error@*:* {{static assertion failed: replace}}
127 
128     (void)std::replace_copy_if(
129         pol, non_fwd, non_fwd, out, pred, val); // expected-error@*:* {{static assertion failed: replace_copy_if}}
130     (void)std::replace_copy_if(
131         pol, it, it, non_fwd, pred, val); // expected-error@*:* {{static assertion failed: replace_copy_if}}
132     (void)std::replace_copy_if(
133         pol, it, it, non_output, pred, val); // expected-error@*:* {{static assertion failed: replace_copy_if}}
134 
135     (void)std::replace_copy(
136         pol, non_fwd, non_fwd, out, val, val); // expected-error@*:* {{static assertion failed: replace_copy}}
137     (void)std::replace_copy(
138         pol, it, it, non_fwd, val, val); // expected-error@*:* {{static assertion failed: replace_copy}}
139     (void)std::replace_copy(
140         pol, it, it, non_output, val, val); // expected-error@*:* {{static assertion failed: replace_copy}}
141   }
142 
143   {
144     (void)std::rotate_copy(
145         pol, non_fwd, non_fwd, non_fwd, out);            // expected-error@*:* {{static assertion failed: rotate_copy}}
146     (void)std::rotate_copy(pol, it, it, it, non_fwd);    // expected-error@*:* {{static assertion failed: rotate_copy}}
147     (void)std::rotate_copy(pol, it, it, it, non_output); // expected-error@*:* {{static assertion failed: rotate_copy}}
148   }
149 
150   {
151     (void)std::sort(pol, non_fwd, non_fwd);       // expected-error@*:* {{static assertion failed: sort}}
152     (void)std::sort(pol, non_fwd, non_fwd, pred); // expected-error@*:* {{static assertion failed: sort}}
153   }
154 
155   {
156     (void)std::stable_sort(pol, non_fwd, non_fwd);       // expected-error@*:* {{static assertion failed: stable_sort}}
157     (void)std::stable_sort(pol, non_fwd, non_fwd, pred); // expected-error@*:* {{static assertion failed: stable_sort}}
158   }
159 
160   {
161     (void)std::transform(pol, non_fwd, non_fwd, out, func); // expected-error@*:* {{static assertion failed: transform}}
162     (void)std::transform(pol, it, it, non_fwd, func);       // expected-error@*:* {{static assertion failed: transform}}
163     (void)std::transform(pol, it, it, non_output, func);    // expected-error@*:* {{static assertion failed: transform}}
164 
165     (void)std::transform(
166         pol, non_fwd, non_fwd, it, out, func);             // expected-error@*:* {{static assertion failed: transform}}
167     (void)std::transform(pol, it, it, non_fwd, out, func); // expected-error@*:* {{static assertion failed: transform}}
168     (void)std::transform(pol, it, it, it, non_fwd, func);  // expected-error@*:* {{static assertion failed: transform}}
169     (void)std::transform(
170         pol, it, it, it, non_output, func); // expected-error@*:* {{static assertion failed: transform}}
171   }
172 
173   {
174     (void)std::reduce(pol, non_fwd, non_fwd);            // expected-error@*:* {{static assertion failed: reduce}}
175     (void)std::reduce(pol, non_fwd, non_fwd, val);       // expected-error@*:* {{static assertion failed: reduce}}
176     (void)std::reduce(pol, non_fwd, non_fwd, val, func); // expected-error@*:* {{static assertion failed: reduce}}
177   }
178 
179   {
180     (void)std::transform_reduce(
181         pol, non_fwd, non_fwd, it, val); // expected-error@*:* {{static assertion failed: transform_reduce}}
182     (void)std::transform_reduce(
183         pol, it, it, non_fwd, val); // expected-error@*:* {{static assertion failed: transform_reduce}}
184 
185     (void)std::transform_reduce(
186         pol, non_fwd, non_fwd, it, val, func, func); // expected-error@*:* {{static assertion failed: transform_reduce}}
187     (void)std::transform_reduce(
188         pol, it, it, non_fwd, val, func, func); // expected-error@*:* {{static assertion failed: transform_reduce}}
189 
190     (void)std::transform_reduce(
191         pol, non_fwd, non_fwd, val, func, func); // expected-error@*:* {{static assertion failed: transform_reduce}}
192   }
193 }
194