diff options
author | Benoit Daloze <eregontp@gmail.com> | 2023-01-07 13:13:03 +0100 |
---|---|---|
committer | Benoit Daloze <eregontp@gmail.com> | 2023-01-07 13:13:03 +0100 |
commit | 897cf122bf491ecf00d6374d944654bc85f8ed0f (patch) | |
tree | 1e43d589efb05a766ab2528b108b692610c6bfde /spec/ruby | |
parent | 651a098ea1526b363e85fd8d3f30e9783f6c5de1 (diff) |
Fix Integer#{<<,>>} specs with large shift width
* The limit depends on the implementation and platform, it seems unavoidable.
* See https://bugs.ruby-lang.org/issues/18518#note-9
Diffstat (limited to 'spec/ruby')
-rw-r--r-- | spec/ruby/core/integer/left_shift_spec.rb | 37 | ||||
-rw-r--r-- | spec/ruby/core/integer/right_shift_spec.rb | 37 |
2 files changed, 46 insertions, 28 deletions
diff --git a/spec/ruby/core/integer/left_shift_spec.rb b/spec/ruby/core/integer/left_shift_spec.rb index 6774e1f3e1..ef434f7a48 100644 --- a/spec/ruby/core/integer/left_shift_spec.rb +++ b/spec/ruby/core/integer/left_shift_spec.rb @@ -191,21 +191,30 @@ describe "Integer#<< (with n << m)" do (0 << bignum_value).should == 0 end - ruby_bug "#18518", ""..."3.4" 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) - } + it "raises RangeError or NoMemoryError 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, bignum_value, coerce_long, coerce_bignum] + + matcher = raise_error(Exception) do |exc| + if RangeError === exc + exc.message.should == 'shift width too big' + else + exc.should.is_a?(NoMemoryError) + end end + + exps.each { |exp| + -> { (1 << exp) }.should matcher + -> { (-1 << exp) }.should matcher + -> { (bignum_value << exp) }.should matcher + -> { (-bignum_value << exp) }.should matcher + } end end end diff --git a/spec/ruby/core/integer/right_shift_spec.rb b/spec/ruby/core/integer/right_shift_spec.rb index 3fef09a30a..c022556a06 100644 --- a/spec/ruby/core/integer/right_shift_spec.rb +++ b/spec/ruby/core/integer/right_shift_spec.rb @@ -213,21 +213,30 @@ describe "Integer#>> (with n >> m)" do (0 >> -bignum_value).should == 0 end - ruby_bug "#18518", ""..."3.4" 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) - } + it "raises RangeError or NoMemoryError 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, -bignum_value, coerce_long, coerce_bignum] + + matcher = raise_error(Exception) do |exc| + if RangeError === exc + exc.message.should == 'shift width too big' + else + exc.should.is_a?(NoMemoryError) + end end + + exps.each { |exp| + -> { (1 >> exp) }.should matcher + -> { (-1 >> exp) }.should matcher + -> { (bignum_value >> exp) }.should matcher + -> { (-bignum_value >> exp) }.should matcher + } end end end |