diff options
Diffstat (limited to 'test/ruby/test_pack.rb')
-rw-r--r-- | test/ruby/test_pack.rb | 110 |
1 files changed, 51 insertions, 59 deletions
diff --git a/test/ruby/test_pack.rb b/test/ruby/test_pack.rb index 9738f82b7e..1ce46e8916 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 = [-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 - $x = [-1073741825] - assert_equal($x, $x.pack("q").unpack("q")) + assert_raise(Encoding::CompatibilityError) do + "foo".unpack("C".encode("UTF-32BE")) + end - $x = [-1] - assert_equal($x, $x.pack("l").unpack("l")) + 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 @@ -428,7 +449,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 +471,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 +486,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) @@ -763,58 +781,32 @@ EXPECTED end def test_pack_garbage - verbose = $VERBOSE - $VERBOSE = false - - assert_silent do - assert_equal "\000", [0].pack("*U") - end - - $VERBOSE = true - - _, err = capture_output do + assert_raise(ArgumentError, %r%unknown pack directive '\*' in '\*U'$%) 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_raise(ArgumentError, %r%unknown unpack directive '\*' in '\*U'$%) do assert_equal [0], "\000".unpack("*U") end - - $VERBOSE = true - - _, err = capture_output 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 |