Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r72070 - in trunk: boost/range libs/range/test
From: neil_at_[hidden]
Date: 2011-05-22 07:16:54


Author: neilgroves
Date: 2011-05-22 07:16:53 EDT (Sun, 22 May 2011)
New Revision: 72070
URL: http://svn.boost.org/trac/boost/changeset/72070

Log:
[boost][range] - ticket 5544 - fix for termination of irange.
Added:
   trunk/libs/range/test/ticket_5544_terminate_irange.cpp (contents, props changed)
Text files modified:
   trunk/boost/range/irange.hpp | 7 ++++---
   trunk/libs/range/test/Jamfile.v2 | 1 +
   trunk/libs/range/test/irange.cpp | 9 ++++++++-
   3 files changed, 13 insertions(+), 4 deletions(-)

Modified: trunk/boost/range/irange.hpp
==============================================================================
--- trunk/boost/range/irange.hpp (original)
+++ trunk/boost/range/irange.hpp 2011-05-22 07:16:53 EDT (Sun, 22 May 2011)
@@ -216,9 +216,10 @@
 
         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),

Modified: trunk/libs/range/test/Jamfile.v2
==============================================================================
--- trunk/libs/range/test/Jamfile.v2 (original)
+++ trunk/libs/range/test/Jamfile.v2 2011-05-22 07:16:53 EDT (Sun, 22 May 2011)
@@ -156,5 +156,6 @@
         [ range-test std_container ]
         [ range-test string ]
         [ range-test sub_range ]
+ [ range-test ticket_5544_terminate_irange ]
     ;
 

Modified: trunk/libs/range/test/irange.cpp
==============================================================================
--- trunk/libs/range/test/irange.cpp (original)
+++ trunk/libs/range/test/irange.cpp 2011-05-22 07:16:53 EDT (Sun, 22 May 2011)
@@ -81,7 +81,6 @@
     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 @@
         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
 

Added: trunk/libs/range/test/ticket_5544_terminate_irange.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/range/test/ticket_5544_terminate_irange.cpp 2011-05-22 07:16:53 EDT (Sun, 22 May 2011)
@@ -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;
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk