diff options
| author | Aaron Patterson <tenderlove@ruby-lang.org> | 2025-12-16 09:10:45 -0800 |
|---|---|---|
| committer | Aaron Patterson <aaron.patterson@gmail.com> | 2025-12-18 14:42:47 -0800 |
| commit | d0b72429a93e54f1f956b4aedfc25c57dc7001aa (patch) | |
| tree | 1e4303fd43d1b99972922bfdbb61fee6836b7bc5 /spec/ruby | |
| parent | 73e930f9f911cf71ecb416c3112a7818bae41cd6 (diff) | |
Add support for signed and unsigned LEB128 to pack/unpack.
This commit adds a new pack format command `R` and `r` for unsigned and
signed LEB128 encoding. The "r" mnemonic is because this is a
"vaRiable" length encoding scheme.
LEB128 is used in various formats including DWARF, WebAssembly, MQTT,
and Protobuf.
[Feature #21785]
Diffstat (limited to 'spec/ruby')
| -rw-r--r-- | spec/ruby/core/array/pack/r_spec.rb | 23 | ||||
| -rw-r--r-- | spec/ruby/core/array/pack/shared/basic.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/core/string/unpack/shared/basic.rb | 2 |
3 files changed, 26 insertions, 3 deletions
diff --git a/spec/ruby/core/array/pack/r_spec.rb b/spec/ruby/core/array/pack/r_spec.rb new file mode 100644 index 0000000000..22be6fa640 --- /dev/null +++ b/spec/ruby/core/array/pack/r_spec.rb @@ -0,0 +1,23 @@ +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/numeric_basic' +require_relative 'shared/integer' + +ruby_version_is "4.0" do + describe "Array#pack with format 'R'" do + it_behaves_like :array_pack_basic, 'R' + it_behaves_like :array_pack_basic_non_float, 'R' + it_behaves_like :array_pack_arguments, 'R' + it_behaves_like :array_pack_numeric_basic, 'R' + it_behaves_like :array_pack_integer, 'R' + end + + describe "Array#pack with format 'r'" do + it_behaves_like :array_pack_basic, 'r' + it_behaves_like :array_pack_basic_non_float, 'r' + it_behaves_like :array_pack_arguments, 'r' + it_behaves_like :array_pack_numeric_basic, 'r' + it_behaves_like :array_pack_integer, 'r' + end +end diff --git a/spec/ruby/core/array/pack/shared/basic.rb b/spec/ruby/core/array/pack/shared/basic.rb index ebd9f75d9d..77d7f2f71c 100644 --- a/spec/ruby/core/array/pack/shared/basic.rb +++ b/spec/ruby/core/array/pack/shared/basic.rb @@ -37,7 +37,7 @@ describe :array_pack_basic_non_float, shared: true do # NOTE: it's just a plan of the Ruby core team it "warns that a directive is unknown" do # additional directive ('a') is required for the X directive - -> { [@obj, @obj].pack("a R" + pack_format) }.should complain(/unknown pack directive 'R'/) + -> { [@obj, @obj].pack("a K" + pack_format) }.should complain(/unknown pack directive 'K'/) -> { [@obj, @obj].pack("a 0" + pack_format) }.should complain(/unknown pack directive '0'/) -> { [@obj, @obj].pack("a :" + pack_format) }.should complain(/unknown pack directive ':'/) end @@ -48,7 +48,7 @@ describe :array_pack_basic_non_float, shared: true do # NOTE: Added this case just to not forget about the decision in the ticket it "raise ArgumentError when a directive is unknown" do # additional directive ('a') is required for the X directive - -> { [@obj, @obj].pack("a R" + pack_format) }.should raise_error(ArgumentError, /unknown pack directive 'R'/) + -> { [@obj, @obj].pack("a K" + pack_format) }.should raise_error(ArgumentError, /unknown pack directive 'K'/) -> { [@obj, @obj].pack("a 0" + pack_format) }.should raise_error(ArgumentError, /unknown pack directive '0'/) -> { [@obj, @obj].pack("a :" + pack_format) }.should raise_error(ArgumentError, /unknown pack directive ':'/) end diff --git a/spec/ruby/core/string/unpack/shared/basic.rb b/spec/ruby/core/string/unpack/shared/basic.rb index b37a447683..0ac2a951ed 100644 --- a/spec/ruby/core/string/unpack/shared/basic.rb +++ b/spec/ruby/core/string/unpack/shared/basic.rb @@ -12,7 +12,7 @@ describe :string_unpack_basic, shared: true do ruby_version_is "3.3" do # https://bugs.ruby-lang.org/issues/19150 it 'raise ArgumentError when a directive is unknown' do - -> { "abcdefgh".unpack("a R" + unpack_format) }.should raise_error(ArgumentError, /unknown unpack directive 'R'/) + -> { "abcdefgh".unpack("a K" + unpack_format) }.should raise_error(ArgumentError, /unknown unpack directive 'K'/) -> { "abcdefgh".unpack("a 0" + unpack_format) }.should raise_error(ArgumentError, /unknown unpack directive '0'/) -> { "abcdefgh".unpack("a :" + unpack_format) }.should raise_error(ArgumentError, /unknown unpack directive ':'/) end |
