msgpack 0.6.0pre1-x64-mingw32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.travis.yml +26 -0
- data/ChangeLog +117 -0
- data/Dockerfile +30 -0
- data/Gemfile +4 -0
- data/LICENSE +177 -0
- data/README.rdoc +129 -0
- data/Rakefile +114 -0
- data/bench/pack.rb +23 -0
- data/bench/pack_log.rb +33 -0
- data/bench/pack_log_long.rb +65 -0
- data/bench/run.sh +14 -0
- data/bench/run_long.sh +35 -0
- data/bench/unpack.rb +21 -0
- data/bench/unpack_log.rb +34 -0
- data/bench/unpack_log_long.rb +67 -0
- data/cross-build.sh +9 -0
- data/doclib/msgpack/buffer.rb +193 -0
- data/doclib/msgpack/core_ext.rb +101 -0
- data/doclib/msgpack/error.rb +14 -0
- data/doclib/msgpack/packer.rb +134 -0
- data/doclib/msgpack/unpacker.rb +146 -0
- data/doclib/msgpack.rb +77 -0
- data/ext/java/org/msgpack/jruby/Buffer.java +221 -0
- data/ext/java/org/msgpack/jruby/Decoder.java +201 -0
- data/ext/java/org/msgpack/jruby/Encoder.java +308 -0
- data/ext/java/org/msgpack/jruby/ExtensionValue.java +136 -0
- data/ext/java/org/msgpack/jruby/MessagePackLibrary.java +107 -0
- data/ext/java/org/msgpack/jruby/Packer.java +78 -0
- data/ext/java/org/msgpack/jruby/Types.java +37 -0
- data/ext/java/org/msgpack/jruby/Unpacker.java +170 -0
- data/ext/msgpack/buffer.c +695 -0
- data/ext/msgpack/buffer.h +447 -0
- data/ext/msgpack/buffer_class.c +507 -0
- data/ext/msgpack/buffer_class.h +32 -0
- data/ext/msgpack/compat.h +113 -0
- data/ext/msgpack/core_ext.c +129 -0
- data/ext/msgpack/core_ext.h +26 -0
- data/ext/msgpack/extconf.rb +28 -0
- data/ext/msgpack/packer.c +168 -0
- data/ext/msgpack/packer.h +441 -0
- data/ext/msgpack/packer_class.c +302 -0
- data/ext/msgpack/packer_class.h +30 -0
- data/ext/msgpack/rbinit.c +33 -0
- data/ext/msgpack/rmem.c +94 -0
- data/ext/msgpack/rmem.h +109 -0
- data/ext/msgpack/sysdep.h +115 -0
- data/ext/msgpack/sysdep_endian.h +50 -0
- data/ext/msgpack/sysdep_types.h +46 -0
- data/ext/msgpack/unpacker.c +771 -0
- data/ext/msgpack/unpacker.h +122 -0
- data/ext/msgpack/unpacker_class.c +405 -0
- data/ext/msgpack/unpacker_class.h +32 -0
- data/lib/msgpack/msgpack.so +0 -0
- data/lib/msgpack/version.rb +3 -0
- data/lib/msgpack.rb +13 -0
- data/msgpack.gemspec +31 -0
- data/msgpack.org.md +46 -0
- data/spec/cases.json +1 -0
- data/spec/cases.msg +0 -0
- data/spec/cases_compact.msg +0 -0
- data/spec/cases_spec.rb +39 -0
- data/spec/cruby/buffer_io_spec.rb +256 -0
- data/spec/cruby/buffer_packer.rb +29 -0
- data/spec/cruby/buffer_spec.rb +572 -0
- data/spec/cruby/buffer_unpacker.rb +19 -0
- data/spec/cruby/packer_spec.rb +120 -0
- data/spec/cruby/unpacker_spec.rb +305 -0
- data/spec/format_spec.rb +282 -0
- data/spec/jruby/benchmarks/shootout_bm.rb +73 -0
- data/spec/jruby/benchmarks/symbolize_keys_bm.rb +25 -0
- data/spec/jruby/msgpack/unpacker_spec.rb +290 -0
- data/spec/jruby/msgpack_spec.rb +142 -0
- data/spec/pack_spec.rb +67 -0
- data/spec/random_compat.rb +24 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/unpack_spec.rb +60 -0
- metadata +209 -0
@@ -0,0 +1,572 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'random_compat'
|
3
|
+
|
4
|
+
describe Buffer do
|
5
|
+
STATIC_EXAMPLES = {}
|
6
|
+
STATIC_EXAMPLES[:empty01] = ''
|
7
|
+
STATIC_EXAMPLES[:empty02] = ''
|
8
|
+
STATIC_EXAMPLES[:copy01] = 'short'
|
9
|
+
STATIC_EXAMPLES[:copy02] = 'short'*2
|
10
|
+
STATIC_EXAMPLES[:ref01] = 'short'*128
|
11
|
+
STATIC_EXAMPLES[:ref02] = 'short'*128*2
|
12
|
+
STATIC_EXAMPLES[:ref03] = 'a'*((1024*1024+2)*2)
|
13
|
+
STATIC_EXAMPLES[:refcopy01] = 'short'*128
|
14
|
+
STATIC_EXAMPLES[:expand01] = 'short'*1024
|
15
|
+
STATIC_EXAMPLES[:expand02] = 'short'*(127+1024)
|
16
|
+
STATIC_EXAMPLES[:offset01] = 'ort'+'short'
|
17
|
+
STATIC_EXAMPLES[:offset02] = 'ort'+'short'*127
|
18
|
+
STATIC_EXAMPLES[:offset03] = 'ort'+'short'*(126+1024)
|
19
|
+
|
20
|
+
if ''.respond_to?(:force_encoding)
|
21
|
+
STATIC_EXAMPLES.each_value {|v| v.force_encoding('ASCII-8BIT') }
|
22
|
+
end
|
23
|
+
STATIC_EXAMPLES.each_value {|v| v.freeze }
|
24
|
+
|
25
|
+
r = Random.new
|
26
|
+
random_seed = r.seed
|
27
|
+
puts "buffer random seed: 0x#{random_seed.to_s(16)}"
|
28
|
+
|
29
|
+
let :random_cases_examples do
|
30
|
+
r = Random.new(random_seed)
|
31
|
+
cases = {}
|
32
|
+
examples = {}
|
33
|
+
|
34
|
+
10.times do |i|
|
35
|
+
b = Buffer.new
|
36
|
+
s = r.bytes(0)
|
37
|
+
r.rand(3).times do
|
38
|
+
n = r.rand(1024*1400)
|
39
|
+
x = r.bytes(n)
|
40
|
+
s << x
|
41
|
+
b << x
|
42
|
+
end
|
43
|
+
r.rand(2).times do
|
44
|
+
n = r.rand(1024*1400)
|
45
|
+
b.read(n)
|
46
|
+
s.slice!(0, n)
|
47
|
+
end
|
48
|
+
key = :"random#{"%02d"%i}"
|
49
|
+
cases[key] = b
|
50
|
+
examples[key] = s
|
51
|
+
end
|
52
|
+
|
53
|
+
[cases, examples]
|
54
|
+
end
|
55
|
+
|
56
|
+
let :static_cases do
|
57
|
+
map = {}
|
58
|
+
map[:empty01] = empty01
|
59
|
+
map[:empty02] = empty02
|
60
|
+
map[:copy01] = copy01
|
61
|
+
map[:copy02] = copy02
|
62
|
+
map[:ref01] = ref01
|
63
|
+
map[:ref02] = ref02
|
64
|
+
map[:ref03] = ref03
|
65
|
+
map[:refcopy01] = refcopy01
|
66
|
+
map[:expand01] = expand01
|
67
|
+
map[:expand02] = expand02
|
68
|
+
map[:offset01] = offset01
|
69
|
+
map[:offset02] = offset02
|
70
|
+
map[:offset03] = offset03
|
71
|
+
map
|
72
|
+
end
|
73
|
+
|
74
|
+
let :static_examples do
|
75
|
+
STATIC_EXAMPLES
|
76
|
+
end
|
77
|
+
|
78
|
+
let :random_cases do
|
79
|
+
random_cases_examples[0]
|
80
|
+
end
|
81
|
+
|
82
|
+
let :random_examples do
|
83
|
+
random_cases_examples[1]
|
84
|
+
end
|
85
|
+
|
86
|
+
let :cases do
|
87
|
+
static_cases.merge(random_cases)
|
88
|
+
end
|
89
|
+
|
90
|
+
let :examples do
|
91
|
+
static_examples.merge(random_examples)
|
92
|
+
end
|
93
|
+
|
94
|
+
let :case_keys do
|
95
|
+
examples.keys
|
96
|
+
end
|
97
|
+
|
98
|
+
let :empty01 do
|
99
|
+
Buffer.new
|
100
|
+
end
|
101
|
+
|
102
|
+
let :empty02 do
|
103
|
+
b = Buffer.new
|
104
|
+
b << 'a'
|
105
|
+
b.read_all(1)
|
106
|
+
b
|
107
|
+
end
|
108
|
+
|
109
|
+
let :copy01 do
|
110
|
+
# one copy chunk
|
111
|
+
b = Buffer.new
|
112
|
+
b << 'short'
|
113
|
+
b
|
114
|
+
end
|
115
|
+
|
116
|
+
let :copy02 do
|
117
|
+
# one copy chunk
|
118
|
+
b = Buffer.new
|
119
|
+
b << 'short'
|
120
|
+
b << 'short'
|
121
|
+
b
|
122
|
+
end
|
123
|
+
|
124
|
+
let :expand02 do
|
125
|
+
# one copy chunk / expanded
|
126
|
+
b = Buffer.new
|
127
|
+
1024.times do
|
128
|
+
b << 'short'
|
129
|
+
end
|
130
|
+
b
|
131
|
+
end
|
132
|
+
|
133
|
+
let :ref01 do
|
134
|
+
# one reference chunk
|
135
|
+
b = Buffer.new
|
136
|
+
b << 'short'*128
|
137
|
+
b.to_s
|
138
|
+
b
|
139
|
+
end
|
140
|
+
|
141
|
+
let :ref02 do
|
142
|
+
# two reference chunks
|
143
|
+
b = Buffer.new
|
144
|
+
b << 'short'*128
|
145
|
+
b.to_s
|
146
|
+
b << 'short'*128
|
147
|
+
b.to_a
|
148
|
+
b
|
149
|
+
end
|
150
|
+
|
151
|
+
let :ref03 do
|
152
|
+
# two reference chunks
|
153
|
+
b = Buffer.new
|
154
|
+
b << 'a'*(1024*1024+2)
|
155
|
+
b << 'a'*(1024*1024+2)
|
156
|
+
b
|
157
|
+
end
|
158
|
+
|
159
|
+
let :refcopy01 do
|
160
|
+
# one reference chunk + one copy chunk
|
161
|
+
b = Buffer.new
|
162
|
+
b << 'short'*127
|
163
|
+
b.to_s
|
164
|
+
b << 'short'
|
165
|
+
b
|
166
|
+
end
|
167
|
+
|
168
|
+
let :expand01 do
|
169
|
+
# one copy chunk / expanded
|
170
|
+
b = Buffer.new
|
171
|
+
1024.times do
|
172
|
+
b << 'short'
|
173
|
+
end
|
174
|
+
b
|
175
|
+
end
|
176
|
+
|
177
|
+
let :expand02 do
|
178
|
+
# one reference chunk + one copy chunk / expanded
|
179
|
+
b = Buffer.new
|
180
|
+
b << 'short'*127
|
181
|
+
b.to_s
|
182
|
+
1024.times do
|
183
|
+
b << 'short'
|
184
|
+
end
|
185
|
+
b
|
186
|
+
end
|
187
|
+
|
188
|
+
let :offset01 do
|
189
|
+
# one copy chunk / offset
|
190
|
+
b = Buffer.new
|
191
|
+
b << 'short'
|
192
|
+
b << 'short'
|
193
|
+
b.skip(2)
|
194
|
+
b
|
195
|
+
end
|
196
|
+
|
197
|
+
let :offset02 do
|
198
|
+
# one reference chunk / offset
|
199
|
+
b = Buffer.new
|
200
|
+
b << 'short'*127
|
201
|
+
b.to_s
|
202
|
+
b.skip(2)
|
203
|
+
b << 'short'
|
204
|
+
b
|
205
|
+
end
|
206
|
+
|
207
|
+
let :offset03 do
|
208
|
+
# one reference chunk / offset + one copy chunk / expanded
|
209
|
+
b = Buffer.new
|
210
|
+
b << 'short'*127
|
211
|
+
b.to_s
|
212
|
+
1024.times do
|
213
|
+
b << 'short'
|
214
|
+
end
|
215
|
+
b.skip(2)
|
216
|
+
b
|
217
|
+
end
|
218
|
+
|
219
|
+
it 'empty?' do
|
220
|
+
empty01.empty?.should == true
|
221
|
+
empty02.empty?.should == true
|
222
|
+
end
|
223
|
+
|
224
|
+
it 'size' do
|
225
|
+
case_keys.each {|k|
|
226
|
+
cases[k].size.should == examples[k].size
|
227
|
+
}
|
228
|
+
end
|
229
|
+
|
230
|
+
it 'short write increments size' do
|
231
|
+
case_keys.each {|k|
|
232
|
+
sz = examples[k].size
|
233
|
+
10.times do |i|
|
234
|
+
cases[k].write 'short'
|
235
|
+
sz += 'short'.size
|
236
|
+
cases[k].size.should == sz
|
237
|
+
end
|
238
|
+
}
|
239
|
+
end
|
240
|
+
|
241
|
+
it 'read with limit decrements size' do
|
242
|
+
case_keys.each {|k|
|
243
|
+
sz = examples[k].size
|
244
|
+
next if sz < 2
|
245
|
+
|
246
|
+
cases[k].read(1).should == examples[k][0,1]
|
247
|
+
sz -= 1
|
248
|
+
cases[k].size.should == sz
|
249
|
+
|
250
|
+
cases[k].read(1).should == examples[k][1,1]
|
251
|
+
sz -= 1
|
252
|
+
cases[k].size.should == sz
|
253
|
+
}
|
254
|
+
end
|
255
|
+
|
256
|
+
it 'read_all with limit decrements size' do
|
257
|
+
case_keys.each {|k|
|
258
|
+
sz = examples[k].size
|
259
|
+
next if sz < 2
|
260
|
+
|
261
|
+
cases[k].read_all(1).should == examples[k][0,1]
|
262
|
+
sz -= 1
|
263
|
+
cases[k].size.should == sz
|
264
|
+
|
265
|
+
cases[k].read_all(1).should == examples[k][1,1]
|
266
|
+
sz -= 1
|
267
|
+
cases[k].size.should == sz
|
268
|
+
}
|
269
|
+
end
|
270
|
+
|
271
|
+
it 'skip decrements size' do
|
272
|
+
case_keys.each {|k|
|
273
|
+
sz = examples[k].size
|
274
|
+
next if sz < 2
|
275
|
+
|
276
|
+
cases[k].skip(1).should == 1
|
277
|
+
sz -= 1
|
278
|
+
cases[k].size.should == sz
|
279
|
+
|
280
|
+
cases[k].skip(1).should == 1
|
281
|
+
sz -= 1
|
282
|
+
cases[k].size.should == sz
|
283
|
+
}
|
284
|
+
end
|
285
|
+
|
286
|
+
it 'skip_all decrements size' do
|
287
|
+
case_keys.each {|k|
|
288
|
+
sz = examples[k].size
|
289
|
+
next if sz < 2
|
290
|
+
|
291
|
+
cases[k].skip_all(1)
|
292
|
+
sz -= 1
|
293
|
+
cases[k].size.should == sz
|
294
|
+
|
295
|
+
cases[k].skip_all(1)
|
296
|
+
sz -= 1
|
297
|
+
cases[k].size.should == sz
|
298
|
+
}
|
299
|
+
end
|
300
|
+
|
301
|
+
it 'read_all against insufficient buffer raises EOFError and consumes nothing' do
|
302
|
+
case_keys.each {|k|
|
303
|
+
sz = examples[k].size
|
304
|
+
lambda {
|
305
|
+
cases[k].read_all(sz+1)
|
306
|
+
}.should raise_error(EOFError)
|
307
|
+
cases[k].size.should == sz
|
308
|
+
}
|
309
|
+
end
|
310
|
+
|
311
|
+
it 'skip_all against insufficient buffer raises EOFError and consumes nothing' do
|
312
|
+
case_keys.each {|k|
|
313
|
+
sz = examples[k].size
|
314
|
+
lambda {
|
315
|
+
cases[k].skip_all(sz+1)
|
316
|
+
}.should raise_error(EOFError)
|
317
|
+
cases[k].size.should == sz
|
318
|
+
}
|
319
|
+
end
|
320
|
+
|
321
|
+
it 'read against insufficient buffer consumes all buffer or return nil' do
|
322
|
+
case_keys.each {|k|
|
323
|
+
sz = examples[k].size
|
324
|
+
if sz == 0
|
325
|
+
cases[k].read(sz+1).should == nil
|
326
|
+
else
|
327
|
+
cases[k].read(sz+1).should == examples[k]
|
328
|
+
end
|
329
|
+
cases[k].size.should == 0
|
330
|
+
}
|
331
|
+
end
|
332
|
+
|
333
|
+
it 'skip against insufficient buffer consumes all buffer' do
|
334
|
+
case_keys.each {|k|
|
335
|
+
sz = examples[k].size
|
336
|
+
cases[k].skip(sz+1).should == examples[k].size
|
337
|
+
cases[k].size.should == 0
|
338
|
+
}
|
339
|
+
end
|
340
|
+
|
341
|
+
it 'read with no arguments consumes all buffer and returns string and do not return nil' do
|
342
|
+
case_keys.each {|k|
|
343
|
+
cases[k].read_all.should == examples[k]
|
344
|
+
cases[k].size.should == 0
|
345
|
+
}
|
346
|
+
end
|
347
|
+
|
348
|
+
it 'read_all with no arguments consumes all buffer and returns string' do
|
349
|
+
case_keys.each {|k|
|
350
|
+
cases[k].read_all.should == examples[k]
|
351
|
+
cases[k].size.should == 0
|
352
|
+
}
|
353
|
+
end
|
354
|
+
|
355
|
+
it 'to_s returns a string and consume nothing' do
|
356
|
+
case_keys.each {|k|
|
357
|
+
cases[k].to_s.should == examples[k]
|
358
|
+
cases[k].size.should == examples[k].size
|
359
|
+
}
|
360
|
+
end
|
361
|
+
|
362
|
+
it 'to_s and modify' do
|
363
|
+
case_keys.each {|k|
|
364
|
+
s = cases[k].to_s
|
365
|
+
s << 'x'
|
366
|
+
cases[k].to_s.should == examples[k]
|
367
|
+
}
|
368
|
+
end
|
369
|
+
|
370
|
+
it 'big write and modify reference' do
|
371
|
+
big2 = "a" * (1024*1024 + 2)
|
372
|
+
|
373
|
+
case_keys.each {|k|
|
374
|
+
big1 = "a" * (1024*1024 + 2)
|
375
|
+
cases[k].write(big1)
|
376
|
+
big1 << 'x'
|
377
|
+
cases[k].read.should == examples[k] + big2
|
378
|
+
}
|
379
|
+
end
|
380
|
+
|
381
|
+
it 'big write -> short write' do
|
382
|
+
biglen = 1024*1024 + 2
|
383
|
+
big1 = "a" * (1024*1024 + 2)
|
384
|
+
|
385
|
+
case_keys.each {|k|
|
386
|
+
sz = examples[k].size
|
387
|
+
|
388
|
+
cases[k].write big1
|
389
|
+
cases[k].size.should == sz + big1.size
|
390
|
+
|
391
|
+
cases[k].write("c")
|
392
|
+
cases[k].size.should == sz + big1.size + 1
|
393
|
+
|
394
|
+
cases[k].read_all.should == examples[k] + big1 + "c"
|
395
|
+
cases[k].size.should == 0
|
396
|
+
cases[k].empty?.should == true
|
397
|
+
}
|
398
|
+
end
|
399
|
+
|
400
|
+
it 'big write 2'do
|
401
|
+
big1 = "a" * (1024*1024 + 2)
|
402
|
+
big2 = "b" * (1024*1024 + 2)
|
403
|
+
|
404
|
+
case_keys.each {|k|
|
405
|
+
sz = examples[k].size
|
406
|
+
|
407
|
+
cases[k].write big1
|
408
|
+
cases[k].write big2
|
409
|
+
cases[k].size.should == sz + big1.size + big2.size
|
410
|
+
|
411
|
+
cases[k].read_all(sz + big1.size).should == examples[k] + big1
|
412
|
+
cases[k].size.should == big2.size
|
413
|
+
|
414
|
+
cases[k].read.should == big2
|
415
|
+
cases[k].size.should == 0
|
416
|
+
cases[k].empty?.should == true
|
417
|
+
}
|
418
|
+
end
|
419
|
+
|
420
|
+
it 'read 0' do
|
421
|
+
case_keys.each {|k|
|
422
|
+
cases[k].read(0).should == ''
|
423
|
+
cases[k].read.should == examples[k]
|
424
|
+
}
|
425
|
+
end
|
426
|
+
|
427
|
+
it 'skip 0' do
|
428
|
+
case_keys.each {|k|
|
429
|
+
cases[k].skip(0).should == 0
|
430
|
+
cases[k].read.should == examples[k]
|
431
|
+
}
|
432
|
+
end
|
433
|
+
|
434
|
+
it 'read_all 0' do
|
435
|
+
case_keys.each {|k|
|
436
|
+
cases[k].read_all(0).should == ''
|
437
|
+
cases[k].read_all.should == examples[k]
|
438
|
+
}
|
439
|
+
end
|
440
|
+
|
441
|
+
it 'skip_all 0' do
|
442
|
+
case_keys.each {|k|
|
443
|
+
cases[k].skip_all(0)
|
444
|
+
cases[k].read_all.should == examples[k]
|
445
|
+
}
|
446
|
+
end
|
447
|
+
|
448
|
+
it 'write_to' do
|
449
|
+
case_keys.each {|k|
|
450
|
+
sio = StringIO.new
|
451
|
+
cases[k].write_to(sio).should == examples[k].size
|
452
|
+
cases[k].size.should == 0
|
453
|
+
sio.string.should == examples[k]
|
454
|
+
}
|
455
|
+
end
|
456
|
+
|
457
|
+
it 'random read/write' do
|
458
|
+
r = Random.new(random_seed)
|
459
|
+
s = r.bytes(0)
|
460
|
+
b = Buffer.new
|
461
|
+
|
462
|
+
10.times {
|
463
|
+
# write
|
464
|
+
r.rand(4).times do
|
465
|
+
n = r.rand(1024*1400)
|
466
|
+
x = r.bytes(n)
|
467
|
+
s << x
|
468
|
+
b.write(x)
|
469
|
+
end
|
470
|
+
|
471
|
+
# read
|
472
|
+
r.rand(3).times do
|
473
|
+
n = r.rand(1024*1400)
|
474
|
+
ex = s.slice!(0, n)
|
475
|
+
ex = nil if ex.empty?
|
476
|
+
x = b.read(n)
|
477
|
+
x.size == ex.size if x != nil
|
478
|
+
x.should == ex
|
479
|
+
b.size.should == s.size
|
480
|
+
end
|
481
|
+
}
|
482
|
+
end
|
483
|
+
|
484
|
+
it 'random read_all/write' do
|
485
|
+
r = Random.new(random_seed)
|
486
|
+
s = r.bytes(0)
|
487
|
+
b = Buffer.new
|
488
|
+
|
489
|
+
10.times {
|
490
|
+
# write
|
491
|
+
r.rand(4).times do
|
492
|
+
n = r.rand(1024*1400)
|
493
|
+
x = r.bytes(n)
|
494
|
+
s << x
|
495
|
+
b.write(x)
|
496
|
+
end
|
497
|
+
|
498
|
+
# read_all
|
499
|
+
r.rand(3).times do
|
500
|
+
n = r.rand(1024*1400)
|
501
|
+
begin
|
502
|
+
x = b.read_all(n)
|
503
|
+
ex = s.slice!(0, n)
|
504
|
+
x.size == n
|
505
|
+
x.should == ex
|
506
|
+
b.size.should == s.size
|
507
|
+
rescue EOFError
|
508
|
+
b.size.should == s.size
|
509
|
+
b.read.should == s
|
510
|
+
s.clear
|
511
|
+
break
|
512
|
+
end
|
513
|
+
end
|
514
|
+
}
|
515
|
+
end
|
516
|
+
|
517
|
+
it 'random skip write' do
|
518
|
+
r = Random.new(random_seed)
|
519
|
+
s = r.bytes(0)
|
520
|
+
b = Buffer.new
|
521
|
+
|
522
|
+
10.times {
|
523
|
+
# write
|
524
|
+
r.rand(4).times do
|
525
|
+
n = r.rand(1024*1400)
|
526
|
+
x = r.bytes(n)
|
527
|
+
s << x
|
528
|
+
b.write(x)
|
529
|
+
end
|
530
|
+
|
531
|
+
# skip
|
532
|
+
r.rand(3).times do
|
533
|
+
n = r.rand(1024*1400)
|
534
|
+
ex = s.slice!(0, n)
|
535
|
+
b.skip(n).should == ex.size
|
536
|
+
b.size.should == s.size
|
537
|
+
end
|
538
|
+
}
|
539
|
+
end
|
540
|
+
|
541
|
+
it 'random skip_all write' do
|
542
|
+
r = Random.new(random_seed)
|
543
|
+
s = r.bytes(0)
|
544
|
+
b = Buffer.new
|
545
|
+
|
546
|
+
10.times {
|
547
|
+
# write
|
548
|
+
r.rand(4).times do
|
549
|
+
n = r.rand(1024*1400)
|
550
|
+
x = r.bytes(n)
|
551
|
+
s << x
|
552
|
+
b.write(x)
|
553
|
+
end
|
554
|
+
|
555
|
+
# skip_all
|
556
|
+
r.rand(3).times do
|
557
|
+
n = r.rand(1024*1400)
|
558
|
+
begin
|
559
|
+
b.skip_all(n)
|
560
|
+
ex = s.slice!(0, n)
|
561
|
+
b.size.should == s.size
|
562
|
+
ensure EOFError
|
563
|
+
b.size.should == s.size
|
564
|
+
b.read.should == s
|
565
|
+
s.clear
|
566
|
+
break
|
567
|
+
end
|
568
|
+
end
|
569
|
+
}
|
570
|
+
end
|
571
|
+
end
|
572
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: ascii-8bit
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Unpacker do
|
5
|
+
let :unpacker do
|
6
|
+
Unpacker.new
|
7
|
+
end
|
8
|
+
|
9
|
+
let :packer do
|
10
|
+
Packer.new
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'buffer' do
|
14
|
+
o1 = unpacker.buffer.object_id
|
15
|
+
unpacker.buffer << 'frsyuki'
|
16
|
+
unpacker.buffer.to_s.should == 'frsyuki'
|
17
|
+
unpacker.buffer.object_id.should == o1
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
# encoding: ascii-8bit
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
require 'stringio'
|
5
|
+
if defined?(Encoding)
|
6
|
+
Encoding.default_external = 'ASCII-8BIT'
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Packer do
|
10
|
+
let :packer do
|
11
|
+
Packer.new
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'initialize' do
|
15
|
+
Packer.new
|
16
|
+
Packer.new(nil)
|
17
|
+
Packer.new(StringIO.new)
|
18
|
+
Packer.new({})
|
19
|
+
Packer.new(StringIO.new, {})
|
20
|
+
end
|
21
|
+
|
22
|
+
#it 'Packer' do
|
23
|
+
# Packer(packer).object_id.should == packer.object_id
|
24
|
+
# Packer(nil).class.should == Packer
|
25
|
+
# Packer('').class.should == Packer
|
26
|
+
# Packer('initbuf').to_s.should == 'initbuf'
|
27
|
+
#end
|
28
|
+
|
29
|
+
it 'write' do
|
30
|
+
packer.write([])
|
31
|
+
packer.to_s.should == "\x90"
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'write_nil' do
|
35
|
+
packer.write_nil
|
36
|
+
packer.to_s.should == "\xc0"
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'write_array_header 0' do
|
40
|
+
packer.write_array_header(0)
|
41
|
+
packer.to_s.should == "\x90"
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'write_array_header 1' do
|
45
|
+
packer.write_array_header(1)
|
46
|
+
packer.to_s.should == "\x91"
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'write_map_header 0' do
|
50
|
+
packer.write_map_header(0)
|
51
|
+
packer.to_s.should == "\x80"
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'write_map_header 1' do
|
55
|
+
packer.write_map_header(1)
|
56
|
+
packer.to_s.should == "\x81"
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'flush' do
|
60
|
+
io = StringIO.new
|
61
|
+
pk = Packer.new(io)
|
62
|
+
pk.write_nil
|
63
|
+
pk.flush
|
64
|
+
pk.to_s.should == ''
|
65
|
+
io.string.should == "\xc0"
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'to_msgpack returns String' do
|
69
|
+
nil.to_msgpack.class.should == String
|
70
|
+
true.to_msgpack.class.should == String
|
71
|
+
false.to_msgpack.class.should == String
|
72
|
+
1.to_msgpack.class.should == String
|
73
|
+
1.0.to_msgpack.class.should == String
|
74
|
+
"".to_msgpack.class.should == String
|
75
|
+
Hash.new.to_msgpack.class.should == String
|
76
|
+
Array.new.to_msgpack.class.should == String
|
77
|
+
end
|
78
|
+
|
79
|
+
class CustomPack01
|
80
|
+
def to_msgpack(pk=nil)
|
81
|
+
return MessagePack.pack(self, pk) unless pk.class == MessagePack::Packer
|
82
|
+
pk.write_array_header(2)
|
83
|
+
pk.write(1)
|
84
|
+
pk.write(2)
|
85
|
+
return pk
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
class CustomPack02
|
90
|
+
def to_msgpack(pk=nil)
|
91
|
+
[1,2].to_msgpack(pk)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'calls custom to_msgpack method' do
|
96
|
+
MessagePack.pack(CustomPack01.new).should == [1,2].to_msgpack
|
97
|
+
MessagePack.pack(CustomPack02.new).should == [1,2].to_msgpack
|
98
|
+
CustomPack01.new.to_msgpack.should == [1,2].to_msgpack
|
99
|
+
CustomPack02.new.to_msgpack.should == [1,2].to_msgpack
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'calls custom to_msgpack method with io' do
|
103
|
+
s01 = StringIO.new
|
104
|
+
MessagePack.pack(CustomPack01.new, s01)
|
105
|
+
s01.string.should == [1,2].to_msgpack
|
106
|
+
|
107
|
+
s02 = StringIO.new
|
108
|
+
MessagePack.pack(CustomPack02.new, s02)
|
109
|
+
s02.string.should == [1,2].to_msgpack
|
110
|
+
|
111
|
+
s03 = StringIO.new
|
112
|
+
CustomPack01.new.to_msgpack(s03)
|
113
|
+
s03.string.should == [1,2].to_msgpack
|
114
|
+
|
115
|
+
s04 = StringIO.new
|
116
|
+
CustomPack02.new.to_msgpack(s04)
|
117
|
+
s04.string.should == [1,2].to_msgpack
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|