1PACK_IS_LITTLE_ENDIAN = "\x01\00".unpack('S')[0] == 0x01
2
3def assert_pack tmpl, packed, unpacked
4  t = tmpl.inspect
5  assert "assert_pack" do
6    assert_equal packed, unpacked.pack(tmpl), "#{unpacked.inspect}.pack(#{t})"
7    assert_equal unpacked, packed.unpack(tmpl), "#{packed.inspect}.unpack(#{t})"
8  end
9end
10
11# pack & unpack 'm' (base64)
12assert('[""].pack("m")') do
13  assert_pack "m", "", [""]
14end
15
16assert('["\0"].pack("m")') do
17  assert_pack "m", "AA==\n", ["\0"]
18end
19
20assert('["\0\0"].pack("m")') do
21  assert_pack "m", "AAA=\n", ["\0\0"]
22end
23
24assert('["\0\0\0"].pack("m")') do
25  assert_pack "m", "AAAA\n", ["\0\0\0"]
26end
27
28assert('["abc..xyzABC..XYZ"].pack("m")') do
29  assert_pack "m", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n", ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"]
30end
31
32assert('"YWJ...".unpack("m") should "abc..xyzABC..XYZ"') do
33  ary = ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"]
34  assert_equal ary, "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n".unpack("m")
35  assert_equal ary, "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWg==\n".unpack("m")
36end
37
38assert('["A", "B"].pack') do
39  assert_equal "QQ==\n", ["A", "B"].pack("m50")
40  assert_equal ["A"], "QQ==\n".unpack("m50")
41  assert_equal "QQ==Qg==", ["A", "B"].pack("m0 m0")
42  assert_equal ["A", "B"], "QQ==Qg==".unpack("m10 m10")
43end
44
45assert('["abc..xyzABC..XYZ"].pack("m0")') do
46  assert_pack "m0", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWg==", ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"]
47end
48
49# pack & unpack 'H'
50assert('["3031"].pack("H*")') do
51  assert_pack "H*", "01", ["3031"]
52end
53
54assert('["10"].pack("H*")') do
55  assert_pack "H*", "\020", ["10"]
56end
57
58assert('[0,1,127,128,255].pack("C*")') do
59  assert_pack "C*", "\x00\x01\x7F\x80\xFF", [0, 1, 127, 128, 255]
60end
61
62# pack "a"
63assert('["abc"].pack("a")') do
64  assert_equal "a", ["abc"].pack("a")
65  assert_equal "abc", ["abc"].pack("a*")
66  assert_equal "abc\0", ["abc"].pack("a4")
67end
68
69# upack "a"
70assert('["abc"].pack("a")') do
71  assert_equal ["abc\0"], "abc\0".unpack("a4")
72  assert_equal ["abc "], "abc ".unpack("a4")
73end
74
75# pack "A"
76assert('["abc"].pack("A")') do
77  assert_equal "a", ["abc"].pack("A")
78  assert_equal "abc", ["abc"].pack("A*")
79  assert_equal "abc ", ["abc"].pack("A4")
80end
81
82# upack "A"
83assert('["abc"].pack("A")') do
84  assert_equal ["abc"], "abc\0".unpack("A4")
85  assert_equal ["abc"], "abc ".unpack("A4")
86end
87
88# regression tests
89assert('issue #1') do
90  assert_equal "\000\001\000\002", [1, 2].pack("nn")
91end
92
93assert 'pack float' do
94  assert_pack 'e', "\x00\x00@@", [3.0]
95  assert_pack 'g', "@@\x00\x00", [3.0]
96
97  if PACK_IS_LITTLE_ENDIAN
98    assert_pack 'f', "\x00\x00@@", [3.0]
99    assert_pack 'F', "\x00\x00@@", [3.0]
100  else
101    assert_pack 'f', "@@\x00\x00", [3.0]
102    assert_pack 'F', "@@\x00\x00", [3.0]
103  end
104end
105
106assert 'pack double' do
107  assert_pack 'E', "\x00\x00\x00\x00\x00\x00\b@", [3.0]
108  assert_pack 'G', "@\b\x00\x00\x00\x00\x00\x00", [3.0]
109
110  if PACK_IS_LITTLE_ENDIAN
111    assert_pack 'd', "\x00\x00\x00\x00\x00\x00\b@", [3.0]
112    assert_pack 'D', "\x00\x00\x00\x00\x00\x00\b@", [3.0]
113  else
114    assert_pack 'd', "@\b\x00\x00\x00\x00\x00\x00", [3.0]
115    assert_pack 'D', "@\b\x00\x00\x00\x00\x00\x00", [3.0]
116  end
117end
118
119assert 'pack/unpack "i"' do
120  int_size = [0].pack('i').size
121  raise "pack('i').size is too small (#{int_size})" if int_size < 2
122
123  if PACK_IS_LITTLE_ENDIAN
124    str = "\xC7\xCF" + "\xFF" * (int_size-2)
125  else
126    str = "\xFF" * (int_size-2) + "\xCF\xC7"
127  end
128  assert_pack 'i', str, [-12345]
129end
130
131assert 'pack/unpack "I"' do
132  uint_size = [0].pack('I').size
133  raise "pack('I').size is too small (#{uint_size})" if uint_size < 2
134
135  if PACK_IS_LITTLE_ENDIAN
136    str = "\x39\x30" + "\0" * (uint_size-2)
137  else
138    str = "\0" * (uint_size-2) + "\x30\x39"
139  end
140  assert_pack 'I', str, [12345]
141end
142
143assert 'pack/unpack "U"' do
144  assert_equal [], "".unpack("U")
145  assert_equal [], "".unpack("U*")
146  assert_equal [65, 66], "ABC".unpack("U2")
147  assert_equal [12371, 12435, 12395, 12385, 12399, 19990, 30028], "こんにちは世界".unpack("U*")
148
149  assert_equal "", [].pack("U")
150  assert_equal "", [].pack("U*")
151  assert_equal "AB", [65, 66, 67].pack("U2")
152  assert_equal "こんにちは世界", [12371, 12435, 12395, 12385, 12399, 19990, 30028].pack("U*")
153
154  assert_equal "\000", [0].pack("U")
155
156  assert_raise(RangeError) { [-0x40000000].pack("U") }
157  assert_raise(RangeError) { [-1].pack("U") }
158  assert_raise(RangeError) { [0x40000000].pack("U") }
159end
160