diff options
Diffstat (limited to 'spec/ruby/core/integer/round_spec.rb')
-rw-r--r-- | spec/ruby/core/integer/round_spec.rb | 48 |
1 files changed, 33 insertions, 15 deletions
diff --git a/spec/ruby/core/integer/round_spec.rb b/spec/ruby/core/integer/round_spec.rb index 5cc9aa3881..5a46e6cba6 100644 --- a/spec/ruby/core/integer/round_spec.rb +++ b/spec/ruby/core/integer/round_spec.rb @@ -1,10 +1,12 @@ require File.expand_path('../../../spec_helper', __FILE__) require File.expand_path('../shared/to_i', __FILE__) +require File.expand_path('../shared/integer_rounding', __FILE__) describe "Integer#round" do it_behaves_like(:integer_to_i, :round) + it_behaves_like(:integer_rounding_positive_precision, :round) - ruby_version_is ""..."2.5" do + ruby_version_is ""..."2.5" do # Not just since 2.4 it "rounds itself as a float if passed a positive precision" do [2, -4, 10**70, -10**100].each do |v| v.round(42).should eql(v.to_f) @@ -12,20 +14,6 @@ describe "Integer#round" do end end - ruby_version_is "2.5" do - it "returns itself if passed a positive precision" do - [2, -4, 10**70, -10**100].each do |v| - v.round(42).should eql(v) - end - end - end - - it "returns itself if passed zero" do - [2, -4, 10**70, -10**100].each do |v| - v.round(0).should eql(v) - end - end - # redmine:5228 it "returns itself rounded if passed a negative value" do +249.round(-2).should eql(+200) @@ -74,4 +62,34 @@ describe "Integer#round" do obj.stub!(:to_int).and_return([]) lambda { 42.round(obj) }.should raise_error(TypeError) end + + ruby_version_is "2.4" do + it "returns different rounded values depending on the half option" do + 25.round(-1, half: :up).should eql(30) + 25.round(-1, half: :down).should eql(20) + 25.round(-1, half: :even).should eql(20) + 35.round(-1, half: :up).should eql(40) + 35.round(-1, half: :down).should eql(30) + 35.round(-1, half: :even).should eql(40) + (-25).round(-1, half: :up).should eql(-30) + (-25).round(-1, half: :down).should eql(-20) + (-25).round(-1, half: :even).should eql(-20) + end + end + + ruby_version_is "2.4"..."2.5" do + it "returns itself as a float if passed a positive precision and the half option" do + 35.round(1, half: :up).should eql(35.0) + 35.round(1, half: :down).should eql(35.0) + 35.round(1, half: :even).should eql(35.0) + end + end + + ruby_version_is "2.5" do + it "returns itself if passed a positive precision and the half option" do + 35.round(1, half: :up).should eql(35) + 35.round(1, half: :down).should eql(35) + 35.round(1, half: :even).should eql(35) + end + end end |