diff --git a/include/boost/range/algorithm/remove_copy_if.hpp b/include/boost/range/algorithm/remove_copy_if.hpp index 791a38f..8febe59 100755 --- a/include/boost/range/algorithm/remove_copy_if.hpp +++ b/include/boost/range/algorithm/remove_copy_if.hpp @@ -30,8 +30,8 @@ namespace boost inline OutputIterator remove_copy_if(SinglePassRange& rng, OutputIterator out_it, Predicate pred) { - boost::function_requires< SinglePassRangeConcept >(); - return std::remove_copy_if(boost::begin(rng), boost::end(rng), out_it, pred); + BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept )); + return std::remove_copy_if(boost::begin(rng), boost::end(rng), out_it, pred); } } diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 2b2466f..645f5f9 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -91,12 +91,19 @@ test-suite range : [ range-test algorithm_test/prev_permutation ] [ range-test algorithm_test/random_shuffle ] [ range-test algorithm_test/remove ] + [ range-test algorithm_test/remove_copy ] + [ range-test algorithm_test/remove_copy_if ] [ range-test algorithm_test/remove_if ] [ range-test algorithm_test/replace ] + [ range-test algorithm_test/replace_copy ] + [ range-test algorithm_test/replace_copy_if ] [ range-test algorithm_test/replace_if ] [ range-test algorithm_test/reverse ] + [ range-test algorithm_test/reverse_copy ] [ range-test algorithm_test/rotate ] + [ range-test algorithm_test/rotate_copy ] [ range-test algorithm_test/search ] + [ range-test algorithm_test/search_n ] [ range-test algorithm_test/set_difference ] [ range-test algorithm_test/set_intersection ] [ range-test algorithm_test/set_symmetric_difference ] @@ -104,8 +111,10 @@ test-suite range : [ range-test algorithm_test/sort ] [ range-test algorithm_test/stable_partition ] [ range-test algorithm_test/stable_sort ] + [ range-test algorithm_test/swap_ranges ] [ range-test algorithm_test/transform ] [ range-test algorithm_test/unique ] + [ range-test algorithm_test/unique_copy ] [ range-test algorithm_test/upper_bound ] [ range-test algorithm_ext_test/copy_n ] [ range-test algorithm_ext_test/erase ] diff --git a/test/algorithm_test/copy_n.cpp b/test/algorithm_test/copy_n.cpp new file mode 100644 index 0000000..59bca9b --- /dev/null +++ b/test/algorithm_test/copy_n.cpp @@ -0,0 +1,63 @@ +// Boost.Range library +// +// 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 + +namespace +{ + template< class Container > + void test_copy_n_impl() + { + Container source; + typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t; + + std::vector target; + target.resize(source.size()); + + typedef BOOST_DEDUCED_TYPENAME range_iterator< std::vector >::type iterator_t; + iterator_t it = boost::copy(source, target.begin()); + + BOOST_CHECK( it == target.end() ); + + BOOST_CHECK_EQUAL_COLLECTIONS( + target.begin(), target.end(), + source.begin(), source.end() + ); + } + + void test_copy_n() + { + test_copy_n_impl< std::vector >(); + test_copy_n_impl< std::list >(); + test_copy_n_impl< std::set >(); + test_copy_n_impl< std::multiset >(); + } +} + +boost::unit_test::test_suite* +init_unit_test_suite(int argc, char* argv[]) +{ + boost::unit_test::test_suite* test + = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.copy_n" ); + + test->add( BOOST_TEST_CASE( &::test_copy_n ) ); + + return test; +} diff --git a/test/algorithm_test/for_each.cpp b/test/algorithm_test/for_each.cpp index ac517ad..37614ad 100644 --- a/test/algorithm_test/for_each.cpp +++ b/test/algorithm_test/for_each.cpp @@ -26,6 +26,12 @@ namespace boost { namespace { + template< class Range > + unsigned int udistance(Range& rng) + { + return static_cast(boost::distance(rng)); + } + template< class SinglePassRange > void test_for_each_impl( SinglePassRange rng ) { @@ -35,12 +41,12 @@ namespace boost // Test the mutable version fn_t result_fn = boost::for_each(rng, fn_t(rng)); - BOOST_CHECK_EQUAL( boost::distance(rng), result_fn.invocation_count() ); + BOOST_CHECK_EQUAL( boost::udistance(rng), result_fn.invocation_count() ); // Test the constant version const SinglePassRange& cref_rng = rng; result_fn = boost::for_each(cref_rng, fn_t(cref_rng)); - BOOST_CHECK_EQUAL( boost::distance(cref_rng), result_fn.invocation_count() ); + BOOST_CHECK_EQUAL( boost::udistance(cref_rng), result_fn.invocation_count() ); } template< class Container > @@ -51,7 +57,7 @@ namespace boost // Test empty Container cont; test_for_each_impl(cont); - + // Test one element cont += 0; test_for_each_impl(cont); @@ -63,7 +69,7 @@ namespace boost void test_for_each() { - boost::array a = { 0,1,2,3,4,5,6,7,8,9 }; + boost::array a = {{ 0,1,2,3,4,5,6,7,8,9 }}; test_for_each_impl(a); test_for_each_t< std::vector >(); @@ -79,8 +85,8 @@ init_unit_test_suite(int argc, char* argv[]) { boost::unit_test::test_suite* test = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.for_each" ); - + test->add( BOOST_TEST_CASE( &boost::test_for_each ) ); - + return test; } diff --git a/test/algorithm_test/remove_copy.cpp b/test/algorithm_test/remove_copy.cpp new file mode 100644 index 0000000..43d0ebe --- /dev/null +++ b/test/algorithm_test/remove_copy.cpp @@ -0,0 +1,98 @@ +// Boost.Range library +// +// 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 +{ + template + void test_append(Iterator target, Value value) + { + *target++ = value; + } + + template< class Container, class Value > + void test_remove_copy_impl( const Container& c, Value to_remove ) + { + typedef typename boost::range_value::type value_type; + std::vector reference; + + typedef BOOST_DEDUCED_TYPENAME std::vector::iterator iterator_t; + + test_append( + std::remove_copy(c.begin(), c.end(), + std::back_inserter(reference), to_remove), + to_remove); + + std::vector test; + test_append( + boost::remove_copy(c, std::back_inserter(test), to_remove), + to_remove); + + BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), + test.begin(), test.end() ); + } + + template< class Container > + void test_remove_copy_impl() + { + using namespace boost::assign; + + Container cont; + test_remove_copy_impl(cont, 0); + + cont.clear(); + cont += 1; + test_remove_copy_impl(cont, 0); + test_remove_copy_impl(cont, 1); + + cont.clear(); + cont += 1,1,1,1,1; + test_remove_copy_impl(cont, 0); + test_remove_copy_impl(cont, 1); + + cont.clear(); + cont += 1,2,3,4,5,6,7,8,9; + test_remove_copy_impl(cont, 1); + test_remove_copy_impl(cont, 9); + test_remove_copy_impl(cont, 4); + } + + void test_remove_copy() + { + test_remove_copy_impl< std::vector >(); + test_remove_copy_impl< std::list >(); + test_remove_copy_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.remove_copy" ); + + test->add( BOOST_TEST_CASE( &test_remove_copy ) ); + + return test; +} + diff --git a/test/algorithm_test/remove_copy_if.cpp b/test/algorithm_test/remove_copy_if.cpp new file mode 100644 index 0000000..0418ddd --- /dev/null +++ b/test/algorithm_test/remove_copy_if.cpp @@ -0,0 +1,103 @@ +// Boost.Range library +// +// 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 +{ + template< class Iterator, class Value > + void test_append(Iterator target, Value value) + { + *target++ = value; + } + + template< class Container, class UnaryPredicate > + void test_remove_copy_if_impl( const Container& c, UnaryPredicate pred ) + { + typedef BOOST_DEDUCED_TYPENAME boost::range_value::type value_type; + std::vector reference; + + test_append( + std::remove_copy_if(c.begin(), c.end(), std::back_inserter(reference), pred), + value_type() + ); + + std::vector test; + test_append( + boost::remove_copy_if(c, std::back_inserter(test), pred), + value_type() + ); + + BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), + test.begin(), test.end() ); + } + + template< class Container > + void test_remove_copy_if_( const Container& c, int to_remove ) + { + test_remove_copy_if_impl(c, boost::bind(std::equal_to(), _1, to_remove)); + test_remove_copy_if_impl(c, boost::bind(std::not_equal_to(), _1, to_remove)); + } + + template< class Container > + void test_remove_copy_if_impl() + { + using namespace boost::assign; + + Container cont; + test_remove_copy_if_(cont, 0); + + cont.clear(); + cont += 1; + test_remove_copy_if_(cont, 0); + test_remove_copy_if_(cont, 1); + + cont.clear(); + cont += 1,1,1,1,1; + test_remove_copy_if_(cont, 0); + test_remove_copy_if_(cont, 1); + + cont.clear(); + cont += 1,2,3,4,5,6,7,8,9; + test_remove_copy_if_(cont, 1); + test_remove_copy_if_(cont, 9); + test_remove_copy_if_(cont, 4); + } + + inline void test_remove_copy_if() + { + test_remove_copy_if_impl< std::vector >(); + test_remove_copy_if_impl< std::list >(); + test_remove_copy_if_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.remove_copy_if" ); + + test->add( BOOST_TEST_CASE( &test_remove_copy_if ) ); + + return test; +} diff --git a/test/algorithm_test/replace_copy.cpp b/test/algorithm_test/replace_copy.cpp new file mode 100644 index 0000000..a2ce612 --- /dev/null +++ b/test/algorithm_test/replace_copy.cpp @@ -0,0 +1,100 @@ +// Boost.Range library +// +// 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 +{ + template + void test_append(Iterator target, Value value) + { + *target++ = value; + } + + template< class Container, class Value > + void test_replace_copy_impl( const Container& c, Value to_replace ) + { + const Value replace_with = to_replace * 2; + + typedef typename boost::range_value::type value_type; + std::vector reference; + + typedef BOOST_DEDUCED_TYPENAME std::vector::iterator iterator_t; + + test_append( + std::replace_copy(c.begin(), c.end(), + std::back_inserter(reference), to_replace, replace_with), + to_replace); + + std::vector test; + test_append( + boost::replace_copy(c, std::back_inserter(test), to_replace, replace_with), + to_replace); + + BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), + test.begin(), test.end() ); + } + + template< class Container > + void test_replace_copy_impl() + { + using namespace boost::assign; + + Container cont; + test_replace_copy_impl(cont, 0); + + cont.clear(); + cont += 1; + test_replace_copy_impl(cont, 0); + test_replace_copy_impl(cont, 1); + + cont.clear(); + cont += 1,1,1,1,1; + test_replace_copy_impl(cont, 0); + test_replace_copy_impl(cont, 1); + + cont.clear(); + cont += 1,2,3,4,5,6,7,8,9; + test_replace_copy_impl(cont, 1); + test_replace_copy_impl(cont, 9); + test_replace_copy_impl(cont, 4); + } + + void test_replace_copy() + { + test_replace_copy_impl< std::vector >(); + test_replace_copy_impl< std::list >(); + test_replace_copy_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.replace_copy" ); + + test->add( BOOST_TEST_CASE( &test_replace_copy ) ); + + return test; +} + diff --git a/test/algorithm_test/replace_copy_if.cpp b/test/algorithm_test/replace_copy_if.cpp new file mode 100644 index 0000000..5a68fe4 --- /dev/null +++ b/test/algorithm_test/replace_copy_if.cpp @@ -0,0 +1,104 @@ +// Boost.Range library +// +// 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 +{ + template< class Iterator, class Value > + void test_append(Iterator target, Value value) + { + *target++ = value; + } + + template< class Container, class UnaryPredicate > + void test_replace_copy_if_impl( const Container& c, UnaryPredicate pred ) + { + typedef BOOST_DEDUCED_TYPENAME boost::range_value::type value_type; + const value_type replace_with = value_type(); + std::vector reference; + + test_append( + std::replace_copy_if(c.begin(), c.end(), std::back_inserter(reference), pred, replace_with), + value_type() + ); + + std::vector test; + test_append( + boost::replace_copy_if(c, std::back_inserter(test), pred, replace_with), + value_type() + ); + + BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), + test.begin(), test.end() ); + } + + template< class Container > + void test_replace_copy_if_( const Container& c, int to_replace ) + { + test_replace_copy_if_impl(c, boost::bind(std::equal_to(), _1, to_replace)); + test_replace_copy_if_impl(c, boost::bind(std::not_equal_to(), _1, to_replace)); + } + + template< class Container > + void test_replace_copy_if_impl() + { + using namespace boost::assign; + + Container cont; + test_replace_copy_if_(cont, 0); + + cont.clear(); + cont += 1; + test_replace_copy_if_(cont, 0); + test_replace_copy_if_(cont, 1); + + cont.clear(); + cont += 1,1,1,1,1; + test_replace_copy_if_(cont, 0); + test_replace_copy_if_(cont, 1); + + cont.clear(); + cont += 1,2,3,4,5,6,7,8,9; + test_replace_copy_if_(cont, 1); + test_replace_copy_if_(cont, 9); + test_replace_copy_if_(cont, 4); + } + + inline void test_replace_copy_if() + { + test_replace_copy_if_impl< std::vector >(); + test_replace_copy_if_impl< std::list >(); + test_replace_copy_if_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.replace_copy_if" ); + + test->add( BOOST_TEST_CASE( &test_replace_copy_if ) ); + + return test; +} diff --git a/test/algorithm_test/reverse_copy.cpp b/test/algorithm_test/reverse_copy.cpp new file mode 100644 index 0000000..8a7d931 --- /dev/null +++ b/test/algorithm_test/reverse_copy.cpp @@ -0,0 +1,88 @@ +// 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 +{ + template + void test_append(OutputIterator out, Value value) + { + *out++ = value; + } + + template + void test_reverse_copy_impl(Container& cont) + { + typedef BOOST_DEDUCED_TYPENAME boost::range_value::type value_type; + std::vector reference; + std::vector test; + + test_append( + std::reverse_copy(cont.begin(), cont.end(), std::back_inserter(reference)), + value_type() + ); + + test_append( + boost::reverse_copy(cont, std::back_inserter(test)), + value_type() + ); + + BOOST_CHECK_EQUAL_COLLECTIONS( + reference.begin(), reference.end(), + test.begin(), test.end() + ); + } + + template + void test_reverse_copy_impl() + { + using namespace boost::assign; + + Container cont; + test_reverse_copy_impl(cont); + + cont.clear(); + cont += 1; + test_reverse_copy_impl(cont); + + cont.clear(); + cont += 1,2,3,4,5,6,7,8,9; + test_reverse_copy_impl(cont); + } + + void test_reverse_copy() + { + test_reverse_copy_impl< std::vector >(); + test_reverse_copy_impl< std::list >(); + test_reverse_copy_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.reverse_copy" ); + + test->add( BOOST_TEST_CASE( &::test_reverse_copy ) ); + + return test; +} diff --git a/test/algorithm_test/rotate_copy.cpp b/test/algorithm_test/rotate_copy.cpp new file mode 100644 index 0000000..c403442 --- /dev/null +++ b/test/algorithm_test/rotate_copy.cpp @@ -0,0 +1,103 @@ +// 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 +{ + template + void test_append(OutputIterator target, Value value) + { + *target++ = value; + } + + template + void test_rotate_copy_impl(Container& cont, Iterator where_it) + { + typedef BOOST_DEDUCED_TYPENAME boost::range_value::type value_type; + std::vector reference; + std::vector test; + + typedef BOOST_DEDUCED_TYPENAME boost::range_iterator::type iterator_t; + + test_append( + std::rotate_copy(cont.begin(), where_it, cont.end(), + std::back_inserter(reference)), + value_type() + ); + + test_append( + boost::rotate_copy(cont, where_it, std::back_inserter(test)), + value_type() + ); + + BOOST_CHECK_EQUAL_COLLECTIONS( + reference.begin(), reference.end(), + test.begin(), test.end() + ); + } + + template + void test_rotate_copy_impl(Container& cont) + { + typedef BOOST_DEDUCED_TYPENAME boost::range_iterator::type iterator_t; + + iterator_t last = cont.end(); + for (iterator_t it = cont.begin(); it != last; ++it) + { + test_rotate_copy_impl(cont, it); + } + } + + template + void test_rotate_copy_impl() + { + using namespace boost::assign; + + Container cont; + test_rotate_copy_impl(cont); + + cont.clear(); + cont += 1; + test_rotate_copy_impl(cont); + + cont.clear(); + cont += 1,2,3,4,5,6,7,8,9; + test_rotate_copy_impl(cont); + } + + void test_rotate_copy() + { + test_rotate_copy_impl< std::vector >(); + test_rotate_copy_impl< std::list >(); + test_rotate_copy_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.rotate_copy" ); + + test->add( BOOST_TEST_CASE( &test_rotate_copy ) ); + + return test; +} diff --git a/test/algorithm_test/search_n.cpp b/test/algorithm_test/search_n.cpp new file mode 100644 index 0000000..d419f9c --- /dev/null +++ b/test/algorithm_test/search_n.cpp @@ -0,0 +1,95 @@ +// Boost.Range library +// +// 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 + +namespace +{ + template< class Container1, class Value > + void test_search_n_impl(Container1& cont1, Value value) + { + typedef BOOST_DEDUCED_TYPENAME Container1::const_iterator const_iterator1_t; + typedef BOOST_DEDUCED_TYPENAME Container1::iterator iterator1_t; + + const Container1& ccont1 = cont1; + + for (std::size_t n = 0; n < cont1.size(); ++n) + { + iterator1_t it = boost::search_n(cont1, n, value); + iterator1_t it2 = boost::search_n(cont1, n, value); + const_iterator1_t cit = boost::search_n(ccont1, n, value); + const_iterator1_t cit2 = boost::search_n(ccont1, n, value); + + BOOST_CHECK( it == std::search_n(cont1.begin(), cont1.end(), n, value) ); + BOOST_CHECK( it2 == std::search_n(cont1.begin(), cont1.end(), n, value) ); + BOOST_CHECK( cit == std::search_n(ccont1.begin(), ccont1.end(), n, value) ); + BOOST_CHECK( cit2 == std::search_n(ccont1.begin(), ccont1.end(), n, value) ); + } + } + + template< class Container1, class Container2 > + void test_search_n_impl() + { + using namespace boost::assign; + + Container1 cont1; + + test_search_n_impl(cont1, 1); + + cont1 += 1; + test_search_n_impl(cont1, 1); + test_search_n_impl(cont1, 0); + + cont1.clear(); + cont1 += 1,1; + test_search_n_impl(cont1, 1); + test_search_n_impl(cont1, 0); + + cont1 += 1,1,1; + test_search_n_impl(cont1, 1); + test_search_n_impl(cont1, 0); + + cont1.clear(); + cont1 += 1,2,3,4,5,6,7,8,9; + test_search_n_impl(cont1, 1); + test_search_n_impl(cont1, 2); + test_search_n_impl(cont1, 5); + test_search_n_impl(cont1, 9); + } + + void test_search_n() + { + test_search_n_impl< std::list, std::list >(); + test_search_n_impl< std::vector, std::vector >(); + test_search_n_impl< std::set, std::set >(); + test_search_n_impl< std::list, std::vector >(); + test_search_n_impl< std::vector, std::list >(); + } +} + +boost::unit_test::test_suite* +init_unit_test_suite(int argc, char* argv[]) +{ + boost::unit_test::test_suite* test + = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.search_n" ); + + test->add( BOOST_TEST_CASE( &test_search_n ) ); + + return test; +} diff --git a/test/algorithm_test/swap_ranges.cpp b/test/algorithm_test/swap_ranges.cpp new file mode 100644 index 0000000..b50431c --- /dev/null +++ b/test/algorithm_test/swap_ranges.cpp @@ -0,0 +1,95 @@ +// Boost.Range library +// +// 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 +{ + template + void test_swap_ranges_impl(const Container1& source1, const Container2& source2) + { + Container1 reference1(source1); + Container2 reference2(source2); + std::swap_ranges(reference1.begin(), reference1.end(), reference2.begin()); + + Container1 test1(source1); + Container2 test2(source2); + boost::swap_ranges(test1, test2); + + BOOST_CHECK_EQUAL_COLLECTIONS( + reference1.begin(), reference1.end(), + test1.begin(), test1.end() + ); + + BOOST_CHECK_EQUAL_COLLECTIONS( + reference2.begin(), reference2.end(), + test2.begin(), test2.end() + ); + } + + template + void test_swap_ranges_impl() + { + using namespace boost::assign; + + Container1 c1; + Container2 c2; + + test_swap_ranges_impl(c1, c2); + + c1.clear(); + c1 += 1; + c2.clear(); + c2 += 2; + test_swap_ranges_impl(c1, c2); + + c1.clear(); + c1 += 1,2,3,4,5,6,7,8,9,10; + c2.clear(); + c2 += 10,9,8,7,6,5,4,3,2,1; + test_swap_ranges_impl(c1, c2); + } + + inline void test_swap_ranges() + { + test_swap_ranges_impl< std::vector, std::vector >(); + test_swap_ranges_impl< std::vector, std::list >(); + test_swap_ranges_impl< std::vector, std::deque >(); + test_swap_ranges_impl< std::list, std::vector >(); + test_swap_ranges_impl< std::list, std::list >(); + test_swap_ranges_impl< std::list, std::deque >(); + test_swap_ranges_impl< std::deque, std::vector >(); + test_swap_ranges_impl< std::deque, std::list >(); + test_swap_ranges_impl< std::deque, 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.swap_ranges" ); + + test->add( BOOST_TEST_CASE( &test_swap_ranges ) ); + + return test; +} diff --git a/test/algorithm_test/unique_copy.cpp b/test/algorithm_test/unique_copy.cpp new file mode 100644 index 0000000..f3a3296 --- /dev/null +++ b/test/algorithm_test/unique_copy.cpp @@ -0,0 +1,136 @@ +// Boost.Range library +// +// 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 +{ + template + void test_append(OutputIterator target, Value value) + { + *target++ = value; + } + + template + void test_unique_copy_impl(Container& c) + { + typedef BOOST_DEDUCED_TYPENAME boost::range_value::type value_type; + std::vector reference; + std::vector test; + + test_append( + std::unique_copy(c.begin(), c.end(), std::back_inserter(reference)), + value_type() + ); + + test_append( + boost::unique_copy(c, std::back_inserter(test)), + value_type() + ); + + BOOST_CHECK_EQUAL_COLLECTIONS(reference.begin(), reference.end(), + test.begin(), test.end()); + } + + template + void test_unique_copy_impl(Container& c, Pred pred) + { + typedef BOOST_DEDUCED_TYPENAME boost::range_value::type value_type; + std::vector reference; + std::vector test; + + test_append( + std::unique_copy(c.begin(), c.end(), std::back_inserter(reference), pred), + value_type() + ); + + test_append( + boost::unique_copy(c, std::back_inserter(test), pred), + value_type() + ); + + BOOST_CHECK_EQUAL_COLLECTIONS(reference.begin(), reference.end(), + test.begin(), test.end()); + } + + template + void test_unique_copy_driver(Pred pred) + { + using namespace boost::assign; + + typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t; + + Container cont; + + test_unique_copy_impl(cont); + test_unique_copy_impl(cont, pred); + + cont.clear(); + cont += 1; + + std::vector temp(cont.begin(), cont.end()); + std::sort(temp.begin(), temp.end()); + cont.assign(temp.begin(), temp.end()); + test_unique_copy_impl(cont); + + std::sort(temp.begin(), temp.end(), pred); + cont.assign(temp.begin(), temp.end()); + test_unique_copy_impl(cont, pred); + + cont.clear(); + cont += 1,2,2,2,2,3,4,5,6,7,8,9; + + temp.assign(cont.begin(), cont.end()); + std::sort(temp.begin(), temp.end()); + cont.assign(temp.begin(), temp.end()); + test_unique_copy_impl(cont); + + std::sort(temp.begin(), temp.end(), pred); + cont.assign(temp.begin(), temp.end()); + test_unique_copy_impl(cont, pred); + } + + template + void test_unique_copy_impl() + { + test_unique_copy_driver(std::less()); + test_unique_copy_driver(std::greater()); + } + + void test_unique_copy() + { + test_unique_copy_impl< std::vector >(); + test_unique_copy_impl< std::list >(); + test_unique_copy_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.unique_copy" ); + + test->add( BOOST_TEST_CASE( &test_unique_copy ) ); + + return test; +} diff --git a/test/replace_copy.cpp b/test/replace_copy.cpp new file mode 100644 index 0000000..e69de29