| 1 | // Boost.Range library |
| 2 | // |
| 3 | // Copyright Neil Groves 2009. Use, modification and |
| 4 | // distribution is subject to the Boost Software License, Version |
| 5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
| 7 | // |
| 8 | // |
| 9 | // For more information, see http://www.boost.org/libs/range/ |
| 10 | // |
| 11 | #include <boost/range/algorithm/count_if.hpp> |
| 12 | |
| 13 | #include <boost/test/test_tools.hpp> |
| 14 | #include <boost/test/unit_test.hpp> |
| 15 | |
| 16 | #include <boost/assign.hpp> |
| 17 | #include "../test_function/false_predicate.hpp" |
| 18 | #include "../test_function/true_predicate.hpp" |
| 19 | #include "../test_function/equal_to_x.hpp" |
| 20 | #include <algorithm> |
| 21 | #include <list> |
| 22 | #include <set> |
| 23 | #include <vector> |
| 24 | |
| 25 | namespace boost |
| 26 | { |
| 27 | namespace |
| 28 | { |
| 29 | template< class Container > |
| 30 | void test_count_if_impl() |
| 31 | { |
| 32 | using namespace boost::range_test_function; |
| 33 | using namespace boost::assign; |
| 34 | |
| 35 | typedef equal_to_x<int> pred_t; |
| 36 | typedef BOOST_DEDUCED_TYPENAME std::iterator_traits<BOOST_DEDUCED_TYPENAME Container::iterator>::difference_type diff_t; |
| 37 | |
| 38 | Container cont; |
| 39 | const Container& cref_cont = cont; |
| 40 | |
| 41 | BOOST_CHECK_EQUAL( 0u, boost::count_if(cont, pred_t(0)) ); |
| 42 | BOOST_CHECK_EQUAL( 0u, boost::count_if(cref_cont, pred_t(0)) ); |
| 43 | BOOST_CHECK_EQUAL( 0u, boost::count_if(boost::make_iterator_range(cont), pred_t(0)) ); |
| 44 | |
| 45 | cont += 1; |
| 46 | BOOST_CHECK_EQUAL( 0u, boost::count_if(cont, pred_t(0)) ); |
| 47 | BOOST_CHECK_EQUAL( 0u, boost::count_if(cref_cont, pred_t(0)) ); |
| 48 | BOOST_CHECK_EQUAL( 0u, boost::count_if(boost::make_iterator_range(cont), pred_t(0)) ); |
| 49 | BOOST_CHECK_EQUAL( 1u, boost::count_if(cont, pred_t(1)) ); |
| 50 | BOOST_CHECK_EQUAL( 1u, boost::count_if(cref_cont, pred_t(1)) ); |
| 51 | BOOST_CHECK_EQUAL( 1u, boost::count_if(boost::make_iterator_range(cont), pred_t(1)) ); |
| 52 | |
| 53 | cont += 2,3,4,5,6,7,8,9; |
| 54 | BOOST_CHECK_EQUAL( 0u, boost::count_if(cont, pred_t(0)) ); |
| 55 | BOOST_CHECK_EQUAL( 0u, boost::count_if(cref_cont, pred_t(0)) ); |
| 56 | BOOST_CHECK_EQUAL( 0u, boost::count_if(boost::make_iterator_range(cont), pred_t(0)) ); |
| 57 | BOOST_CHECK_EQUAL( 1u, boost::count_if(cont, pred_t(1)) ); |
| 58 | BOOST_CHECK_EQUAL( 1u, boost::count_if(cref_cont, pred_t(1)) ); |
| 59 | BOOST_CHECK_EQUAL( 1u, boost::count_if(boost::make_iterator_range(cont), pred_t(1)) ); |
| 60 | |
| 61 | cont += 2; |
| 62 | BOOST_CHECK_EQUAL( 0u, boost::count_if(cont, pred_t(0)) ); |
| 63 | BOOST_CHECK_EQUAL( 0u, boost::count_if(cref_cont, pred_t(0)) ); |
| 64 | BOOST_CHECK_EQUAL( 0u, boost::count_if(boost::make_iterator_range(cont), pred_t(0)) ); |
| 65 | BOOST_CHECK_EQUAL( 1u, boost::count_if(cont, pred_t(1)) ); |
| 66 | BOOST_CHECK_EQUAL( 1u, boost::count_if(cref_cont, pred_t(1)) ); |
| 67 | BOOST_CHECK_EQUAL( 1u, boost::count_if(boost::make_iterator_range(cont), pred_t(1)) ); |
| 68 | BOOST_CHECK_EQUAL( 2u, boost::count_if(cont, pred_t(2)) ); |
| 69 | BOOST_CHECK_EQUAL( 2u, boost::count_if(cref_cont, pred_t(2)) ); |
| 70 | BOOST_CHECK_EQUAL( 2u, boost::count_if(boost::make_iterator_range(cont), pred_t(2)) ); |
| 71 | |
| 72 | BOOST_CHECK_EQUAL( 0u, boost::count_if(cont, false_predicate()) ); |
| 73 | BOOST_CHECK_EQUAL( 0u, boost::count_if(cref_cont, false_predicate()) ); |
| 74 | BOOST_CHECK_EQUAL( 0u, boost::count_if(boost::make_iterator_range(cont), false_predicate()) ); |
| 75 | |
| 76 | BOOST_CHECK_EQUAL( static_cast<diff_t>(cont.size()), boost::count_if(cont, true_predicate()) ); |
| 77 | BOOST_CHECK_EQUAL( static_cast<diff_t>(cont.size()), boost::count_if(cref_cont, true_predicate()) ); |
| 78 | BOOST_CHECK_EQUAL( static_cast<diff_t>(cont.size()), boost::count_if(boost::make_iterator_range(cont), true_predicate()) ); |
| 79 | } |
| 80 | |
| 81 | void test_count_if() |
| 82 | { |
| 83 | test_count_if_impl< std::vector<int> >(); |
| 84 | test_count_if_impl< std::list<int> >(); |
| 85 | test_count_if_impl< std::multiset<int> >(); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | |
| 91 | boost::unit_test::test_suite* |
| 92 | init_unit_test_suite(int argc, char* argv[]) |
| 93 | { |
| 94 | boost::unit_test::test_suite* test |
| 95 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.count_if" ); |
| 96 | |
| 97 | test->add( BOOST_TEST_CASE( &boost::test_count_if ) ); |
| 98 | |
| 99 | return test; |
| 100 | } |
| 101 | |