• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //   copy_if(InIter first, InIter last, OutIter result, Pred pred);
17 
18 #include <algorithm>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 #include "test_iterators.h"
23 
24 // #if TEST_STD_VER > 17
25 // TEST_CONSTEXPR bool test_constexpr() {
26 //     int ia[] = {2, 4, 6, 8, 6};
27 //     int ic[] = {0, 0, 0, 0, 0, 0};
28 //
29 // 	auto p = std::copy_if(std::begin(ia), std::end(ia), std::begin(ic), is6);
30 // 	return std::all_of(std::begin(ic), p, [](int a){return a == 6;})
31 // 	    && std::all_of(p, std::end(ic),   [](int a){return a == 0;})
32 // 	    ;
33 //     }
34 // #endif
35 
36 struct Pred
37 {
operator ()Pred38     bool operator()(int i) {return i % 3 == 0;}
39 };
40 
41 template <class InIter, class OutIter>
42 void
test()43 test()
44 {
45     const unsigned N = 1000;
46     int ia[N];
47     for (unsigned i = 0; i < N; ++i)
48         ia[i] = i;
49     int ib[N] = {0};
50 
51     OutIter r = std::copy_if(InIter(ia), InIter(ia+N), OutIter(ib), Pred());
52     assert(base(r) == ib+N/3+1);
53     for (unsigned i = 0; i < N/3+1; ++i)
54         assert(ib[i] % 3 == 0);
55 }
56 
main()57 int main()
58 {
59     test<input_iterator<const int*>, output_iterator<int*> >();
60     test<input_iterator<const int*>, input_iterator<int*> >();
61     test<input_iterator<const int*>, forward_iterator<int*> >();
62     test<input_iterator<const int*>, bidirectional_iterator<int*> >();
63     test<input_iterator<const int*>, random_access_iterator<int*> >();
64     test<input_iterator<const int*>, int*>();
65 
66     test<forward_iterator<const int*>, output_iterator<int*> >();
67     test<forward_iterator<const int*>, input_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*>, input_iterator<int*> >();
75     test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
76     test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
77     test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
78     test<bidirectional_iterator<const int*>, int*>();
79 
80     test<random_access_iterator<const int*>, output_iterator<int*> >();
81     test<random_access_iterator<const int*>, input_iterator<int*> >();
82     test<random_access_iterator<const int*>, forward_iterator<int*> >();
83     test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
84     test<random_access_iterator<const int*>, random_access_iterator<int*> >();
85     test<random_access_iterator<const int*>, int*>();
86 
87     test<const int*, output_iterator<int*> >();
88     test<const int*, input_iterator<int*> >();
89     test<const int*, forward_iterator<int*> >();
90     test<const int*, bidirectional_iterator<int*> >();
91     test<const int*, random_access_iterator<int*> >();
92     test<const int*, int*>();
93 
94 // #if TEST_STD_VER > 17
95 //     static_assert(test_constexpr());
96 // #endif
97 }
98