2012-11-05 19:49:57 -05:00
|
|
|
# coding: US-ASCII
|
2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-09-04 12:18:59 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestPack < Test::Unit::TestCase
|
|
|
|
def test_pack
|
|
|
|
$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)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-05 07:28:27 -04:00
|
|
|
assert_equal(ary.length, ary2.length)
|
|
|
|
assert_equal(ary.join(':'), ary2.join(':'))
|
2003-09-06 13:27:58 -04:00
|
|
|
assert_match(/def/, $x)
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
$x = [-1073741825]
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal($x, $x.pack("q").unpack("q"))
|
2006-02-16 21:21:39 -05:00
|
|
|
|
|
|
|
$x = [-1]
|
|
|
|
assert_equal($x, $x.pack("l").unpack("l"))
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
2003-12-26 00:05:37 -05:00
|
|
|
|
2010-02-25 09:00:48 -05:00
|
|
|
def test_pack_n
|
|
|
|
assert_equal "\000\000", [0].pack('n')
|
|
|
|
assert_equal "\000\001", [1].pack('n')
|
|
|
|
assert_equal "\000\002", [2].pack('n')
|
|
|
|
assert_equal "\000\003", [3].pack('n')
|
|
|
|
assert_equal "\377\376", [65534].pack('n')
|
|
|
|
assert_equal "\377\377", [65535].pack('n')
|
|
|
|
|
|
|
|
assert_equal "\200\000", [2**15].pack('n')
|
|
|
|
assert_equal "\177\377", [-2**15-1].pack('n')
|
|
|
|
assert_equal "\377\377", [-1].pack('n')
|
|
|
|
|
|
|
|
assert_equal "\000\001\000\001", [1,1].pack('n*')
|
|
|
|
assert_equal "\000\001\000\001\000\001", [1,1,1].pack('n*')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unpack_n
|
|
|
|
assert_equal 1, "\000\001".unpack('n')[0]
|
|
|
|
assert_equal 2, "\000\002".unpack('n')[0]
|
|
|
|
assert_equal 3, "\000\003".unpack('n')[0]
|
|
|
|
assert_equal 65535, "\377\377".unpack('n')[0]
|
|
|
|
assert_equal [1,1], "\000\001\000\001".unpack('n*')
|
|
|
|
assert_equal [1,1,1], "\000\001\000\001\000\001".unpack('n*')
|
|
|
|
end
|
|
|
|
|
2003-12-26 00:05:37 -05:00
|
|
|
def test_pack_N
|
|
|
|
assert_equal "\000\000\000\000", [0].pack('N')
|
|
|
|
assert_equal "\000\000\000\001", [1].pack('N')
|
|
|
|
assert_equal "\000\000\000\002", [2].pack('N')
|
|
|
|
assert_equal "\000\000\000\003", [3].pack('N')
|
|
|
|
assert_equal "\377\377\377\376", [4294967294].pack('N')
|
|
|
|
assert_equal "\377\377\377\377", [4294967295].pack('N')
|
|
|
|
|
2004-01-22 13:06:38 -05:00
|
|
|
assert_equal "\200\000\000\000", [2**31].pack('N')
|
|
|
|
assert_equal "\177\377\377\377", [-2**31-1].pack('N')
|
|
|
|
assert_equal "\377\377\377\377", [-1].pack('N')
|
|
|
|
|
2003-12-26 00:05:37 -05:00
|
|
|
assert_equal "\000\000\000\001\000\000\000\001", [1,1].pack('N*')
|
|
|
|
assert_equal "\000\000\000\001\000\000\000\001\000\000\000\001", [1,1,1].pack('N*')
|
|
|
|
end
|
2004-01-04 13:01:06 -05:00
|
|
|
|
|
|
|
def test_unpack_N
|
|
|
|
assert_equal 1, "\000\000\000\001".unpack('N')[0]
|
|
|
|
assert_equal 2, "\000\000\000\002".unpack('N')[0]
|
|
|
|
assert_equal 3, "\000\000\000\003".unpack('N')[0]
|
|
|
|
assert_equal 4294967295, "\377\377\377\377".unpack('N')[0]
|
|
|
|
assert_equal [1,1], "\000\000\000\001\000\000\000\001".unpack('N*')
|
|
|
|
assert_equal [1,1,1], "\000\000\000\001\000\000\000\001\000\000\000\001".unpack('N*')
|
|
|
|
end
|
2004-03-31 20:53:32 -05:00
|
|
|
|
2010-10-14 09:12:56 -04:00
|
|
|
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))
|
2015-06-12 08:22:36 -04:00
|
|
|
psize = [nil].pack('p').bytesize
|
|
|
|
if psize == 4
|
|
|
|
assert_equal("\x01\x02\x03\x04", [0x01020304].pack("j"+mod))
|
|
|
|
assert_equal("\x01\x02\x03\x04", [0x01020304].pack("J"+mod))
|
|
|
|
elsif psize == 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
|
2010-10-14 09:12:56 -04:00
|
|
|
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))
|
2015-06-12 08:22:36 -04:00
|
|
|
if psize == 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
|
|
|
|
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|
|
2010-10-14 09:12:56 -04:00
|
|
|
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))
|
2015-06-12 08:22:36 -04:00
|
|
|
psize = [nil].pack('p').bytesize
|
|
|
|
if psize == 4
|
|
|
|
assert_equal("\x04\x03\x02\x01", [0x01020304].pack("j"+mod))
|
|
|
|
assert_equal("\x04\x03\x02\x01", [0x01020304].pack("J"+mod))
|
|
|
|
elsif psize == 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
|
2010-10-14 09:12:56 -04:00
|
|
|
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))
|
2015-06-12 08:22:36 -04:00
|
|
|
if psize == 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
|
|
|
|
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|
|
2010-10-14 09:12:56 -04:00
|
|
|
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
|
|
|
|
|
2010-02-25 09:00:48 -05:00
|
|
|
def test_integer_endian
|
|
|
|
s = [1].pack("s")
|
2011-02-12 09:17:54 -05:00
|
|
|
assert_include(["\0\1", "\1\0"], s)
|
2010-02-25 09:00:48 -05:00
|
|
|
if s == "\0\1"
|
2010-10-14 09:12:56 -04:00
|
|
|
_integer_big_endian()
|
2010-02-25 09:00:48 -05:00
|
|
|
else
|
2010-10-14 09:12:56 -04:00
|
|
|
_integer_little_endian()
|
2010-02-25 09:00:48 -05:00
|
|
|
end
|
2010-10-15 03:42:21 -04:00
|
|
|
assert_equal("\x01\x02\x02\x01", [0x0102,0x0102].pack("s>s<"))
|
|
|
|
assert_equal([0x0102,0x0102], "\x01\x02\x02\x01".unpack("s>s<"))
|
2010-02-25 09:00:48 -05:00
|
|
|
end
|
|
|
|
|
2010-10-14 09:12:56 -04:00
|
|
|
def test_integer_endian_explicit
|
|
|
|
_integer_big_endian('>')
|
|
|
|
_integer_little_endian('<')
|
|
|
|
end
|
|
|
|
|
2004-03-31 20:53:32 -05:00
|
|
|
def test_pack_U
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(RangeError) { [-0x40000001].pack("U") }
|
|
|
|
assert_raise(RangeError) { [-0x40000000].pack("U") }
|
|
|
|
assert_raise(RangeError) { [-1].pack("U") }
|
2004-03-31 20:53:32 -05:00
|
|
|
assert_equal "\000", [0].pack("U")
|
2008-10-29 05:40:38 -04:00
|
|
|
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")
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(RangeError) { [0x80000000].pack("U") }
|
|
|
|
assert_raise(RangeError) { [0x100000000].pack("U") }
|
2004-03-31 20:53:32 -05:00
|
|
|
end
|
2008-01-23 01:04:13 -05:00
|
|
|
|
|
|
|
def test_pack_P
|
|
|
|
a = ["abc"]
|
|
|
|
assert_equal a, a.pack("P").unpack("P*")
|
|
|
|
assert_equal "a", a.pack("P").unpack("P")[0]
|
|
|
|
assert_equal a, a.pack("P").freeze.unpack("P*")
|
2008-05-15 10:37:18 -04:00
|
|
|
assert_raise(ArgumentError) { (a.pack("P") + "").unpack("P*") }
|
2008-01-23 01:04:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_p
|
|
|
|
a = ["abc"]
|
|
|
|
assert_equal a, a.pack("p").unpack("p*")
|
|
|
|
assert_equal a[0], a.pack("p").unpack("p")[0]
|
|
|
|
assert_equal a, a.pack("p").freeze.unpack("p*")
|
2008-05-15 10:37:18 -04:00
|
|
|
assert_raise(ArgumentError) { (a.pack("p") + "").unpack("p*") }
|
2014-02-04 00:07:21 -05:00
|
|
|
assert_equal a, (a.pack("p") << "d").unpack("p*")
|
2008-01-23 01:04:13 -05:00
|
|
|
end
|
2008-01-31 09:08:14 -05:00
|
|
|
|
|
|
|
def test_format_string_modified
|
|
|
|
fmt = "CC"
|
|
|
|
o = Object.new
|
|
|
|
class << o; self; end.class_eval do
|
|
|
|
define_method(:to_int) { fmt.clear; 0 }
|
|
|
|
end
|
|
|
|
assert_raise(RuntimeError) do
|
|
|
|
[o, o].pack(fmt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_comment
|
|
|
|
assert_equal("\0\1", [0,1].pack(" C #foo \n C "))
|
|
|
|
assert_equal([0,1], "\0\1".unpack(" C #foo \n C "))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_illegal_bang
|
|
|
|
assert_raise(ArgumentError) { [].pack("a!") }
|
|
|
|
assert_raise(ArgumentError) { "".unpack("a!") }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_aA
|
|
|
|
assert_equal("f", ["foo"].pack("A"))
|
|
|
|
assert_equal("f", ["foo"].pack("a"))
|
|
|
|
assert_equal("foo", ["foo"].pack("A*"))
|
|
|
|
assert_equal("foo", ["foo"].pack("a*"))
|
|
|
|
assert_equal("fo", ["foo"].pack("A2"))
|
|
|
|
assert_equal("fo", ["foo"].pack("a2"))
|
|
|
|
assert_equal("foo ", ["foo"].pack("A4"))
|
|
|
|
assert_equal("foo\0", ["foo"].pack("a4"))
|
|
|
|
assert_equal(" ", [nil].pack("A"))
|
|
|
|
assert_equal("\0", [nil].pack("a"))
|
|
|
|
assert_equal("", [nil].pack("A*"))
|
|
|
|
assert_equal("", [nil].pack("a*"))
|
|
|
|
assert_equal(" ", [nil].pack("A2"))
|
|
|
|
assert_equal("\0\0", [nil].pack("a2"))
|
|
|
|
|
|
|
|
assert_equal("foo" + "\0" * 27, ["foo"].pack("a30"))
|
|
|
|
|
|
|
|
assert_equal(["f"], "foo\0".unpack("A"))
|
|
|
|
assert_equal(["f"], "foo\0".unpack("a"))
|
|
|
|
assert_equal(["foo"], "foo\0".unpack("A4"))
|
|
|
|
assert_equal(["foo\0"], "foo\0".unpack("a4"))
|
|
|
|
assert_equal(["foo"], "foo ".unpack("A4"))
|
|
|
|
assert_equal(["foo "], "foo ".unpack("a4"))
|
|
|
|
assert_equal(["foo"], "foo".unpack("A4"))
|
|
|
|
assert_equal(["foo"], "foo".unpack("a4"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_Z
|
|
|
|
assert_equal("f", ["foo"].pack("Z"))
|
|
|
|
assert_equal("foo\0", ["foo"].pack("Z*"))
|
|
|
|
assert_equal("fo", ["foo"].pack("Z2"))
|
|
|
|
assert_equal("foo\0\0", ["foo"].pack("Z5"))
|
|
|
|
assert_equal("\0", [nil].pack("Z"))
|
|
|
|
assert_equal("\0", [nil].pack("Z*"))
|
|
|
|
assert_equal("\0\0", [nil].pack("Z2"))
|
|
|
|
|
|
|
|
assert_equal(["f"], "foo\0".unpack("Z"))
|
|
|
|
assert_equal(["foo"], "foo".unpack("Z*"))
|
|
|
|
assert_equal(["foo"], "foo\0".unpack("Z*"))
|
|
|
|
assert_equal(["foo"], "foo".unpack("Z5"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_bB
|
|
|
|
assert_equal("\xff\x00", ["1111111100000000"].pack("b*"))
|
|
|
|
assert_equal("\x01\x02", ["1000000001000000"].pack("b*"))
|
|
|
|
assert_equal("", ["1"].pack("b0"))
|
|
|
|
assert_equal("\x01", ["1"].pack("b1"))
|
|
|
|
assert_equal("\x01\x00", ["1"].pack("b2"))
|
|
|
|
assert_equal("\x01\x00", ["1"].pack("b3"))
|
|
|
|
assert_equal("\x01\x00\x00", ["1"].pack("b4"))
|
|
|
|
assert_equal("\x01\x00\x00", ["1"].pack("b5"))
|
|
|
|
assert_equal("\x01\x00\x00\x00", ["1"].pack("b6"))
|
|
|
|
|
|
|
|
assert_equal("\xff\x00", ["1111111100000000"].pack("B*"))
|
|
|
|
assert_equal("\x01\x02", ["0000000100000010"].pack("B*"))
|
|
|
|
assert_equal("", ["1"].pack("B0"))
|
|
|
|
assert_equal("\x80", ["1"].pack("B1"))
|
|
|
|
assert_equal("\x80\x00", ["1"].pack("B2"))
|
|
|
|
assert_equal("\x80\x00", ["1"].pack("B3"))
|
|
|
|
assert_equal("\x80\x00\x00", ["1"].pack("B4"))
|
|
|
|
assert_equal("\x80\x00\x00", ["1"].pack("B5"))
|
|
|
|
assert_equal("\x80\x00\x00\x00", ["1"].pack("B6"))
|
|
|
|
|
|
|
|
assert_equal(["1111111100000000"], "\xff\x00".unpack("b*"))
|
|
|
|
assert_equal(["1000000001000000"], "\x01\x02".unpack("b*"))
|
|
|
|
assert_equal([""], "".unpack("b0"))
|
|
|
|
assert_equal(["1"], "\x01".unpack("b1"))
|
|
|
|
assert_equal(["10"], "\x01".unpack("b2"))
|
|
|
|
assert_equal(["100"], "\x01".unpack("b3"))
|
|
|
|
|
|
|
|
assert_equal(["1111111100000000"], "\xff\x00".unpack("B*"))
|
|
|
|
assert_equal(["0000000100000010"], "\x01\x02".unpack("B*"))
|
|
|
|
assert_equal([""], "".unpack("B0"))
|
|
|
|
assert_equal(["1"], "\x80".unpack("B1"))
|
|
|
|
assert_equal(["10"], "\x80".unpack("B2"))
|
|
|
|
assert_equal(["100"], "\x80".unpack("B3"))
|
2012-10-19 09:13:32 -04:00
|
|
|
|
|
|
|
assert_equal(Encoding::US_ASCII, "\xff\x00".unpack("b*")[0].encoding)
|
|
|
|
assert_equal(Encoding::US_ASCII, "\xff\x00".unpack("B*")[0].encoding)
|
2008-01-31 09:08:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_hH
|
|
|
|
assert_equal("\x01\xfe", ["10ef"].pack("h*"))
|
|
|
|
assert_equal("", ["10ef"].pack("h0"))
|
|
|
|
assert_equal("\x01\x0e", ["10ef"].pack("h3"))
|
|
|
|
assert_equal("\x01\xfe\x0", ["10ef"].pack("h5"))
|
2008-12-08 04:31:44 -05:00
|
|
|
assert_equal("\xff\x0f", ["fff"].pack("h3"))
|
|
|
|
assert_equal("\xff\x0f", ["fff"].pack("h4"))
|
|
|
|
assert_equal("\xff\x0f\0", ["fff"].pack("h5"))
|
|
|
|
assert_equal("\xff\x0f\0", ["fff"].pack("h6"))
|
|
|
|
assert_equal("\xff\x0f\0\0", ["fff"].pack("h7"))
|
|
|
|
assert_equal("\xff\x0f\0\0", ["fff"].pack("h8"))
|
2008-01-31 09:08:14 -05:00
|
|
|
|
|
|
|
assert_equal("\x10\xef", ["10ef"].pack("H*"))
|
|
|
|
assert_equal("", ["10ef"].pack("H0"))
|
|
|
|
assert_equal("\x10\xe0", ["10ef"].pack("H3"))
|
|
|
|
assert_equal("\x10\xef\x0", ["10ef"].pack("H5"))
|
2008-12-08 04:31:44 -05:00
|
|
|
assert_equal("\xff\xf0", ["fff"].pack("H3"))
|
|
|
|
assert_equal("\xff\xf0", ["fff"].pack("H4"))
|
|
|
|
assert_equal("\xff\xf0\0", ["fff"].pack("H5"))
|
|
|
|
assert_equal("\xff\xf0\0", ["fff"].pack("H6"))
|
|
|
|
assert_equal("\xff\xf0\0\0", ["fff"].pack("H7"))
|
|
|
|
assert_equal("\xff\xf0\0\0", ["fff"].pack("H8"))
|
2008-01-31 09:08:14 -05:00
|
|
|
|
|
|
|
assert_equal(["10ef"], "\x01\xfe".unpack("h*"))
|
|
|
|
assert_equal([""], "\x01\xfe".unpack("h0"))
|
|
|
|
assert_equal(["1"], "\x01\xfe".unpack("h1"))
|
|
|
|
assert_equal(["10"], "\x01\xfe".unpack("h2"))
|
|
|
|
assert_equal(["10e"], "\x01\xfe".unpack("h3"))
|
|
|
|
assert_equal(["10ef"], "\x01\xfe".unpack("h4"))
|
|
|
|
assert_equal(["10ef"], "\x01\xfe".unpack("h5"))
|
|
|
|
|
|
|
|
assert_equal(["10ef"], "\x10\xef".unpack("H*"))
|
|
|
|
assert_equal([""], "\x10\xef".unpack("H0"))
|
|
|
|
assert_equal(["1"], "\x10\xef".unpack("H1"))
|
|
|
|
assert_equal(["10"], "\x10\xef".unpack("H2"))
|
|
|
|
assert_equal(["10e"], "\x10\xef".unpack("H3"))
|
|
|
|
assert_equal(["10ef"], "\x10\xef".unpack("H4"))
|
|
|
|
assert_equal(["10ef"], "\x10\xef".unpack("H5"))
|
2012-10-19 09:13:32 -04:00
|
|
|
|
|
|
|
assert_equal(Encoding::US_ASCII, "\x10\xef".unpack("h*")[0].encoding)
|
|
|
|
assert_equal(Encoding::US_ASCII, "\x10\xef".unpack("H*")[0].encoding)
|
2008-01-31 09:08:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_cC
|
|
|
|
assert_equal("\0\1\xff", [0, 1, 255].pack("c*"))
|
|
|
|
assert_equal("\0\1\xff", [0, 1, -1].pack("c*"))
|
|
|
|
|
|
|
|
assert_equal("\0\1\xff", [0, 1, 255].pack("C*"))
|
|
|
|
assert_equal("\0\1\xff", [0, 1, -1].pack("C*"))
|
|
|
|
|
|
|
|
assert_equal([0, 1, -1], "\0\1\xff".unpack("c*"))
|
|
|
|
|
|
|
|
assert_equal([0, 1, 255], "\0\1\xff".unpack("C*"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_sS
|
|
|
|
s1 = [513, -514].pack("s*")
|
|
|
|
s2 = [513, 65022].pack("S*")
|
|
|
|
assert_equal(s1, s2)
|
|
|
|
assert_equal([513, -514], s2.unpack("s*"))
|
|
|
|
assert_equal([513, 65022], s1.unpack("S*"))
|
|
|
|
|
|
|
|
s1 = [513, -514].pack("s!*")
|
|
|
|
s2 = [513, 65022].pack("S!*")
|
|
|
|
assert_equal(s1, s2)
|
|
|
|
assert_equal([513, -514], s2.unpack("s!*"))
|
|
|
|
assert_equal([513, 65022], s1.unpack("S!*"))
|
2010-02-25 09:00:48 -05:00
|
|
|
|
|
|
|
assert_equal(2, [1].pack("s").bytesize)
|
|
|
|
assert_equal(2, [1].pack("S").bytesize)
|
|
|
|
assert_operator(2, :<=, [1].pack("s!").bytesize)
|
|
|
|
assert_operator(2, :<=, [1].pack("S!").bytesize)
|
2008-01-31 09:08:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_iI
|
|
|
|
s1 = [67305985, -50462977].pack("i*")
|
|
|
|
s2 = [67305985, 4244504319].pack("I*")
|
|
|
|
assert_equal(s1, s2)
|
|
|
|
assert_equal([67305985, -50462977], s2.unpack("i*"))
|
|
|
|
assert_equal([67305985, 4244504319], s1.unpack("I*"))
|
|
|
|
|
|
|
|
s1 = [67305985, -50462977].pack("i!*")
|
|
|
|
s2 = [67305985, 4244504319].pack("I!*")
|
2008-02-10 16:52:25 -05:00
|
|
|
assert_equal([67305985, -50462977], s1.unpack("i!*"))
|
|
|
|
assert_equal([67305985, 4244504319], s2.unpack("I!*"))
|
2010-02-25 09:00:48 -05:00
|
|
|
|
|
|
|
assert_operator(4, :<=, [1].pack("i").bytesize)
|
|
|
|
assert_operator(4, :<=, [1].pack("I").bytesize)
|
|
|
|
assert_operator(4, :<=, [1].pack("i!").bytesize)
|
|
|
|
assert_operator(4, :<=, [1].pack("I!").bytesize)
|
2008-01-31 09:08:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_lL
|
|
|
|
s1 = [67305985, -50462977].pack("l*")
|
|
|
|
s2 = [67305985, 4244504319].pack("L*")
|
|
|
|
assert_equal(s1, s2)
|
|
|
|
assert_equal([67305985, -50462977], s2.unpack("l*"))
|
|
|
|
assert_equal([67305985, 4244504319], s1.unpack("L*"))
|
|
|
|
|
|
|
|
s1 = [67305985, -50462977].pack("l!*")
|
|
|
|
s2 = [67305985, 4244504319].pack("L!*")
|
2008-02-10 16:52:25 -05:00
|
|
|
assert_equal([67305985, -50462977], s1.unpack("l!*"))
|
|
|
|
assert_equal([67305985, 4244504319], s2.unpack("L!*"))
|
2010-02-25 09:00:48 -05:00
|
|
|
|
|
|
|
assert_equal(4, [1].pack("l").bytesize)
|
|
|
|
assert_equal(4, [1].pack("L").bytesize)
|
|
|
|
assert_operator(4, :<=, [1].pack("l!").bytesize)
|
|
|
|
assert_operator(4, :<=, [1].pack("L!").bytesize)
|
2008-01-31 09:08:14 -05:00
|
|
|
end
|
|
|
|
|
2018-01-02 01:41:47 -05:00
|
|
|
require 'rbconfig'
|
2008-01-31 09:08:14 -05:00
|
|
|
def test_pack_unpack_qQ
|
|
|
|
s1 = [578437695752307201, -506097522914230529].pack("q*")
|
|
|
|
s2 = [578437695752307201, 17940646550795321087].pack("Q*")
|
|
|
|
assert_equal(s1, s2)
|
|
|
|
assert_equal([578437695752307201, -506097522914230529], s2.unpack("q*"))
|
|
|
|
assert_equal([578437695752307201, 17940646550795321087], s1.unpack("Q*"))
|
2010-02-25 09:00:48 -05:00
|
|
|
|
2013-04-03 08:36:57 -04:00
|
|
|
# Note: q! and Q! should not work on platform which has no long long type.
|
2013-04-05 20:10:22 -04:00
|
|
|
# Is there a such platform now?
|
2018-01-02 01:41:47 -05:00
|
|
|
# @shyouhei: Yes. gcc -ansi is one of such platform.
|
2013-04-02 07:28:57 -04:00
|
|
|
s1 = [578437695752307201, -506097522914230529].pack("q!*")
|
|
|
|
s2 = [578437695752307201, 17940646550795321087].pack("Q!*")
|
|
|
|
assert_equal([578437695752307201, -506097522914230529], s2.unpack("q!*"))
|
|
|
|
assert_equal([578437695752307201, 17940646550795321087], s1.unpack("Q!*"))
|
|
|
|
|
2010-02-25 09:00:48 -05:00
|
|
|
assert_equal(8, [1].pack("q").bytesize)
|
|
|
|
assert_equal(8, [1].pack("Q").bytesize)
|
2013-04-02 07:28:57 -04:00
|
|
|
assert_operator(8, :<=, [1].pack("q!").bytesize)
|
|
|
|
assert_operator(8, :<=, [1].pack("Q!").bytesize)
|
2018-01-02 01:41:47 -05:00
|
|
|
end if RbConfig::CONFIG['HAVE_LONG_LONG']
|
2008-01-31 09:08:14 -05:00
|
|
|
|
2015-06-12 08:22:36 -04:00
|
|
|
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
|
|
|
|
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)
|
|
|
|
elsif psize == 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
|
|
|
|
|
2008-01-31 09:08:14 -05:00
|
|
|
def test_pack_unpack_nN
|
|
|
|
assert_equal("\000\000\000\001\377\377\177\377\200\000\377\377", [0,1,-1,32767,-32768,65535].pack("n*"))
|
|
|
|
assert_equal("\000\000\000\000\000\000\000\001\377\377\377\377", [0,1,-1].pack("N*"))
|
|
|
|
|
|
|
|
assert_equal([0,1,65535,32767,32768,65535], "\000\000\000\001\377\377\177\377\200\000\377\377".unpack("n*"))
|
|
|
|
assert_equal([0,1,4294967295], "\000\000\000\000\000\000\000\001\377\377\377\377".unpack("N*"))
|
2010-02-25 09:00:48 -05:00
|
|
|
|
|
|
|
assert_equal(2, [1].pack("n").bytesize)
|
|
|
|
assert_equal(4, [1].pack("N").bytesize)
|
2008-01-31 09:08:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_vV
|
|
|
|
assert_equal("\000\000\001\000\377\377\377\177\000\200\377\377", [0,1,-1,32767,-32768,65535].pack("v*"))
|
|
|
|
assert_equal("\000\000\000\000\001\000\000\000\377\377\377\377", [0,1,-1].pack("V*"))
|
|
|
|
|
|
|
|
assert_equal([0,1,65535,32767,32768,65535], "\000\000\001\000\377\377\377\177\000\200\377\377".unpack("v*"))
|
|
|
|
assert_equal([0,1,4294967295], "\000\000\000\000\001\000\000\000\377\377\377\377".unpack("V*"))
|
2010-02-25 09:00:48 -05:00
|
|
|
|
|
|
|
assert_equal(2, [1].pack("v").bytesize)
|
|
|
|
assert_equal(4, [1].pack("V").bytesize)
|
2008-01-31 09:08:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_fdeEgG
|
|
|
|
inf = 1.0/0.0
|
|
|
|
nan = inf/inf
|
|
|
|
[0.0, 1.0, 3.0, inf, -inf, nan].each do |x|
|
|
|
|
%w(f d e E g G).each do |f|
|
|
|
|
v = [x].pack(f).unpack(f)
|
|
|
|
if x.nan?
|
2013-12-13 09:20:53 -05:00
|
|
|
assert_predicate(v.first, :nan?)
|
2008-01-31 09:08:14 -05:00
|
|
|
else
|
|
|
|
assert_equal([x], v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_x
|
|
|
|
assert_equal("", [].pack("x0"))
|
|
|
|
assert_equal("\0", [].pack("x"))
|
|
|
|
assert_equal("\0" * 30, [].pack("x30"))
|
|
|
|
|
|
|
|
assert_equal([0, 2], "\x00\x00\x02".unpack("CxC"))
|
|
|
|
assert_raise(ArgumentError) { "".unpack("x") }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_X
|
|
|
|
assert_equal("\x00\x02", [0, 1, 2].pack("CCXC"))
|
|
|
|
assert_equal("\x02", [0, 1, 2].pack("CCX2C"))
|
|
|
|
assert_raise(ArgumentError) { [].pack("X") }
|
|
|
|
|
|
|
|
assert_equal([0, 2, 2], "\x00\x02".unpack("CCXC"))
|
|
|
|
assert_raise(ArgumentError) { "".unpack("X") }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_atmark
|
|
|
|
assert_equal("\x01\x00\x00\x02", [1, 2].pack("C@3C"))
|
|
|
|
assert_equal("\x02", [1, 2].pack("C@0C"))
|
|
|
|
assert_equal("\x01\x02", [1, 2].pack("C@1C"))
|
|
|
|
|
|
|
|
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") }
|
2018-03-28 06:12:17 -04:00
|
|
|
|
|
|
|
pos = RbConfig::LIMITS["UINTPTR_MAX"] - 99 # -100
|
|
|
|
assert_raise(RangeError) {"0123456789".unpack("@#{pos}C10")}
|
2008-01-31 09:08:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_percent
|
|
|
|
assert_raise(ArgumentError) { [].pack("%") }
|
|
|
|
assert_raise(ArgumentError) { "".unpack("%") }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_U
|
|
|
|
assert_equal([0], [0].pack("U").unpack("U"))
|
|
|
|
assert_equal([0x80], [0x80].pack("U").unpack("U"))
|
|
|
|
assert_equal([0x800], [0x800].pack("U").unpack("U"))
|
|
|
|
assert_equal([0x10000], [0x10000].pack("U").unpack("U"))
|
|
|
|
assert_equal([0x400000], [0x400000].pack("U").unpack("U"))
|
|
|
|
|
|
|
|
assert_raise(ArgumentError) { "\x80".unpack("U") }
|
|
|
|
assert_raise(ArgumentError) { "\xff".unpack("U") }
|
|
|
|
assert_raise(ArgumentError) { "\xfc\x00".unpack("U") }
|
|
|
|
assert_raise(ArgumentError) { "\xc0\xc0".unpack("U") }
|
|
|
|
assert_raise(ArgumentError) { "\xe0\x80\x80".unpack("U") }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_u
|
|
|
|
assert_equal("", [""].pack("u"))
|
|
|
|
assert_equal("!80``\n", ["a"].pack("u"))
|
|
|
|
assert_equal("#86)C\n", ["abc"].pack("u"))
|
|
|
|
assert_equal("$86)C9```\n", ["abcd"].pack("u"))
|
|
|
|
assert_equal("M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n", ["a"*45].pack("u"))
|
|
|
|
assert_equal("M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n!80``\n", ["a"*46].pack("u"))
|
|
|
|
assert_equal("&86)C9&5F\n#9VAI\n", ["abcdefghi"].pack("u6"))
|
|
|
|
|
2008-12-06 03:55:57 -05:00
|
|
|
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"))
|
2012-07-18 10:57:40 -04:00
|
|
|
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
|
2008-12-06 03:55:57 -05:00
|
|
|
|
2008-01-31 09:08:14 -05:00
|
|
|
assert_equal([""], "".unpack("u"))
|
|
|
|
assert_equal(["a"], "!80``\n".unpack("u"))
|
|
|
|
assert_equal(["abc"], "#86)C\n".unpack("u"))
|
|
|
|
assert_equal(["abcd"], "$86)C9```\n".unpack("u"))
|
|
|
|
assert_equal(["a"*45], "M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n".unpack("u"))
|
|
|
|
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"))
|
|
|
|
|
2014-10-17 10:28:34 -04:00
|
|
|
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.
|
|
|
|
|
2008-01-31 09:08:14 -05:00
|
|
|
assert_equal(["\x00"], "\"\n".unpack("u"))
|
|
|
|
assert_equal(["\x00"], "! \r \n".unpack("u"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_m
|
|
|
|
assert_equal("", [""].pack("m"))
|
|
|
|
assert_equal("AA==\n", ["\0"].pack("m"))
|
|
|
|
assert_equal("AAA=\n", ["\0\0"].pack("m"))
|
|
|
|
assert_equal("AAAA\n", ["\0\0\0"].pack("m"))
|
|
|
|
assert_equal("/w==\n", ["\377"].pack("m"))
|
|
|
|
assert_equal("//8=\n", ["\377\377"].pack("m"))
|
|
|
|
assert_equal("////\n", ["\377\377\377"].pack("m"))
|
|
|
|
|
|
|
|
assert_equal([""], "".unpack("m"))
|
|
|
|
assert_equal(["\0"], "AA==\n".unpack("m"))
|
|
|
|
assert_equal(["\0\0"], "AAA=\n".unpack("m"))
|
|
|
|
assert_equal(["\0\0\0"], "AAAA\n".unpack("m"))
|
|
|
|
assert_equal(["\377"], "/w==\n".unpack("m"))
|
|
|
|
assert_equal(["\377\377"], "//8=\n".unpack("m"))
|
|
|
|
assert_equal(["\377\377\377"], "////\n".unpack("m"))
|
2013-04-18 01:03:00 -04:00
|
|
|
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"))
|
2014-07-10 21:09:05 -04:00
|
|
|
|
|
|
|
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;
|
2008-01-31 09:08:14 -05:00
|
|
|
end
|
|
|
|
|
2021-11-16 16:09:25 -05:00
|
|
|
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
|
|
|
|
|
2008-09-25 08:24:54 -04:00
|
|
|
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
|
|
|
|
|
2008-01-31 09:08:14 -05:00
|
|
|
def test_pack_unpack_M
|
|
|
|
assert_equal("a b c\td =\n\ne=\n", ["a b c\td \ne"].pack("M"))
|
|
|
|
assert_equal(["a b c\td \ne"], "a b c\td =\n\ne=\n".unpack("M"))
|
|
|
|
assert_equal("=00=\n", ["\0"].pack("M"))
|
|
|
|
assert_equal("a"*73+"=\na=\n", ["a"*74].pack("M"))
|
|
|
|
assert_equal(("a"*73+"=\n")*14+"a=\n", ["a"*1023].pack("M"))
|
|
|
|
assert_equal(["\0"], "=00=\n".unpack("M"))
|
|
|
|
assert_equal(["a"*74], ("a"*73+"=\na=\n").unpack("M"))
|
|
|
|
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"))
|
2012-07-24 00:24:20 -04:00
|
|
|
assert_equal(["=0Z=\n"], "=0Z=\n".unpack("M"))
|
2011-01-14 00:03:22 -05:00
|
|
|
assert_equal([""], "=\r\n".unpack("M"))
|
|
|
|
assert_equal(["\xC6\xF7"], "=C6=F7".unpack('M*'))
|
2012-07-23 22:36:15 -04:00
|
|
|
|
|
|
|
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"))
|
2017-09-29 04:00:47 -04:00
|
|
|
|
|
|
|
bug = '[ruby-core:83055] [Bug #13949]'
|
|
|
|
s = "abcdef".unpack1("M")
|
|
|
|
assert_equal(Encoding::ASCII_8BIT, s.encoding)
|
|
|
|
assert_predicate(s, :ascii_only?, bug)
|
2008-01-31 09:08:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_P2
|
|
|
|
assert_raise(ArgumentError) { ["abc"].pack("P4") }
|
|
|
|
assert_raise(ArgumentError) { [""].pack("P") }
|
|
|
|
|
|
|
|
assert_equal([], ".".unpack("P"))
|
|
|
|
assert_equal([], ".".unpack("p"))
|
|
|
|
assert_equal([nil], ("\0" * 1024).unpack("P"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_p2
|
2010-04-29 01:46:04 -04:00
|
|
|
assert_match(/\A\0*\Z/, [nil].pack("p"))
|
2008-01-31 09:08:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pack_unpack_w
|
|
|
|
assert_equal("\000", [0].pack("w"))
|
|
|
|
assert_equal("\001", [1].pack("w"))
|
|
|
|
assert_equal("\177", [127].pack("w"))
|
|
|
|
assert_equal("\201\000", [128].pack("w"))
|
|
|
|
assert_equal("\377\177", [0x3fff].pack("w"))
|
|
|
|
assert_equal("\201\200\000", [0x4000].pack("w"))
|
|
|
|
assert_equal("\203\377\377\377\177", [0x3fffffff].pack("w"))
|
|
|
|
assert_equal("\204\200\200\200\000", [0x40000000].pack("w"))
|
|
|
|
assert_equal("\217\377\377\377\177", [0xffffffff].pack("w"))
|
|
|
|
assert_equal("\220\200\200\200\000", [0x100000000].pack("w"))
|
|
|
|
assert_raise(ArgumentError) { [-1].pack("w") }
|
|
|
|
|
|
|
|
assert_equal([0], "\000".unpack("w"))
|
|
|
|
assert_equal([1], "\001".unpack("w"), [1])
|
|
|
|
assert_equal([127], "\177".unpack("w"), [127])
|
|
|
|
assert_equal([128], "\201\000".unpack("w"), [128])
|
|
|
|
assert_equal([0x3fff], "\377\177".unpack("w"), [0x3fff])
|
|
|
|
assert_equal([0x4000], "\201\200\000".unpack("w"), [0x4000])
|
|
|
|
assert_equal([0x3fffffff], "\203\377\377\377\177".unpack("w"), [0x3fffffff])
|
|
|
|
assert_equal([0x40000000], "\204\200\200\200\000".unpack("w"), [0x40000000])
|
|
|
|
assert_equal([0xffffffff], "\217\377\377\377\177".unpack("w"), [0xffffffff])
|
|
|
|
assert_equal([0x100000000], "\220\200\200\200\000".unpack("w"), [0x100000000])
|
|
|
|
end
|
2008-05-15 10:37:18 -04:00
|
|
|
|
2008-05-17 13:56:41 -04:00
|
|
|
def test_length_too_big
|
|
|
|
assert_raise(RangeError) { [].pack("C100000000000000000000") }
|
|
|
|
end
|
2010-02-18 07:53:31 -05:00
|
|
|
|
|
|
|
def test_short_string
|
2010-02-24 10:32:06 -05:00
|
|
|
%w[n N v V s S i I l L q Q s! S! i! I! l! l!].each {|fmt|
|
2010-02-18 07:53:31 -05:00
|
|
|
str = [1].pack(fmt)
|
|
|
|
assert_equal([1,nil], str.unpack("#{fmt}2"))
|
|
|
|
}
|
|
|
|
end
|
2010-11-15 16:39:39 -05:00
|
|
|
|
|
|
|
def test_short_with_block
|
|
|
|
bug4059 = '[ruby-core:33193]'
|
|
|
|
result = :ok
|
|
|
|
assert_nil("".unpack("i") {|x| result = x}, bug4059)
|
|
|
|
assert_equal(:ok, result)
|
|
|
|
end
|
2012-04-10 18:33:58 -04:00
|
|
|
|
|
|
|
def test_pack_garbage
|
2012-04-10 21:15:56 -04:00
|
|
|
verbose = $VERBOSE
|
|
|
|
$VERBOSE = false
|
|
|
|
|
2012-04-10 18:33:58 -04:00
|
|
|
assert_silent do
|
|
|
|
assert_equal "\000", [0].pack("*U")
|
|
|
|
end
|
|
|
|
|
|
|
|
$VERBOSE = true
|
|
|
|
|
2021-09-13 08:14:42 -04:00
|
|
|
_, err = capture_output do
|
2012-04-10 18:33:58 -04:00
|
|
|
assert_equal "\000", [0].pack("*U")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match %r%unknown pack directive '\*' in '\*U'$%, err
|
|
|
|
ensure
|
|
|
|
$VERBOSE = verbose
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unpack_garbage
|
2012-04-10 21:15:56 -04:00
|
|
|
verbose = $VERBOSE
|
|
|
|
$VERBOSE = false
|
|
|
|
|
2012-04-10 18:33:58 -04:00
|
|
|
assert_silent do
|
|
|
|
assert_equal [0], "\000".unpack("*U")
|
|
|
|
end
|
|
|
|
|
|
|
|
$VERBOSE = true
|
|
|
|
|
2021-09-13 08:14:42 -04:00
|
|
|
_, err = capture_output do
|
2012-04-10 18:33:58 -04:00
|
|
|
assert_equal [0], "\000".unpack("*U")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match %r%unknown unpack directive '\*' in '\*U'$%, err
|
|
|
|
ensure
|
|
|
|
$VERBOSE = verbose
|
|
|
|
end
|
|
|
|
|
2014-11-06 09:55:44 -05:00
|
|
|
def test_invalid_warning
|
|
|
|
assert_warning(/unknown pack directive ',' in ','/) {
|
|
|
|
[].pack(",")
|
|
|
|
}
|
2014-11-06 09:58:43 -05:00
|
|
|
assert_warning(/\A[ -~]+\Z/) {
|
|
|
|
[].pack("\x7f")
|
|
|
|
}
|
|
|
|
assert_warning(/\A(.* in '\u{3042}'\n)+\z/) {
|
2016-04-08 21:25:11 -04:00
|
|
|
[].pack("\u{3042}")
|
2014-11-06 09:58:43 -05:00
|
|
|
}
|
2018-11-15 19:25:54 -05:00
|
|
|
|
|
|
|
assert_warning(/\A.* in '.*U'\Z/) {
|
|
|
|
assert_equal "\000", [0].pack("\0U")
|
|
|
|
}
|
|
|
|
assert_warning(/\A.* in '.*U'\Z/) {
|
|
|
|
"\000".unpack("\0U")
|
|
|
|
}
|
2014-11-06 09:55:44 -05:00
|
|
|
end
|
2015-12-13 04:33:40 -05:00
|
|
|
|
|
|
|
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
|
2016-12-01 08:08:20 -05:00
|
|
|
|
|
|
|
def test_pack_with_buffer
|
|
|
|
buf = String.new(capacity: 100)
|
|
|
|
|
2017-12-11 19:46:34 -05:00
|
|
|
assert_raise_with_message(FrozenError, /frozen/) {
|
2016-12-01 08:08:20 -05:00
|
|
|
[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
|
|
|
|
|
2016-12-01 08:20:47 -05:00
|
|
|
[0xBABE_F00D].pack('@4N', buffer: buf)
|
2016-12-01 08:08:20 -05:00
|
|
|
assert_equal "\xDE\xAD\xBE\xEF\xBA\xBE\xF0\x0D", buf
|
|
|
|
assert_equal addr, [buf].pack('p')
|
|
|
|
|
2016-12-01 08:20:47 -05:00
|
|
|
[0xBAAD_FACE].pack('@10N', buffer: buf)
|
2016-12-01 08:08:20 -05:00
|
|
|
assert_equal "\xDE\xAD\xBE\xEF\xBA\xBE\xF0\x0D\0\0\xBA\xAD\xFA\xCE", buf
|
|
|
|
|
|
|
|
assert_equal addr, [buf].pack('p')
|
|
|
|
end
|
2016-12-01 09:18:32 -05:00
|
|
|
|
2016-12-26 01:32:00 -05:00
|
|
|
def test_unpack_with_block
|
|
|
|
ret = []; "ABCD".unpack("CCCC") {|v| ret << v }
|
|
|
|
assert_equal [65, 66, 67, 68], ret
|
2016-12-29 11:16:32 -05:00
|
|
|
ret = []; "A".unpack("B*") {|v| ret << v.dup }
|
2016-12-26 01:32:00 -05:00
|
|
|
assert_equal ["01000001"], ret
|
|
|
|
end
|
|
|
|
|
2016-12-01 09:18:32 -05:00
|
|
|
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")
|
2016-12-26 01:32:00 -05:00
|
|
|
assert_equal "01000001", "A".unpack1("B*")
|
2016-12-01 09:18:32 -05:00
|
|
|
end
|
2021-10-18 10:23:54 -04:00
|
|
|
|
|
|
|
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
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|