From f516379853f36d143d820c55d5eeaa9fc410ef52 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Thu, 29 Apr 2021 12:51:05 -0700 Subject: Fix Enumerator::ArithmeticSequence handling of float ranges Depending on the float range, there could be an off-by-one error, where the last result that should be in the range was missed. Fix this by checking if the computed value for the expected value outside the range is still inside the range, and if so, increment the step size. Fixes [Bug #16612] --- spec/ruby/core/range/step_spec.rb | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'spec/ruby/core/range/step_spec.rb') diff --git a/spec/ruby/core/range/step_spec.rb b/spec/ruby/core/range/step_spec.rb index e284551ab3..1c2e30742c 100644 --- a/spec/ruby/core/range/step_spec.rb +++ b/spec/ruby/core/range/step_spec.rb @@ -207,10 +207,12 @@ describe "Range#step" do ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5]) end - it "returns Float values of 'step * n + begin < end'" do - (1.0...6.4).step(1.8) { |x| ScratchPad << x } - (1.0...55.6).step(18.2) { |x| ScratchPad << x } - ScratchPad.recorded.should eql([1.0, 2.8, 4.6, 1.0, 19.2, 37.4]) + ruby_version_is '3.1' do + it "returns Float values of 'step * n + begin < end'" do + (1.0...6.4).step(1.8) { |x| ScratchPad << x } + (1.0...55.6).step(18.2) { |x| ScratchPad << x } + ScratchPad.recorded.should eql([1.0, 2.8, 4.6, 1.0, 19.2, 37.4, 55.599999999999994]) + end end it "handles infinite values at either end" do @@ -457,19 +459,21 @@ describe "Range#step" do (-1.0...1.0).step(0.5).size.should == 4 end - it "returns the range size when there's no step_size" do - (-2..2).step.size.should == 5 - (-2.0..2.0).step.size.should == 5 - (-2..2.0).step.size.should == 5 - (-2.0..2).step.size.should == 5 - (1.0..6.4).step(1.8).size.should == 4 - (1.0..12.7).step(1.3).size.should == 10 - (-2...2).step.size.should == 4 - (-2.0...2.0).step.size.should == 4 - (-2...2.0).step.size.should == 4 - (-2.0...2).step.size.should == 4 - (1.0...6.4).step(1.8).size.should == 3 - (1.0...55.6).step(18.2).size.should == 3 + ruby_version_is '3.1' do + it "returns the range size when there's no step_size" do + (-2..2).step.size.should == 5 + (-2.0..2.0).step.size.should == 5 + (-2..2.0).step.size.should == 5 + (-2.0..2).step.size.should == 5 + (1.0..6.4).step(1.8).size.should == 4 + (1.0..12.7).step(1.3).size.should == 10 + (-2...2).step.size.should == 4 + (-2.0...2.0).step.size.should == 4 + (-2...2.0).step.size.should == 4 + (-2.0...2).step.size.should == 4 + (1.0...6.4).step(1.8).size.should == 3 + (1.0...55.6).step(18.2).size.should == 4 + end end it "returns nil with begin and end are String" do -- cgit v1.2.3