xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/copy_if.pass.cpp (revision 2df59c50688c122bbcae7467d3eaf862c3ea3088)
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 // #if TEST_STD_VER > 17
24 // TEST_CONSTEXPR bool test_constexpr() {
25 //     int ia[] = {2, 4, 6, 8, 6};
26 //     int ic[] = {0, 0, 0, 0, 0, 0};
27 //
28 //     auto p = std::copy_if(std::begin(ia), std::end(ia), std::begin(ic), is6);
29 //     return std::all_of(std::begin(ic), p, [](int a){return a == 6;})
30 //         && std::all_of(p, std::end(ic),   [](int a){return a == 0;})
31 //         ;
32 //     }
33 // #endif
34 
35 struct Pred
36 {
37     bool operator()(int i) {return i % 3 == 0;}
38 };
39 
40 template <class InIter, class OutIter>
41 void
42 test()
43 {
44     const unsigned N = 1000;
45     int ia[N];
46     for (unsigned i = 0; i < N; ++i)
47         ia[i] = i;
48     int ib[N] = {0};
49 
50     OutIter r = std::copy_if(InIter(ia), InIter(ia+N), OutIter(ib), Pred());
51     assert(base(r) == ib+N/3+1);
52     for (unsigned i = 0; i < N/3+1; ++i)
53         assert(ib[i] % 3 == 0);
54 }
55 
56 int main(int, char**)
57 {
58     test<input_iterator<const int*>, output_iterator<int*> >();
59     test<input_iterator<const int*>, input_iterator<int*> >();
60     test<input_iterator<const int*>, forward_iterator<int*> >();
61     test<input_iterator<const int*>, bidirectional_iterator<int*> >();
62     test<input_iterator<const int*>, random_access_iterator<int*> >();
63     test<input_iterator<const int*>, int*>();
64 
65     test<forward_iterator<const int*>, output_iterator<int*> >();
66     test<forward_iterator<const int*>, input_iterator<int*> >();
67     test<forward_iterator<const int*>, forward_iterator<int*> >();
68     test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
69     test<forward_iterator<const int*>, random_access_iterator<int*> >();
70     test<forward_iterator<const int*>, int*>();
71 
72     test<bidirectional_iterator<const int*>, output_iterator<int*> >();
73     test<bidirectional_iterator<const int*>, input_iterator<int*> >();
74     test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
75     test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
76     test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
77     test<bidirectional_iterator<const int*>, int*>();
78 
79     test<random_access_iterator<const int*>, output_iterator<int*> >();
80     test<random_access_iterator<const int*>, input_iterator<int*> >();
81     test<random_access_iterator<const int*>, forward_iterator<int*> >();
82     test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
83     test<random_access_iterator<const int*>, random_access_iterator<int*> >();
84     test<random_access_iterator<const int*>, int*>();
85 
86     test<const int*, output_iterator<int*> >();
87     test<const int*, input_iterator<int*> >();
88     test<const int*, forward_iterator<int*> >();
89     test<const int*, bidirectional_iterator<int*> >();
90     test<const int*, random_access_iterator<int*> >();
91     test<const int*, int*>();
92 
93 // #if TEST_STD_VER > 17
94 //     static_assert(test_constexpr());
95 // #endif
96 
97   return 0;
98 }
99