summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Wu <[email protected]>2024-07-23 18:21:38 -0400
committerBenoit Daloze <[email protected]>2024-07-24 18:20:30 +0200
commitfbb981b9f819d24c4e11132c02ec02401eabcc5e (patch)
tree8fa1940f730c73dcb2b5b5f16f3b256c1c81342b
parent12e6cf77efae6804063dbebe84e4080ca78958e7 (diff)
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.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/11130
-rw-r--r--spec/mspec/lib/mspec/helpers/numeric.rb7
-rw-r--r--spec/ruby/optional/capi/bignum_spec.rb24
2 files changed, 18 insertions, 13 deletions
diff --git a/spec/mspec/lib/mspec/helpers/numeric.rb b/spec/mspec/lib/mspec/helpers/numeric.rb
index 46d3d7b182..3be3dcdcd1 100644
--- a/spec/mspec/lib/mspec/helpers/numeric.rb
+++ b/spec/mspec/lib/mspec/helpers/numeric.rb
@@ -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
diff --git a/spec/ruby/optional/capi/bignum_spec.rb b/spec/ruby/optional/capi/bignum_spec.rb
index c29d6182e8..179f053eec 100644
--- a/spec/ruby/optional/capi/bignum_spec.rb
+++ b/spec/ruby/optional/capi/bignum_spec.rb
@@ -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