aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/core/integer/round_spec.rb
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-20 20:18:52 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-20 20:18:52 +0000
commit1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch)
treea3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/core/integer/round_spec.rb
parent75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff)
downloadruby-1d15d5f08032acf1b7bceacbb450d617ff6e0931.tar.gz
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory. [Misc #13792] [ruby-core:82287] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/core/integer/round_spec.rb')
-rw-r--r--spec/ruby/core/integer/round_spec.rb77
1 files changed, 77 insertions, 0 deletions
diff --git a/spec/ruby/core/integer/round_spec.rb b/spec/ruby/core/integer/round_spec.rb
new file mode 100644
index 0000000000..5cc9aa3881
--- /dev/null
+++ b/spec/ruby/core/integer/round_spec.rb
@@ -0,0 +1,77 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/to_i', __FILE__)
+
+describe "Integer#round" do
+ it_behaves_like(:integer_to_i, :round)
+
+ ruby_version_is ""..."2.5" do
+ 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)
+ end
+ 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)
+ -249.round(-2).should eql(-200)
+ (+25 * 10**70 - 1).round(-71).should eql(+20 * 10**70)
+ (-25 * 10**70 + 1).round(-71).should eql(-20 * 10**70)
+ end
+
+ it "returns itself rounded to nearest if passed a negative value" do
+ +250.round(-2).should eql(+300)
+ -250.round(-2).should eql(-300)
+ (+25 * 10**70).round(-71).should eql(+30 * 10**70)
+ (-25 * 10**70).round(-71).should eql(-30 * 10**70)
+ end
+
+ platform_is_not wordsize: 32 do
+ it "raises a RangeError when passed a big negative value" do
+ lambda { 42.round(fixnum_min) }.should raise_error(RangeError)
+ end
+ end
+
+ it "raises a RangeError when passed Float::INFINITY" do
+ lambda { 42.round(Float::INFINITY) }.should raise_error(RangeError)
+ end
+
+ it "raises a RangeError when passed a beyond signed int" do
+ lambda { 42.round(1<<31) }.should raise_error(RangeError)
+ end
+
+ it "raises a TypeError when passed a String" do
+ lambda { 42.round("4") }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError when its argument cannot be converted to an Integer" do
+ lambda { 42.round(nil) }.should raise_error(TypeError)
+ end
+
+ it "calls #to_int on the argument to convert it to an Integer" do
+ obj = mock("Object")
+ obj.should_receive(:to_int).and_return(0)
+ 42.round(obj)
+ end
+
+ it "raises a TypeError when #to_int does not return an Integer" do
+ obj = mock("Object")
+ obj.stub!(:to_int).and_return([])
+ lambda { 42.round(obj) }.should raise_error(TypeError)
+ end
+end