-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathbuffer_spec.rb
More file actions
572 lines (492 loc) · 11.1 KB
/
Copy pathbuffer_spec.rb
File metadata and controls
572 lines (492 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
require 'spec_helper'
require 'random_compat'
describe Buffer do
STATIC_EXAMPLES = {}
STATIC_EXAMPLES[:empty01] = ''
STATIC_EXAMPLES[:empty02] = ''
STATIC_EXAMPLES[:copy01] = 'short'
STATIC_EXAMPLES[:copy02] = 'short'*2
STATIC_EXAMPLES[:ref01] = 'short'*128
STATIC_EXAMPLES[:ref02] = 'short'*128*2
STATIC_EXAMPLES[:ref03] = 'a'*((1024*1024+2)*2)
STATIC_EXAMPLES[:refcopy01] = 'short'*128
STATIC_EXAMPLES[:expand01] = 'short'*1024
STATIC_EXAMPLES[:expand02] = 'short'*(127+1024)
STATIC_EXAMPLES[:offset01] = 'ort'+'short'
STATIC_EXAMPLES[:offset02] = 'ort'+'short'*127
STATIC_EXAMPLES[:offset03] = 'ort'+'short'*(126+1024)
if ''.respond_to?(:force_encoding)
STATIC_EXAMPLES.each_value {|v| v.force_encoding('ASCII-8BIT') }
end
STATIC_EXAMPLES.each_value {|v| v.freeze }
r = Random.new
random_seed = r.seed
puts "buffer random seed: 0x#{random_seed.to_s(16)}"
let :random_cases_examples do
r = Random.new(random_seed)
cases = {}
examples = {}
10.times do |i|
b = Buffer.new
s = r.bytes(0)
r.rand(3).times do
n = r.rand(1024*1400)
x = r.bytes(n)
s << x
b << x
end
r.rand(2).times do
n = r.rand(1024*1400)
b.read(n)
s.slice!(0, n)
end
key = :"random#{"%02d"%i}"
cases[key] = b
examples[key] = s
end
[cases, examples]
end
let :static_cases do
map = {}
map[:empty01] = empty01
map[:empty02] = empty02
map[:copy01] = copy01
map[:copy02] = copy02
map[:ref01] = ref01
map[:ref02] = ref02
map[:ref03] = ref03
map[:refcopy01] = refcopy01
map[:expand01] = expand01
map[:expand02] = expand02
map[:offset01] = offset01
map[:offset02] = offset02
map[:offset03] = offset03
map
end
let :static_examples do
STATIC_EXAMPLES
end
let :random_cases do
random_cases_examples[0]
end
let :random_examples do
random_cases_examples[1]
end
let :cases do
static_cases.merge(random_cases)
end
let :examples do
static_examples.merge(random_examples)
end
let :case_keys do
examples.keys
end
let :empty01 do
Buffer.new
end
let :empty02 do
b = Buffer.new
b << 'a'
b.read_all(1)
b
end
let :copy01 do
# one copy chunk
b = Buffer.new
b << 'short'
b
end
let :copy02 do
# one copy chunk
b = Buffer.new
b << 'short'
b << 'short'
b
end
let :expand02 do
# one copy chunk / expanded
b = Buffer.new
1024.times do
b << 'short'
end
b
end
let :ref01 do
# one reference chunk
b = Buffer.new
b << 'short'*128
b.to_s
b
end
let :ref02 do
# two reference chunks
b = Buffer.new
b << 'short'*128
b.to_s
b << 'short'*128
b.to_a
b
end
let :ref03 do
# two reference chunks
b = Buffer.new
b << 'a'*(1024*1024+2)
b << 'a'*(1024*1024+2)
b
end
let :refcopy01 do
# one reference chunk + one copy chunk
b = Buffer.new
b << 'short'*127
b.to_s
b << 'short'
b
end
let :expand01 do
# one copy chunk / expanded
b = Buffer.new
1024.times do
b << 'short'
end
b
end
let :expand02 do
# one reference chunk + one copy chunk / expanded
b = Buffer.new
b << 'short'*127
b.to_s
1024.times do
b << 'short'
end
b
end
let :offset01 do
# one copy chunk / offset
b = Buffer.new
b << 'short'
b << 'short'
b.skip(2)
b
end
let :offset02 do
# one reference chunk / offset
b = Buffer.new
b << 'short'*127
b.to_s
b.skip(2)
b << 'short'
b
end
let :offset03 do
# one reference chunk / offset + one copy chunk / expanded
b = Buffer.new
b << 'short'*127
b.to_s
1024.times do
b << 'short'
end
b.skip(2)
b
end
it 'empty?' do
empty01.empty?.should == true
empty02.empty?.should == true
end
it 'size' do
case_keys.each {|k|
cases[k].size.should == examples[k].size
}
end
it 'short write increments size' do
case_keys.each {|k|
sz = examples[k].size
10.times do |i|
cases[k].write 'short'
sz += 'short'.size
cases[k].size.should == sz
end
}
end
it 'read with limit decrements size' do
case_keys.each {|k|
sz = examples[k].size
next if sz < 2
cases[k].read(1).should == examples[k][0,1]
sz -= 1
cases[k].size.should == sz
cases[k].read(1).should == examples[k][1,1]
sz -= 1
cases[k].size.should == sz
}
end
it 'read_all with limit decrements size' do
case_keys.each {|k|
sz = examples[k].size
next if sz < 2
cases[k].read_all(1).should == examples[k][0,1]
sz -= 1
cases[k].size.should == sz
cases[k].read_all(1).should == examples[k][1,1]
sz -= 1
cases[k].size.should == sz
}
end
it 'skip decrements size' do
case_keys.each {|k|
sz = examples[k].size
next if sz < 2
cases[k].skip(1).should == 1
sz -= 1
cases[k].size.should == sz
cases[k].skip(1).should == 1
sz -= 1
cases[k].size.should == sz
}
end
it 'skip_all decrements size' do
case_keys.each {|k|
sz = examples[k].size
next if sz < 2
cases[k].skip_all(1)
sz -= 1
cases[k].size.should == sz
cases[k].skip_all(1)
sz -= 1
cases[k].size.should == sz
}
end
it 'read_all against insufficient buffer raises EOFError and consumes nothing' do
case_keys.each {|k|
sz = examples[k].size
lambda {
cases[k].read_all(sz+1)
}.should raise_error(EOFError)
cases[k].size.should == sz
}
end
it 'skip_all against insufficient buffer raises EOFError and consumes nothing' do
case_keys.each {|k|
sz = examples[k].size
lambda {
cases[k].skip_all(sz+1)
}.should raise_error(EOFError)
cases[k].size.should == sz
}
end
it 'read against insufficient buffer consumes all buffer or return nil' do
case_keys.each {|k|
sz = examples[k].size
if sz == 0
cases[k].read(sz+1).should == nil
else
cases[k].read(sz+1).should == examples[k]
end
cases[k].size.should == 0
}
end
it 'skip against insufficient buffer consumes all buffer' do
case_keys.each {|k|
sz = examples[k].size
cases[k].skip(sz+1).should == examples[k].size
cases[k].size.should == 0
}
end
it 'read with no arguments consumes all buffer and returns string and do not return nil' do
case_keys.each {|k|
cases[k].read_all.should == examples[k]
cases[k].size.should == 0
}
end
it 'read_all with no arguments consumes all buffer and returns string' do
case_keys.each {|k|
cases[k].read_all.should == examples[k]
cases[k].size.should == 0
}
end
it 'to_s returns a string and consume nothing' do
case_keys.each {|k|
cases[k].to_s.should == examples[k]
cases[k].size.should == examples[k].size
}
end
it 'to_s and modify' do
case_keys.each {|k|
s = cases[k].to_s
s << 'x'
cases[k].to_s.should == examples[k]
}
end
it 'big write and modify reference' do
big2 = "a" * (1024*1024 + 2)
case_keys.each {|k|
big1 = "a" * (1024*1024 + 2)
cases[k].write(big1)
big1 << 'x'
cases[k].read.should == examples[k] + big2
}
end
it 'big write -> short write' do
biglen = 1024*1024 + 2
big1 = "a" * (1024*1024 + 2)
case_keys.each {|k|
sz = examples[k].size
cases[k].write big1
cases[k].size.should == sz + big1.size
cases[k].write("c")
cases[k].size.should == sz + big1.size + 1
cases[k].read_all.should == examples[k] + big1 + "c"
cases[k].size.should == 0
cases[k].empty?.should == true
}
end
it 'big write 2'do
big1 = "a" * (1024*1024 + 2)
big2 = "b" * (1024*1024 + 2)
case_keys.each {|k|
sz = examples[k].size
cases[k].write big1
cases[k].write big2
cases[k].size.should == sz + big1.size + big2.size
cases[k].read_all(sz + big1.size).should == examples[k] + big1
cases[k].size.should == big2.size
cases[k].read.should == big2
cases[k].size.should == 0
cases[k].empty?.should == true
}
end
it 'read 0' do
case_keys.each {|k|
cases[k].read(0).should == ''
cases[k].read.should == examples[k]
}
end
it 'skip 0' do
case_keys.each {|k|
cases[k].skip(0).should == 0
cases[k].read.should == examples[k]
}
end
it 'read_all 0' do
case_keys.each {|k|
cases[k].read_all(0).should == ''
cases[k].read_all.should == examples[k]
}
end
it 'skip_all 0' do
case_keys.each {|k|
cases[k].skip_all(0)
cases[k].read_all.should == examples[k]
}
end
it 'write_to' do
case_keys.each {|k|
sio = StringIO.new
cases[k].write_to(sio).should == examples[k].size
cases[k].size.should == 0
sio.string.should == examples[k]
}
end
it 'random read/write' do
r = Random.new(random_seed)
s = r.bytes(0)
b = Buffer.new
10.times {
# write
r.rand(4).times do
n = r.rand(1024*1400)
x = r.bytes(n)
s << x
b.write(x)
end
# read
r.rand(3).times do
n = r.rand(1024*1400)
ex = s.slice!(0, n)
ex = nil if ex.empty?
x = b.read(n)
x.size == ex.size if x != nil
x.should == ex
b.size.should == s.size
end
}
end
it 'random read_all/write' do
r = Random.new(random_seed)
s = r.bytes(0)
b = Buffer.new
10.times {
# write
r.rand(4).times do
n = r.rand(1024*1400)
x = r.bytes(n)
s << x
b.write(x)
end
# read_all
r.rand(3).times do
n = r.rand(1024*1400)
begin
x = b.read_all(n)
ex = s.slice!(0, n)
x.size == n
x.should == ex
b.size.should == s.size
rescue EOFError
b.size.should == s.size
b.read.should == s
s.clear
break
end
end
}
end
it 'random skip write' do
r = Random.new(random_seed)
s = r.bytes(0)
b = Buffer.new
10.times {
# write
r.rand(4).times do
n = r.rand(1024*1400)
x = r.bytes(n)
s << x
b.write(x)
end
# skip
r.rand(3).times do
n = r.rand(1024*1400)
ex = s.slice!(0, n)
b.skip(n).should == ex.size
b.size.should == s.size
end
}
end
it 'random skip_all write' do
r = Random.new(random_seed)
s = r.bytes(0)
b = Buffer.new
10.times {
# write
r.rand(4).times do
n = r.rand(1024*1400)
x = r.bytes(n)
s << x
b.write(x)
end
# skip_all
r.rand(3).times do
n = r.rand(1024*1400)
begin
b.skip_all(n)
ex = s.slice!(0, n)
b.size.should == s.size
ensure EOFError
b.size.should == s.size
b.read.should == s
s.clear
break
end
end
}
end
end