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
This commit is contained in:
parent
651a098ea1
commit
897cf122bf
@ -191,21 +191,30 @@ describe "Integer#<< (with n << m)" do
|
|||||||
(0 << bignum_value).should == 0
|
(0 << bignum_value).should == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
ruby_bug "#18518", ""..."3.4" do
|
it "raises RangeError or NoMemoryError when m > 0 and n != 0" do
|
||||||
it "raises NoMemoryError when m > 0 and n != 0" do
|
# https://bugs.ruby-lang.org/issues/18518#note-9
|
||||||
coerce_long = mock("long")
|
limit = RUBY_ENGINE == 'ruby' ? 2**67 : 2**32
|
||||||
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|
|
coerce_long = mock("long")
|
||||||
-> { (1 << exp) }.should raise_error(NoMemoryError)
|
coerce_long.stub!(:to_int).and_return(limit)
|
||||||
-> { (-1 << exp) }.should raise_error(NoMemoryError)
|
coerce_bignum = mock("bignum")
|
||||||
-> { (bignum_value << exp) }.should raise_error(NoMemoryError)
|
coerce_bignum.stub!(:to_int).and_return(bignum_value)
|
||||||
-> { (-bignum_value << exp) }.should raise_error(NoMemoryError)
|
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
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -213,21 +213,30 @@ describe "Integer#>> (with n >> m)" do
|
|||||||
(0 >> -bignum_value).should == 0
|
(0 >> -bignum_value).should == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
ruby_bug "#18518", ""..."3.4" do
|
it "raises RangeError or NoMemoryError when m < 0 and n != 0" do
|
||||||
it "raises NoMemoryError when m < 0 and n != 0" do
|
# https://bugs.ruby-lang.org/issues/18518#note-9
|
||||||
coerce_long = mock("long")
|
limit = RUBY_ENGINE == 'ruby' ? 2**67 : 2**32
|
||||||
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|
|
coerce_long = mock("long")
|
||||||
-> { (1 >> exp) }.should raise_error(NoMemoryError)
|
coerce_long.stub!(:to_int).and_return(-limit)
|
||||||
-> { (-1 >> exp) }.should raise_error(NoMemoryError)
|
coerce_bignum = mock("bignum")
|
||||||
-> { (bignum_value >> exp) }.should raise_error(NoMemoryError)
|
coerce_bignum.stub!(:to_int).and_return(-bignum_value)
|
||||||
-> { (-bignum_value >> exp) }.should raise_error(NoMemoryError)
|
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
|
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
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user