Boost.Range added unit tests for extended algorithms.

Put the extended algorithms into boost::range in a similar manner to the standard algorithms.
Added iota as an extended algorithm.
Fixed defects in the extended algorithms brought to light by the new unit tests.

[SVN r61042]
This commit is contained in:
Neil Groves
2010-04-04 14:14:12 +00:00
parent 6cc4cbf1a1
commit cee9ffc1a8
23 changed files with 993 additions and 125 deletions

View File

@ -0,0 +1,60 @@
// Boost.Range library
//
// Copyright Neil Groves 2010. 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 <boost/range/algorithm_ext/copy_n.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/iterator.hpp>
#include <algorithm>
#include <list>
#include <vector>
namespace
{
template< class Container >
void test_copy_n_impl()
{
std::vector<std::size_t> source;
for (std::size_t i = 0; i < 10; ++i)
source.push_back(i);
for (std::size_t k = 0; k < 10; ++k)
{
std::vector<std::size_t> reference;
for (std::size_t j = 0; j < k; ++j)
reference.push_back(j);
Container test;
boost::copy_n(source, k, std::back_inserter(test));
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
}
void test_copy_n()
{
test_copy_n_impl< std::vector<std::size_t> >();
test_copy_n_impl< std::list<std::size_t> >();
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.copy_n" );
test->add( BOOST_TEST_CASE( &test_copy_n ) );
return test;
}

View File

@ -0,0 +1,128 @@
// Boost.Range library
//
// Copyright Neil Groves 2010. 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 <boost/range/algorithm_ext/erase.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/iterator.hpp>
#include <algorithm>
#include <list>
#include <vector>
namespace
{
template< class Container >
void test_erase_impl()
{
Container source;
for (int i = 0; i < 10; ++i)
source.push_back(i);
Container reference(source);
Container test(source);
typedef BOOST_DEDUCED_TYPENAME Container::iterator iterator_t;
iterator_t first_ref = reference.begin();
iterator_t last_ref = reference.end();
boost::iterator_range< iterator_t > test_range(test.begin(), test.end());
reference.erase(first_ref, last_ref);
boost::erase(test, test_range);
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
void test_erase()
{
test_erase_impl<std::vector<int> >();
test_erase_impl<std::list<int> >();
}
template< class Container >
void test_remove_erase_impl()
{
Container source;
for (int i = 0; i < 10; ++i)
source.push_back(i);
Container reference(source);
Container test(source);
boost::remove_erase(test, 5);
reference.erase( std::find(reference.begin(), reference.end(), 5) );
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
void test_remove_erase()
{
test_remove_erase_impl<std::vector<int> >();
test_remove_erase_impl<std::list<int> >();
}
struct is_even
{
typedef bool result_type;
typedef int argument_type;
bool operator()(int x) const { return x % 2 == 0; }
};
template< class Container >
void test_remove_erase_if_impl()
{
Container source;
for (int i = 0; i < 10; ++i)
source.push_back(i);
Container reference;
typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iterator_t;
iterator_t last_source = source.end();
is_even pred;
for (iterator_t it_source = source.begin(); it_source != last_source; ++it_source)
{
if (!pred(*it_source))
reference.push_back(*it_source);
}
Container test(source);
boost::remove_erase_if(test, is_even());
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
void test_remove_erase_if()
{
test_remove_erase_if_impl<std::vector<int> >();
test_remove_erase_if_impl<std::list<int> >();
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.erase" );
test->add( BOOST_TEST_CASE( &test_erase ) );
test->add( BOOST_TEST_CASE( &test_remove_erase ) );
test->add( BOOST_TEST_CASE( &test_remove_erase_if ) );
return test;
}

View File

@ -0,0 +1,98 @@
// Boost.Range library
//
// Copyright Neil Groves 2010. 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 <boost/range/algorithm_ext/for_each.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/iterator.hpp>
#include <algorithm>
#include <list>
#include <vector>
namespace
{
struct MockBinaryFn
{
typedef void result_type;
typedef int first_argument_type;
typedef int second_argument_type;
void operator()(int x, int y)
{
xs.push_back(x);
ys.push_back(y);
}
std::vector<int> xs;
std::vector<int> ys;
};
template< class Range1, class Range2 >
void test_for_each_impl( Range1& rng1, Range2& rng2 )
{
MockBinaryFn fn = boost::range::for_each(rng1, rng2, MockBinaryFn());
BOOST_CHECK_EQUAL_COLLECTIONS( ::boost::begin(rng1), ::boost::end(rng1),
fn.xs.begin(), fn.xs.end() );
BOOST_CHECK_EQUAL_COLLECTIONS( ::boost::begin(rng2), ::boost::end(rng2),
fn.ys.begin(), fn.ys.end() );
}
template< class Collection1, class Collection2 >
void test_for_each_impl(const int max_count)
{
Collection1 c1;
for (int i = 0; i < max_count; ++i)
c1.push_back(i);
Collection2 c2;
for (int i = 0; i < max_count; ++i)
c2.push_back(i);
test_for_each_impl(c1, c2);
const Collection1& const_c1 = c1;
const Collection2& const_c2 = c2;
test_for_each_impl(c1, const_c2);
test_for_each_impl(const_c1, c2);
test_for_each_impl(const_c1, const_c2);
}
template< class Collection1, class Collection2 >
void test_for_each_impl()
{
test_for_each_impl< Collection1, Collection2 >(0);
test_for_each_impl< Collection1, Collection2 >(1);
test_for_each_impl< Collection1, Collection2 >(10);
}
void test_for_each()
{
test_for_each_impl< std::vector<int>, std::vector<int> >();
test_for_each_impl< std::list<int>, std::list<int> >();
test_for_each_impl< std::vector<int>, std::list<int> >();
test_for_each_impl< std::list<int>, std::vector<int> >();
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.for_each" );
test->add( BOOST_TEST_CASE( &test_for_each ) );
return test;
}

View File

@ -0,0 +1,72 @@
// Boost.Range library
//
// Copyright Neil Groves 2010. 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 <boost/range/algorithm_ext/insert.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/iterator.hpp>
#include <boost/range/irange.hpp>
#include <algorithm>
#include <list>
#include <vector>
namespace
{
template< class Container >
void test_insert_impl( int n )
{
Container test;
boost::insert( test, test.end(), boost::irange(0, n) );
Container reference;
for (int i = 0; i < n; ++i)
reference.push_back(i);
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
// Do it again so that we are inserting into a non-empty target
boost::insert( test, test.end(), boost::irange(0, n) );
for (int j = 0; j < n; ++j)
reference.push_back(j);
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
template< class Container >
void test_insert_impl()
{
test_insert_impl< Container >(0);
test_insert_impl< Container >(1);
test_insert_impl< Container >(2);
test_insert_impl< Container >(100);
}
void test_insert()
{
test_insert_impl< std::vector<std::size_t> >();
test_insert_impl< std::list<std::size_t> >();
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.insert" );
test->add( BOOST_TEST_CASE( &test_insert ) );
return test;
}

View File

@ -0,0 +1,70 @@
// Boost.Range library
//
// Copyright Neil Groves 2010. 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 <boost/range/algorithm_ext/iota.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/iterator.hpp>
#include <algorithm>
#include <list>
#include <vector>
namespace
{
template< class Container >
void test_iota_impl(std::size_t n)
{
Container test;
test.resize( n );
boost::iota( test, n );
Container reference;
reference.resize( n );
std::size_t i = n;
typedef BOOST_DEDUCED_TYPENAME Container::iterator iterator_t;
iterator_t last = reference.end();
for (iterator_t it = reference.begin(); it != last; ++it, ++i)
{
*it = i;
}
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
template< class Container >
void test_iota_impl()
{
test_iota_impl< Container >(0);
test_iota_impl< Container >(1);
test_iota_impl< Container >(2);
test_iota_impl< Container >(100);
}
void test_iota()
{
test_iota_impl< std::vector<std::size_t> >();
test_iota_impl< std::list<std::size_t> >();
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.iota" );
test->add( BOOST_TEST_CASE( &test_iota ) );
return test;
}

View File

@ -0,0 +1,62 @@
// Boost.Range library
//
// Copyright Neil Groves 2010. 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 <boost/range/algorithm_ext/is_sorted.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/iterator.hpp>
#include <algorithm>
#include <list>
#include <vector>
namespace
{
template< class Container >
void test_is_sorted_impl()
{
Container ascending;
Container descending;
// Empty ranges are regarded as sorted against any predicate.
BOOST_CHECK( boost::is_sorted(ascending) );
BOOST_CHECK( boost::is_sorted(ascending, std::less<std::size_t>()) );
BOOST_CHECK( boost::is_sorted(ascending, std::greater<std::size_t>()) );
for (std::size_t i = 0; i < 10; ++i)
{
ascending.push_back(i);
descending.push_back(9 - i);
}
BOOST_CHECK( boost::is_sorted(ascending) );
BOOST_CHECK( !boost::is_sorted(descending) );
BOOST_CHECK( !boost::is_sorted(ascending, std::greater<std::size_t>()) );
BOOST_CHECK( boost::is_sorted(descending, std::greater<std::size_t>()) );
}
void test_is_sorted()
{
test_is_sorted_impl< std::vector<std::size_t> >();
test_is_sorted_impl< std::list<std::size_t> >();
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.is_sorted" );
test->add( BOOST_TEST_CASE( &test_is_sorted ) );
return test;
}

View File

@ -0,0 +1,74 @@
// Boost.Range library
//
// Copyright Neil Groves 2010. 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 <boost/range/algorithm_ext/overwrite.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/iterator.hpp>
#include <algorithm>
#include <list>
#include <vector>
namespace
{
template< class Container >
void test_overwrite_impl(std::size_t n)
{
Container overwrite_source;
for (std::size_t i = 0; i < n; ++i)
overwrite_source.push_back(i);
Container reference;
reference.resize(n);
std::copy(overwrite_source.begin(), overwrite_source.end(), reference.begin());
Container test;
test.resize(n);
boost::overwrite(overwrite_source, test);
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
test.clear();
test.resize(n);
const Container& const_overwrite_source = overwrite_source;
boost::overwrite(const_overwrite_source, test);
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
template< class Container >
void test_overwrite_impl()
{
test_overwrite_impl<Container>(0);
test_overwrite_impl<Container>(1);
test_overwrite_impl<Container>(10);
}
void test_overwrite()
{
test_overwrite_impl< std::vector<std::size_t> >();
test_overwrite_impl< std::list<std::size_t> >();
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.overwrite" );
test->add( BOOST_TEST_CASE( &test_overwrite ) );
return test;
}

View File

@ -0,0 +1,72 @@
// Boost.Range library
//
// Copyright Neil Groves 2010. 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 <boost/range/algorithm_ext/push_back.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/iterator.hpp>
#include <boost/range/irange.hpp>
#include <algorithm>
#include <list>
#include <vector>
namespace
{
template< class Container >
void test_push_back_impl(std::size_t n)
{
Container reference;
for (std::size_t i = 0; i < n; ++i)
reference.push_back(i);
Container test;
boost::push_back(test, boost::irange<std::size_t>(0, n));
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
// Do it again to push onto non-empty container
for (std::size_t j = 0; j < n; ++j)
reference.push_back(j);
boost::push_back(test, boost::irange<std::size_t>(0, n));
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
template< class Container >
void test_push_back_impl()
{
test_push_back_impl< Container >(0);
test_push_back_impl< Container >(1);
test_push_back_impl< Container >(2);
test_push_back_impl< Container >(100);
}
void test_push_back()
{
test_push_back_impl< std::vector<std::size_t> >();
test_push_back_impl< std::list<std::size_t> >();
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.push_back" );
test->add( BOOST_TEST_CASE( &test_push_back ) );
return test;
}

View File

@ -0,0 +1,71 @@
// Boost.Range library
//
// Copyright Neil Groves 2010. 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 <boost/range/algorithm_ext/push_front.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/iterator.hpp>
#include <boost/range/irange.hpp>
#include <algorithm>
#include <list>
#include <vector>
namespace
{
template< class Container >
void test_push_front_impl(std::size_t n)
{
Container reference;
for (std::size_t i = 0; i < n; ++i)
reference.push_back(i);
Container test;
boost::push_front(test, boost::irange<std::size_t>(0, n));
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
// Do it again to push onto non-empty container
reference.insert(reference.begin(), reference.begin(), reference.end());
boost::push_front(test, boost::irange<std::size_t>(0, n));
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
template< class Container >
void test_push_front_impl()
{
test_push_front_impl< Container >(0);
test_push_front_impl< Container >(1);
test_push_front_impl< Container >(2);
test_push_front_impl< Container >(100);
}
void test_push_front()
{
test_push_front_impl< std::vector<std::size_t> >();
test_push_front_impl< std::list<std::size_t> >();
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.push_front" );
test->add( BOOST_TEST_CASE( &test_push_front ) );
return test;
}