diff options
Diffstat (limited to 'test/ruby/test_pack.rb')
| -rw-r--r-- | test/ruby/test_pack.rb | 523 |
1 files changed, 439 insertions, 84 deletions
diff --git a/test/ruby/test_pack.rb b/test/ruby/test_pack.rb index 95d5ea3392..ca089f09c3 100644 --- a/test/ruby/test_pack.rb +++ b/test/ruby/test_pack.rb @@ -1,23 +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 @@ -70,73 +91,120 @@ class TestPack < Test::Unit::TestCase assert_equal [1,1,1], "\000\000\000\001\000\000\000\001\000\000\000\001".unpack('N*') end + def _integer_big_endian(mod='') + assert_equal("\x01\x02", [0x0102].pack("s"+mod)) + assert_equal("\x01\x02", [0x0102].pack("S"+mod)) + assert_equal("\x01\x02\x03\x04", [0x01020304].pack("l"+mod)) + 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)) + 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)) + 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 + assert_match(/\A\x00*\x01\x02\z/, [0x0102].pack("s!"+mod)) + assert_match(/\A\x00*\x01\x02\z/, [0x0102].pack("S!"+mod)) + assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("i"+mod)) + assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("I"+mod)) + assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("i!"+mod)) + 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)) + 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)) + 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 + %w[s S l L q Q j J s! S! i I i! I! l! L! j! J!].each {|fmt| + fmt += mod + nuls = [0].pack(fmt) + v = 0 + s = "".force_encoding("ascii-8bit") + nuls.bytesize.times {|i| + j = i + 40 + v = v * 256 + j + s << [j].pack("C") + } + assert_equal(s, [v].pack(fmt), "[#{v}].pack(#{fmt.dump})") + assert_equal([v], s.unpack(fmt), "#{s.dump}.unpack(#{fmt.dump})") + s2 = s+s + fmt2 = fmt+"*" + assert_equal([v,v], s2.unpack(fmt2), "#{s2.dump}.unpack(#{fmt2.dump})") + } + end + + def _integer_little_endian(mod='') + assert_equal("\x02\x01", [0x0102].pack("s"+mod)) + assert_equal("\x02\x01", [0x0102].pack("S"+mod)) + assert_equal("\x04\x03\x02\x01", [0x01020304].pack("l"+mod)) + 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)) + 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)) + 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 + assert_match(/\A\x02\x01\x00*\z/, [0x0102].pack("s!"+mod)) + assert_match(/\A\x02\x01\x00*\z/, [0x0102].pack("S!"+mod)) + assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("i"+mod)) + assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("I"+mod)) + assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("i!"+mod)) + 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)) + 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)) + 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 + %w[s S l L q Q j J s! S! i I i! I! l! L! j! J!].each {|fmt| + fmt += mod + nuls = [0].pack(fmt) + v = 0 + s = "".force_encoding("ascii-8bit") + nuls.bytesize.times {|i| + j = i+40 + v = v * 256 + j + s << [j].pack("C") + } + s.reverse! + assert_equal(s, [v].pack(fmt), "[#{v}].pack(#{fmt.dump})") + assert_equal([v], s.unpack(fmt), "#{s.dump}.unpack(#{fmt.dump})") + s2 = s+s + fmt2 = fmt+"*" + assert_equal([v,v], s2.unpack(fmt2), "#{s2.dump}.unpack(#{fmt2.dump})") + } + end + def test_integer_endian s = [1].pack("s") - assert_operator(["\0\1", "\1\0"], :include?, s) + assert_include(["\0\1", "\1\0"], s) if s == "\0\1" - # big endian - assert_equal("\x01\x02", [0x0102].pack("s")) - assert_equal("\x01\x02", [0x0102].pack("S")) - assert_equal("\x01\x02\x03\x04", [0x01020304].pack("l")) - assert_equal("\x01\x02\x03\x04", [0x01020304].pack("L")) - assert_equal("\x01\x02\x03\x04\x05\x06\x07\x08", [0x0102030405060708].pack("q")) - assert_equal("\x01\x02\x03\x04\x05\x06\x07\x08", [0x0102030405060708].pack("Q")) - assert_match(/\A\x00*\x01\x02\z/, [0x0102].pack("s!")) - assert_match(/\A\x00*\x01\x02\z/, [0x0102].pack("S!")) - assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("i")) - assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("I")) - assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("i!")) - assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("I!")) - assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("l!")) - assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("L!")) - %w[s S l L q Q s! S! i I i! I! l! L!].each {|fmt| - nuls = [0].pack(fmt) - v = 0 - s = "" - nuls.bytesize.times {|i| - j = i + 40 - v = v * 256 + j - s << [j].pack("C") - } - assert_equal(s, [v].pack(fmt), "[#{v}].pack(#{fmt.dump})") - assert_equal([v], s.unpack(fmt), "#{s.dump}.unpack(#{fmt.dump})") - s2 = s+s - fmt2 = fmt+"*" - assert_equal([v,v], s2.unpack(fmt2), "#{s2.dump}.unpack(#{fmt2.dump})") - } + _integer_big_endian() else - # little endian - assert_equal("\x02\x01", [0x0102].pack("s")) - assert_equal("\x02\x01", [0x0102].pack("S")) - assert_equal("\x04\x03\x02\x01", [0x01020304].pack("l")) - assert_equal("\x04\x03\x02\x01", [0x01020304].pack("L")) - assert_equal("\x08\x07\x06\x05\x04\x03\x02\x01", [0x0102030405060708].pack("q")) - assert_equal("\x08\x07\x06\x05\x04\x03\x02\x01", [0x0102030405060708].pack("Q")) - assert_match(/\A\x02\x01\x00*\z/, [0x0102].pack("s!")) - assert_match(/\A\x02\x01\x00*\z/, [0x0102].pack("S!")) - assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("i")) - assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("I")) - assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("i!")) - assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("I!")) - assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("l!")) - assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("L!")) - %w[s S l L q Q s! S! i I i! I! l! L!].each {|fmt| - nuls = [0].pack(fmt) - v = 0 - s = "" - nuls.bytesize.times {|i| - j = i+40 - v = v * 256 + j - s << [j].pack("C") - } - s.reverse! - assert_equal(s, [v].pack(fmt), "[#{v}].pack(#{fmt.dump})") - assert_equal([v], s.unpack(fmt), "#{s.dump}.unpack(#{fmt.dump})") - s2 = s+s - fmt2 = fmt+"*" - assert_equal([v,v], s2.unpack(fmt2), "#{s2.dump}.unpack(#{fmt2.dump})") - } + _integer_little_endian() end + assert_equal("\x01\x02\x02\x01", [0x0102,0x0102].pack("s>s<")) + assert_equal([0x0102,0x0102], "\x01\x02\x02\x01".unpack("s>s<")) + end + + def test_integer_endian_explicit + _integer_big_endian('>') + _integer_little_endian('<') end def test_pack_U @@ -144,9 +212,9 @@ class TestPack < Test::Unit::TestCase assert_raise(RangeError) { [-0x40000000].pack("U") } assert_raise(RangeError) { [-1].pack("U") } assert_equal "\000", [0].pack("U") - assert_equal "\374\277\277\277\277\277", [0x3fffffff].pack("U") - assert_equal "\375\200\200\200\200\200", [0x40000000].pack("U") - assert_equal "\375\277\277\277\277\277", [0x7fffffff].pack("U") + assert_equal "\374\277\277\277\277\277".force_encoding(Encoding::UTF_8), [0x3fffffff].pack("U") + assert_equal "\375\200\200\200\200\200".force_encoding(Encoding::UTF_8), [0x40000000].pack("U") + assert_equal "\375\277\277\277\277\277".force_encoding(Encoding::UTF_8), [0x7fffffff].pack("U") assert_raise(RangeError) { [0x80000000].pack("U") } assert_raise(RangeError) { [0x100000000].pack("U") } end @@ -165,13 +233,14 @@ class TestPack < Test::Unit::TestCase assert_equal a[0], a.pack("p").unpack("p")[0] assert_equal a, a.pack("p").freeze.unpack("p*") assert_raise(ArgumentError) { (a.pack("p") + "").unpack("p*") } + assert_equal a, (a.pack("p") << "d").unpack("p*") end def test_format_string_modified fmt = "CC" o = Object.new class << o; self; end.class_eval do - define_method(:to_int) { fmt.replace ""; 0 } + define_method(:to_int) { fmt.clear; 0 } end assert_raise(RuntimeError) do [o, o].pack(fmt) @@ -265,6 +334,9 @@ class TestPack < Test::Unit::TestCase assert_equal(["1"], "\x80".unpack("B1")) assert_equal(["10"], "\x80".unpack("B2")) assert_equal(["100"], "\x80".unpack("B3")) + + assert_equal(Encoding::US_ASCII, "\xff\x00".unpack("b*")[0].encoding) + assert_equal(Encoding::US_ASCII, "\xff\x00".unpack("B*")[0].encoding) end def test_pack_unpack_hH @@ -305,6 +377,9 @@ class TestPack < Test::Unit::TestCase assert_equal(["10e"], "\x10\xef".unpack("H3")) assert_equal(["10ef"], "\x10\xef".unpack("H4")) assert_equal(["10ef"], "\x10\xef".unpack("H5")) + + assert_equal(Encoding::US_ASCII, "\x10\xef".unpack("h*")[0].encoding) + assert_equal(Encoding::US_ASCII, "\x10\xef".unpack("H*")[0].encoding) end def test_pack_unpack_cC @@ -381,8 +456,53 @@ class TestPack < Test::Unit::TestCase assert_equal([578437695752307201, -506097522914230529], s2.unpack("q*")) assert_equal([578437695752307201, 17940646550795321087], s1.unpack("Q*")) + # Note: q! and Q! should not work on platform which has no long long type. + # Is there a such platform now? + # @shyouhei: Yes. gcc -ansi is one of such platform. + s1 = [578437695752307201, -506097522914230529].pack("q!*") + s2 = [578437695752307201, 17940646550795321087].pack("Q!*") + assert_equal([578437695752307201, -506097522914230529], s2.unpack("q!*")) + assert_equal([578437695752307201, 17940646550795321087], s1.unpack("Q!*")) + assert_equal(8, [1].pack("q").bytesize) assert_equal(8, [1].pack("Q").bytesize) + assert_operator(8, :<=, [1].pack("q!").bytesize) + assert_operator(8, :<=, [1].pack("Q!").bytesize) + end if RbConfig::CONFIG['HAVE_LONG_LONG'] + + def test_pack_unpack_jJ + case J_SIZE + when 4 + s1 = [67305985, -50462977].pack("j*") + s2 = [67305985, 4244504319].pack("J*") + assert_equal(s1, s2) + assert_equal([67305985, -50462977], s2.unpack("j*")) + assert_equal([67305985, 4244504319], s1.unpack("J*")) + + s1 = [67305985, -50462977].pack("j!*") + s2 = [67305985, 4244504319].pack("J!*") + assert_equal([67305985, -50462977], s1.unpack("j!*")) + assert_equal([67305985, 4244504319], s2.unpack("J!*")) + + assert_equal(4, [1].pack("j").bytesize) + assert_equal(4, [1].pack("J").bytesize) + when 8 + s1 = [578437695752307201, -506097522914230529].pack("j*") + s2 = [578437695752307201, 17940646550795321087].pack("J*") + assert_equal(s1, s2) + assert_equal([578437695752307201, -506097522914230529], s2.unpack("j*")) + assert_equal([578437695752307201, 17940646550795321087], s1.unpack("J*")) + + s1 = [578437695752307201, -506097522914230529].pack("j!*") + s2 = [578437695752307201, 17940646550795321087].pack("J!*") + assert_equal([578437695752307201, -506097522914230529], s2.unpack("j!*")) + assert_equal([578437695752307201, 17940646550795321087], s1.unpack("J!*")) + + assert_equal(8, [1].pack("j").bytesize) + assert_equal(8, [1].pack("J").bytesize) + else + assert false, "we don't know such platform now." + end end def test_pack_unpack_nN @@ -414,7 +534,7 @@ class TestPack < Test::Unit::TestCase %w(f d e E g G).each do |f| v = [x].pack(f).unpack(f) if x.nan? - assert(v.first.nan?) + assert_predicate(v.first, :nan?) else assert_equal([x], v) end @@ -448,6 +568,9 @@ class TestPack < Test::Unit::TestCase assert_equal([1, 2], "\x01\x00\x00\x02".unpack("C@3C")) assert_equal([nil], "\x00".unpack("@1C")) # is it OK? assert_raise(ArgumentError) { "\x00".unpack("@2C") } + + pos = RbConfig::LIMITS["UINTPTR_MAX"] - 99 # -100 + assert_raise(RangeError) {"0123456789".unpack("@#{pos}C10")} end def test_pack_unpack_percent @@ -481,6 +604,10 @@ class TestPack < Test::Unit::TestCase assert_equal("M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n!80``\n", ["a"*46].pack("u0")) assert_equal("M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n!80``\n", ["a"*46].pack("u1")) assert_equal("M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n!80``\n", ["a"*46].pack("u2")) + assert_equal(<<EXPECTED, ["a"*80].pack("u68")) +_86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A +186%A86%A86%A86%A86%A86$` +EXPECTED assert_equal([""], "".unpack("u")) assert_equal(["a"], "!80``\n".unpack("u")) @@ -490,6 +617,11 @@ class TestPack < Test::Unit::TestCase assert_equal(["a"*46], "M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n!80``\n".unpack("u")) assert_equal(["abcdefghi"], "&86)C9&5F\n#9VAI\n".unpack("u")) + assert_equal(["abcdef"], "#86)C\n#9&5F\n".unpack("u")) + assert_equal(["abcdef"], "#86)CX\n#9&5FX\n".unpack("u")) # X is a (dummy) checksum. + assert_equal(["abcdef"], "#86)C\r\n#9&5F\r\n".unpack("u")) + assert_equal(["abcdef"], "#86)CX\r\n#9&5FX\r\n".unpack("u")) # X is a (dummy) checksum. + assert_equal(["\x00"], "\"\n".unpack("u")) assert_equal(["\x00"], "! \r \n".unpack("u")) end @@ -510,6 +642,56 @@ class TestPack < Test::Unit::TestCase assert_equal(["\377"], "/w==\n".unpack("m")) assert_equal(["\377\377"], "//8=\n".unpack("m")) assert_equal(["\377\377\377"], "////\n".unpack("m")) + assert_equal([""], "A\n".unpack("m")) + assert_equal(["\0"], "AA\n".unpack("m")) + assert_equal(["\0"], "AA=\n".unpack("m")) + assert_equal(["\0\0"], "AAA\n".unpack("m")) + + bug10019 = '[ruby-core:63604] [Bug #10019]' + size = ((4096-4)/4*3+1) + assert_separately(%W[- #{size} #{bug10019}], <<-'end;') + size = ARGV.shift.to_i + bug = ARGV.shift + assert_equal(size, ["a"*size].pack("m#{size+2}").unpack("m")[0].size, bug) + 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")) + assert_equal("AAA=", ["\0\0"].pack("m0")) + assert_equal("AAAA", ["\0\0\0"].pack("m0")) + assert_equal("/w==", ["\377"].pack("m0")) + assert_equal("//8=", ["\377\377"].pack("m0")) + assert_equal("////", ["\377\377\377"].pack("m0")) + + assert_equal([""], "".unpack("m0")) + assert_equal(["\0"], "AA==".unpack("m0")) + assert_equal(["\0\0"], "AAA=".unpack("m0")) + assert_equal(["\0\0\0"], "AAAA".unpack("m0")) + assert_equal(["\377"], "/w==".unpack("m0")) + assert_equal(["\377\377"], "//8=".unpack("m0")) + assert_equal(["\377\377\377"], "////".unpack("m0")) + + assert_raise(ArgumentError) { "^".unpack("m0") } + assert_raise(ArgumentError) { "A".unpack("m0") } + assert_raise(ArgumentError) { "A^".unpack("m0") } + assert_raise(ArgumentError) { "AA".unpack("m0") } + assert_raise(ArgumentError) { "AA=".unpack("m0") } + assert_raise(ArgumentError) { "AA===".unpack("m0") } + assert_raise(ArgumentError) { "AA=x".unpack("m0") } + assert_raise(ArgumentError) { "AAA".unpack("m0") } + assert_raise(ArgumentError) { "AAA^".unpack("m0") } + assert_raise(ArgumentError) { "AB==".unpack("m0") } + assert_raise(ArgumentError) { "AAB=".unpack("m0") } end def test_pack_unpack_M @@ -523,8 +705,23 @@ class TestPack < Test::Unit::TestCase assert_equal(["a"*1023], (("a"*73+"=\n")*14+"a=\n").unpack("M")) assert_equal(["\x0a"], "=0a=\n".unpack("M")) assert_equal(["\x0a"], "=0A=\n".unpack("M")) - assert_equal([""], "=0Z=\n".unpack("M")) + assert_equal(["=0Z=\n"], "=0Z=\n".unpack("M")) assert_equal([""], "=\r\n".unpack("M")) + assert_equal(["\xC6\xF7"], "=C6=F7".unpack('M*')) + + assert_equal(["pre123after"], "pre=31=32=33after".unpack("M")) + assert_equal(["preafter"], "pre=\nafter".unpack("M")) + assert_equal(["preafter"], "pre=\r\nafter".unpack("M")) + assert_equal(["pre="], "pre=".unpack("M")) + assert_equal(["pre=\r"], "pre=\r".unpack("M")) + assert_equal(["pre=hoge"], "pre=hoge".unpack("M")) + assert_equal(["pre==31after"], "pre==31after".unpack("M")) + assert_equal(["pre===31after"], "pre===31after".unpack("M")) + + bug = '[ruby-core:83055] [Bug #13949]' + s = "abcdef".unpack1("M") + assert_equal(Encoding::ASCII_8BIT, s.encoding) + assert_predicate(s, :ascii_only?, bug) end def test_pack_unpack_P2 @@ -565,14 +762,8 @@ class TestPack < Test::Unit::TestCase assert_equal([0x100000000], "\220\200\200\200\000".unpack("w"), [0x100000000]) end - def test_modify_under_safe4 - s = "foo" - assert_raise(SecurityError) do - Thread.new do - $SAFE = 4 - s.replace "" - end.join - end + def test_length_too_big + assert_raise(RangeError) { [].pack("C100000000000000000000") } end def test_short_string @@ -581,4 +772,168 @@ class TestPack < Test::Unit::TestCase assert_equal([1,nil], str.unpack("#{fmt}2")) } end + + def test_short_with_block + bug4059 = '[ruby-core:33193]' + result = :ok + assert_nil("".unpack("i") {|x| result = x}, bug4059) + assert_equal(:ok, result) + end + + def test_pack_garbage + assert_raise(ArgumentError, %r%unknown pack directive '\*' in '\*U'$%) do + assert_equal "\000", [0].pack("*U") + end + end + + def test_unpack_garbage + assert_raise(ArgumentError, %r%unknown unpack directive '\*' in '\*U'$%) do + assert_equal [0], "\000".unpack("*U") + end + end + + def test_invalid_warning + assert_raise(ArgumentError, /unknown pack directive ',' in ','/) { + [].pack(",") + } + assert_raise(ArgumentError, /\A[ -~]+\Z/) { + [].pack("\x7f") + } + assert_raise(ArgumentError, /\A(.* in '\u{3042}'\n)+\z/) { + [].pack("\u{3042}") + } + + assert_raise(ArgumentError, /\A.* in '.*U'\Z/) { + assert_equal "\000", [0].pack("\0U") + } + assert_raise(ArgumentError, /\A.* in '.*U'\Z/) { + "\000".unpack("\0U") + } + end + + def test_pack_resize + assert_separately([], <<-'end;') + ary = [] + obj = Class.new { + define_method(:to_str) { + ary.clear() + ary = nil + GC.start + "TALOS" + } + }.new + + ary.push(obj) + ary.push(".") + + assert_raise_with_message(ArgumentError, /too few/) {ary.pack("AA")} + end; + end + + def test_pack_with_buffer + buf = String.new(capacity: 100) + + assert_raise_with_message(FrozenError, /frozen/) { + [0xDEAD_BEEF].pack('N', buffer: 'foo'.freeze) + } + assert_raise_with_message(TypeError, /must be String/) { + [0xDEAD_BEEF].pack('N', buffer: Object.new) + } + + addr = [buf].pack('p') + + [0xDEAD_BEEF].pack('N', buffer: buf) + assert_equal "\xDE\xAD\xBE\xEF", buf + + [0xBABE_F00D].pack('@4N', buffer: buf) + assert_equal "\xDE\xAD\xBE\xEF\xBA\xBE\xF0\x0D", buf + assert_equal addr, [buf].pack('p') + + [0xBAAD_FACE].pack('@10N', buffer: buf) + assert_equal "\xDE\xAD\xBE\xEF\xBA\xBE\xF0\x0D\0\0\xBA\xAD\xFA\xCE", buf + + assert_equal addr, [buf].pack('p') + end + + def test_unpack_with_block + ret = []; "ABCD".unpack("CCCC") {|v| ret << v } + assert_equal [65, 66, 67, 68], ret + ret = []; "A".unpack("B*") {|v| ret << v.dup } + assert_equal ["01000001"], ret + end + + def test_unpack1 + assert_equal 65, "A".unpack1("C") + assert_equal 68, "ABCD".unpack1("x3C") + assert_equal 0x3042, "\u{3042 3044 3046}".unpack1("U*") + 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 "01000001", "YZA".unpack1("B*", offset: 2) + assert_nil "abc".unpack1("C", offset: 3) + assert_raise_with_message(ArgumentError, /offset can't be negative/) { + "a".unpack1("C", offset: -1) + } + 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 ["01000001"], "YZA".unpack("B*", offset: 2) + assert_equal [nil, nil, nil], "abc".unpack("CCC", offset: 3) + assert_raise_with_message(ArgumentError, /offset can't be negative/) { + "a".unpack("C", offset: -1) + } + 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 end |
