From d0a05da4c1fbd5f44ec891f1dccdef9578680204 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Fri, 21 Sep 2012 14:52:38 +0000 Subject: [PATCH] Fix bugs in copy_if; add basic tests. Refs #7400. Thanks to Hideaki Takei for the catch [SVN r80618] --- test/Jamfile.v2 | 1 + test/copy_if_test1.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 test/copy_if_test1.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index f8a346a..1d13cec 100755 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -32,6 +32,7 @@ import testing ; [ run ordered_test.cpp : : : : ordered_test ] [ run find_if_not_test1.cpp : : : : find_if_not_test1 ] + [ run copy_if_test1.cpp : : : : copy_if_test1 ] [ run copy_n_test1.cpp : : : : copy_n_test1 ] [ run iota_test1.cpp : : : : iota_test1 ] diff --git a/test/copy_if_test1.cpp b/test/copy_if_test1.cpp new file mode 100644 index 0000000..cc3c7ae --- /dev/null +++ b/test/copy_if_test1.cpp @@ -0,0 +1,87 @@ +/* + 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 +#include +#include + +#include +#include +#include +#include +#include + +#include + +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 +void test_sequence ( Container const &c ) { + + typedef typename Container::value_type value_type; + std::vector 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 ( 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 ())); + +// Some of the elements + v.clear (); + ba::copy_if ( c.begin (), c.end (), back_inserter ( v ), is_even ); + BOOST_CHECK ( v.size () == 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 () == std::count_if ( c.begin (), c.end (), is_even )); + BOOST_CHECK ( ba::all_of ( v.begin (), v.end (), is_even )); + } + + +void test_sequence1 () { + std::vector v; + for ( int i = 5; i < 15; ++i ) + v.push_back ( i ); + test_sequence ( v ); + + std::list l; + for ( int i = 25; i > 15; --i ) + l.push_back ( i ); + test_sequence ( l ); + } + + +int test_main( int , char* [] ) +{ + test_sequence1 (); + return 0; +}