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