diff options
Diffstat (limited to 'spec/ruby/core/integer/divide_spec.rb')
| -rw-r--r-- | spec/ruby/core/integer/divide_spec.rb | 73 |
1 files changed, 52 insertions, 21 deletions
diff --git a/spec/ruby/core/integer/divide_spec.rb b/spec/ruby/core/integer/divide_spec.rb index 2d49307628..0d5e16e986 100644 --- a/spec/ruby/core/integer/divide_spec.rb +++ b/spec/ruby/core/integer/divide_spec.rb @@ -2,13 +2,7 @@ require_relative '../../spec_helper' require_relative 'shared/arithmetic_coerce' describe "Integer#/" do - ruby_version_is "2.4"..."2.5" do - it_behaves_like :integer_arithmetic_coerce_rescue, :/ - end - - ruby_version_is "2.5" do - it_behaves_like :integer_arithmetic_coerce_not_rescue, :/ - end + it_behaves_like :integer_arithmetic_coerce_not_rescue, :/ context "fixnum" do it "returns self divided by the given argument" do @@ -18,6 +12,17 @@ describe "Integer#/" do it "supports dividing negative numbers" do (-1 / 10).should == -1 + (-1 / 10**10).should == -1 + (-1 / 10**20).should == -1 + end + + it "preservers sign correctly" do + (4 / 3).should == 1 + (4 / -3).should == -2 + (-4 / 3).should == -2 + (-4 / -3).should == 1 + (0 / -3).should == 0 + (0 / 3).should == 0 end it "returns result the same class as the argument" do @@ -27,7 +32,7 @@ describe "Integer#/" do end it "raises a ZeroDivisionError if the given argument is zero and not a Float" do - lambda { 1 / 0 }.should raise_error(ZeroDivisionError) + -> { 1 / 0 }.should raise_error(ZeroDivisionError) end it "does NOT raise ZeroDivisionError if the given argument is zero and is a Float" do @@ -41,9 +46,9 @@ describe "Integer#/" do end it "raises a TypeError when given a non-Integer" do - lambda { 13 / mock('10') }.should raise_error(TypeError) - lambda { 13 / "10" }.should raise_error(TypeError) - lambda { 13 / :symbol }.should raise_error(TypeError) + -> { 13 / mock('10') }.should raise_error(TypeError) + -> { 13 / "10" }.should raise_error(TypeError) + -> { 13 / :symbol }.should raise_error(TypeError) end end @@ -53,7 +58,7 @@ describe "Integer#/" do end it "returns self divided by other" do - (@bignum / 4).should == 2305843009213693974 + (@bignum / 4).should == 4611686018427387926 (@bignum / bignum_value(2)).should == 1 @@ -64,17 +69,26 @@ describe "Integer#/" do ((10**50) / -(10**40 + 1)).should == -10000000000 end + it "preservers sign correctly" do + (4 / bignum_value).should == 0 + (4 / -bignum_value).should == -1 + (-4 / bignum_value).should == -1 + (-4 / -bignum_value).should == 0 + (0 / bignum_value).should == 0 + (0 / -bignum_value).should == 0 + end + it "returns self divided by Float" do not_supported_on :opal do - (bignum_value(88) / 4294967295.0).should be_close(2147483648.5, TOLERANCE) + (bignum_value(88) / 4294967295.0).should be_close(4294967297.0, TOLERANCE) end - (bignum_value(88) / 4294967295.5).should be_close(2147483648.25, TOLERANCE) + (bignum_value(88) / 4294967295.5).should be_close(4294967296.5, TOLERANCE) end it "returns result the same class as the argument" do - (@bignum / 4).should == 2305843009213693974 - (@bignum / 4.0).should be_close(2305843009213693974, TOLERANCE) - (@bignum / Rational(4, 1)).should == Rational(2305843009213693974, 1) + (@bignum / 4).should == 4611686018427387926 + (@bignum / 4.0).should be_close(4611686018427387926, TOLERANCE) + (@bignum / Rational(4, 1)).should == Rational(4611686018427387926, 1) end it "does NOT raise ZeroDivisionError if other is zero and is a Float" do @@ -83,13 +97,30 @@ describe "Integer#/" do end it "raises a ZeroDivisionError if other is zero and not a Float" do - lambda { @bignum / 0 }.should raise_error(ZeroDivisionError) + -> { @bignum / 0 }.should raise_error(ZeroDivisionError) end it "raises a TypeError when given a non-numeric" do - lambda { @bignum / mock('10') }.should raise_error(TypeError) - lambda { @bignum / "2" }.should raise_error(TypeError) - lambda { @bignum / :symbol }.should raise_error(TypeError) + -> { @bignum / mock('10') }.should raise_error(TypeError) + -> { @bignum / "2" }.should raise_error(TypeError) + -> { @bignum / :symbol }.should raise_error(TypeError) end end + + it "coerces the RHS and calls #coerce" do + obj = mock("integer plus") + obj.should_receive(:coerce).with(6).and_return([6, 3]) + (6 / obj).should == 2 + end + + it "coerces the RHS and calls #coerce even if it's private" do + obj = Object.new + class << obj + private def coerce(n) + [n, 3] + end + end + + (6 / obj).should == 2 + end end |
