From df1a3a334f1dffe00411e9eeed5181794de9d583 Mon Sep 17 00:00:00 2001 From: Neil Groves Date: Sun, 22 May 2011 11:16:53 +0000 Subject: [PATCH] [boost][range] - ticket 5544 - fix for termination of irange. [SVN r72070] --- include/boost/range/irange.hpp | 7 ++-- test/Jamfile.v2 | 1 + test/irange.cpp | 9 ++++- test/ticket_5544_terminate_irange.cpp | 47 +++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 test/ticket_5544_terminate_irange.cpp diff --git a/include/boost/range/irange.hpp b/include/boost/range/irange.hpp index 8045550..fe36e9d 100644 --- a/include/boost/range/irange.hpp +++ b/include/boost/range/irange.hpp @@ -216,9 +216,10 @@ namespace boost typedef typename range_detail::integer_iterator_with_step iterator_t; - const std::ptrdiff_t last_step - = (static_cast(last) - static_cast(first)) - / (static_cast(step_size)); + const std::ptrdiff_t l = static_cast(last); + const std::ptrdiff_t f = static_cast(first); + const std::ptrdiff_t sz = static_cast(step_size); + const std::ptrdiff_t last_step = (l + ((l-f) % sz) - f) / sz; return strided_integer_range( iterator_t(first, 0, step_size), diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 7086fd0..f1fb3aa 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -156,5 +156,6 @@ test-suite range : [ range-test std_container ] [ range-test string ] [ range-test sub_range ] + [ range-test ticket_5544_terminate_irange ] ; diff --git a/test/irange.cpp b/test/irange.cpp index d71f184..985e07a 100644 --- a/test/irange.cpp +++ b/test/irange.cpp @@ -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(first, last, step_size); test_irange_impl(first, last, step_size); test_irange_impl(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 diff --git a/test/ticket_5544_terminate_irange.cpp b/test/ticket_5544_terminate_irange.cpp new file mode 100644 index 0000000..fa64436 --- /dev/null +++ b/test/ticket_5544_terminate_irange.cpp @@ -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 +#include + +#include +#include + +#include + +namespace boost +{ + namespace + { + void test_irange_termination() + { + std::vector reference; + for (int i = 0; i < 9; i += 2) + reference.push_back(i); + + std::vector 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; +}