cbor 0.5.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.travis.yml +5 -0
- data/ChangeLog +87 -0
- data/README.rdoc +180 -0
- data/Rakefile +94 -0
- data/cbor.gemspec +26 -0
- data/doclib/cbor.rb +80 -0
- data/doclib/cbor/buffer.rb +193 -0
- data/doclib/cbor/core_ext.rb +133 -0
- data/doclib/cbor/error.rb +14 -0
- data/doclib/cbor/packer.rb +133 -0
- data/doclib/cbor/simple.rb +15 -0
- data/doclib/cbor/tagged.rb +16 -0
- data/doclib/cbor/unpacker.rb +138 -0
- data/ext/cbor/buffer.c +693 -0
- data/ext/cbor/buffer.h +469 -0
- data/ext/cbor/buffer_class.c +516 -0
- data/ext/cbor/buffer_class.h +41 -0
- data/ext/cbor/cbor.h +69 -0
- data/ext/cbor/compat.h +136 -0
- data/ext/cbor/core_ext.c +181 -0
- data/ext/cbor/core_ext.h +35 -0
- data/ext/cbor/extconf.rb +25 -0
- data/ext/cbor/packer.c +169 -0
- data/ext/cbor/packer.h +337 -0
- data/ext/cbor/packer_class.c +304 -0
- data/ext/cbor/packer_class.h +39 -0
- data/ext/cbor/rbinit.c +51 -0
- data/ext/cbor/renamer.h +56 -0
- data/ext/cbor/rmem.c +103 -0
- data/ext/cbor/rmem.h +118 -0
- data/ext/cbor/sysdep.h +135 -0
- data/ext/cbor/sysdep_endian.h +59 -0
- data/ext/cbor/sysdep_types.h +55 -0
- data/ext/cbor/unpacker.c +735 -0
- data/ext/cbor/unpacker.h +133 -0
- data/ext/cbor/unpacker_class.c +417 -0
- data/ext/cbor/unpacker_class.h +39 -0
- data/lib/cbor.rb +9 -0
- data/lib/cbor/version.rb +3 -0
- data/spec/buffer_io_spec.rb +260 -0
- data/spec/buffer_spec.rb +576 -0
- data/spec/cases.cbor +0 -0
- data/spec/cases.cbor_stream +0 -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/format_spec.rb +445 -0
- data/spec/packer_spec.rb +127 -0
- data/spec/random_compat.rb +24 -0
- data/spec/spec_helper.rb +45 -0
- data/spec/unpacker_spec.rb +238 -0
- metadata +196 -0
@@ -0,0 +1,39 @@
|
|
1
|
+
/*
|
2
|
+
* CBOR for Ruby
|
3
|
+
*
|
4
|
+
* Copyright (C) 2013 Carsten Bormann
|
5
|
+
*
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License").
|
7
|
+
*
|
8
|
+
* Based on:
|
9
|
+
***********/
|
10
|
+
/*
|
11
|
+
* MessagePack for Ruby
|
12
|
+
*
|
13
|
+
* Copyright (C) 2008-2013 Sadayuki Furuhashi
|
14
|
+
*
|
15
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
16
|
+
* you may not use this file except in compliance with the License.
|
17
|
+
* You may obtain a copy of the License at
|
18
|
+
*
|
19
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
20
|
+
*
|
21
|
+
* Unless required by applicable law or agreed to in writing, software
|
22
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
23
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
24
|
+
* See the License for the specific language governing permissions and
|
25
|
+
* limitations under the License.
|
26
|
+
*/
|
27
|
+
#ifndef MSGPACK_RUBY_UNPACKER_CLASS_H__
|
28
|
+
#define MSGPACK_RUBY_UNPACKER_CLASS_H__
|
29
|
+
|
30
|
+
#include "unpacker.h"
|
31
|
+
|
32
|
+
extern VALUE cMessagePack_Unpacker;
|
33
|
+
|
34
|
+
void MessagePack_Unpacker_module_init(VALUE mMessagePack);
|
35
|
+
|
36
|
+
VALUE MessagePack_unpack(int argc, VALUE* argv);
|
37
|
+
|
38
|
+
#endif
|
39
|
+
|
data/lib/cbor.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
here = File.expand_path(File.dirname(__FILE__))
|
2
|
+
require File.join(here, 'cbor', 'version')
|
3
|
+
begin
|
4
|
+
m = /(\d+.\d+)/.match(RUBY_VERSION)
|
5
|
+
ver = m[1]
|
6
|
+
require File.join(here, 'cbor', ver, 'cbor')
|
7
|
+
rescue LoadError
|
8
|
+
require File.join(here, 'cbor', 'cbor')
|
9
|
+
end
|
data/lib/cbor/version.rb
ADDED
@@ -0,0 +1,260 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'random_compat'
|
3
|
+
|
4
|
+
require 'stringio'
|
5
|
+
if defined?(Encoding)
|
6
|
+
Encoding.default_external = 'ASCII-8BIT'
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Buffer do
|
10
|
+
r = Random.new
|
11
|
+
random_seed = r.seed
|
12
|
+
puts "buffer_io random seed: 0x#{random_seed.to_s(16)}"
|
13
|
+
|
14
|
+
let :source do
|
15
|
+
''
|
16
|
+
end
|
17
|
+
|
18
|
+
def set_source(s)
|
19
|
+
source.replace(s)
|
20
|
+
end
|
21
|
+
|
22
|
+
let :io do
|
23
|
+
StringIO.new(source.dup)
|
24
|
+
end
|
25
|
+
|
26
|
+
let :buffer do
|
27
|
+
Buffer.new(io)
|
28
|
+
end
|
29
|
+
|
30
|
+
let :repetition do
|
31
|
+
ENV['SLOW'] ? 3 : 50
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'io returns internal io' do
|
35
|
+
buffer.io.should == io
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'close closes internal io' do
|
39
|
+
io.should_receive(:close)
|
40
|
+
buffer.close
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'short feed and read all' do
|
44
|
+
set_source 'aa'
|
45
|
+
buffer.read.should == 'aa'
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'short feed and read short' do
|
49
|
+
set_source 'aa'
|
50
|
+
buffer.read(1).should == 'a'
|
51
|
+
buffer.read(1).should == 'a'
|
52
|
+
buffer.read(1).should == nil
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'long feed and read all' do
|
56
|
+
set_source ' '*(1024*1024)
|
57
|
+
s = buffer.read
|
58
|
+
s.size.should == source.size
|
59
|
+
s.should == source
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'long feed and read mixed' do
|
63
|
+
set_source ' '*(1024*1024)
|
64
|
+
buffer.read(10).should == source.slice!(0, 10)
|
65
|
+
buffer.read(10).should == source.slice!(0, 10)
|
66
|
+
buffer.read(10).should == source.slice!(0, 10)
|
67
|
+
s = buffer.read
|
68
|
+
s.size.should == source.size
|
69
|
+
s.should == source
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'eof' do
|
73
|
+
set_source ''
|
74
|
+
buffer.read.should == ''
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'eof 2' do
|
78
|
+
set_source 'a'
|
79
|
+
buffer.read.should == 'a'
|
80
|
+
buffer.read.should == ''
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'write short once and flush' do
|
84
|
+
buffer.write('aa')
|
85
|
+
buffer.flush
|
86
|
+
io.string.should == 'aa'
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'write short twice and flush' do
|
90
|
+
buffer.write('a')
|
91
|
+
buffer.write('a')
|
92
|
+
buffer.flush
|
93
|
+
io.string.should == 'aa'
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'write long once and flush' do
|
97
|
+
s = ' '*(1024*1024)
|
98
|
+
buffer.write s
|
99
|
+
buffer.flush
|
100
|
+
io.string.size.should == s.size
|
101
|
+
io.string.should == s
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'write short multi and flush' do
|
105
|
+
s = ' '*(1024*1024)
|
106
|
+
1024.times {
|
107
|
+
buffer.write ' '*1024
|
108
|
+
}
|
109
|
+
buffer.flush
|
110
|
+
io.string.size.should == s.size
|
111
|
+
io.string.should == s
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'random read' do
|
115
|
+
r = Random.new(random_seed)
|
116
|
+
|
117
|
+
repetition.times {
|
118
|
+
fragments = []
|
119
|
+
|
120
|
+
r.rand(4).times do
|
121
|
+
n = r.rand(1024*1400)
|
122
|
+
s = r.bytes(n)
|
123
|
+
fragments << s
|
124
|
+
end
|
125
|
+
|
126
|
+
io = StringIO.new(fragments.join)
|
127
|
+
b = Buffer.new(io)
|
128
|
+
|
129
|
+
fragments.each {|s|
|
130
|
+
x = b.read(s.size)
|
131
|
+
x.size.should == s.size
|
132
|
+
x.should == s
|
133
|
+
}
|
134
|
+
b.empty?.should == true
|
135
|
+
b.read.should == ''
|
136
|
+
}
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'random read_all' do
|
140
|
+
r = Random.new(random_seed)
|
141
|
+
|
142
|
+
repetition.times {
|
143
|
+
fragments = []
|
144
|
+
sx = r.bytes(0)
|
145
|
+
|
146
|
+
r.rand(4).times do
|
147
|
+
n = r.rand(1024*1400)
|
148
|
+
s = r.bytes(n)
|
149
|
+
fragments << s
|
150
|
+
end
|
151
|
+
|
152
|
+
io = StringIO.new(fragments.join)
|
153
|
+
b = Buffer.new(io)
|
154
|
+
|
155
|
+
fragments.each {|s|
|
156
|
+
x = b.read_all(s.size)
|
157
|
+
x.size.should == s.size
|
158
|
+
x.should == s
|
159
|
+
}
|
160
|
+
b.empty?.should == true
|
161
|
+
lambda {
|
162
|
+
b.read_all(1)
|
163
|
+
}.should raise_error(EOFError)
|
164
|
+
}
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'random skip' do
|
168
|
+
r = Random.new(random_seed)
|
169
|
+
|
170
|
+
repetition.times {
|
171
|
+
fragments = []
|
172
|
+
|
173
|
+
r.rand(4).times do
|
174
|
+
n = r.rand(1024*1400)
|
175
|
+
s = r.bytes(n)
|
176
|
+
fragments << s
|
177
|
+
end
|
178
|
+
|
179
|
+
io = StringIO.new(fragments.join)
|
180
|
+
b = Buffer.new(io)
|
181
|
+
|
182
|
+
fragments.each {|s|
|
183
|
+
b.skip(s.size).should == s.size
|
184
|
+
}
|
185
|
+
b.empty?.should == true
|
186
|
+
b.skip(1).should == 0
|
187
|
+
}
|
188
|
+
end
|
189
|
+
|
190
|
+
it 'random skip_all' do
|
191
|
+
r = Random.new(random_seed)
|
192
|
+
|
193
|
+
repetition.times {
|
194
|
+
fragments = []
|
195
|
+
|
196
|
+
r.rand(4).times do
|
197
|
+
n = r.rand(1024*1400)
|
198
|
+
s = r.bytes(n)
|
199
|
+
fragments << s
|
200
|
+
end
|
201
|
+
|
202
|
+
io = StringIO.new(fragments.join)
|
203
|
+
b = Buffer.new(io)
|
204
|
+
|
205
|
+
fragments.each {|s|
|
206
|
+
lambda {
|
207
|
+
b.skip_all(s.size)
|
208
|
+
}.should_not raise_error()
|
209
|
+
}
|
210
|
+
b.empty?.should == true
|
211
|
+
lambda {
|
212
|
+
b.skip_all(1)
|
213
|
+
}.should raise_error(EOFError)
|
214
|
+
}
|
215
|
+
end
|
216
|
+
|
217
|
+
it 'random write and flush' do
|
218
|
+
r = Random.new(random_seed)
|
219
|
+
|
220
|
+
repetition.times {
|
221
|
+
s = r.bytes(0)
|
222
|
+
io = StringIO.new
|
223
|
+
b = Buffer.new(io)
|
224
|
+
|
225
|
+
r.rand(4).times do
|
226
|
+
n = r.rand(1024*1400)
|
227
|
+
x = r.bytes(n)
|
228
|
+
s << x
|
229
|
+
b.write(x)
|
230
|
+
end
|
231
|
+
|
232
|
+
(io.string.size + b.size).should == s.size
|
233
|
+
|
234
|
+
b.flush
|
235
|
+
|
236
|
+
io.string.size.should == s.size
|
237
|
+
io.string.should == s
|
238
|
+
}
|
239
|
+
end
|
240
|
+
|
241
|
+
it 'random write and clear' do
|
242
|
+
r = Random.new(random_seed)
|
243
|
+
b = Buffer.new
|
244
|
+
|
245
|
+
repetition.times {
|
246
|
+
s = r.bytes(0)
|
247
|
+
|
248
|
+
r.rand(4).times do
|
249
|
+
n = r.rand(1024*1400)
|
250
|
+
x = r.bytes(n)
|
251
|
+
s << x
|
252
|
+
b.write(x)
|
253
|
+
end
|
254
|
+
|
255
|
+
b.size.should == s.size
|
256
|
+
b.clear
|
257
|
+
}
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
data/spec/buffer_spec.rb
ADDED
@@ -0,0 +1,576 @@
|
|
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 :repetition do
|
30
|
+
ENV['SLOW'] ? 2 : 10
|
31
|
+
end
|
32
|
+
|
33
|
+
let :random_cases_examples do
|
34
|
+
r = Random.new(random_seed)
|
35
|
+
cases = {}
|
36
|
+
examples = {}
|
37
|
+
|
38
|
+
repetition.times do |i|
|
39
|
+
b = Buffer.new
|
40
|
+
s = r.bytes(0)
|
41
|
+
r.rand(3).times do
|
42
|
+
n = r.rand(1024*1400)
|
43
|
+
x = r.bytes(n)
|
44
|
+
s << x
|
45
|
+
b << x
|
46
|
+
end
|
47
|
+
r.rand(2).times do
|
48
|
+
n = r.rand(1024*1400)
|
49
|
+
b.read(n)
|
50
|
+
s.slice!(0, n)
|
51
|
+
end
|
52
|
+
key = :"random#{"%02d"%i}"
|
53
|
+
cases[key] = b
|
54
|
+
examples[key] = s
|
55
|
+
end
|
56
|
+
|
57
|
+
[cases, examples]
|
58
|
+
end
|
59
|
+
|
60
|
+
let :static_cases do
|
61
|
+
map = {}
|
62
|
+
map[:empty01] = empty01
|
63
|
+
map[:empty02] = empty02
|
64
|
+
map[:copy01] = copy01
|
65
|
+
map[:copy02] = copy02
|
66
|
+
map[:ref01] = ref01
|
67
|
+
map[:ref02] = ref02
|
68
|
+
map[:ref03] = ref03
|
69
|
+
map[:refcopy01] = refcopy01
|
70
|
+
map[:expand01] = expand01
|
71
|
+
map[:expand02] = expand02
|
72
|
+
map[:offset01] = offset01
|
73
|
+
map[:offset02] = offset02
|
74
|
+
map[:offset03] = offset03
|
75
|
+
map
|
76
|
+
end
|
77
|
+
|
78
|
+
let :static_examples do
|
79
|
+
STATIC_EXAMPLES
|
80
|
+
end
|
81
|
+
|
82
|
+
let :random_cases do
|
83
|
+
random_cases_examples[0]
|
84
|
+
end
|
85
|
+
|
86
|
+
let :random_examples do
|
87
|
+
random_cases_examples[1]
|
88
|
+
end
|
89
|
+
|
90
|
+
let :cases do
|
91
|
+
static_cases.merge(random_cases)
|
92
|
+
end
|
93
|
+
|
94
|
+
let :examples do
|
95
|
+
static_examples.merge(random_examples)
|
96
|
+
end
|
97
|
+
|
98
|
+
let :case_keys do
|
99
|
+
examples.keys
|
100
|
+
end
|
101
|
+
|
102
|
+
let :empty01 do
|
103
|
+
Buffer.new
|
104
|
+
end
|
105
|
+
|
106
|
+
let :empty02 do
|
107
|
+
b = Buffer.new
|
108
|
+
b << 'a'
|
109
|
+
b.read_all(1)
|
110
|
+
b
|
111
|
+
end
|
112
|
+
|
113
|
+
let :copy01 do
|
114
|
+
# one copy chunk
|
115
|
+
b = Buffer.new
|
116
|
+
b << 'short'
|
117
|
+
b
|
118
|
+
end
|
119
|
+
|
120
|
+
let :copy02 do
|
121
|
+
# one copy chunk
|
122
|
+
b = Buffer.new
|
123
|
+
b << 'short'
|
124
|
+
b << 'short'
|
125
|
+
b
|
126
|
+
end
|
127
|
+
|
128
|
+
let :expand02 do
|
129
|
+
# one copy chunk / expanded
|
130
|
+
b = Buffer.new
|
131
|
+
1024.times do
|
132
|
+
b << 'short'
|
133
|
+
end
|
134
|
+
b
|
135
|
+
end
|
136
|
+
|
137
|
+
let :ref01 do
|
138
|
+
# one reference chunk
|
139
|
+
b = Buffer.new
|
140
|
+
b << 'short'*128
|
141
|
+
b.to_s
|
142
|
+
b
|
143
|
+
end
|
144
|
+
|
145
|
+
let :ref02 do
|
146
|
+
# two reference chunks
|
147
|
+
b = Buffer.new
|
148
|
+
b << 'short'*128
|
149
|
+
b.to_s
|
150
|
+
b << 'short'*128
|
151
|
+
b.to_a
|
152
|
+
b
|
153
|
+
end
|
154
|
+
|
155
|
+
let :ref03 do
|
156
|
+
# two reference chunks
|
157
|
+
b = Buffer.new
|
158
|
+
b << 'a'*(1024*1024+2)
|
159
|
+
b << 'a'*(1024*1024+2)
|
160
|
+
b
|
161
|
+
end
|
162
|
+
|
163
|
+
let :refcopy01 do
|
164
|
+
# one reference chunk + one copy chunk
|
165
|
+
b = Buffer.new
|
166
|
+
b << 'short'*127
|
167
|
+
b.to_s
|
168
|
+
b << 'short'
|
169
|
+
b
|
170
|
+
end
|
171
|
+
|
172
|
+
let :expand01 do
|
173
|
+
# one copy chunk / expanded
|
174
|
+
b = Buffer.new
|
175
|
+
1024.times do
|
176
|
+
b << 'short'
|
177
|
+
end
|
178
|
+
b
|
179
|
+
end
|
180
|
+
|
181
|
+
let :expand02 do
|
182
|
+
# one reference chunk + one copy chunk / expanded
|
183
|
+
b = Buffer.new
|
184
|
+
b << 'short'*127
|
185
|
+
b.to_s
|
186
|
+
1024.times do
|
187
|
+
b << 'short'
|
188
|
+
end
|
189
|
+
b
|
190
|
+
end
|
191
|
+
|
192
|
+
let :offset01 do
|
193
|
+
# one copy chunk / offset
|
194
|
+
b = Buffer.new
|
195
|
+
b << 'short'
|
196
|
+
b << 'short'
|
197
|
+
b.skip(2)
|
198
|
+
b
|
199
|
+
end
|
200
|
+
|
201
|
+
let :offset02 do
|
202
|
+
# one reference chunk / offset
|
203
|
+
b = Buffer.new
|
204
|
+
b << 'short'*127
|
205
|
+
b.to_s
|
206
|
+
b.skip(2)
|
207
|
+
b << 'short'
|
208
|
+
b
|
209
|
+
end
|
210
|
+
|
211
|
+
let :offset03 do
|
212
|
+
# one reference chunk / offset + one copy chunk / expanded
|
213
|
+
b = Buffer.new
|
214
|
+
b << 'short'*127
|
215
|
+
b.to_s
|
216
|
+
1024.times do
|
217
|
+
b << 'short'
|
218
|
+
end
|
219
|
+
b.skip(2)
|
220
|
+
b
|
221
|
+
end
|
222
|
+
|
223
|
+
it 'empty?' do
|
224
|
+
empty01.empty?.should == true
|
225
|
+
empty02.empty?.should == true
|
226
|
+
end
|
227
|
+
|
228
|
+
it 'size' do
|
229
|
+
case_keys.each {|k|
|
230
|
+
cases[k].size.should == examples[k].size
|
231
|
+
}
|
232
|
+
end
|
233
|
+
|
234
|
+
it 'short write increments size' do
|
235
|
+
case_keys.each {|k|
|
236
|
+
sz = examples[k].size
|
237
|
+
10.times do |i|
|
238
|
+
cases[k].write 'short'
|
239
|
+
sz += 'short'.size
|
240
|
+
cases[k].size.should == sz
|
241
|
+
end
|
242
|
+
}
|
243
|
+
end
|
244
|
+
|
245
|
+
it 'read with limit decrements size' do
|
246
|
+
case_keys.each {|k|
|
247
|
+
sz = examples[k].size
|
248
|
+
next if sz < 2
|
249
|
+
|
250
|
+
cases[k].read(1).should == examples[k][0,1]
|
251
|
+
sz -= 1
|
252
|
+
cases[k].size.should == sz
|
253
|
+
|
254
|
+
cases[k].read(1).should == examples[k][1,1]
|
255
|
+
sz -= 1
|
256
|
+
cases[k].size.should == sz
|
257
|
+
}
|
258
|
+
end
|
259
|
+
|
260
|
+
it 'read_all with limit decrements size' do
|
261
|
+
case_keys.each {|k|
|
262
|
+
sz = examples[k].size
|
263
|
+
next if sz < 2
|
264
|
+
|
265
|
+
cases[k].read_all(1).should == examples[k][0,1]
|
266
|
+
sz -= 1
|
267
|
+
cases[k].size.should == sz
|
268
|
+
|
269
|
+
cases[k].read_all(1).should == examples[k][1,1]
|
270
|
+
sz -= 1
|
271
|
+
cases[k].size.should == sz
|
272
|
+
}
|
273
|
+
end
|
274
|
+
|
275
|
+
it 'skip decrements size' do
|
276
|
+
case_keys.each {|k|
|
277
|
+
sz = examples[k].size
|
278
|
+
next if sz < 2
|
279
|
+
|
280
|
+
cases[k].skip(1).should == 1
|
281
|
+
sz -= 1
|
282
|
+
cases[k].size.should == sz
|
283
|
+
|
284
|
+
cases[k].skip(1).should == 1
|
285
|
+
sz -= 1
|
286
|
+
cases[k].size.should == sz
|
287
|
+
}
|
288
|
+
end
|
289
|
+
|
290
|
+
it 'skip_all decrements size' do
|
291
|
+
case_keys.each {|k|
|
292
|
+
sz = examples[k].size
|
293
|
+
next if sz < 2
|
294
|
+
|
295
|
+
cases[k].skip_all(1)
|
296
|
+
sz -= 1
|
297
|
+
cases[k].size.should == sz
|
298
|
+
|
299
|
+
cases[k].skip_all(1)
|
300
|
+
sz -= 1
|
301
|
+
cases[k].size.should == sz
|
302
|
+
}
|
303
|
+
end
|
304
|
+
|
305
|
+
it 'read_all against insufficient buffer raises EOFError and consumes nothing' do
|
306
|
+
case_keys.each {|k|
|
307
|
+
sz = examples[k].size
|
308
|
+
lambda {
|
309
|
+
cases[k].read_all(sz+1)
|
310
|
+
}.should raise_error(EOFError)
|
311
|
+
cases[k].size.should == sz
|
312
|
+
}
|
313
|
+
end
|
314
|
+
|
315
|
+
it 'skip_all against insufficient buffer raises EOFError and consumes nothing' do
|
316
|
+
case_keys.each {|k|
|
317
|
+
sz = examples[k].size
|
318
|
+
lambda {
|
319
|
+
cases[k].skip_all(sz+1)
|
320
|
+
}.should raise_error(EOFError)
|
321
|
+
cases[k].size.should == sz
|
322
|
+
}
|
323
|
+
end
|
324
|
+
|
325
|
+
it 'read against insufficient buffer consumes all buffer or return nil' do
|
326
|
+
case_keys.each {|k|
|
327
|
+
sz = examples[k].size
|
328
|
+
if sz == 0
|
329
|
+
cases[k].read(sz+1).should == nil
|
330
|
+
else
|
331
|
+
cases[k].read(sz+1).should == examples[k]
|
332
|
+
end
|
333
|
+
cases[k].size.should == 0
|
334
|
+
}
|
335
|
+
end
|
336
|
+
|
337
|
+
it 'skip against insufficient buffer consumes all buffer' do
|
338
|
+
case_keys.each {|k|
|
339
|
+
sz = examples[k].size
|
340
|
+
cases[k].skip(sz+1).should == examples[k].size
|
341
|
+
cases[k].size.should == 0
|
342
|
+
}
|
343
|
+
end
|
344
|
+
|
345
|
+
it 'read with no arguments consumes all buffer and returns string and do not return nil' do
|
346
|
+
case_keys.each {|k|
|
347
|
+
cases[k].read_all.should == examples[k]
|
348
|
+
cases[k].size.should == 0
|
349
|
+
}
|
350
|
+
end
|
351
|
+
|
352
|
+
it 'read_all with no arguments consumes all buffer and returns string' do
|
353
|
+
case_keys.each {|k|
|
354
|
+
cases[k].read_all.should == examples[k]
|
355
|
+
cases[k].size.should == 0
|
356
|
+
}
|
357
|
+
end
|
358
|
+
|
359
|
+
it 'to_s returns a string and consume nothing' do
|
360
|
+
case_keys.each {|k|
|
361
|
+
cases[k].to_s.should == examples[k]
|
362
|
+
cases[k].size.should == examples[k].size
|
363
|
+
}
|
364
|
+
end
|
365
|
+
|
366
|
+
it 'to_s and modify' do
|
367
|
+
case_keys.each {|k|
|
368
|
+
s = cases[k].to_s
|
369
|
+
s << 'x'
|
370
|
+
cases[k].to_s.should == examples[k]
|
371
|
+
}
|
372
|
+
end
|
373
|
+
|
374
|
+
it 'big write and modify reference' do
|
375
|
+
big2 = "a" * (1024*1024 + 2)
|
376
|
+
|
377
|
+
case_keys.each {|k|
|
378
|
+
big1 = "a" * (1024*1024 + 2)
|
379
|
+
cases[k].write(big1)
|
380
|
+
big1 << 'x'
|
381
|
+
cases[k].read.should == examples[k] + big2
|
382
|
+
}
|
383
|
+
end
|
384
|
+
|
385
|
+
it 'big write -> short write' do
|
386
|
+
biglen = 1024*1024 + 2
|
387
|
+
big1 = "a" * (1024*1024 + 2)
|
388
|
+
|
389
|
+
case_keys.each {|k|
|
390
|
+
sz = examples[k].size
|
391
|
+
|
392
|
+
cases[k].write big1
|
393
|
+
cases[k].size.should == sz + big1.size
|
394
|
+
|
395
|
+
cases[k].write("c")
|
396
|
+
cases[k].size.should == sz + big1.size + 1
|
397
|
+
|
398
|
+
cases[k].read_all.should == examples[k] + big1 + "c"
|
399
|
+
cases[k].size.should == 0
|
400
|
+
cases[k].empty?.should == true
|
401
|
+
}
|
402
|
+
end
|
403
|
+
|
404
|
+
it 'big write 2'do
|
405
|
+
big1 = "a" * (1024*1024 + 2)
|
406
|
+
big2 = "b" * (1024*1024 + 2)
|
407
|
+
|
408
|
+
case_keys.each {|k|
|
409
|
+
sz = examples[k].size
|
410
|
+
|
411
|
+
cases[k].write big1
|
412
|
+
cases[k].write big2
|
413
|
+
cases[k].size.should == sz + big1.size + big2.size
|
414
|
+
|
415
|
+
cases[k].read_all(sz + big1.size).should == examples[k] + big1
|
416
|
+
cases[k].size.should == big2.size
|
417
|
+
|
418
|
+
cases[k].read.should == big2
|
419
|
+
cases[k].size.should == 0
|
420
|
+
cases[k].empty?.should == true
|
421
|
+
}
|
422
|
+
end
|
423
|
+
|
424
|
+
it 'read 0' do
|
425
|
+
case_keys.each {|k|
|
426
|
+
cases[k].read(0).should == ''
|
427
|
+
cases[k].read.should == examples[k]
|
428
|
+
}
|
429
|
+
end
|
430
|
+
|
431
|
+
it 'skip 0' do
|
432
|
+
case_keys.each {|k|
|
433
|
+
cases[k].skip(0).should == 0
|
434
|
+
cases[k].read.should == examples[k]
|
435
|
+
}
|
436
|
+
end
|
437
|
+
|
438
|
+
it 'read_all 0' do
|
439
|
+
case_keys.each {|k|
|
440
|
+
cases[k].read_all(0).should == ''
|
441
|
+
cases[k].read_all.should == examples[k]
|
442
|
+
}
|
443
|
+
end
|
444
|
+
|
445
|
+
it 'skip_all 0' do
|
446
|
+
case_keys.each {|k|
|
447
|
+
cases[k].skip_all(0)
|
448
|
+
cases[k].read_all.should == examples[k]
|
449
|
+
}
|
450
|
+
end
|
451
|
+
|
452
|
+
it 'write_to' do
|
453
|
+
case_keys.each {|k|
|
454
|
+
sio = StringIO.new
|
455
|
+
cases[k].write_to(sio).should == examples[k].size
|
456
|
+
cases[k].size.should == 0
|
457
|
+
sio.string.should == examples[k]
|
458
|
+
}
|
459
|
+
end
|
460
|
+
|
461
|
+
it 'random read/write' do
|
462
|
+
r = Random.new(random_seed)
|
463
|
+
s = r.bytes(0)
|
464
|
+
b = Buffer.new
|
465
|
+
|
466
|
+
10.times {
|
467
|
+
# write
|
468
|
+
r.rand(4).times do
|
469
|
+
n = r.rand(1024*1400)
|
470
|
+
x = r.bytes(n)
|
471
|
+
s << x
|
472
|
+
b.write(x)
|
473
|
+
end
|
474
|
+
|
475
|
+
# read
|
476
|
+
r.rand(3).times do
|
477
|
+
n = r.rand(1024*1400)
|
478
|
+
ex = s.slice!(0, n)
|
479
|
+
ex = nil if ex.empty?
|
480
|
+
x = b.read(n)
|
481
|
+
x.size == ex.size if x != nil
|
482
|
+
x.should == ex
|
483
|
+
b.size.should == s.size
|
484
|
+
end
|
485
|
+
}
|
486
|
+
end
|
487
|
+
|
488
|
+
it 'random read_all/write' do
|
489
|
+
r = Random.new(random_seed)
|
490
|
+
s = r.bytes(0)
|
491
|
+
b = Buffer.new
|
492
|
+
|
493
|
+
10.times {
|
494
|
+
# write
|
495
|
+
r.rand(4).times do
|
496
|
+
n = r.rand(1024*1400)
|
497
|
+
x = r.bytes(n)
|
498
|
+
s << x
|
499
|
+
b.write(x)
|
500
|
+
end
|
501
|
+
|
502
|
+
# read_all
|
503
|
+
r.rand(3).times do
|
504
|
+
n = r.rand(1024*1400)
|
505
|
+
begin
|
506
|
+
x = b.read_all(n)
|
507
|
+
ex = s.slice!(0, n)
|
508
|
+
x.size == n
|
509
|
+
x.should == ex
|
510
|
+
b.size.should == s.size
|
511
|
+
rescue EOFError
|
512
|
+
b.size.should == s.size
|
513
|
+
b.read.should == s
|
514
|
+
s.clear
|
515
|
+
break
|
516
|
+
end
|
517
|
+
end
|
518
|
+
}
|
519
|
+
end
|
520
|
+
|
521
|
+
it 'random skip write' do
|
522
|
+
r = Random.new(random_seed)
|
523
|
+
s = r.bytes(0)
|
524
|
+
b = Buffer.new
|
525
|
+
|
526
|
+
10.times {
|
527
|
+
# write
|
528
|
+
r.rand(4).times do
|
529
|
+
n = r.rand(1024*1400)
|
530
|
+
x = r.bytes(n)
|
531
|
+
s << x
|
532
|
+
b.write(x)
|
533
|
+
end
|
534
|
+
|
535
|
+
# skip
|
536
|
+
r.rand(3).times do
|
537
|
+
n = r.rand(1024*1400)
|
538
|
+
ex = s.slice!(0, n)
|
539
|
+
b.skip(n).should == ex.size
|
540
|
+
b.size.should == s.size
|
541
|
+
end
|
542
|
+
}
|
543
|
+
end
|
544
|
+
|
545
|
+
it 'random skip_all write' do
|
546
|
+
r = Random.new(random_seed)
|
547
|
+
s = r.bytes(0)
|
548
|
+
b = Buffer.new
|
549
|
+
|
550
|
+
10.times {
|
551
|
+
# write
|
552
|
+
r.rand(4).times do
|
553
|
+
n = r.rand(1024*1400)
|
554
|
+
x = r.bytes(n)
|
555
|
+
s << x
|
556
|
+
b.write(x)
|
557
|
+
end
|
558
|
+
|
559
|
+
# skip_all
|
560
|
+
r.rand(3).times do
|
561
|
+
n = r.rand(1024*1400)
|
562
|
+
begin
|
563
|
+
b.skip_all(n)
|
564
|
+
ex = s.slice!(0, n)
|
565
|
+
b.size.should == s.size
|
566
|
+
ensure EOFError
|
567
|
+
b.size.should == s.size
|
568
|
+
b.read.should == s
|
569
|
+
s.clear
|
570
|
+
break
|
571
|
+
end
|
572
|
+
end
|
573
|
+
}
|
574
|
+
end
|
575
|
+
end
|
576
|
+
|