mochilo 1.3.0 → 2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +2 -5
- data/Gemfile +0 -4
- data/Gemfile.lock +1 -1
- data/LICENSE +21 -0
- data/README.md +12 -26
- data/docs/format-spec.md +23 -54
- data/docs/why-banana-pack.md +5 -3
- data/ext/mochilo/buffer.c +11 -14
- data/ext/mochilo/buffer.h +4 -5
- data/ext/mochilo/mochilo.h +26 -27
- data/ext/mochilo/mochilo.rb.c +15 -42
- data/ext/mochilo/mochilo_api.h +0 -39
- data/ext/mochilo/mochilo_pack.c +65 -130
- data/ext/mochilo/mochilo_unpack.c +51 -32
- data/lib/mochilo/version.rb +1 -1
- data/mochilo.gemspec +3 -1
- data/script/bootstrap +1 -1
- data/test/pack_test.rb +162 -80
- data/test/setup.rb +0 -9
- data/test/unpack_test.rb +206 -48
- metadata +24 -13
- data/MIT-LICENSE +0 -20
data/test/pack_test.rb
CHANGED
@@ -4,7 +4,7 @@ require File.expand_path('../setup', __FILE__)
|
|
4
4
|
require 'mochilo'
|
5
5
|
require 'stringio'
|
6
6
|
|
7
|
-
class MochiloPackTest <
|
7
|
+
class MochiloPackTest < Minitest::Test
|
8
8
|
|
9
9
|
OBJECTS = [
|
10
10
|
{"hello" => "world"},
|
@@ -52,122 +52,222 @@ class MochiloPackTest < MiniTest::Unit::TestCase
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def test_pack_positive_fixed
|
55
|
-
|
55
|
+
[0,127].each do |int|
|
56
|
+
assert_equal int.chr, Mochilo.pack(int)
|
57
|
+
end
|
56
58
|
end
|
57
59
|
|
58
60
|
def test_pack_negative_fixed
|
59
|
-
assert_equal "\
|
61
|
+
assert_equal "\xFF", Mochilo.pack(-1)
|
62
|
+
assert_equal "\xE0", Mochilo.pack(-32)
|
60
63
|
end
|
61
64
|
|
62
65
|
def test_pack_uint8
|
63
|
-
|
66
|
+
[128,255].each do |int|
|
67
|
+
assert_equal "\xCC#{int.chr}", Mochilo.pack(int)
|
68
|
+
end
|
64
69
|
end
|
65
70
|
|
66
71
|
def test_pack_uint16
|
67
|
-
|
72
|
+
[256,((2**16)-1)].each do |int|
|
73
|
+
packed = [int].pack("n")
|
74
|
+
assert_equal "\xCD#{packed}", Mochilo.pack(int)
|
75
|
+
end
|
68
76
|
end
|
69
77
|
|
70
78
|
def test_pack_uint32
|
71
|
-
|
79
|
+
[(2**16),((2**32)-1)].each do |int|
|
80
|
+
packed = [int].pack("N")
|
81
|
+
assert_equal "\xCE#{packed}", Mochilo.pack(int)
|
82
|
+
end
|
72
83
|
end
|
73
84
|
|
74
85
|
def test_pack_uint64
|
75
|
-
|
86
|
+
[(2**32),((2**64)-1)].each do |int|
|
87
|
+
packed = [int].pack("Q>")
|
88
|
+
assert_equal "\xCF#{packed}", Mochilo.pack(int)
|
89
|
+
end
|
76
90
|
end
|
77
91
|
|
78
92
|
def test_pack_int8
|
79
|
-
assert_equal "\xD0\
|
93
|
+
assert_equal "\xD0\xDF", Mochilo.pack(-33)
|
94
|
+
assert_equal "\xD0\x80", Mochilo.pack(-128)
|
80
95
|
end
|
81
96
|
|
82
97
|
def test_pack_int16
|
83
|
-
assert_equal "\xD1\
|
98
|
+
assert_equal "\xD1\xFF\x7F", Mochilo.pack(-129)
|
99
|
+
assert_equal "\xD1\x80\x00", Mochilo.pack(-32768)
|
84
100
|
end
|
85
101
|
|
86
102
|
def test_pack_int32
|
87
|
-
assert_equal "\xD2\
|
103
|
+
assert_equal "\xD2\xFF\xFF\x7F\xFF", Mochilo.pack(-32769)
|
104
|
+
assert_equal "\xD2\x80\x00\x00\x00", Mochilo.pack(-2147483648)
|
88
105
|
end
|
89
106
|
|
90
107
|
def test_pack_int64
|
91
|
-
assert_equal "\xD3\xFF\xFF\xFF\
|
108
|
+
assert_equal "\xD3\xFF\xFF\xFF\xFF\x7F\xFF\xFF\xFF", Mochilo.pack(-2147483649)
|
109
|
+
assert_equal "\xD3\x80\x00\x00\x00\x00\x00\x00\x00", Mochilo.pack(-9223372036854775808)
|
92
110
|
end
|
93
111
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
112
|
+
def test_pack_fixed_str
|
113
|
+
str = "a".force_encoding('UTF-8')
|
114
|
+
assert_equal "\xA1#{str}", Mochilo.pack(str)
|
115
|
+
|
116
|
+
str = ("a"*31).force_encoding('UTF-8')
|
117
|
+
assert_equal "\xBF#{str}", Mochilo.pack(str)
|
99
118
|
end
|
100
119
|
|
101
|
-
def
|
102
|
-
|
120
|
+
def test_pack_str8
|
121
|
+
str = ("a"*32).force_encoding('UTF-8')
|
122
|
+
assert_equal "\xD9#{str.bytesize.chr}#{str}", Mochilo.pack(str)
|
123
|
+
|
124
|
+
str = ("a"*255).force_encoding('UTF-8')
|
125
|
+
assert_equal "\xD9#{str.bytesize.chr}#{str}", Mochilo.pack(str)
|
103
126
|
end
|
104
127
|
|
105
|
-
def
|
106
|
-
str = "
|
107
|
-
assert_equal "\
|
128
|
+
def test_pack_str16
|
129
|
+
str = ("a"*256).force_encoding('UTF-8')
|
130
|
+
assert_equal "\xDA\x01\x00#{str}", Mochilo.pack(str)
|
131
|
+
|
132
|
+
str = ("a"*(2**16-1)).force_encoding('UTF-8')
|
133
|
+
assert_equal "\xDA\xFF\xFF#{str}", Mochilo.pack(str)
|
108
134
|
end
|
109
135
|
|
110
|
-
def
|
111
|
-
str = ("a"*
|
112
|
-
assert_equal "\
|
136
|
+
def test_pack_str32
|
137
|
+
str = ("a"*(2**16)).force_encoding('UTF-8')
|
138
|
+
assert_equal "\xDB\x00\x01\x00\x00#{str}", Mochilo.pack(str)
|
139
|
+
|
140
|
+
# This will create a 4GB string, so let's just skip that ;)
|
141
|
+
# str = ("a"*(2**32-1)).force_encoding('UTF-8')
|
142
|
+
# assert_equal "\xDB\x00\x01\x00\x00#{str}", Mochilo.pack(str)
|
113
143
|
end
|
114
144
|
|
115
|
-
def
|
116
|
-
|
145
|
+
def test_pack_enc8
|
146
|
+
str = ("a"*32).force_encoding('ISO-8859-1')
|
147
|
+
assert_equal "\xC7#{str.bytesize.chr}\x0C#{str}", Mochilo.pack(str)
|
148
|
+
|
149
|
+
str = ("a"*255).force_encoding('ISO-8859-1')
|
150
|
+
assert_equal "\xC7#{str.bytesize.chr}\x0C#{str}", Mochilo.pack(str)
|
117
151
|
end
|
118
152
|
|
119
|
-
def
|
120
|
-
|
121
|
-
assert_equal "\
|
153
|
+
def test_pack_enc16
|
154
|
+
str = ("a"*256).force_encoding('ISO-8859-1')
|
155
|
+
assert_equal "\xC8\x01\x00\x0C#{str}", Mochilo.pack(str)
|
156
|
+
|
157
|
+
str = ("a"*(2**16-1)).force_encoding('ISO-8859-1')
|
158
|
+
assert_equal "\xC8\xFF\xFF\x0C#{str}", Mochilo.pack(str)
|
122
159
|
end
|
123
160
|
|
124
|
-
def
|
125
|
-
|
126
|
-
assert_equal "\
|
161
|
+
def test_pack_enc32
|
162
|
+
str = ("a"*(2**16)).force_encoding('ISO-8859-1')
|
163
|
+
assert_equal "\xC9\x00\x01\x00\x00\x0C#{str}", Mochilo.pack(str)
|
164
|
+
|
165
|
+
# This would create a 4GB string, so let's just skip that ;)
|
166
|
+
# str = ("a"*(2**32-1)).force_encoding('ISO-8859-1')
|
167
|
+
# assert_equal "\xC9\x00\x01\x00\x00\x0C#{str}", Mochilo.pack(str)
|
127
168
|
end
|
128
169
|
|
129
|
-
def
|
130
|
-
|
170
|
+
def test_pack_bin8
|
171
|
+
str = ("a"*32).force_encoding('binary')
|
172
|
+
assert_equal "\xC4#{str.bytesize.chr}#{str}", Mochilo.pack(str)
|
173
|
+
|
174
|
+
str = ("a"*255).force_encoding('binary')
|
175
|
+
assert_equal "\xC4#{str.bytesize.chr}#{str}", Mochilo.pack(str)
|
131
176
|
end
|
132
177
|
|
133
|
-
def
|
134
|
-
|
135
|
-
assert_equal "\
|
178
|
+
def test_pack_bin16
|
179
|
+
str = ("a"*256).force_encoding('binary')
|
180
|
+
assert_equal "\xC5\x01\x00#{str}", Mochilo.pack(str)
|
181
|
+
|
182
|
+
str = ("a"*(2**16-1)).force_encoding('binary')
|
183
|
+
assert_equal "\xC5\xFF\xFF#{str}", Mochilo.pack(str)
|
136
184
|
end
|
137
185
|
|
138
|
-
def
|
139
|
-
|
140
|
-
assert_equal "\
|
186
|
+
def test_pack_bin32
|
187
|
+
str = ("a"*(2**16)).force_encoding('binary')
|
188
|
+
assert_equal "\xC6\x00\x01\x00\x00#{str}", Mochilo.pack(str)
|
189
|
+
|
190
|
+
# This would create a 4GB string, so let's just skip that ;)
|
191
|
+
# str = ("a"*(2**32-1)).force_encoding('UTF-8')
|
192
|
+
# assert_equal "\xC6\x00\x01\x00\x00#{str}", Mochilo.pack(str)
|
141
193
|
end
|
142
194
|
|
143
|
-
def
|
144
|
-
|
145
|
-
fine = ("a"*0xff).to_sym
|
195
|
+
def test_pack_fixed_array
|
196
|
+
assert_equal "\x90", Mochilo.pack([])
|
146
197
|
|
147
|
-
|
148
|
-
|
149
|
-
|
198
|
+
arr = (1..15).to_a
|
199
|
+
expected = "\x9F"
|
200
|
+
arr.each {|i| expected << Mochilo.pack(i)}
|
201
|
+
assert_equal expected, Mochilo.pack(arr)
|
202
|
+
end
|
150
203
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
204
|
+
def test_pack_array16
|
205
|
+
arr = (1..16).to_a
|
206
|
+
expected = "\xDC\x00\x10"
|
207
|
+
arr.each {|i| expected << Mochilo.pack(i)}
|
208
|
+
assert_equal expected, Mochilo.pack(arr)
|
209
|
+
|
210
|
+
arr = (1..2**16-1).to_a
|
211
|
+
expected = "\xDC\xFF\xFF"
|
212
|
+
arr.each {|i| expected << Mochilo.pack(i)}
|
213
|
+
assert_equal expected, Mochilo.pack(arr)
|
156
214
|
end
|
157
215
|
|
158
|
-
def
|
159
|
-
|
160
|
-
|
216
|
+
def test_pack_array32
|
217
|
+
arr = (1..2**16).to_a
|
218
|
+
expected = "\xDD\x00\x01\x00\x00"
|
219
|
+
arr.each {|i| expected << Mochilo.pack(i)}
|
220
|
+
assert_equal expected, Mochilo.pack(arr)
|
221
|
+
|
222
|
+
# This would create an array with 4294967295 entries in it, so let's not
|
223
|
+
# arr = (1..2**32-1).to_a
|
224
|
+
# expected = "\xDD\xFF\xFF\xFF\xFF"
|
225
|
+
# arr.each {|i| expected << Mochilo.pack(i)}
|
226
|
+
# assert_equal expected, Mochilo.pack(arr)
|
161
227
|
end
|
162
228
|
|
163
|
-
def
|
164
|
-
|
229
|
+
def test_pack_fixed_map
|
230
|
+
assert_equal "\x80", Mochilo.pack({})
|
231
|
+
|
232
|
+
arr = (1..15).to_a
|
233
|
+
hash = {}
|
234
|
+
arr.each {|i| hash[i] = i }
|
235
|
+
expected = "\x8F"
|
236
|
+
arr.each {|i| expected << (Mochilo.pack(i) + Mochilo.pack(i))}
|
237
|
+
assert_equal expected, Mochilo.pack(hash)
|
165
238
|
end
|
166
239
|
|
167
|
-
def
|
168
|
-
|
240
|
+
def test_pack_map16
|
241
|
+
arr = (1..16).to_a
|
242
|
+
hash = {}
|
243
|
+
arr.each {|i| hash[i] = i }
|
244
|
+
expected = "\xDE\x00\x10"
|
245
|
+
arr.each {|i| expected << (Mochilo.pack(i) + Mochilo.pack(i))}
|
246
|
+
assert_equal expected, Mochilo.pack(hash)
|
247
|
+
|
248
|
+
arr = (1..2**16-1).to_a
|
249
|
+
hash = {}
|
250
|
+
arr.each {|i| hash[i] = i }
|
251
|
+
expected = "\xDE\xFF\xFF"
|
252
|
+
arr.each {|i| expected << (Mochilo.pack(i) + Mochilo.pack(i))}
|
253
|
+
assert_equal expected, Mochilo.pack(hash)
|
254
|
+
end
|
169
255
|
|
170
|
-
|
256
|
+
def test_pack_map32
|
257
|
+
arr = (1..2**16).to_a
|
258
|
+
hash = {}
|
259
|
+
arr.each {|i| hash[i] = i }
|
260
|
+
expected = "\xDF\x00\x01\x00\x00"
|
261
|
+
arr.each {|i| expected << (Mochilo.pack(i) + Mochilo.pack(i))}
|
262
|
+
assert_equal expected, Mochilo.pack(hash)
|
263
|
+
|
264
|
+
# This would create a hash with 4294967295 entries in it, so let's not
|
265
|
+
# arr = (1..2**32-1).to_a
|
266
|
+
# hash = {}
|
267
|
+
# arr.each {|i| hash[i] = i }
|
268
|
+
# expected = "\xDF\xFF\xFF\xFF\xFF"
|
269
|
+
# arr.each {|i| expected << (Mochilo.pack(i) + Mochilo.pack(i))}
|
270
|
+
# assert_equal expected, Mochilo.pack(hash)
|
171
271
|
end
|
172
272
|
|
173
273
|
def test_pack_unsupported_type
|
@@ -176,27 +276,9 @@ class MochiloPackTest < MiniTest::Unit::TestCase
|
|
176
276
|
end
|
177
277
|
end
|
178
278
|
|
179
|
-
def
|
180
|
-
|
181
|
-
|
182
|
-
[
|
183
|
-
/pa.tern/,
|
184
|
-
/thing/im,
|
185
|
-
].each do |re|
|
186
|
-
assert_equal re, Mochilo.unpack(Mochilo.pack(re))
|
279
|
+
def test_pack_symbol_fails
|
280
|
+
assert_raises Mochilo::PackError do
|
281
|
+
Mochilo.pack(:symbol)
|
187
282
|
end
|
188
283
|
end
|
189
|
-
|
190
|
-
def test_time
|
191
|
-
offset = -13*60*60 # I don't know if this is possible. There shouldn't be anything with a greater absolute value.
|
192
|
-
t = Time.gm(2042, 7, 21, 3, 32, 37, 974010).getlocal(offset)
|
193
|
-
expected = "\xD6" +
|
194
|
-
"\x00\x00\x00\x00\x88\x77\x66\x55" + # sec
|
195
|
-
"\x00\x00\x00\x00\x00\x0E\xDC\xBA" + # usec
|
196
|
-
"\xFF\xFF\x49\x30" # utc_offset
|
197
|
-
assert_equal expected, Mochilo.pack(t)
|
198
|
-
unpacked = Mochilo.unpack(expected)
|
199
|
-
assert_equal t, unpacked
|
200
|
-
assert_equal t.utc_offset, unpacked.utc_offset
|
201
|
-
end
|
202
284
|
end
|
data/test/setup.rb
CHANGED
@@ -14,12 +14,3 @@ require 'minitest/autorun'
|
|
14
14
|
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
15
15
|
$LOAD_PATH.unshift File.expand_path('..', __FILE__)
|
16
16
|
|
17
|
-
class CustomType
|
18
|
-
def initialize(str)
|
19
|
-
@str = str
|
20
|
-
end
|
21
|
-
|
22
|
-
def to_bpack
|
23
|
-
Mochilo.pack(@str)
|
24
|
-
end
|
25
|
-
end
|
data/test/unpack_test.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
# encoding:
|
1
|
+
# encoding: ascii-8bit
|
2
2
|
require File.expand_path('../setup', __FILE__)
|
3
3
|
|
4
4
|
require 'mochilo'
|
5
5
|
|
6
|
-
class MochiloUnpackTest <
|
6
|
+
class MochiloUnpackTest < Minitest::Test
|
7
7
|
BUFFERS = [
|
8
8
|
"\xCC\x80",
|
9
9
|
"\xCD\x04\xD2",
|
@@ -39,14 +39,14 @@ class MochiloUnpackTest < MiniTest::Unit::TestCase
|
|
39
39
|
assert_equal nil, Mochilo.unpack("\xC0")
|
40
40
|
end
|
41
41
|
|
42
|
-
def test_unpack_true
|
43
|
-
assert_equal true, Mochilo.unpack("\xC3")
|
44
|
-
end
|
45
|
-
|
46
42
|
def test_unpack_false
|
47
43
|
assert_equal false, Mochilo.unpack("\xC2")
|
48
44
|
end
|
49
45
|
|
46
|
+
def test_unpack_true
|
47
|
+
assert_equal true, Mochilo.unpack("\xC3")
|
48
|
+
end
|
49
|
+
|
50
50
|
def xtest_unpack_float
|
51
51
|
# ruby uses double's internally so we can't reliably
|
52
52
|
# test for a float without some hacks
|
@@ -57,103 +57,261 @@ class MochiloUnpackTest < MiniTest::Unit::TestCase
|
|
57
57
|
end
|
58
58
|
|
59
59
|
def test_unpack_positive_fixed
|
60
|
-
|
60
|
+
(0..127).each do |int|
|
61
|
+
assert_equal int, Mochilo.unpack(int.chr)
|
62
|
+
end
|
61
63
|
end
|
62
64
|
|
63
65
|
def test_unpack_negative_fixed
|
64
|
-
assert_equal -
|
66
|
+
assert_equal -1, Mochilo.unpack("\xFF")
|
67
|
+
assert_equal -32, Mochilo.unpack("\xE0")
|
65
68
|
end
|
66
69
|
|
67
70
|
def test_unpack_uint8
|
68
|
-
|
71
|
+
[128,255].each do |int|
|
72
|
+
assert_equal int, Mochilo.unpack("\xCC#{int.chr}")
|
73
|
+
end
|
69
74
|
end
|
70
75
|
|
71
76
|
def test_unpack_uint16
|
72
|
-
|
77
|
+
[256,((2**16)-1)].each do |int|
|
78
|
+
packed = [int].pack("n")
|
79
|
+
assert_equal int, Mochilo.unpack("\xCD#{packed}")
|
80
|
+
end
|
73
81
|
end
|
74
82
|
|
75
83
|
def test_unpack_uint32
|
76
|
-
|
84
|
+
[(2**16),((2**32)-1)].each do |int|
|
85
|
+
packed = [int].pack("N")
|
86
|
+
assert_equal int, Mochilo.unpack("\xCE#{packed}")
|
87
|
+
end
|
77
88
|
end
|
78
89
|
|
79
90
|
def test_unpack_uint64
|
80
|
-
|
91
|
+
[(2**32),((2**64)-1)].each do |int|
|
92
|
+
packed = [int].pack("Q>")
|
93
|
+
assert_equal int, Mochilo.unpack("\xCF#{packed}")
|
94
|
+
end
|
81
95
|
end
|
82
96
|
|
83
97
|
def test_unpack_int8
|
84
|
-
assert_equal -
|
98
|
+
assert_equal -33, Mochilo.unpack("\xD0\xDF")
|
99
|
+
assert_equal -128, Mochilo.unpack("\xD0\x80")
|
85
100
|
end
|
86
101
|
|
87
102
|
def test_unpack_int16
|
88
|
-
assert_equal -
|
103
|
+
assert_equal -129, Mochilo.unpack("\xD1\xFF\x7F")
|
104
|
+
assert_equal -32768, Mochilo.unpack("\xD1\x80\x00")
|
89
105
|
end
|
90
106
|
|
91
107
|
def test_unpack_int32
|
92
|
-
assert_equal -
|
108
|
+
assert_equal -32769, Mochilo.unpack("\xD2\xFF\xFF\x7F\xFF")
|
109
|
+
assert_equal -2147483648, Mochilo.unpack("\xD2\x80\x00\x00\x00")
|
93
110
|
end
|
94
111
|
|
95
112
|
def test_unpack_int64
|
96
|
-
assert_equal -
|
113
|
+
assert_equal -2147483649, Mochilo.unpack("\xD3\xFF\xFF\xFF\xFF\x7F\xFF\xFF\xFF")
|
114
|
+
assert_equal -9223372036854775808, Mochilo.unpack("\xD3\x80\x00\x00\x00\x00\x00\x00\x00")
|
97
115
|
end
|
98
116
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
117
|
+
def test_unpack_fixed_str
|
118
|
+
str = "a".force_encoding('UTF-8')
|
119
|
+
val = Mochilo.unpack("\xA1#{str}")
|
120
|
+
assert_equal str, val
|
121
|
+
assert_equal Encoding::UTF_8, val.encoding
|
122
|
+
|
123
|
+
str = ("a"*31).force_encoding('UTF-8')
|
124
|
+
val = Mochilo.unpack("\xBF#{str}")
|
125
|
+
assert_equal str, val
|
126
|
+
assert_equal Encoding::UTF_8, val.encoding
|
104
127
|
end
|
105
128
|
|
106
|
-
def
|
107
|
-
|
129
|
+
def test_unpack_str8
|
130
|
+
str = ("a"*32).force_encoding('UTF-8')
|
131
|
+
val = Mochilo.unpack("\xD9#{str.bytesize.chr}#{str}")
|
132
|
+
assert_equal str, val
|
133
|
+
assert_equal Encoding::UTF_8, val.encoding
|
134
|
+
|
135
|
+
str = ("a"*255).force_encoding('UTF-8')
|
136
|
+
val = Mochilo.unpack("\xD9#{str.bytesize.chr}#{str}")
|
137
|
+
assert_equal str, val
|
138
|
+
assert_equal Encoding::UTF_8, val.encoding
|
108
139
|
end
|
109
140
|
|
110
|
-
def
|
111
|
-
str = "
|
112
|
-
|
141
|
+
def test_unpack_str16
|
142
|
+
str = ("a"*256).force_encoding('UTF-8')
|
143
|
+
val = Mochilo.unpack("\xDA\x01\x00#{str}")
|
144
|
+
assert_equal str, val
|
145
|
+
assert_equal Encoding::UTF_8, val.encoding
|
146
|
+
|
147
|
+
str = ("a"*(2**16-1)).force_encoding('UTF-8')
|
148
|
+
val = Mochilo.unpack("\xDA\xFF\xFF#{str}")
|
149
|
+
assert_equal str, val
|
150
|
+
assert_equal Encoding::UTF_8, val.encoding
|
113
151
|
end
|
114
152
|
|
115
|
-
def
|
116
|
-
|
117
|
-
|
118
|
-
|
153
|
+
def test_unpack_str32
|
154
|
+
str = ("a"*(2**16)).force_encoding('UTF-8')
|
155
|
+
val = Mochilo.unpack("\xDB\x00\x01\x00\x00#{str}")
|
156
|
+
assert_equal str, val
|
157
|
+
assert_equal Encoding::UTF_8, val.encoding
|
158
|
+
|
159
|
+
# This will create a 4GB string, so let's just skip that ;)
|
160
|
+
# str = ("a"*(2**32-1)).force_encoding('UTF-8')
|
161
|
+
# val = Mochilo.unpack("\xDB\xFF\xFF\xFF\xFF#{str}")
|
162
|
+
# assert_equal str, val
|
163
|
+
# assert_equal Encoding::UTF_8, val.encoding
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_unpack_enc8
|
167
|
+
str = ("a"*32).force_encoding('ISO-8859-1')
|
168
|
+
val = Mochilo.unpack("\xC7#{str.bytesize.chr}\x0C#{str}")
|
169
|
+
assert_equal str, val
|
170
|
+
assert_equal Encoding::ISO_8859_1, val.encoding
|
171
|
+
|
172
|
+
str = ("a"*255).force_encoding('ISO-8859-1')
|
173
|
+
val = Mochilo.unpack("\xC7#{str.bytesize.chr}\x0C#{str}")
|
174
|
+
assert_equal str, val
|
175
|
+
assert_equal Encoding::ISO_8859_1, val.encoding
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_unpack_enc16
|
179
|
+
str = ("a"*256).force_encoding('ISO-8859-1')
|
180
|
+
val = Mochilo.unpack("\xC8\x01\x00\x0C#{str}")
|
181
|
+
assert_equal str, val
|
182
|
+
assert_equal Encoding::ISO_8859_1, val.encoding
|
119
183
|
|
120
|
-
|
184
|
+
str = ("a"*(2**16-1)).force_encoding('ISO-8859-1')
|
185
|
+
val = Mochilo.unpack("\xC8\xFF\xFF\x0C#{str}")
|
186
|
+
assert_equal str, val
|
187
|
+
assert_equal Encoding::ISO_8859_1, val.encoding
|
121
188
|
end
|
122
189
|
|
123
|
-
def
|
124
|
-
str = ("a"*
|
125
|
-
|
190
|
+
def test_unpack_enc32
|
191
|
+
str = ("a"*(2**16)).force_encoding('ISO-8859-1')
|
192
|
+
val = Mochilo.unpack("\xC9\x00\x01\x00\x00\x0C#{str}")
|
193
|
+
assert_equal str, val
|
194
|
+
assert_equal Encoding::ISO_8859_1, val.encoding
|
195
|
+
|
196
|
+
# This would create a 4GB string, so let's just skip that ;)
|
197
|
+
# str = ("a"*(2**32-1)).force_encoding('ISO-8859-1')
|
198
|
+
# val = Mochilo.unpack("\xC9\x00\x01\x00\x00\x0C#{str}")
|
199
|
+
# assert_equal str, val
|
200
|
+
# assert_equal Encoding::ISO_8859_1, val.encoding
|
126
201
|
end
|
127
202
|
|
128
|
-
def
|
129
|
-
|
203
|
+
def test_unpack_bin8
|
204
|
+
str = ("a"*32).force_encoding('binary')
|
205
|
+
val = Mochilo.unpack("\xC4#{str.bytesize.chr}#{str}")
|
206
|
+
assert_equal str, val
|
207
|
+
assert_equal Encoding::ASCII_8BIT, val.encoding
|
208
|
+
|
209
|
+
str = ("a"*255).force_encoding('binary')
|
210
|
+
val = Mochilo.unpack("\xC4#{str.bytesize.chr}#{str}")
|
211
|
+
assert_equal str, val
|
212
|
+
assert_equal Encoding::ASCII_8BIT, val.encoding
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_unpack_bin16
|
216
|
+
str = ("a"*256).force_encoding('binary')
|
217
|
+
val = Mochilo.unpack("\xC5\x01\x00#{str}")
|
218
|
+
assert_equal str, val
|
219
|
+
assert_equal Encoding::ASCII_8BIT, val.encoding
|
220
|
+
|
221
|
+
str = ("a"*(2**16-1)).force_encoding('binary')
|
222
|
+
val = Mochilo.unpack("\xC5\xFF\xFF#{str}")
|
223
|
+
assert_equal str, val
|
224
|
+
assert_equal Encoding::ASCII_8BIT, val.encoding
|
225
|
+
end
|
226
|
+
|
227
|
+
def test_unpack_bin32
|
228
|
+
str = ("a"*(2**16)).force_encoding('binary')
|
229
|
+
val = Mochilo.unpack("\xC6\x00\x01\x00\x00#{str}")
|
230
|
+
assert_equal str, val
|
231
|
+
assert_equal Encoding::ASCII_8BIT, val.encoding
|
232
|
+
|
233
|
+
# This would create a 4GB string, so let's just skip that ;)
|
234
|
+
# str = ("a"*(2**32-1)).force_encoding('UTF-8')
|
235
|
+
# val = Mochilo.unpack("\xC6\x00\x01\x00\x00#{str}")
|
236
|
+
# assert_equal str, val
|
237
|
+
# assert_equal Encoding::ASCII_8BIT, val.encoding
|
130
238
|
end
|
131
239
|
|
132
240
|
def test_unpack_fixed_array
|
133
241
|
assert_equal [], Mochilo.unpack("\x90")
|
134
|
-
|
242
|
+
|
243
|
+
arr = (1..15).to_a
|
244
|
+
data = "\x9F"
|
245
|
+
arr.each {|i| data << Mochilo.pack(i)}
|
246
|
+
assert_equal arr, Mochilo.unpack(data)
|
135
247
|
end
|
136
248
|
|
137
249
|
def test_unpack_array16
|
138
|
-
|
139
|
-
|
250
|
+
arr = (1..16).to_a
|
251
|
+
data = "\xDC\x00\x10"
|
252
|
+
arr.each {|i| data << Mochilo.pack(i)}
|
253
|
+
assert_equal arr, Mochilo.unpack(data)
|
254
|
+
|
255
|
+
arr = (1..2**16-1).to_a
|
256
|
+
data = "\xDC\xFF\xFF"
|
257
|
+
arr.each {|i| data << Mochilo.pack(i)}
|
258
|
+
assert_equal arr, Mochilo.unpack(data)
|
140
259
|
end
|
141
260
|
|
142
|
-
def
|
143
|
-
|
261
|
+
def test_unpack_array32
|
262
|
+
arr = (1..2**16).to_a
|
263
|
+
data = "\xDD\x00\x01\x00\x00"
|
264
|
+
arr.each {|i| data << Mochilo.pack(i)}
|
265
|
+
assert_equal arr, Mochilo.unpack(data)
|
266
|
+
|
267
|
+
# This would create an array with 4294967295 entries in it, so let's not
|
268
|
+
# arr = (1..2**32-1).to_a
|
269
|
+
# data = "\xDD\xFF\xFF\xFF\xFF"
|
270
|
+
# arr.each {|i| data << Mochilo.pack(i)}
|
271
|
+
# assert_equal arr, Mochilo.unpack(data)
|
144
272
|
end
|
145
273
|
|
146
|
-
def
|
274
|
+
def test_pack_fixed_map
|
147
275
|
assert_equal({}, Mochilo.unpack("\x80"))
|
148
|
-
|
276
|
+
|
277
|
+
arr = (1..15).to_a
|
278
|
+
hash = {}
|
279
|
+
arr.each {|i| hash[i] = i }
|
280
|
+
data = "\x8F"
|
281
|
+
arr.each {|i| data << (Mochilo.pack(i) + Mochilo.pack(i))}
|
282
|
+
assert_equal hash, Mochilo.unpack(data)
|
149
283
|
end
|
150
284
|
|
151
|
-
def
|
152
|
-
|
153
|
-
|
285
|
+
def test_pack_map16
|
286
|
+
arr = (1..16).to_a
|
287
|
+
hash = {}
|
288
|
+
arr.each {|i| hash[i] = i }
|
289
|
+
data = "\xDE\x00\x10"
|
290
|
+
arr.each {|i| data << (Mochilo.pack(i) + Mochilo.pack(i))}
|
291
|
+
assert_equal hash, Mochilo.unpack(data)
|
292
|
+
|
293
|
+
arr = (1..2**16-1).to_a
|
294
|
+
hash = {}
|
295
|
+
arr.each {|i| hash[i] = i }
|
296
|
+
data = "\xDE\xFF\xFF"
|
297
|
+
arr.each {|i| data << (Mochilo.pack(i) + Mochilo.pack(i))}
|
298
|
+
assert_equal hash, Mochilo.unpack(data)
|
154
299
|
end
|
155
300
|
|
156
|
-
def
|
157
|
-
|
301
|
+
def test_pack_map32
|
302
|
+
arr = (1..2**16).to_a
|
303
|
+
hash = {}
|
304
|
+
arr.each {|i| hash[i] = i }
|
305
|
+
data = "\xDF\x00\x01\x00\x00"
|
306
|
+
arr.each {|i| data << (Mochilo.pack(i) + Mochilo.pack(i))}
|
307
|
+
assert_equal hash, Mochilo.unpack(data)
|
308
|
+
|
309
|
+
# This would create a hash with 4294967295 entries in it, so let's not
|
310
|
+
# arr = (1..2**32-1).to_a
|
311
|
+
# hash = {}
|
312
|
+
# arr.each {|i| hash[i] = i }
|
313
|
+
# data = "\xDF\xFF\xFF\xFF\xFF"
|
314
|
+
# arr.each {|i| data << (Mochilo.pack(i) + Mochilo.pack(i))}
|
315
|
+
# assert_equal hash, Mochilo.unpack(data)
|
158
316
|
end
|
159
317
|
end
|