Skip to content

Commit

Permalink
{Numeric|Range}#step: Specs for float edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Sep 19, 2011
1 parent 932a420 commit 564e081
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
18 changes: 17 additions & 1 deletion core/numeric/step_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
end

describe "Numeric#step with [stop, +step] when self, stop or step is a Float" do
it "yields while increasing self by step while stop < stop" do
it "yields while increasing self by step while < stop" do
1.5.step(5, 1, &@prc)
ScratchPad.recorded.should == [1.5, 2.5, 3.5, 4.5]
end
Expand All @@ -150,6 +150,14 @@
2.5.step(1.5, 1, &@prc)
ScratchPad.recorded.should == []
end

ruby_bug "redmine #4576", "1.9.3" do
it "is careful about not yielding a value greater than limit" do
# As 9*1.3+1.0 == 12.700000000000001 > 12.7, we test:
1.0.step(12.7, 1.3, &@prc)
ScratchPad.recorded.should eql [1.0, 2.3, 3.6, 4.9, 6.2, 7.5, 8.8, 10.1, 11.4]
end
end
end

describe "Numeric#step with [stop, -step] when self, stop or step is a Float" do
Expand All @@ -167,6 +175,14 @@
1.step(5, -1.5, &@prc)
ScratchPad.recorded.should == []
end

ruby_bug "redmine #4576", "1.9.3" do
it "is careful about not yielding a value smaller than limit" do
# As -9*1.3-1.0 == -12.700000000000001 < -12.7, we test:
-1.0.step(-12.7, -1.3, &@prc)
ScratchPad.recorded.should eql [-1.0, -2.3, -3.6, -4.9, -6.2, -7.5, -8.8, -10.1, -11.4]
end
end
end

describe "Numeric#step with [stop, +Infinity]" do
Expand Down
5 changes: 5 additions & 0 deletions core/range/step_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
# Because 3 * 18.2 + 1.0 == 55.599999999999994, we have:
(1.0...55.6).step(18.2).to_a.should eql [1.0, 19.2, 37.4, 55.599999999999994]
end

it "returns float values of the form step * n + begin and never bigger than the end value if the range is inclusive" do
# As 9*1.3+1.0 == 12.700000000000001 > 12.7, we test:
(1.0..12.7).step(1.3).to_a.should eql [1.0, 2.3, 3.6, 4.9, 6.2, 7.5, 8.8, 10.1, 11.4]
end
end
end
end

0 comments on commit 564e081

Please sign in to comment.