summaryrefslogtreecommitdiff
path: root/test/ruby/test_pack.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_pack.rb')
-rw-r--r--test/ruby/test_pack.rb340
1 files changed, 281 insertions, 59 deletions
diff --git a/test/ruby/test_pack.rb b/test/ruby/test_pack.rb
index c9ca9c3dfd..6e5f0fe7ff 100644
--- a/test/ruby/test_pack.rb
+++ b/test/ruby/test_pack.rb
@@ -1,25 +1,44 @@
# coding: US-ASCII
# frozen_string_literal: false
require 'test/unit'
+require 'rbconfig'
+require 'rbconfig/sizeof'
class TestPack < Test::Unit::TestCase
+ # Note: the size of intptr_t and uintptr_t should be equal.
+ J_SIZE = RbConfig::SIZEOF['uintptr_t']
+
def test_pack
- $format = "c2x5CCxsdils_l_a6";
+ format = "c2x5CCxsdils_l_a6";
# Need the expression in here to force ary[5] to be numeric. This avoids
# test2 failing because ary2 goes str->numeric->str and ary does not.
ary = [1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,-32767,-123456,"abcdef"]
- $x = ary.pack($format)
- ary2 = $x.unpack($format)
+ x = ary.pack(format)
+ ary2 = x.unpack(format)
assert_equal(ary.length, ary2.length)
assert_equal(ary.join(':'), ary2.join(':'))
- assert_match(/def/, $x)
+ assert_match(/def/, x)
- $x = [-1073741825]
- assert_equal($x, $x.pack("q").unpack("q"))
+ x = [-1073741825]
+ assert_equal(x, x.pack("q").unpack("q"))
- $x = [-1]
- assert_equal($x, $x.pack("l").unpack("l"))
+ x = [-1]
+ assert_equal(x, x.pack("l").unpack("l"))
+ end
+
+ def test_ascii_incompatible
+ assert_raise(Encoding::CompatibilityError) do
+ ["foo"].pack("u".encode("UTF-32BE"))
+ end
+
+ assert_raise(Encoding::CompatibilityError) do
+ "foo".unpack("C".encode("UTF-32BE"))
+ end
+
+ assert_raise(Encoding::CompatibilityError) do
+ "foo".unpack1("C".encode("UTF-32BE"))
+ end
end
def test_pack_n
@@ -79,11 +98,11 @@ class TestPack < Test::Unit::TestCase
assert_equal("\x01\x02\x03\x04", [0x01020304].pack("L"+mod))
assert_equal("\x01\x02\x03\x04\x05\x06\x07\x08", [0x0102030405060708].pack("q"+mod))
assert_equal("\x01\x02\x03\x04\x05\x06\x07\x08", [0x0102030405060708].pack("Q"+mod))
- psize = [nil].pack('p').bytesize
- if psize == 4
+ case J_SIZE
+ when 4
assert_equal("\x01\x02\x03\x04", [0x01020304].pack("j"+mod))
assert_equal("\x01\x02\x03\x04", [0x01020304].pack("J"+mod))
- elsif psize == 8
+ when 8
assert_equal("\x01\x02\x03\x04\x05\x06\x07\x08", [0x0102030405060708].pack("j"+mod))
assert_equal("\x01\x02\x03\x04\x05\x06\x07\x08", [0x0102030405060708].pack("J"+mod))
end
@@ -95,10 +114,11 @@ class TestPack < Test::Unit::TestCase
assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("I!"+mod))
assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("l!"+mod))
assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("L!"+mod))
- if psize == 4
+ case J_SIZE
+ when 4
assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("j!"+mod))
assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("J!"+mod))
- elsif psize == 8
+ when 8
assert_match(/\A\x00*\x01\x02\x03\x04\x05\x06\x07\x08\z/, [0x0102030405060708].pack("j!"+mod))
assert_match(/\A\x00*\x01\x02\x03\x04\x05\x06\x07\x08\z/, [0x0102030405060708].pack("J!"+mod))
end
@@ -127,11 +147,11 @@ class TestPack < Test::Unit::TestCase
assert_equal("\x04\x03\x02\x01", [0x01020304].pack("L"+mod))
assert_equal("\x08\x07\x06\x05\x04\x03\x02\x01", [0x0102030405060708].pack("q"+mod))
assert_equal("\x08\x07\x06\x05\x04\x03\x02\x01", [0x0102030405060708].pack("Q"+mod))
- psize = [nil].pack('p').bytesize
- if psize == 4
+ case J_SIZE
+ when 4
assert_equal("\x04\x03\x02\x01", [0x01020304].pack("j"+mod))
assert_equal("\x04\x03\x02\x01", [0x01020304].pack("J"+mod))
- elsif psize == 8
+ when 8
assert_equal("\x08\x07\x06\x05\x04\x03\x02\x01", [0x0102030405060708].pack("j"+mod))
assert_equal("\x08\x07\x06\x05\x04\x03\x02\x01", [0x0102030405060708].pack("J"+mod))
end
@@ -143,10 +163,11 @@ class TestPack < Test::Unit::TestCase
assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("I!"+mod))
assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("l!"+mod))
assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("L!"+mod))
- if psize == 4
+ case J_SIZE
+ when 4
assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("j!"+mod))
assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("J!"+mod))
- elsif psize == 8
+ when 8
assert_match(/\A\x08\x07\x06\x05\x04\x03\x02\x01\x00*\z/, [0x0102030405060708].pack("j!"+mod))
assert_match(/\A\x08\x07\x06\x05\x04\x03\x02\x01\x00*\z/, [0x0102030405060708].pack("J!"+mod))
end
@@ -182,8 +203,8 @@ class TestPack < Test::Unit::TestCase
end
def test_integer_endian_explicit
- _integer_big_endian('>')
- _integer_little_endian('<')
+ _integer_big_endian('>')
+ _integer_little_endian('<')
end
def test_pack_U
@@ -262,6 +283,15 @@ class TestPack < Test::Unit::TestCase
assert_equal(["foo "], "foo ".unpack("a4"))
assert_equal(["foo"], "foo".unpack("A4"))
assert_equal(["foo"], "foo".unpack("a4"))
+
+ assert_equal(["foo", 4], "foo\0 ".unpack("A4^"))
+ assert_equal(["foo\0", 4], "foo\0 ".unpack("a4^"))
+ assert_equal(["foo", 4], "foo ".unpack("A4^"))
+ assert_equal(["foo ", 4], "foo ".unpack("a4^"))
+ assert_equal(["foo", 3], "foo".unpack("A4^"))
+ assert_equal(["foo", 3], "foo".unpack("a4^"))
+ assert_equal(["foo", 6], "foo\0 ".unpack("A*^"))
+ assert_equal(["foo", 6], "foo ".unpack("A*^"))
end
def test_pack_unpack_Z
@@ -277,6 +307,11 @@ class TestPack < Test::Unit::TestCase
assert_equal(["foo"], "foo".unpack("Z*"))
assert_equal(["foo"], "foo\0".unpack("Z*"))
assert_equal(["foo"], "foo".unpack("Z5"))
+
+ assert_equal(["foo", 3], "foo".unpack("Z*^"))
+ assert_equal(["foo", 4], "foo\0".unpack("Z*^"))
+ assert_equal(["foo", 3], "foo".unpack("Z5^"))
+ assert_equal(["foo", 5], "foo\0\0\0".unpack("Z5^"))
end
def test_pack_unpack_bB
@@ -428,7 +463,6 @@ class TestPack < Test::Unit::TestCase
assert_operator(4, :<=, [1].pack("L!").bytesize)
end
- require 'rbconfig'
def test_pack_unpack_qQ
s1 = [578437695752307201, -506097522914230529].pack("q*")
s2 = [578437695752307201, 17940646550795321087].pack("Q*")
@@ -451,10 +485,8 @@ class TestPack < Test::Unit::TestCase
end if RbConfig::CONFIG['HAVE_LONG_LONG']
def test_pack_unpack_jJ
- # Note: we assume that the size of intptr_t and uintptr_t equals to the size
- # of real pointer.
- psize = [nil].pack("p").bytesize
- if psize == 4
+ case J_SIZE
+ when 4
s1 = [67305985, -50462977].pack("j*")
s2 = [67305985, 4244504319].pack("J*")
assert_equal(s1, s2)
@@ -468,7 +500,7 @@ class TestPack < Test::Unit::TestCase
assert_equal(4, [1].pack("j").bytesize)
assert_equal(4, [1].pack("J").bytesize)
- elsif psize == 8
+ when 8
s1 = [578437695752307201, -506097522914230529].pack("j*")
s2 = [578437695752307201, 17940646550795321087].pack("J*")
assert_equal(s1, s2)
@@ -531,6 +563,8 @@ class TestPack < Test::Unit::TestCase
assert_equal([0, 2], "\x00\x00\x02".unpack("CxC"))
assert_raise(ArgumentError) { "".unpack("x") }
+
+ assert_equal([0, 1, 2, 2, 3], "\x00\x00\x02".unpack("C^x^C^"))
end
def test_pack_unpack_X
@@ -540,6 +574,7 @@ class TestPack < Test::Unit::TestCase
assert_equal([0, 2, 2], "\x00\x02".unpack("CCXC"))
assert_raise(ArgumentError) { "".unpack("X") }
+ assert_equal([0, 1, 2, 2, 1, 2, 2], "\x00\x02".unpack("C^C^X^C^"))
end
def test_pack_unpack_atmark
@@ -553,6 +588,17 @@ class TestPack < Test::Unit::TestCase
pos = RbConfig::LIMITS["UINTPTR_MAX"] - 99 # -100
assert_raise(RangeError) {"0123456789".unpack("@#{pos}C10")}
+
+ assert_equal([1, 3, 4], "\x01\x00\x00\x02".unpack("x^@3^x^"))
+ end
+
+ def test_unpack_carret
+ assert_equal([0], "abc".unpack("^"))
+ assert_equal([2], "abc".unpack("^", offset: 2))
+ assert_equal([97, nil, 1], "a".unpack("CC^"))
+
+ assert_raise(ArgumentError) { "".unpack("^!") }
+ assert_raise(ArgumentError) { "".unpack("^_") }
end
def test_pack_unpack_percent
@@ -638,6 +684,14 @@ EXPECTED
end;
end
+ def test_bug_18343
+ bug18343 = '[ruby-core:106096] [Bug #18343]'
+ assert_separately(%W[- #{bug18343}], <<-'end;')
+ bug = ARGV.shift
+ assert_raise(ArgumentError, bug){[0].pack('c', {})}
+ end;
+ end
+
def test_pack_unpack_m0
assert_equal("", [""].pack("m0"))
assert_equal("AA==", ["\0"].pack("m0"))
@@ -755,58 +809,32 @@ EXPECTED
end
def test_pack_garbage
- verbose = $VERBOSE
- $VERBOSE = false
-
- assert_silent do
+ assert_raise(ArgumentError, %r%unknown pack directive '\*' in '\*U'$%) do
assert_equal "\000", [0].pack("*U")
end
-
- $VERBOSE = true
-
- _, err = capture_output do
- assert_equal "\000", [0].pack("*U")
- end
-
- assert_match %r%unknown pack directive '\*' in '\*U'$%, err
- ensure
- $VERBOSE = verbose
end
def test_unpack_garbage
- verbose = $VERBOSE
- $VERBOSE = false
-
- assert_silent do
- assert_equal [0], "\000".unpack("*U")
- end
-
- $VERBOSE = true
-
- _, err = capture_output do
+ assert_raise(ArgumentError, %r%unknown unpack directive '\*' in '\*U'$%) do
assert_equal [0], "\000".unpack("*U")
end
-
- assert_match %r%unknown unpack directive '\*' in '\*U'$%, err
- ensure
- $VERBOSE = verbose
end
def test_invalid_warning
- assert_warning(/unknown pack directive ',' in ','/) {
+ assert_raise(ArgumentError, /unknown pack directive ',' in ','/) {
[].pack(",")
}
- assert_warning(/\A[ -~]+\Z/) {
+ assert_raise(ArgumentError, /\A[ -~]+\Z/) {
[].pack("\x7f")
}
- assert_warning(/\A(.* in '\u{3042}'\n)+\z/) {
+ assert_raise(ArgumentError, /\A(.* in '\u{3042}'\n)+\z/) {
[].pack("\u{3042}")
}
- assert_warning(/\A.* in '.*U'\Z/) {
+ assert_raise(ArgumentError, /\A.* in '.*U'\Z/) {
assert_equal "\000", [0].pack("\0U")
}
- assert_warning(/\A.* in '.*U'\Z/) {
+ assert_raise(ArgumentError, /\A.* in '.*U'\Z/) {
"\000".unpack("\0U")
}
end
@@ -853,6 +881,19 @@ EXPECTED
assert_equal "\xDE\xAD\xBE\xEF\xBA\xBE\xF0\x0D\0\0\xBA\xAD\xFA\xCE", buf
assert_equal addr, [buf].pack('p')
+
+ assert_packing_buffer_fail("b*")
+ assert_packing_buffer_fail("B*")
+ assert_packing_buffer_fail("h*")
+ assert_packing_buffer_fail("H*")
+ assert_packing_buffer_fail("u", 16384)
+ assert_packing_buffer_fail("m", 16384)
+ assert_packing_buffer_fail("M", 16384)
+ end
+
+ def assert_packing_buffer_fail(fmt, size = 8192)
+ s = "\x01".b * size
+ assert_raise(ArgumentError) {[s].pack(fmt, buffer: s)}
end
def test_unpack_with_block
@@ -869,4 +910,185 @@ EXPECTED
assert_equal "hogefuga", "aG9nZWZ1Z2E=".unpack1("m")
assert_equal "01000001", "A".unpack1("B*")
end
+
+ def test_unpack1_offset
+ assert_equal 65, "ZA".unpack1("C", offset: 1)
+ assert_equal 65, "ZA".unpack1("C", offset: -1)
+ assert_equal "01000001", "YZA".unpack1("B*", offset: 2)
+ assert_nil "abc".unpack1("C", offset: 3)
+ assert_raise_with_message(ArgumentError, /offset outside of string/) {
+ "a".unpack1("C", offset: 2)
+ }
+ assert_raise_with_message(ArgumentError, /offset outside of string/) {
+ "a".unpack1("C", offset: -2)
+ }
+ assert_nil "a".unpack1("C", offset: 1)
+ end
+
+ def test_unpack_offset
+ assert_equal [65], "ZA".unpack("C", offset: 1)
+ assert_equal [65], "ZA".unpack("C", offset: -1)
+ assert_equal ["01000001"], "YZA".unpack("B*", offset: 2)
+ assert_equal [nil, nil, nil], "abc".unpack("CCC", offset: 3)
+ assert_raise_with_message(ArgumentError, /offset outside of string/) {
+ "a".unpack("C", offset: 2)
+ }
+ assert_raise_with_message(ArgumentError, /offset outside of string/) {
+ "a".unpack("C", offset: -2)
+ }
+ assert_equal [nil], "a".unpack("C", offset: 1)
+ end
+
+ def test_monkey_pack
+ assert_separately([], <<-'end;')
+ $-w = false
+ class Array
+ alias :old_pack :pack
+ def pack _; "oh no"; end
+ end
+
+ v = [2 ** 15].pack('n')
+
+ class Array
+ alias :pack :old_pack
+ end
+
+ assert_equal "oh no", v
+ end;
+ end
+
+ def test_monkey_pack_buffer
+ assert_separately([], <<-'end;')
+ $-w = false
+ class Array
+ alias :old_pack :pack
+ def pack _, buffer:; buffer << " no"; end
+ end
+
+ def test
+ b = +"oh"
+ [2 ** 15].pack('n', buffer: b)
+ end
+
+ v = test
+
+ class Array
+ alias :pack :old_pack
+ end
+
+ assert_equal "oh no", v
+ end;
+ end
+
+ def test_unpack_broken_R
+ assert_equal([nil], "\xFF".unpack("R"))
+ assert_nil("\xFF".unpack1("R"))
+ assert_equal([nil], "\xFF".unpack("r"))
+ assert_nil("\xFF".unpack1("r"))
+
+ bytes = [256].pack("r")
+ assert_equal([256, nil, nil, nil], (bytes + "\xFF").unpack("rrrr"))
+
+ bytes = [256].pack("R")
+ assert_equal([256, nil, nil, nil], (bytes + "\xFF").unpack("RRRR"))
+
+ assert_equal([], "\xFF".unpack("R*"))
+ assert_equal([], "\xFF".unpack("r*"))
+ end
+
+ def test_pack_unpack_R
+ # ULEB128 encoding (unsigned)
+ assert_equal("\x00", [0].pack("R"))
+ assert_equal("\x01", [1].pack("R"))
+ assert_equal("\x7f", [127].pack("R"))
+ assert_equal("\x80\x01", [128].pack("R"))
+ assert_equal("\xff\x7f", [0x3fff].pack("R"))
+ assert_equal("\x80\x80\x01", [0x4000].pack("R"))
+ assert_equal("\xff\xff\xff\xff\x0f", [0xffffffff].pack("R"))
+ assert_equal("\x80\x80\x80\x80\x10", [0x100000000].pack("R"))
+ assert_equal("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01", [0xffff_ffff_ffff_ffff].pack("R"))
+ assert_equal("\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x1f", [0xffff_ffff_ffff_ffff_ffff_ffff].pack("R"))
+
+ # Multiple values
+ assert_equal("\x01\x02", [1, 2].pack("R*"))
+ assert_equal("\x7f\x80\x01", [127, 128].pack("R*"))
+
+ # Negative numbers should raise an error
+ assert_raise(ArgumentError) { [-1].pack("R") }
+ assert_raise(ArgumentError) { [-100].pack("R") }
+
+ # Unpack tests
+ assert_equal([0], "\x00".unpack("R"))
+ assert_equal([1], "\x01".unpack("R"))
+ assert_equal([127], "\x7f".unpack("R"))
+ assert_equal([128], "\x80\x01".unpack("R"))
+ assert_equal([0x3fff], "\xff\x7f".unpack("R"))
+ assert_equal([0x4000], "\x80\x80\x01".unpack("R"))
+ assert_equal([0xffffffff], "\xff\xff\xff\xff\x0f".unpack("R"))
+ assert_equal([0x100000000], "\x80\x80\x80\x80\x10".unpack("R"))
+ assert_equal([0xffff_ffff_ffff_ffff], "\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01".unpack("R"))
+ assert_equal([0xffff_ffff_ffff_ffff_ffff_ffff].pack("R"), "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x1f")
+
+ # Multiple values
+ assert_equal([1, 2], "\x01\x02".unpack("R*"))
+ assert_equal([127, 128], "\x7f\x80\x01".unpack("R*"))
+
+ # Round-trip test
+ values = [0, 1, 127, 128, 0x3fff, 0x4000, 0xffffffff, 0x100000000]
+ assert_equal(values, values.pack("R*").unpack("R*"))
+ end
+
+ def test_pack_unpack_r
+ # SLEB128 encoding (signed)
+ assert_equal("\x00", [0].pack("r"))
+ assert_equal("\x01", [1].pack("r"))
+ assert_equal("\x7f", [-1].pack("r"))
+ assert_equal("\x7e", [-2].pack("r"))
+ assert_equal("\xff\x00", [127].pack("r"))
+ assert_equal("\x80\x01", [128].pack("r"))
+ assert_equal("\x81\x7f", [-127].pack("r"))
+ assert_equal("\x80\x7f", [-128].pack("r"))
+
+ # Larger positive numbers
+ assert_equal("\xff\xff\x00", [0x3fff].pack("r"))
+ assert_equal("\x80\x80\x01", [0x4000].pack("r"))
+
+ # Larger negative numbers
+ assert_equal("\x81\x80\x7f", [-0x3fff].pack("r"))
+ assert_equal("\x80\x80\x7f", [-0x4000].pack("r"))
+
+ # Very large numbers
+ assert_equal("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1F", [0xffff_ffff_ffff_ffff_ffff_ffff].pack("r"))
+ assert_equal("\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80`", [-0xffff_ffff_ffff_ffff_ffff_ffff].pack("r"))
+
+ # Multiple values
+ assert_equal("\x00\x01\x7f", [0, 1, -1].pack("r*"))
+
+ # Unpack tests
+ assert_equal([0], "\x00".unpack("r"))
+ assert_equal([1], "\x01".unpack("r"))
+ assert_equal([-1], "\x7f".unpack("r"))
+ assert_equal([-2], "\x7e".unpack("r"))
+ assert_equal([127], "\xff\x00".unpack("r"))
+ assert_equal([128], "\x80\x01".unpack("r"))
+ assert_equal([-127], "\x81\x7f".unpack("r"))
+ assert_equal([-128], "\x80\x7f".unpack("r"))
+
+ # Larger numbers
+ assert_equal([0x3fff], "\xff\xff\x00".unpack("r"))
+ assert_equal([0x4000], "\x80\x80\x01".unpack("r"))
+ assert_equal([-0x3fff], "\x81\x80\x7f".unpack("r"))
+ assert_equal([-0x4000], "\x80\x80\x7f".unpack("r"))
+
+ # Very large numbers
+ assert_equal("\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x1f", [0xffff_ffff_ffff_ffff_ffff_ffff].pack("r"))
+ assert_equal("\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80`", [-0xffff_ffff_ffff_ffff_ffff_ffff].pack("r"))
+
+ # Multiple values
+ assert_equal([0, 1, -1], "\x00\x01\x7f".unpack("r*"))
+
+ # Round-trip test
+ values = [0, 1, -1, 127, -127, 128, -128, 0x3fff, -0x3fff, 0x4000, -0x4000]
+ assert_equal(values, values.pack("r*").unpack("r*"))
+ end
end