xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove_copy_if.pass.cpp (revision 5e97d37b960840cabf32dfef3503b28ba5d21dc2)
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 //   remove_copy_if(InIter first, InIter last, OutIter result, Pred pred);
16 
17 #include <algorithm>
18 #include <functional>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 #include "test_iterators.h"
23 
equalToTwo(int v)24 TEST_CONSTEXPR bool equalToTwo(int v) { return v == 2; }
25 
26 #if TEST_STD_VER > 17
test_constexpr()27 TEST_CONSTEXPR bool test_constexpr() {
28     int ia[] = {1, 3, 5, 2, 5, 6};
29     int ib[std::size(ia)] = {0};
30 
31     auto it = std::remove_copy_if(std::begin(ia), std::end(ia), std::begin(ib), equalToTwo);
32 
33     return std::distance(std::begin(ib), it) == static_cast<int>(std::size(ia) - 1)   // we removed one element
34         && std::none_of(std::begin(ib), it, equalToTwo)
35         && std::all_of (it, std::end(ib), [](int a) {return a == 0;})
36            ;
37     }
38 #endif
39 
40 template <class InIter, class OutIter>
41 void
test()42 test()
43 {
44     int ia[] = {0, 1, 2, 3, 4, 2, 3, 4, 2};
45     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
46     int ib[sa];
47     OutIter r = std::remove_copy_if(InIter(ia), InIter(ia+sa),
48                                     OutIter(ib), equalToTwo);
49     assert(base(r) == ib + sa-3);
50     assert(ib[0] == 0);
51     assert(ib[1] == 1);
52     assert(ib[2] == 3);
53     assert(ib[3] == 4);
54     assert(ib[4] == 3);
55     assert(ib[5] == 4);
56 }
57 
main(int,char **)58 int main(int, char**)
59 {
60     test<cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();
61     test<cpp17_input_iterator<const int*>, forward_iterator<int*> >();
62     test<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
63     test<cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
64     test<cpp17_input_iterator<const int*>, int*>();
65 
66     test<forward_iterator<const int*>, cpp17_output_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*>, cpp17_output_iterator<int*> >();
73     test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
74     test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
75     test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
76     test<bidirectional_iterator<const int*>, int*>();
77 
78     test<random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
79     test<random_access_iterator<const int*>, forward_iterator<int*> >();
80     test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
81     test<random_access_iterator<const int*>, random_access_iterator<int*> >();
82     test<random_access_iterator<const int*>, int*>();
83 
84     test<const int*, cpp17_output_iterator<int*> >();
85     test<const int*, forward_iterator<int*> >();
86     test<const int*, bidirectional_iterator<int*> >();
87     test<const int*, random_access_iterator<int*> >();
88     test<const int*, int*>();
89 
90 #if TEST_STD_VER > 17
91     static_assert(test_constexpr());
92 #endif
93 
94   return 0;
95 }
96