1 # pack & unpack 'm' (base64)
2 assert('[""].pack("m")') do
5 [ary].pack("m") == str and
6 str.unpack("m") == [ary]
9 assert('["\0"].pack("m")') do
12 [ary].pack("m") == str and
13 str.unpack("m") == [ary]
16 assert('["\0\0"].pack("m")') do
19 [ary].pack("m") == str and
20 str.unpack("m") == [ary]
23 assert('["\0\0\0"].pack("m")') do
26 [ary].pack("m") == str and
27 str.unpack("m") == [ary]
30 assert('["abc..xyzABC..XYZ"].pack("m")') do
31 ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"].pack("m") == "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n"
34 assert('"YWJ...".unpack("m") should "abc..xyzABC..XYZ"') do
35 str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
36 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n".unpack("m") == [str] and
37 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWg==\n".unpack("m") == [str]
41 assert('["3031"].pack("H*")') do
44 [ary].pack("H*") == str and
45 str.unpack("H*") == [ary]
48 assert('["10"].pack("H*")') do
51 [ary].pack("H*") == str and
52 str.unpack("H*") == [ary]
55 assert('[0,1,127,128,255].pack("C*")') do
56 ary = [ 0, 1, 127, 128, 255 ]
57 str = "\x00\x01\x7F\x80\xFF"
58 ary.pack("C*") == str and str.unpack("C*") == ary
62 assert('["abc"].pack("a")') do
63 ["abc"].pack("a") == "a" and
64 ["abc"].pack("a*") == "abc" and
65 ["abc"].pack("a4") == "abc\0"
69 assert('["abc"].pack("a")') do
70 "abc\0".unpack("a4") == ["abc\0"] and
71 "abc ".unpack("a4") == ["abc "]
75 assert('["abc"].pack("A")') do
76 ["abc"].pack("A") == "a" and
77 ["abc"].pack("A*") == "abc" and
78 ["abc"].pack("A4") == "abc "
82 assert('["abc"].pack("A")') do
83 "abc\0".unpack("A4") == ["abc"] and
84 "abc ".unpack("A4") == ["abc"]
89 [1, 2].pack("nn") == "\000\001\000\002"
92 def assert_pack tmpl, packed, unpacked
93 assert_equal packed, unpacked.pack(tmpl)
94 assert_equal unpacked, packed.unpack(tmpl)
97 PACK_IS_LITTLE_ENDIAN = "\x01\00".unpack('S')[0] == 0x01
99 assert 'pack float' do
100 assert_pack 'e', "\x00\x00@@", [3.0]
101 assert_pack 'g', "@@\x00\x00", [3.0]
103 if PACK_IS_LITTLE_ENDIAN
104 assert_pack 'f', "\x00\x00@@", [3.0]
105 assert_pack 'F', "\x00\x00@@", [3.0]
107 assert_pack 'f', "@@\x00\x00", [3.0]
108 assert_pack 'F', "@@\x00\x00", [3.0]
112 assert 'pack double' do
113 assert_pack 'E', "\x00\x00\x00\x00\x00\x00\b@", [3.0]
114 assert_pack 'G', "@\b\x00\x00\x00\x00\x00\x00", [3.0]
116 if PACK_IS_LITTLE_ENDIAN
117 assert_pack 'd', "\x00\x00\x00\x00\x00\x00\b@", [3.0]
118 assert_pack 'D', "\x00\x00\x00\x00\x00\x00\b@", [3.0]
120 assert_pack 'd', "@\b\x00\x00\x00\x00\x00\x00", [3.0]
121 assert_pack 'D', "@\b\x00\x00\x00\x00\x00\x00", [3.0]
125 assert 'pack/unpack "i"' do
126 int_size = [0].pack('i').size
127 raise "pack('i').size is too small (#{int_size})" if int_size < 2
129 if PACK_IS_LITTLE_ENDIAN
130 str = "\xC7\xCF" + "\xFF" * (int_size-2)
132 str = "\xFF" * (int_size-2) + "\xCF\xC7"
134 assert_pack 'i', str, [-12345]
137 assert 'pack/unpack "I"' do
138 uint_size = [0].pack('I').size
139 raise "pack('I').size is too small (#{uint_size})" if uint_size < 2
141 if PACK_IS_LITTLE_ENDIAN
142 str = "\x39\x30" + "\0" * (uint_size-2)
144 str = "\0" * (uint_size-2) + "\x30\x39"
146 assert_pack 'I', str, [12345]
149 assert 'pack/unpack "U"' do
150 assert_equal [], "".unpack("U")
151 assert_equal [], "".unpack("U*")
152 assert_equal [65, 66], "ABC".unpack("U2")
153 assert_equal [12371, 12435, 12395, 12385, 12399, 19990, 30028], "こんにちは世界".unpack("U*")
155 assert_equal "", [].pack("U")
156 assert_equal "", [].pack("U*")
157 assert_equal "AB", [65, 66, 67].pack("U2")
158 assert_equal "こんにちは世界", [12371, 12435, 12395, 12385, 12399, 19990, 30028].pack("U*")
160 assert_equal "\000", [0].pack("U")
162 assert_raise(RangeError) { [-0x40000000].pack("U") }
163 assert_raise(RangeError) { [-1].pack("U") }
164 assert_raise(RangeError) { [0x40000000].pack("U") }