msgpack 0.5.10-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
+
@@ -0,0 +1,299 @@
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
+ # TODO initialize
14
+
15
+ it 'read_array_header succeeds' do
16
+ unpacker.feed("\x91")
17
+ unpacker.read_array_header.should == 1
18
+ end
19
+
20
+ it 'read_array_header fails' do
21
+ unpacker.feed("\x81")
22
+ lambda {
23
+ unpacker.read_array_header
24
+ }.should raise_error(MessagePack::TypeError)
25
+ end
26
+
27
+ it 'read_map_header converts an map to key-value sequence' do
28
+ packer.write_array_header(2)
29
+ packer.write("e")
30
+ packer.write(1)
31
+ unpacker = Unpacker.new
32
+ unpacker.feed(packer.to_s)
33
+ unpacker.read_array_header.should == 2
34
+ unpacker.read.should == "e"
35
+ unpacker.read.should == 1
36
+ end
37
+
38
+ it 'read_map_header succeeds' do
39
+ unpacker.feed("\x81")
40
+ unpacker.read_map_header.should == 1
41
+ end
42
+
43
+ it 'read_map_header converts an map to key-value sequence' do
44
+ packer.write_map_header(1)
45
+ packer.write("k")
46
+ packer.write("v")
47
+ unpacker = Unpacker.new
48
+ unpacker.feed(packer.to_s)
49
+ unpacker.read_map_header.should == 1
50
+ unpacker.read.should == "k"
51
+ unpacker.read.should == "v"
52
+ end
53
+
54
+ it 'read_map_header fails' do
55
+ unpacker.feed("\x91")
56
+ lambda {
57
+ unpacker.read_map_header
58
+ }.should raise_error(MessagePack::TypeError)
59
+ end
60
+
61
+ it 'skip_nil succeeds' do
62
+ unpacker.feed("\xc0")
63
+ unpacker.skip_nil.should == true
64
+ end
65
+
66
+ it 'skip_nil fails' do
67
+ unpacker.feed("\x90")
68
+ unpacker.skip_nil.should == false
69
+ end
70
+
71
+ it 'skip skips objects' do
72
+ packer.write(1)
73
+ packer.write(2)
74
+ packer.write(3)
75
+ packer.write(4)
76
+ packer.write(5)
77
+
78
+ unpacker = Unpacker.new
79
+ unpacker.feed(packer.to_s)
80
+
81
+ unpacker.read.should == 1
82
+ unpacker.skip
83
+ unpacker.read.should == 3
84
+ unpacker.skip
85
+ unpacker.read.should == 5
86
+ end
87
+
88
+ it 'read raises EOFError' do
89
+ lambda {
90
+ unpacker.read
91
+ }.should raise_error(EOFError)
92
+ end
93
+
94
+ it 'skip raises EOFError' do
95
+ lambda {
96
+ unpacker.skip
97
+ }.should raise_error(EOFError)
98
+ end
99
+
100
+ it 'skip_nil raises EOFError' do
101
+ lambda {
102
+ unpacker.skip_nil
103
+ }.should raise_error(EOFError)
104
+ end
105
+
106
+ let :sample_object do
107
+ [1024, {["a","b"]=>["c","d"]}, ["e","f"], "d", 70000, 4.12, 1.5, 1.5, 1.5]
108
+ end
109
+
110
+ it 'feed and each continue internal state' do
111
+ raw = sample_object.to_msgpack.to_s * 4
112
+ objects = []
113
+
114
+ raw.split(//).each do |b|
115
+ unpacker.feed(b)
116
+ unpacker.each {|c|
117
+ objects << c
118
+ }
119
+ end
120
+
121
+ objects.should == [sample_object] * 4
122
+ end
123
+
124
+ it 'feed_each continues internal state' do
125
+ raw = sample_object.to_msgpack.to_s * 4
126
+ objects = []
127
+
128
+ raw.split(//).each do |b|
129
+ unpacker.feed_each(b) {|c|
130
+ objects << c
131
+ }
132
+ end
133
+
134
+ objects.should == [sample_object] * 4
135
+ end
136
+
137
+ it 'reset clears internal buffer' do
138
+ # 1-element array
139
+ unpacker.feed("\x91")
140
+ unpacker.reset
141
+ unpacker.feed("\x01")
142
+
143
+ unpacker.each.map {|x| x }.should == [1]
144
+ end
145
+
146
+ it 'reset clears internal state' do
147
+ # 1-element array
148
+ unpacker.feed("\x91")
149
+ unpacker.each.map {|x| x }.should == []
150
+
151
+ unpacker.reset
152
+
153
+ unpacker.feed("\x01")
154
+ unpacker.each.map {|x| x }.should == [1]
155
+ end
156
+
157
+ it 'frozen short strings' do
158
+ raw = sample_object.to_msgpack.to_s.force_encoding('UTF-8')
159
+ lambda {
160
+ unpacker.feed_each(raw.freeze) { }
161
+ }.should_not raise_error
162
+ end
163
+
164
+ it 'frozen long strings' do
165
+ raw = (sample_object.to_msgpack.to_s * 10240).force_encoding('UTF-8')
166
+ lambda {
167
+ unpacker.feed_each(raw.freeze) { }
168
+ }.should_not raise_error
169
+ end
170
+
171
+ it 'read raises level stack too deep error' do
172
+ 512.times { packer.write_array_header(1) }
173
+ packer.write(nil)
174
+
175
+ unpacker = Unpacker.new
176
+ unpacker.feed(packer.to_s)
177
+ lambda {
178
+ unpacker.read
179
+ }.should raise_error(MessagePack::StackError)
180
+ end
181
+
182
+ it 'skip raises level stack too deep error' do
183
+ 512.times { packer.write_array_header(1) }
184
+ packer.write(nil)
185
+
186
+ unpacker = Unpacker.new
187
+ unpacker.feed(packer.to_s)
188
+ lambda {
189
+ unpacker.skip
190
+ }.should raise_error(MessagePack::StackError)
191
+ end
192
+
193
+ it 'read raises invalid byte error' do
194
+ unpacker.feed("\xc1")
195
+ lambda {
196
+ unpacker.read
197
+ }.should raise_error(MessagePack::MalformedFormatError)
198
+ end
199
+
200
+ it 'skip raises invalid byte error' do
201
+ unpacker.feed("\xc1")
202
+ lambda {
203
+ unpacker.skip
204
+ }.should raise_error(MessagePack::MalformedFormatError)
205
+ end
206
+
207
+ it "gc mark" do
208
+ raw = sample_object.to_msgpack.to_s * 4
209
+
210
+ n = 0
211
+ raw.split(//).each do |b|
212
+ GC.start
213
+ unpacker.feed_each(b) {|o|
214
+ GC.start
215
+ o.should == sample_object
216
+ n += 1
217
+ }
218
+ GC.start
219
+ end
220
+
221
+ n.should == 4
222
+ end
223
+
224
+ it "buffer" do
225
+ orig = "a"*32*1024*4
226
+ raw = orig.to_msgpack.to_s
227
+
228
+ n = 655
229
+ times = raw.size / n
230
+ times += 1 unless raw.size % n == 0
231
+
232
+ off = 0
233
+ parsed = false
234
+
235
+ times.times do
236
+ parsed.should == false
237
+
238
+ seg = raw[off, n]
239
+ off += seg.length
240
+
241
+ unpacker.feed_each(seg) {|obj|
242
+ parsed.should == false
243
+ obj.should == orig
244
+ parsed = true
245
+ }
246
+ end
247
+
248
+ parsed.should == true
249
+ end
250
+
251
+ it 'MessagePack.unpack symbolize_keys' do
252
+ symbolized_hash = {:a => 'b', :c => 'd'}
253
+ MessagePack.load(MessagePack.pack(symbolized_hash), :symbolize_keys => true).should == symbolized_hash
254
+ MessagePack.unpack(MessagePack.pack(symbolized_hash), :symbolize_keys => true).should == symbolized_hash
255
+ end
256
+
257
+ it 'Unpacker#unpack symbolize_keys' do
258
+ unpacker = Unpacker.new(:symbolize_keys => true)
259
+ symbolized_hash = {:a => 'b', :c => 'd'}
260
+ unpacker.feed(MessagePack.pack(symbolized_hash)).read.should == symbolized_hash
261
+ end
262
+
263
+ it "msgpack str 8 type" do
264
+ MessagePack.unpack([0xd9, 0x00].pack('C*')).should == ""
265
+ MessagePack.unpack([0xd9, 0x01].pack('C*') + 'a').should == "a"
266
+ MessagePack.unpack([0xd9, 0x02].pack('C*') + 'aa').should == "aa"
267
+ end
268
+
269
+ it "msgpack str 16 type" do
270
+ MessagePack.unpack([0xda, 0x00, 0x00].pack('C*')).should == ""
271
+ MessagePack.unpack([0xda, 0x00, 0x01].pack('C*') + 'a').should == "a"
272
+ MessagePack.unpack([0xda, 0x00, 0x02].pack('C*') + 'aa').should == "aa"
273
+ end
274
+
275
+ it "msgpack str 32 type" do
276
+ MessagePack.unpack([0xdb, 0x00, 0x00, 0x00, 0x00].pack('C*')).should == ""
277
+ MessagePack.unpack([0xdb, 0x00, 0x00, 0x00, 0x01].pack('C*') + 'a').should == "a"
278
+ MessagePack.unpack([0xdb, 0x00, 0x00, 0x00, 0x02].pack('C*') + 'aa').should == "aa"
279
+ end
280
+
281
+ it "msgpack bin 8 type" do
282
+ MessagePack.unpack([0xc4, 0x00].pack('C*')).should == ""
283
+ MessagePack.unpack([0xc4, 0x01].pack('C*') + 'a').should == "a"
284
+ MessagePack.unpack([0xc4, 0x02].pack('C*') + 'aa').should == "aa"
285
+ end
286
+
287
+ it "msgpack bin 16 type" do
288
+ MessagePack.unpack([0xc5, 0x00, 0x00].pack('C*')).should == ""
289
+ MessagePack.unpack([0xc5, 0x00, 0x01].pack('C*') + 'a').should == "a"
290
+ MessagePack.unpack([0xc5, 0x00, 0x02].pack('C*') + 'aa').should == "aa"
291
+ end
292
+
293
+ it "msgpack bin 32 type" do
294
+ MessagePack.unpack([0xc6, 0x00, 0x00, 0x00, 0x00].pack('C*')).should == ""
295
+ MessagePack.unpack([0xc6, 0x00, 0x00, 0x00, 0x01].pack('C*') + 'a').should == "a"
296
+ MessagePack.unpack([0xc6, 0x00, 0x00, 0x00, 0x02].pack('C*') + 'aa').should == "aa"
297
+ end
298
+ end
299
+
@@ -0,0 +1,227 @@
1
+ # encoding: ascii-8bit
2
+ require 'spec_helper'
3
+
4
+ describe MessagePack do
5
+ it "nil" do
6
+ check 1, nil
7
+ end
8
+
9
+ it "true" do
10
+ check 1, true
11
+ end
12
+
13
+ it "false" do
14
+ check 1, false
15
+ end
16
+
17
+ it "zero" do
18
+ check 1, 0
19
+ end
20
+
21
+ it "positive fixnum" do
22
+ check 1, 1
23
+ check 1, (1 << 6)
24
+ check 1, (1 << 7)-1
25
+ end
26
+
27
+ it "positive int 8" do
28
+ check 1, -1
29
+ check 2, (1 << 7)
30
+ check 2, (1 << 8) - 1
31
+ end
32
+
33
+ it "positive int 16" do
34
+ check 3, (1 << 8)
35
+ check 3, (1 << 16) - 1
36
+ end
37
+
38
+ it "positive int 32" do
39
+ check 5, (1 << 16)
40
+ check 5, (1 << 32) - 1
41
+ end
42
+
43
+ it "positive int 64" do
44
+ check 9, (1 << 32)
45
+ #check 9, (1<<64)-1
46
+ end
47
+
48
+ it "negative fixnum" do
49
+ check 1, -1
50
+ check 1, -((1 << 5)-1)
51
+ check 1, -(1 << 5)
52
+ end
53
+
54
+ it "negative int 8" do
55
+ check 2, -((1 << 5)+1)
56
+ check 2, -(1 << 7)
57
+ end
58
+
59
+ it "negative int 16" do
60
+ check 3, -((1 << 7)+1)
61
+ check 3, -(1 << 15)
62
+ end
63
+
64
+ it "negative int 32" do
65
+ check 5, -((1 << 15)+1)
66
+ check 5, -(1 << 31)
67
+ end
68
+
69
+ it "negative int 64" do
70
+ check 9, -((1 << 31)+1)
71
+ check 9, -(1 << 63)
72
+ end
73
+
74
+ it "double" do
75
+ check 9, 1.0
76
+ check 9, 0.1
77
+ check 9, -0.1
78
+ check 9, -1.0
79
+ end
80
+
81
+ it "fixraw" do
82
+ check_raw 1, 0
83
+ check_raw 1, (1 << 5)-1
84
+ end
85
+
86
+ it "raw 16" do
87
+ check_raw 3, (1 << 5)
88
+ check_raw 3, (1 << 16)-1
89
+ end
90
+
91
+ it "raw 32" do
92
+ check_raw 5, (1 << 16)
93
+ #check_raw 5, (1 << 32)-1 # memory error
94
+ end
95
+
96
+ it "fixarray" do
97
+ check_array 1, 0
98
+ check_array 1, (1 << 4)-1
99
+ end
100
+
101
+ it "array 16" do
102
+ check_array 3, (1 << 4)
103
+ #check_array 3, (1 << 16)-1
104
+ end
105
+
106
+ it "array 32" do
107
+ #check_array 5, (1 << 16)
108
+ #check_array 5, (1 << 32)-1 # memory error
109
+ end
110
+
111
+ it "nil" do
112
+ match nil, "\xc0"
113
+ end
114
+
115
+ it "false" do
116
+ match false, "\xc2"
117
+ end
118
+
119
+ it "true" do
120
+ match true, "\xc3"
121
+ end
122
+
123
+ it "0" do
124
+ match 0, "\x00"
125
+ end
126
+
127
+ it "127" do
128
+ match 127, "\x7f"
129
+ end
130
+
131
+ it "128" do
132
+ match 128, "\xcc\x80"
133
+ end
134
+
135
+ it "256" do
136
+ match 256, "\xcd\x01\x00"
137
+ end
138
+
139
+ it "-1" do
140
+ match -1, "\xff"
141
+ end
142
+
143
+ it "-33" do
144
+ match -33, "\xd0\xdf"
145
+ end
146
+
147
+ it "-129" do
148
+ match -129, "\xd1\xff\x7f"
149
+ end
150
+
151
+ it "{1=>1}" do
152
+ obj = {1=>1}
153
+ match obj, "\x81\x01\x01"
154
+ end
155
+
156
+ it "1.0" do
157
+ match 1.0, "\xcb\x3f\xf0\x00\x00\x00\x00\x00\x00"
158
+ end
159
+
160
+ it "[]" do
161
+ match [], "\x90"
162
+ end
163
+
164
+ it "[0, 1, ..., 14]" do
165
+ obj = (0..14).to_a
166
+ match obj, "\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e"
167
+ end
168
+
169
+ it "[0, 1, ..., 15]" do
170
+ obj = (0..15).to_a
171
+ match obj, "\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
172
+ end
173
+
174
+ it "{}" do
175
+ obj = {}
176
+ match obj, "\x80"
177
+ end
178
+
179
+ ## FIXME
180
+ # it "{0=>0, 1=>1, ..., 14=>14}" do
181
+ # a = (0..14).to_a;
182
+ # match Hash[*a.zip(a).flatten], "\x8f\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x04\x04\x0a\x0a"
183
+ # end
184
+ #
185
+ # it "{0=>0, 1=>1, ..., 15=>15}" do
186
+ # a = (0..15).to_a;
187
+ # match Hash[*a.zip(a).flatten], "\xde\x00\x10\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x0f\x0f\x04\x04\x0a\x0a"
188
+ # end
189
+
190
+ ## FIXME
191
+ # it "fixmap" do
192
+ # check_map 1, 0
193
+ # check_map 1, (1<<4)-1
194
+ # end
195
+ #
196
+ # it "map 16" do
197
+ # check_map 3, (1<<4)
198
+ # check_map 3, (1<<16)-1
199
+ # end
200
+ #
201
+ # it "map 32" do
202
+ # check_map 5, (1<<16)
203
+ # #check_map 5, (1<<32)-1 # memory error
204
+ # end
205
+
206
+ def check(len, obj)
207
+ raw = obj.to_msgpack.to_s
208
+ raw.length.should == len
209
+ MessagePack.unpack(raw).should == obj
210
+ end
211
+
212
+ def check_raw(overhead, num)
213
+ rawstr = " "*num
214
+ rawstr.force_encoding("UTF-8")
215
+ check num+overhead, rawstr
216
+ end
217
+
218
+ def check_array(overhead, num)
219
+ check num+overhead, Array.new(num)
220
+ end
221
+
222
+ def match(obj, buf)
223
+ raw = obj.to_msgpack.to_s
224
+ raw.should == buf
225
+ end
226
+ end
227
+