| 
									
										
										
										
											2012-09-21 14:52:38 +00:00
										 |  |  | /* 
 | 
					
						
							|  |  |  |    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>
 | 
					
						
							| 
									
										
										
										
											2013-02-08 17:21:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define BOOST_TEST_MAIN
 | 
					
						
							|  |  |  | #include <boost/test/unit_test.hpp>
 | 
					
						
							| 
									
										
										
										
											2012-09-21 14:52:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <algorithm>
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | #include <list>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/algorithm/cxx11/all_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_sequence ( 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 ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-11 16:54:57 +00:00
										 |  |  | //  All the elements
 | 
					
						
							| 
									
										
										
										
											2012-09-21 14:52:38 +00:00
										 |  |  |     v.clear (); | 
					
						
							|  |  |  |     ba::copy_if ( c.begin (), c.end (), back_inserter ( v ), is_true); | 
					
						
							|  |  |  |     BOOST_CHECK ( v.size () == c.size ()); | 
					
						
							|  |  |  |     BOOST_CHECK ( std::equal ( c.begin (), c.end (), v.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 ( c.begin (), c.end (), v.begin ())); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-11 16:54:57 +00:00
										 |  |  | //  Some of the elements
 | 
					
						
							| 
									
										
										
										
											2012-09-21 14:52:38 +00:00
										 |  |  |     v.clear (); | 
					
						
							|  |  |  |     ba::copy_if ( c.begin (), c.end (), back_inserter ( v ), is_even ); | 
					
						
							| 
									
										
										
										
											2013-02-08 17:21:43 +00:00
										 |  |  |     BOOST_CHECK ( v.size () == (size_t) std::count_if ( c.begin (), c.end (), is_even )); | 
					
						
							| 
									
										
										
										
											2012-09-21 14:52:38 +00:00
										 |  |  |     BOOST_CHECK ( ba::all_of ( v.begin (), v.end (), is_even )); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     v.clear (); | 
					
						
							|  |  |  |     ba::copy_if ( c, back_inserter ( v ), is_even ); | 
					
						
							| 
									
										
										
										
											2013-02-08 17:21:43 +00:00
										 |  |  |     BOOST_CHECK ( v.size () == (size_t) std::count_if ( c.begin (), c.end (), is_even )); | 
					
						
							| 
									
										
										
										
											2012-09-21 14:52:38 +00:00
										 |  |  |     BOOST_CHECK ( ba::all_of ( v.begin (), v.end (), is_even )); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void test_sequence1 () { | 
					
						
							|  |  |  |     std::vector<int> v; | 
					
						
							|  |  |  |     for ( int i = 5; i < 15; ++i ) | 
					
						
							|  |  |  |         v.push_back ( i ); | 
					
						
							|  |  |  |     test_sequence  ( v ); | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     std::list<int> l; | 
					
						
							|  |  |  |     for ( int i = 25; i > 15; --i ) | 
					
						
							|  |  |  |         l.push_back ( i ); | 
					
						
							|  |  |  |     test_sequence  ( l );    | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-08 17:21:43 +00:00
										 |  |  | BOOST_AUTO_TEST_CASE( test_main ) | 
					
						
							| 
									
										
										
										
											2012-09-21 14:52:38 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   test_sequence1 (); | 
					
						
							|  |  |  | } |