// Copyright Neil Groves 2009. Use, modification and // distribution is subject to 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/libs/range/ // #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace { template void test(Container& cont) { Container reference(cont); Container test(cont); bool reference_result = std::binary_search(reference.begin(), reference.end(), 5); bool test_result = boost::binary_search(test, 5); BOOST_CHECK( reference_result == test_result ); BOOST_CHECK( test_result == boost::binary_search(boost::make_iterator_range(test), 5) ); BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), test.begin(), test.end() ); } template void sort_container(Container& cont, BinaryPredicate pred) { typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t; std::vector temp(cont.begin(), cont.end()); std::sort(temp.begin(), temp.end(), pred); cont.assign(temp.begin(), temp.end()); } template void test_pred(Container& cont, BinaryPredicate pred) { Container reference(cont); Container test(cont); sort_container(reference, pred); sort_container(test, pred); bool reference_result = std::binary_search(reference.begin(), reference.end(), 5, pred); bool test_result = boost::binary_search(test, 5, pred); BOOST_CHECK( test_result == boost::binary_search(boost::make_iterator_range(test), 5, pred) ); BOOST_CHECK( reference_result == test_result ); BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), test.begin(), test.end() ); } template void test_binary_search_impl() { using namespace boost::assign; Container cont; test(cont); test_pred(cont, std::less()); test_pred(cont, std::greater()); cont.clear(); cont += 1; test(cont); test_pred(cont, std::less()); test_pred(cont, std::greater()); cont.clear(); cont += 1,2,3,4,5,6,7,8,9; test(cont); test_pred(cont, std::less()); test_pred(cont, std::greater()); } void test_binary_search() { test_binary_search_impl< std::vector >(); test_binary_search_impl< std::list >(); test_binary_search_impl< std::deque >(); } } } boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) { boost::unit_test::test_suite* test = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.binary_search" ); test->add( BOOST_TEST_CASE( &boost::test_binary_search ) ); return test; }