1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <algorithm>
11
12 // template<InputIterator InIter, OutputIterator<auto, InIter::reference> OutIter,
13 // Predicate<auto, InIter::value_type> Pred>
14 // requires CopyConstructible<Pred>
15 // constexpr OutIter // constexpr after C++17
16 // remove_copy_if(InIter first, InIter last, OutIter result, Pred pred);
17
18 #include <algorithm>
19 #include <functional>
20 #include <cassert>
21
22 #include "test_macros.h"
23 #include "test_iterators.h"
24
equalToTwo(int v)25 TEST_CONSTEXPR bool equalToTwo(int v) { return v == 2; }
26
27 #if TEST_STD_VER > 17
test_constexpr()28 TEST_CONSTEXPR bool test_constexpr() {
29 int ia[] = {1, 3, 5, 2, 5, 6};
30 int ib[std::size(ia)] = {0};
31
32 auto it = std::remove_copy_if(std::begin(ia), std::end(ia), std::begin(ib), equalToTwo);
33
34 return std::distance(std::begin(ib), it) == (std::size(ia) - 1) // we removed one element
35 && std::none_of(std::begin(ib), it, equalToTwo)
36 && std::all_of (it, std::end(ib), [](int a) {return a == 0;})
37 ;
38 }
39 #endif
40
41 template <class InIter, class OutIter>
42 void
test()43 test()
44 {
45 int ia[] = {0, 1, 2, 3, 4, 2, 3, 4, 2};
46 const unsigned sa = sizeof(ia)/sizeof(ia[0]);
47 int ib[sa];
48 OutIter r = std::remove_copy_if(InIter(ia), InIter(ia+sa),
49 OutIter(ib), equalToTwo);
50 assert(base(r) == ib + sa-3);
51 assert(ib[0] == 0);
52 assert(ib[1] == 1);
53 assert(ib[2] == 3);
54 assert(ib[3] == 4);
55 assert(ib[4] == 3);
56 assert(ib[5] == 4);
57 }
58
main()59 int main()
60 {
61 test<input_iterator<const int*>, output_iterator<int*> >();
62 test<input_iterator<const int*>, forward_iterator<int*> >();
63 test<input_iterator<const int*>, bidirectional_iterator<int*> >();
64 test<input_iterator<const int*>, random_access_iterator<int*> >();
65 test<input_iterator<const int*>, int*>();
66
67 test<forward_iterator<const int*>, output_iterator<int*> >();
68 test<forward_iterator<const int*>, forward_iterator<int*> >();
69 test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
70 test<forward_iterator<const int*>, random_access_iterator<int*> >();
71 test<forward_iterator<const int*>, int*>();
72
73 test<bidirectional_iterator<const int*>, output_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*>, forward_iterator<int*> >();
81 test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
82 test<random_access_iterator<const int*>, random_access_iterator<int*> >();
83 test<random_access_iterator<const int*>, int*>();
84
85 test<const int*, output_iterator<int*> >();
86 test<const int*, forward_iterator<int*> >();
87 test<const int*, bidirectional_iterator<int*> >();
88 test<const int*, random_access_iterator<int*> >();
89 test<const int*, int*>();
90
91 #if TEST_STD_VER > 17
92 static_assert(test_constexpr());
93 #endif
94 }
95