Stop depending on Integer#size to return `sizeof(long)`

There is no guarantee that Integer#size will continue to return
`sizeof(long)` for small integers.

Use the `l!` specifier for Array#pack instead. It is a public
interface that has a direct relationship with the `long` type.
This commit is contained in:
Alan Wu 2024-07-23 18:21:38 -04:00 коммит произвёл Benoit Daloze
Родитель 12e6cf77ef
Коммит fbb981b9f8
2 изменённых файлов: 18 добавлений и 13 удалений

Просмотреть файл

@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mspec/guards/platform'
def nan_value
@ -15,11 +16,13 @@ def bignum_value(plus = 0)
end
def max_long
2**(0.size * 8 - 1) - 1
long_byte_size = [0].pack('l!').size
2**(long_byte_size * 8 - 1) - 1
end
def min_long
-(2**(0.size * 8 - 1))
long_byte_size = [0].pack('l!').size
-(2**(long_byte_size * 8 - 1))
end
# This is a bit hairy, but we need to be able to write specs that cover the

Просмотреть файл

@ -7,21 +7,23 @@ def ensure_bignum(n)
n
end
full_range_longs = (fixnum_max == 2**(0.size * 8 - 1) - 1)
full_range_longs = (fixnum_max == max_long)
max_ulong = begin
require 'rbconfig/sizeof'
RbConfig::LIMITS['ULONG_MAX']
rescue LoadError
nil
end
# If the system doesn't offer ULONG_MAX, assume 2's complement and derive it
# from LONG_MAX.
max_ulong ||= 2 * (max_long + 1) - 1
describe "CApiBignumSpecs" do
before :each do
@s = CApiBignumSpecs.new
if full_range_longs
@max_long = 2**(0.size * 8 - 1) - 1
@min_long = -@max_long - 1
@max_ulong = ensure_bignum(2**(0.size * 8) - 1)
else
@max_long = ensure_bignum(2**(0.size * 8 - 1) - 1)
@min_long = ensure_bignum(-@max_long - 1)
@max_ulong = ensure_bignum(2**(0.size * 8) - 1)
end
@max_long = max_long
@min_long = min_long
@max_ulong = ensure_bignum(max_ulong)
end
describe "rb_big2long" do