Upgrade to 1.46.0
[platform/upstream/nghttp2.git] / third-party / mruby / mrbgems / mruby-pack / test / pack.rb
1 PACK_IS_LITTLE_ENDIAN = "\x01\00".unpack('S')[0] == 0x01
2
3 def 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
9 end
10
11 # pack & unpack 'm' (base64)
12 assert('[""].pack("m")') do
13   assert_pack "m", "", [""]
14 end
15
16 assert('["\0"].pack("m")') do
17   assert_pack "m", "AA==\n", ["\0"]
18 end
19
20 assert('["\0\0"].pack("m")') do
21   assert_pack "m", "AAA=\n", ["\0\0"]
22 end
23
24 assert('["\0\0\0"].pack("m")') do
25   assert_pack "m", "AAAA\n", ["\0\0\0"]
26 end
27
28 assert('["abc..xyzABC..XYZ"].pack("m")') do
29   assert_pack "m", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n", ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"]
30 end
31
32 assert('"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")
36 end
37
38 assert('["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")
43 end
44
45 assert('["abc..xyzABC..XYZ"].pack("m0")') do
46   assert_pack "m0", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWg==", ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"]
47 end
48
49 # pack & unpack 'H'
50 assert('["3031"].pack("H*")') do
51   assert_pack "H*", "01", ["3031"]
52 end
53
54 assert('["10"].pack("H*")') do
55   assert_pack "H*", "\020", ["10"]
56 end
57
58 assert('[0,1,127,128,255].pack("C*")') do
59   assert_pack "C*", "\x00\x01\x7F\x80\xFF", [0, 1, 127, 128, 255]
60 end
61
62 # pack "a"
63 assert('["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")
67 end
68
69 # upack "a"
70 assert('["abc"].pack("a")') do
71   assert_equal ["abc\0"], "abc\0".unpack("a4")
72   assert_equal ["abc "], "abc ".unpack("a4")
73 end
74
75 # pack "A"
76 assert('["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")
80 end
81
82 # upack "A"
83 assert('["abc"].pack("A")') do
84   assert_equal ["abc"], "abc\0".unpack("A4")
85   assert_equal ["abc"], "abc ".unpack("A4")
86 end
87
88 # regression tests
89 assert('issue #1') do
90   assert_equal "\000\001\000\002", [1, 2].pack("nn")
91 end
92
93 assert '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
104 end
105
106 assert '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
117 end
118
119 assert '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]
129 end
130
131 assert '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]
141 end
142
143 assert '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") }
159 end