diff options
Diffstat (limited to 'spec/ruby/core/integer')
-rw-r--r-- | spec/ruby/core/integer/ceil_spec.rb | 15 | ||||
-rw-r--r-- | spec/ruby/core/integer/digits_spec.rb | 34 | ||||
-rw-r--r-- | spec/ruby/core/integer/dup_spec.rb | 10 | ||||
-rw-r--r-- | spec/ruby/core/integer/floor_spec.rb | 15 | ||||
-rw-r--r-- | spec/ruby/core/integer/integer_spec.rb | 7 | ||||
-rw-r--r-- | spec/ruby/core/integer/round_spec.rb | 48 | ||||
-rw-r--r-- | spec/ruby/core/integer/shared/integer_rounding.rb | 31 | ||||
-rw-r--r-- | spec/ruby/core/integer/sqrt_spec.rb | 33 | ||||
-rw-r--r-- | spec/ruby/core/integer/truncate_spec.rb | 15 |
9 files changed, 193 insertions, 15 deletions
diff --git a/spec/ruby/core/integer/ceil_spec.rb b/spec/ruby/core/integer/ceil_spec.rb index 7a49ede0df..31c56f378d 100644 --- a/spec/ruby/core/integer/ceil_spec.rb +++ b/spec/ruby/core/integer/ceil_spec.rb @@ -1,6 +1,21 @@ 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#ceil" do it_behaves_like(:integer_to_i, :ceil) + it_behaves_like(:integer_rounding_positive_precision, :ceil) + + ruby_version_is "2.4" do + context "precision argument specified as part of the ceil method is negative" do + it "returns the smallest integer greater than self with at least precision.abs trailing zeros" do + 18.ceil(-1).should eql(20) + 18.ceil(-2).should eql(100) + 18.ceil(-3).should eql(1000) + -1832.ceil(-1).should eql(-1830) + -1832.ceil(-2).should eql(-1800) + -1832.ceil(-3).should eql(-1000) + end + end + end end diff --git a/spec/ruby/core/integer/digits_spec.rb b/spec/ruby/core/integer/digits_spec.rb new file mode 100644 index 0000000000..3546a654eb --- /dev/null +++ b/spec/ruby/core/integer/digits_spec.rb @@ -0,0 +1,34 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +ruby_version_is "2.4" do + describe "Integer#digits" do + it "returns an array of place values in base-10 by default" do + 12345.digits.should == [5,4,3,2,1] + end + + it "returns digits by place value of a given radix" do + 12345.digits(7).should == [4,6,6,0,5] + end + + it "converts the radix with #to_int" do + 12345.digits(mock_int(2)).should == [1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1] + end + + it "returns [0] when called on 0, regardless of base" do + 0.digits.should == [0] + 0.digits(7).should == [0] + end + + it "raises ArgumentError when calling with a radix less than 2" do + lambda { 12345.digits(1) }.should raise_error(ArgumentError) + end + + it "raises ArgumentError when calling with a negative radix" do + lambda { 12345.digits(-2) }.should raise_error(ArgumentError) + end + + it "raises Math::DomainError when calling digits on a negative number" do + lambda { -12345.digits(7) }.should raise_error(Math::DomainError) + end + end +end diff --git a/spec/ruby/core/integer/dup_spec.rb b/spec/ruby/core/integer/dup_spec.rb new file mode 100644 index 0000000000..f46bdf89bd --- /dev/null +++ b/spec/ruby/core/integer/dup_spec.rb @@ -0,0 +1,10 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +ruby_version_is '2.4' do + describe "Integer#dup" do + it "returns self" do + int = 2 + int.dup.should equal(int) + end + end +end diff --git a/spec/ruby/core/integer/floor_spec.rb b/spec/ruby/core/integer/floor_spec.rb index b533a84ad4..9babcd9a3e 100644 --- a/spec/ruby/core/integer/floor_spec.rb +++ b/spec/ruby/core/integer/floor_spec.rb @@ -1,6 +1,21 @@ 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#floor" do it_behaves_like(:integer_to_i, :floor) + it_behaves_like(:integer_rounding_positive_precision, :floor) + + ruby_version_is "2.4" do + context "precision argument specified as part of the floor method is negative" do + it "returns the largest integer less than self with at least precision.abs trailing zeros" do + 1832.floor(-1).should eql(1830) + 1832.floor(-2).should eql(1800) + 1832.floor(-3).should eql(1000) + -1832.floor(-1).should eql(-1840) + -1832.floor(-2).should eql(-1900) + -1832.floor(-3).should eql(-2000) + end + end + end end diff --git a/spec/ruby/core/integer/integer_spec.rb b/spec/ruby/core/integer/integer_spec.rb index a6f406cba0..393f072563 100644 --- a/spec/ruby/core/integer/integer_spec.rb +++ b/spec/ruby/core/integer/integer_spec.rb @@ -4,6 +4,13 @@ describe "Integer" do it "includes Comparable" do Integer.include?(Comparable).should == true end + + ruby_version_is "2.4" do + it "is the class of both small and large integers" do + 42.class.should equal(Integer) + bignum_value.class.should equal(Integer) + end + end end describe "Integer#integer?" do 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 diff --git a/spec/ruby/core/integer/shared/integer_rounding.rb b/spec/ruby/core/integer/shared/integer_rounding.rb new file mode 100644 index 0000000000..ecbda1bb4a --- /dev/null +++ b/spec/ruby/core/integer/shared/integer_rounding.rb @@ -0,0 +1,31 @@ +describe :integer_rounding_positive_precision, shared: true do + it "returns self if not passed a precision" do + [2, -4, 10**70, -10**100].each do |v| + v.send(@method).should eql(v) + end + end + + ruby_version_is "2.4" do + it "returns self if passed a precision of zero" do + [2, -4, 10**70, -10**100].each do |v| + v.send(@method, 0).should eql(v) + end + end + end + + ruby_version_is "2.4"..."2.5" do + it "returns itself as a float if passed a positive precision" do + [2, -4, 10**70, -10**100].each do |v| + v.send(@method, 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.send(@method, 42).should eql(v) + end + end + end +end diff --git a/spec/ruby/core/integer/sqrt_spec.rb b/spec/ruby/core/integer/sqrt_spec.rb new file mode 100644 index 0000000000..b7d9ef441b --- /dev/null +++ b/spec/ruby/core/integer/sqrt_spec.rb @@ -0,0 +1,33 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +ruby_version_is "2.5" do + describe "Integer.sqrt" do + it "returns an integer" do + Integer.sqrt(10).should be_kind_of(Integer) + end + + it "returns the integer square root of the argument" do + Integer.sqrt(0).should == 0 + Integer.sqrt(1).should == 1 + Integer.sqrt(24).should == 4 + Integer.sqrt(25).should == 5 + Integer.sqrt(10**400).should == 10**200 + end + + it "raises a Math::DomainError if the argument is negative" do + lambda { Integer.sqrt(-4) }.should raise_error(Math::DomainError) + end + + it "accepts any argument that can be coerced to Integer" do + Integer.sqrt(10.0).should == 3 + end + + it "converts the argument with #to_int" do + Integer.sqrt(mock_int(10)).should == 3 + end + + it "raises a TypeError if the argument cannot be coerced to Integer" do + lambda { Integer.sqrt("test") }.should raise_error(TypeError) + end + end +end diff --git a/spec/ruby/core/integer/truncate_spec.rb b/spec/ruby/core/integer/truncate_spec.rb index b503b1e4e8..429ab1a312 100644 --- a/spec/ruby/core/integer/truncate_spec.rb +++ b/spec/ruby/core/integer/truncate_spec.rb @@ -1,6 +1,21 @@ 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#truncate" do it_behaves_like(:integer_to_i, :truncate) + it_behaves_like(:integer_rounding_positive_precision, :truncate) + + ruby_version_is "2.4" do + context "precision argument specified as part of the truncate method is negative" do + it "returns an integer with at least precision.abs trailing zeros" do + 1832.truncate(-1).should eql(1830) + 1832.truncate(-2).should eql(1800) + 1832.truncate(-3).should eql(1000) + -1832.truncate(-1).should eql(-1830) + -1832.truncate(-2).should eql(-1800) + -1832.truncate(-3).should eql(-1000) + end + end + end end |