1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
/*
Copyright (c) Marshall Clow 2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
*/
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/copy_if.hpp>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <boost/algorithm/cxx11/all_of.hpp>
#include <boost/algorithm/cxx11/none_of.hpp>
namespace ba = boost::algorithm;
// namespace ba = boost;
bool is_true ( int v ) { return true; }
bool is_false ( int v ) { return false; }
bool is_even ( int v ) { return v % 2 == 0; }
bool is_odd ( int v ) { return v % 2 == 1; }
template <typename Container>
void test_copy_if ( Container const &c ) {
typedef typename Container::value_type value_type;
std::vector<value_type> v;
// None of the elements
v.clear ();
ba::copy_if ( c.begin (), c.end (), back_inserter ( v ), is_false);
BOOST_CHECK ( v.size () == 0 );
v.clear ();
ba::copy_if ( c, back_inserter ( v ), is_false);
BOOST_CHECK ( v.size () == 0 );
// All the elements
v.clear ();
ba::copy_if ( c.begin (), c.end (), back_inserter ( v ), is_true);
BOOST_CHECK ( v.size () == c.size ());
BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
v.clear ();
ba::copy_if ( c, back_inserter ( v ), is_true);
BOOST_CHECK ( v.size () == c.size ());
BOOST_CHECK ( v.size () == c.size ());
BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
// Some of the elements
v.clear ();
ba::copy_if ( c.begin (), c.end (), back_inserter ( v ), is_even );
BOOST_CHECK ( v.size () == (size_t) std::count_if ( c.begin (), c.end (), is_even ));
BOOST_CHECK ( ba::all_of ( v.begin (), v.end (), is_even ));
v.clear ();
ba::copy_if ( c, back_inserter ( v ), is_even );
BOOST_CHECK ( v.size () == (size_t) std::count_if ( c.begin (), c.end (), is_even ));
BOOST_CHECK ( ba::all_of ( v.begin (), v.end (), is_even ));
}
template <typename Container>
void test_copy_while ( Container const &c ) {
typedef typename Container::value_type value_type;
typename Container::const_iterator it;
std::vector<value_type> v;
// None of the elements
v.clear ();
ba::copy_while ( c.begin (), c.end (), back_inserter ( v ), is_false);
BOOST_CHECK ( v.size () == 0 );
v.clear ();
ba::copy_while ( c, back_inserter ( v ), is_false);
BOOST_CHECK ( v.size () == 0 );
// All the elements
v.clear ();
ba::copy_while ( c.begin (), c.end (), back_inserter ( v ), is_true);
BOOST_CHECK ( v.size () == c.size ());
BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
v.clear ();
ba::copy_while ( c, back_inserter ( v ), is_true);
BOOST_CHECK ( v.size () == c.size ());
BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
// Some of the elements
v.clear ();
it = ba::copy_while ( c.begin (), c.end (), back_inserter ( v ), is_even ).first;
BOOST_CHECK ( v.size () == (size_t) std::distance ( c.begin (), it ));
BOOST_CHECK ( it == c.end () || !is_even ( *it ));
BOOST_CHECK ( ba::all_of ( v.begin (), v.end (), is_even ));
BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
v.clear ();
it = ba::copy_while ( c, back_inserter ( v ), is_even ).first;
BOOST_CHECK ( v.size () == (size_t) std::distance ( c.begin (), it ));
BOOST_CHECK ( it == c.end () || !is_even ( *it ));
BOOST_CHECK ( ba::all_of ( v.begin (), v.end (), is_even ));
BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
}
template <typename Container>
void test_copy_until ( Container const &c ) {
typedef typename Container::value_type value_type;
typename Container::const_iterator it;
std::vector<value_type> v;
// None of the elements
v.clear ();
ba::copy_until ( c.begin (), c.end (), back_inserter ( v ), is_true);
BOOST_CHECK ( v.size () == 0 );
v.clear ();
ba::copy_until ( c, back_inserter ( v ), is_true);
BOOST_CHECK ( v.size () == 0 );
// All the elements
v.clear ();
ba::copy_until ( c.begin (), c.end (), back_inserter ( v ), is_false);
BOOST_CHECK ( v.size () == c.size ());
BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
v.clear ();
ba::copy_until ( c, back_inserter ( v ), is_false);
BOOST_CHECK ( v.size () == c.size ());
BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
// Some of the elements
v.clear ();
it = ba::copy_until ( c.begin (), c.end (), back_inserter ( v ), is_even ).first;
BOOST_CHECK ( v.size () == (size_t) std::distance ( c.begin (), it ));
BOOST_CHECK ( it == c.end () || is_even ( *it ));
BOOST_CHECK ( ba::none_of ( v.begin (), v.end (), is_even ));
BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
v.clear ();
it = ba::copy_until ( c, back_inserter ( v ), is_even ).first;
BOOST_CHECK ( v.size () == (size_t) std::distance ( c.begin (), it ));
BOOST_CHECK ( it == c.end () || is_even ( *it ));
BOOST_CHECK ( ba::none_of ( v.begin (), v.end (), is_even ));
BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
}
void test_sequence1 () {
std::vector<int> v;
for ( int i = 5; i < 15; ++i )
v.push_back ( i );
test_copy_if ( v );
test_copy_while ( v );
test_copy_until ( v );
std::list<int> l;
for ( int i = 25; i > 15; --i )
l.push_back ( i );
test_copy_if ( l );
test_copy_while ( l );
test_copy_until ( l );
}
BOOST_AUTO_TEST_CASE( test_main )
{
test_sequence1 ();
}
|