diff options
author | Jeremy Evans <code@jeremyevans.net> | 2021-04-29 12:51:05 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2021-05-29 08:56:15 -0700 |
commit | f516379853f36d143d820c55d5eeaa9fc410ef52 (patch) | |
tree | e5a9e14896dee5767d0d33834613bd56c590ce66 /spec/ruby/core | |
parent | e56ba6231f77dd0aa88a1ce737a342baafc884c7 (diff) | |
download | ruby-f516379853f36d143d820c55d5eeaa9fc410ef52.tar.gz |
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]
Diffstat (limited to 'spec/ruby/core')
-rw-r--r-- | spec/ruby/core/range/step_spec.rb | 38 |
1 files changed, 21 insertions, 17 deletions
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 |