[前][次][番号順一覧][スレッド一覧]

ruby-changes:54699

From: mrkn <ko1@a...>
Date: Thu, 24 Jan 2019 14:30:57 +0900 (JST)
Subject: [ruby-changes:54699] mrkn:r66914 (trunk): numeric.c: Fix negative step with float components

mrkn	2019-01-24 14:30:42 +0900 (Thu, 24 Jan 2019)

  New Revision: 66914

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66914

  Log:
    numeric.c: Fix negative step with float components
    
    * numeric.c (ruby_float_step): fix negative step with float components.
    
    * test/ruby/test_numeric.c (test_step_bug15537): add tests.
    
    * test/ruby/test_range.c (test_step_bug15537): add tests.
    
    [Bug #15537] [ruby-core:91101]
    
    From: shuujii (Shuji KOBAYASHI) <shuujii@g...>

  Modified files:
    trunk/numeric.c
    trunk/test/ruby/test_numeric.rb
    trunk/test/ruby/test_range.rb
Index: numeric.c
===================================================================
--- numeric.c	(revision 66913)
+++ numeric.c	(revision 66914)
@@ -2514,9 +2514,9 @@ int https://github.com/ruby/ruby/blob/trunk/numeric.c#L2514
 ruby_float_step(VALUE from, VALUE to, VALUE step, int excl, int allow_endless)
 {
     if (RB_TYPE_P(from, T_FLOAT) || RB_TYPE_P(to, T_FLOAT) || RB_TYPE_P(step, T_FLOAT)) {
-	double beg = NUM2DBL(from);
-	double end = (allow_endless && NIL_P(to)) ? HUGE_VAL : NUM2DBL(to);
 	double unit = NUM2DBL(step);
+	double beg = NUM2DBL(from);
+	double end = (allow_endless && NIL_P(to)) ? (unit < 0 ? -1 : 1)*HUGE_VAL : NUM2DBL(to);
 	double n = ruby_float_step_size(beg, end, unit, excl);
 	long i;
 
Index: test/ruby/test_range.rb
===================================================================
--- test/ruby/test_range.rb	(revision 66913)
+++ test/ruby/test_range.rb	(revision 66914)
@@ -322,6 +322,11 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L322
     assert_equal(["a", "b", "c"], a)
   end
 
+  def test_step_bug15537
+    assert_equal([10.0, 9.0, 8.0, 7.0], (10 ..).step(-1.0).take(4))
+    assert_equal([10.0, 9.0, 8.0, 7.0], (10.0 ..).step(-1).take(4))
+  end
+
   def test_percent_step
     aseq = (1..10) % 2
     assert_equal(Enumerator::ArithmeticSequence, aseq.class)
Index: test/ruby/test_numeric.rb
===================================================================
--- test/ruby/test_numeric.rb	(revision 66913)
+++ test/ruby/test_numeric.rb	(revision 66914)
@@ -339,6 +339,20 @@ class TestNumeric < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_numeric.rb#L339
     assert_step [bignum]*4, [bignum, by: 0, to: 0], inf: true
   end
 
+  def test_step_bug15537
+    assert_step [10.0, 8.0, 6.0, 4.0, 2.0], [10.0, 1, -2]
+    assert_step [10.0, 8.0, 6.0, 4.0, 2.0], [10.0, to: 1, by: -2]
+    assert_step [10.0, 8.0, 6.0, 4.0, 2.0], [10.0, 1, -2]
+    assert_step [10.0, 8.0, 6.0, 4.0, 2.0], [10, to: 1.0, by: -2]
+    assert_step [10.0, 8.0, 6.0, 4.0, 2.0], [10, 1.0, -2]
+
+    assert_step [10.0, 9.0, 8.0, 7.0], [10, by: -1.0], inf: true
+    assert_step [10.0, 9.0, 8.0, 7.0], [10, by: -1.0, to: nil], inf: true
+    assert_step [10.0, 9.0, 8.0, 7.0], [10, nil, -1.0], inf: true
+    assert_step [10.0, 9.0, 8.0, 7.0], [10.0, by: -1], inf: true
+    assert_step [10.0, 9.0, 8.0, 7.0], [10.0, nil, -1], inf: true
+  end
+
   def test_num2long
     assert_raise(TypeError) { 1 & nil }
     assert_raise(TypeError) { 1 & 1.0 }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]