diff options
Diffstat (limited to 'spec/ruby/core/integer')
-rw-r--r-- | spec/ruby/core/integer/bit_and_spec.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/integer/bit_or_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/integer/bit_xor_spec.rb | 4 | ||||
-rw-r--r-- | spec/ruby/core/integer/ceildiv_spec.rb | 22 | ||||
-rw-r--r-- | spec/ruby/core/integer/coerce_spec.rb | 13 | ||||
-rw-r--r-- | spec/ruby/core/integer/div_spec.rb | 8 | ||||
-rw-r--r-- | spec/ruby/core/integer/left_shift_spec.rb | 32 | ||||
-rw-r--r-- | spec/ruby/core/integer/right_shift_spec.rb | 32 | ||||
-rw-r--r-- | spec/ruby/core/integer/shared/arithmetic_coerce.rb | 20 | ||||
-rw-r--r-- | spec/ruby/core/integer/try_convert_spec.rb | 12 | ||||
-rw-r--r-- | spec/ruby/core/integer/zero_spec.rb | 12 |
11 files changed, 83 insertions, 80 deletions
diff --git a/spec/ruby/core/integer/bit_and_spec.rb b/spec/ruby/core/integer/bit_and_spec.rb index 8de5a14aaa..e7face39ac 100644 --- a/spec/ruby/core/integer/bit_and_spec.rb +++ b/spec/ruby/core/integer/bit_and_spec.rb @@ -30,7 +30,7 @@ describe "Integer#&" do it "coerces the rhs and calls #coerce" do obj = mock("fixnum bit and") - obj.should_receive(:coerce).with(6).and_return([3, 6]) + obj.should_receive(:coerce).with(6).and_return([6, 3]) (6 & obj).should == 2 end diff --git a/spec/ruby/core/integer/bit_or_spec.rb b/spec/ruby/core/integer/bit_or_spec.rb index 6f4279c170..fdf8a191e5 100644 --- a/spec/ruby/core/integer/bit_or_spec.rb +++ b/spec/ruby/core/integer/bit_or_spec.rb @@ -30,9 +30,9 @@ describe "Integer#|" do end it "coerces the rhs and calls #coerce" do - obj = mock("fixnum bit and") - obj.should_receive(:coerce).with(6).and_return([3, 6]) - (6 & obj).should == 2 + obj = mock("fixnum bit or") + obj.should_receive(:coerce).with(6).and_return([6, 3]) + (6 | obj).should == 7 end it "raises a TypeError when passed a Float" do diff --git a/spec/ruby/core/integer/bit_xor_spec.rb b/spec/ruby/core/integer/bit_xor_spec.rb index f1150a20d5..1f46bc52f3 100644 --- a/spec/ruby/core/integer/bit_xor_spec.rb +++ b/spec/ruby/core/integer/bit_xor_spec.rb @@ -28,8 +28,8 @@ describe "Integer#^" do end it "coerces the rhs and calls #coerce" do - obj = mock("fixnum bit and") - obj.should_receive(:coerce).with(6).and_return([3, 6]) + obj = mock("fixnum bit xor") + obj.should_receive(:coerce).with(6).and_return([6, 3]) (6 ^ obj).should == 5 end diff --git a/spec/ruby/core/integer/ceildiv_spec.rb b/spec/ruby/core/integer/ceildiv_spec.rb new file mode 100644 index 0000000000..18d07c66d0 --- /dev/null +++ b/spec/ruby/core/integer/ceildiv_spec.rb @@ -0,0 +1,22 @@ +require_relative '../../spec_helper' + +describe "Integer#ceildiv" do + ruby_version_is '3.2' do + it "returns a quotient of division which is rounded up to the nearest integer" do + 0.ceildiv(3).should eql(0) + 1.ceildiv(3).should eql(1) + 3.ceildiv(3).should eql(1) + 4.ceildiv(3).should eql(2) + + 4.ceildiv(-3).should eql(-1) + -4.ceildiv(3).should eql(-1) + -4.ceildiv(-3).should eql(2) + + 3.ceildiv(1.2).should eql(3) + 3.ceildiv(6/5r).should eql(3) + + (10**100-11).ceildiv(10**99-1).should eql(10) + (10**100-9).ceildiv(10**99-1).should eql(11) + end + end +end diff --git a/spec/ruby/core/integer/coerce_spec.rb b/spec/ruby/core/integer/coerce_spec.rb index f1f3256032..1d6dc9713f 100644 --- a/spec/ruby/core/integer/coerce_spec.rb +++ b/spec/ruby/core/integer/coerce_spec.rb @@ -1,7 +1,5 @@ require_relative '../../spec_helper' -require 'bigdecimal' - describe "Integer#coerce" do context "fixnum" do describe "when given a Fixnum" do @@ -90,15 +88,4 @@ describe "Integer#coerce" do ary.should == [1.2, a.to_f] end end - - context "bigdecimal" do - it "produces Floats" do - x, y = 3.coerce(BigDecimal("3.4")) - x.class.should == Float - x.should == 3.4 - y.class.should == Float - y.should == 3.0 - end - end - end diff --git a/spec/ruby/core/integer/div_spec.rb b/spec/ruby/core/integer/div_spec.rb index 344e095179..2eb9c0623b 100644 --- a/spec/ruby/core/integer/div_spec.rb +++ b/spec/ruby/core/integer/div_spec.rb @@ -143,4 +143,12 @@ describe "Integer#div" do -> { @bignum.div(-0) }.should raise_error(ZeroDivisionError) end end + + context "rational" do + it "returns self divided by the given argument as an Integer" do + 2.div(6/5r).should == 1 + 1.div(6/5r).should == 0 + 5.div(6/5r).should == 4 + end + end end diff --git a/spec/ruby/core/integer/left_shift_spec.rb b/spec/ruby/core/integer/left_shift_spec.rb index 135af90421..0781371d93 100644 --- a/spec/ruby/core/integer/left_shift_spec.rb +++ b/spec/ruby/core/integer/left_shift_spec.rb @@ -191,21 +191,23 @@ describe "Integer#<< (with n << m)" do (0 << bignum_value).should == 0 end - ruby_bug "#18518", ""..."3.3" do - it "raises NoMemoryError when m > 0 and n != 0" do - coerce_long = mock("long") - coerce_long.stub!(:to_int).and_return(2**40) - coerce_bignum = mock("bignum") - coerce_bignum.stub!(:to_int).and_return(bignum_value) - exps = [2**40, bignum_value, coerce_long, coerce_bignum] - - exps.each { |exp| - -> { (1 << exp) }.should raise_error(NoMemoryError) - -> { (-1 << exp) }.should raise_error(NoMemoryError) - -> { (bignum_value << exp) }.should raise_error(NoMemoryError) - -> { (-bignum_value << exp) }.should raise_error(NoMemoryError) - } - end + it "raises RangeError when m > 0 and n != 0" do + # https://bugs.ruby-lang.org/issues/18518#note-9 + limit = RUBY_ENGINE == 'ruby' ? 2**67 : 2**32 + + coerce_long = mock("long") + coerce_long.stub!(:to_int).and_return(limit) + coerce_bignum = mock("bignum") + coerce_bignum.stub!(:to_int).and_return(bignum_value) + exps = [limit, coerce_long] + exps << bignum_value << coerce_bignum if bignum_value >= limit + + exps.each { |exp| + -> { (1 << exp) }.should raise_error(RangeError, 'shift width too big') + -> { (-1 << exp) }.should raise_error(RangeError, 'shift width too big') + -> { (bignum_value << exp) }.should raise_error(RangeError, 'shift width too big') + -> { (-bignum_value << exp) }.should raise_error(RangeError, 'shift width too big') + } end end end diff --git a/spec/ruby/core/integer/right_shift_spec.rb b/spec/ruby/core/integer/right_shift_spec.rb index 81405667b2..e91613d8d1 100644 --- a/spec/ruby/core/integer/right_shift_spec.rb +++ b/spec/ruby/core/integer/right_shift_spec.rb @@ -213,21 +213,23 @@ describe "Integer#>> (with n >> m)" do (0 >> -bignum_value).should == 0 end - ruby_bug "#18518", ""..."3.3" do - it "raises NoMemoryError when m < 0 and n != 0" do - coerce_long = mock("long") - coerce_long.stub!(:to_int).and_return(-(2**40)) - coerce_bignum = mock("bignum") - coerce_bignum.stub!(:to_int).and_return(-bignum_value) - exps = [-(2**40), -bignum_value, coerce_long, coerce_bignum] - - exps.each { |exp| - -> { (1 >> exp) }.should raise_error(NoMemoryError) - -> { (-1 >> exp) }.should raise_error(NoMemoryError) - -> { (bignum_value >> exp) }.should raise_error(NoMemoryError) - -> { (-bignum_value >> exp) }.should raise_error(NoMemoryError) - } - end + it "raises RangeError when m < 0 and n != 0" do + # https://bugs.ruby-lang.org/issues/18518#note-9 + limit = RUBY_ENGINE == 'ruby' ? 2**67 : 2**32 + + coerce_long = mock("long") + coerce_long.stub!(:to_int).and_return(-limit) + coerce_bignum = mock("bignum") + coerce_bignum.stub!(:to_int).and_return(-bignum_value) + exps = [-limit, coerce_long] + exps << -bignum_value << coerce_bignum if bignum_value >= limit + + exps.each { |exp| + -> { (1 >> exp) }.should raise_error(RangeError, 'shift width too big') + -> { (-1 >> exp) }.should raise_error(RangeError, 'shift width too big') + -> { (bignum_value >> exp) }.should raise_error(RangeError, 'shift width too big') + -> { (-bignum_value >> exp) }.should raise_error(RangeError, 'shift width too big') + } end end end diff --git a/spec/ruby/core/integer/shared/arithmetic_coerce.rb b/spec/ruby/core/integer/shared/arithmetic_coerce.rb index 4c0cbcb999..1260192df1 100644 --- a/spec/ruby/core/integer/shared/arithmetic_coerce.rb +++ b/spec/ruby/core/integer/shared/arithmetic_coerce.rb @@ -1,25 +1,5 @@ require_relative '../fixtures/classes' -describe :integer_arithmetic_coerce_rescue, shared: true do - it "rescues exception (StandardError and subclasses) raised in other#coerce and raises TypeError" do - b = mock("numeric with failed #coerce") - b.should_receive(:coerce).and_raise(IntegerSpecs::CoerceError) - - # e.g. 1 + b - -> { 1.send(@method, b) }.should raise_error(TypeError, /MockObject can't be coerced into Integer/) - end - - it "does not rescue Exception and StandardError siblings raised in other#coerce" do - [Exception, NoMemoryError].each do |exception| - b = mock("numeric with failed #coerce") - b.should_receive(:coerce).and_raise(exception) - - # e.g. 1 + b - -> { 1.send(@method, b) }.should raise_error(exception) - end - end -end - describe :integer_arithmetic_coerce_not_rescue, shared: true do it "does not rescue exception raised in other#coerce" do b = mock("numeric with failed #coerce") diff --git a/spec/ruby/core/integer/try_convert_spec.rb b/spec/ruby/core/integer/try_convert_spec.rb index 45c66eec79..4bc7d3851a 100644 --- a/spec/ruby/core/integer/try_convert_spec.rb +++ b/spec/ruby/core/integer/try_convert_spec.rb @@ -28,7 +28,17 @@ ruby_version_is "3.1" do it "sends #to_int to the argument and raises TypeError if it's not a kind of Integer" do obj = mock("to_int") obj.should_receive(:to_int).and_return(Object.new) - -> { Integer.try_convert obj }.should raise_error(TypeError) + -> { + Integer.try_convert obj + }.should raise_error(TypeError, "can't convert MockObject to Integer (MockObject#to_int gives Object)") + end + + it "responds with a different error message when it raises a TypeError, depending on the type of the non-Integer object :to_int returns" do + obj = mock("to_int") + obj.should_receive(:to_int).and_return("A String") + -> { + Integer.try_convert obj + }.should raise_error(TypeError, "can't convert MockObject to Integer (MockObject#to_int gives String)") end it "does not rescue exceptions raised by #to_int" do diff --git a/spec/ruby/core/integer/zero_spec.rb b/spec/ruby/core/integer/zero_spec.rb index 2dac50c406..bd362c4181 100644 --- a/spec/ruby/core/integer/zero_spec.rb +++ b/spec/ruby/core/integer/zero_spec.rb @@ -7,15 +7,7 @@ describe "Integer#zero?" do -1.should_not.zero? end - ruby_version_is "3.0" do - it "Integer#zero? overrides Numeric#zero?" do - 42.method(:zero?).owner.should == Integer - end - end - - ruby_version_is ""..."3.0" do - it "Integer#zero? uses Numeric#zero?" do - 42.method(:zero?).owner.should == Numeric - end + it "Integer#zero? overrides Numeric#zero?" do + 42.method(:zero?).owner.should == Integer end end |