[boost][range] - ticket 5544 - fix for termination of irange.

[SVN r72070]
This commit is contained in:
Neil Groves
2011-05-22 11:16:53 +00:00
parent 126e6861d7
commit df1a3a334f
4 changed files with 60 additions and 4 deletions

View File

@ -216,9 +216,10 @@ namespace boost
typedef typename range_detail::integer_iterator_with_step<Integer> iterator_t;
const std::ptrdiff_t last_step
= (static_cast<std::ptrdiff_t>(last) - static_cast<std::ptrdiff_t>(first))
/ (static_cast<std::ptrdiff_t>(step_size));
const std::ptrdiff_t l = static_cast<std::ptrdiff_t>(last);
const std::ptrdiff_t f = static_cast<std::ptrdiff_t>(first);
const std::ptrdiff_t sz = static_cast<std::ptrdiff_t>(step_size);
const std::ptrdiff_t last_step = (l + ((l-f) % sz) - f) / sz;
return strided_integer_range<Integer>(
iterator_t(first, 0, step_size),

View File

@ -156,5 +156,6 @@ test-suite range :
[ range-test std_container ]
[ range-test string ]
[ range-test sub_range ]
[ range-test ticket_5544_terminate_irange ]
;

View File

@ -81,7 +81,6 @@ namespace boost
void test_irange(int first, int last, int step_size)
{
BOOST_ASSERT( step_size != 0 );
BOOST_ASSERT( (last - first) % step_size == 0 );
test_irange_impl<signed char>(first, last, step_size);
test_irange_impl<unsigned char>(first, last, step_size);
test_irange_impl<signed short>(first, last, step_size);
@ -114,8 +113,16 @@ namespace boost
test_irange(10, 0, -1);
test_irange(0, 2, 2);
test_irange(2, 0, -2);
test_irange(0, 9, 2);
test_irange(10, 20, 5);
test_irange(20, 10, -5);
test_irange(0, 0, 3);
test_irange(0, 1, 3);
test_irange(0, 2, 3);
test_irange(0, 3, 3);
test_irange(0, 4, 3);
test_irange(0, 10, 3);
}
} // namespace boost

View File

@ -0,0 +1,47 @@
// Boost.Range library
//
// Copyright Neil Groves 2011. 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/irange.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <vector>
namespace boost
{
namespace
{
void test_irange_termination()
{
std::vector<int> reference;
for (int i = 0; i < 9; i += 2)
reference.push_back(i);
std::vector<int> actual;
boost::push_back(actual, boost::irange(0,9,2));
BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(),
reference.begin(), reference.end());
}
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.ticket_5544" );
test->add( BOOST_TEST_CASE( &boost::test_irange_termination ) );
return test;
}