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 // <algorithm>
10
11 // template<InputIterator InIter, OutputIterator<auto, InIter::reference> OutIter,
12 // Predicate<auto, InIter::value_type> Pred>
13 // requires CopyConstructible<Pred>
14 // constexpr OutIter // constexpr after C++17
15 // copy_if(InIter first, InIter last, OutIter result, Pred pred);
16
17 #include <algorithm>
18 #include <cassert>
19
20 #include "test_macros.h"
21 #include "test_iterators.h"
22
23 struct Pred
24 {
operator ()Pred25 TEST_CONSTEXPR_CXX14 bool operator()(int i) {return i % 3 == 0;}
26 };
27
28 template <class InIter, class OutIter>
29 TEST_CONSTEXPR_CXX20 void
test_copy_if()30 test_copy_if()
31 {
32 const unsigned N = 1000;
33 int ia[N] = {};
34 for (unsigned i = 0; i < N; ++i)
35 ia[i] = i;
36 int ib[N] = {0};
37
38 OutIter r = std::copy_if(InIter(ia), InIter(ia+N), OutIter(ib), Pred());
39 assert(base(r) == ib+N/3+1);
40 for (unsigned i = 0; i < N/3+1; ++i)
41 assert(ib[i] % 3 == 0);
42 }
43
44 TEST_CONSTEXPR_CXX20 bool
test()45 test()
46 {
47 test_copy_if<cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();
48 test_copy_if<cpp17_input_iterator<const int*>, cpp17_input_iterator<int*> >();
49 test_copy_if<cpp17_input_iterator<const int*>, forward_iterator<int*> >();
50 test_copy_if<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
51 test_copy_if<cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
52 test_copy_if<cpp17_input_iterator<const int*>, int*>();
53
54 test_copy_if<forward_iterator<const int*>, cpp17_output_iterator<int*> >();
55 test_copy_if<forward_iterator<const int*>, cpp17_input_iterator<int*> >();
56 test_copy_if<forward_iterator<const int*>, forward_iterator<int*> >();
57 test_copy_if<forward_iterator<const int*>, bidirectional_iterator<int*> >();
58 test_copy_if<forward_iterator<const int*>, random_access_iterator<int*> >();
59 test_copy_if<forward_iterator<const int*>, int*>();
60
61 test_copy_if<bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();
62 test_copy_if<bidirectional_iterator<const int*>, cpp17_input_iterator<int*> >();
63 test_copy_if<bidirectional_iterator<const int*>, forward_iterator<int*> >();
64 test_copy_if<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
65 test_copy_if<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
66 test_copy_if<bidirectional_iterator<const int*>, int*>();
67
68 test_copy_if<random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
69 test_copy_if<random_access_iterator<const int*>, cpp17_input_iterator<int*> >();
70 test_copy_if<random_access_iterator<const int*>, forward_iterator<int*> >();
71 test_copy_if<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
72 test_copy_if<random_access_iterator<const int*>, random_access_iterator<int*> >();
73 test_copy_if<random_access_iterator<const int*>, int*>();
74
75 test_copy_if<const int*, cpp17_output_iterator<int*> >();
76 test_copy_if<const int*, cpp17_input_iterator<int*> >();
77 test_copy_if<const int*, forward_iterator<int*> >();
78 test_copy_if<const int*, bidirectional_iterator<int*> >();
79 test_copy_if<const int*, random_access_iterator<int*> >();
80 test_copy_if<const int*, int*>();
81
82 return true;
83 }
84
main(int,char **)85 int main(int, char**)
86 {
87 test();
88
89 #if TEST_STD_VER > 17
90 static_assert(test());
91 #endif
92
93 return 0;
94 }
95