msgpack 0.6.0-x86-mswin32-60

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +20 -0
  3. data/.travis.yml +26 -0
  4. data/ChangeLog +129 -0
  5. data/Dockerfile +62 -0
  6. data/LICENSE +177 -0
  7. data/README.rdoc +141 -0
  8. data/Rakefile +68 -0
  9. data/bench/pack.rb +23 -0
  10. data/bench/pack_log.rb +33 -0
  11. data/bench/pack_log_long.rb +65 -0
  12. data/bench/run.sh +14 -0
  13. data/bench/run_long.sh +35 -0
  14. data/bench/unpack.rb +21 -0
  15. data/bench/unpack_log.rb +34 -0
  16. data/bench/unpack_log_long.rb +67 -0
  17. data/doclib/msgpack.rb +77 -0
  18. data/doclib/msgpack/buffer.rb +193 -0
  19. data/doclib/msgpack/core_ext.rb +101 -0
  20. data/doclib/msgpack/error.rb +14 -0
  21. data/doclib/msgpack/packer.rb +134 -0
  22. data/doclib/msgpack/unpacker.rb +146 -0
  23. data/ext/java/org/msgpack/jruby/Buffer.java +221 -0
  24. data/ext/java/org/msgpack/jruby/Decoder.java +201 -0
  25. data/ext/java/org/msgpack/jruby/Encoder.java +308 -0
  26. data/ext/java/org/msgpack/jruby/ExtensionValue.java +136 -0
  27. data/ext/java/org/msgpack/jruby/MessagePackLibrary.java +107 -0
  28. data/ext/java/org/msgpack/jruby/Packer.java +78 -0
  29. data/ext/java/org/msgpack/jruby/Types.java +37 -0
  30. data/ext/java/org/msgpack/jruby/Unpacker.java +170 -0
  31. data/ext/msgpack/buffer.c +695 -0
  32. data/ext/msgpack/buffer.h +447 -0
  33. data/ext/msgpack/buffer_class.c +507 -0
  34. data/ext/msgpack/buffer_class.h +32 -0
  35. data/ext/msgpack/compat.h +114 -0
  36. data/ext/msgpack/core_ext.c +129 -0
  37. data/ext/msgpack/core_ext.h +26 -0
  38. data/ext/msgpack/extconf.rb +30 -0
  39. data/ext/msgpack/packer.c +168 -0
  40. data/ext/msgpack/packer.h +441 -0
  41. data/ext/msgpack/packer_class.c +302 -0
  42. data/ext/msgpack/packer_class.h +30 -0
  43. data/ext/msgpack/rbinit.c +33 -0
  44. data/ext/msgpack/rmem.c +94 -0
  45. data/ext/msgpack/rmem.h +109 -0
  46. data/ext/msgpack/sysdep.h +115 -0
  47. data/ext/msgpack/sysdep_endian.h +50 -0
  48. data/ext/msgpack/sysdep_types.h +46 -0
  49. data/ext/msgpack/unpacker.c +771 -0
  50. data/ext/msgpack/unpacker.h +122 -0
  51. data/ext/msgpack/unpacker_class.c +405 -0
  52. data/ext/msgpack/unpacker_class.h +32 -0
  53. data/lib/msgpack.rb +13 -0
  54. data/lib/msgpack/version.rb +3 -0
  55. data/msgpack.gemspec +31 -0
  56. data/msgpack.org.md +46 -0
  57. data/spec/cases.json +1 -0
  58. data/spec/cases.msg +0 -0
  59. data/spec/cases_compact.msg +0 -0
  60. data/spec/cases_spec.rb +39 -0
  61. data/spec/cruby/buffer_io_spec.rb +256 -0
  62. data/spec/cruby/buffer_packer.rb +29 -0
  63. data/spec/cruby/buffer_spec.rb +572 -0
  64. data/spec/cruby/buffer_unpacker.rb +19 -0
  65. data/spec/cruby/packer_spec.rb +120 -0
  66. data/spec/cruby/unpacker_spec.rb +305 -0
  67. data/spec/format_spec.rb +282 -0
  68. data/spec/jruby/benchmarks/shootout_bm.rb +73 -0
  69. data/spec/jruby/benchmarks/symbolize_keys_bm.rb +25 -0
  70. data/spec/jruby/msgpack/unpacker_spec.rb +290 -0
  71. data/spec/jruby/msgpack_spec.rb +142 -0
  72. data/spec/pack_spec.rb +67 -0
  73. data/spec/random_compat.rb +24 -0
  74. data/spec/spec_helper.rb +27 -0
  75. data/spec/unpack_spec.rb +60 -0
  76. metadata +208 -0
@@ -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,305 @@
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, 0x00].pack('C*')).encoding.should == Encoding::UTF_8
266
+ MessagePack.unpack([0xd9, 0x01].pack('C*') + 'a').should == "a"
267
+ MessagePack.unpack([0xd9, 0x02].pack('C*') + 'aa').should == "aa"
268
+ end
269
+
270
+ it "msgpack str 16 type" do
271
+ MessagePack.unpack([0xda, 0x00, 0x00].pack('C*')).should == ""
272
+ MessagePack.unpack([0xda, 0x00, 0x00].pack('C*')).encoding.should == Encoding::UTF_8
273
+ MessagePack.unpack([0xda, 0x00, 0x01].pack('C*') + 'a').should == "a"
274
+ MessagePack.unpack([0xda, 0x00, 0x02].pack('C*') + 'aa').should == "aa"
275
+ end
276
+
277
+ it "msgpack str 32 type" do
278
+ MessagePack.unpack([0xdb, 0x00, 0x00, 0x00, 0x00].pack('C*')).should == ""
279
+ MessagePack.unpack([0xdb, 0x00, 0x00, 0x00, 0x00].pack('C*')).encoding.should == Encoding::UTF_8
280
+ MessagePack.unpack([0xdb, 0x00, 0x00, 0x00, 0x01].pack('C*') + 'a').should == "a"
281
+ MessagePack.unpack([0xdb, 0x00, 0x00, 0x00, 0x02].pack('C*') + 'aa').should == "aa"
282
+ end
283
+
284
+ it "msgpack bin 8 type" do
285
+ MessagePack.unpack([0xc4, 0x00].pack('C*')).should == ""
286
+ MessagePack.unpack([0xc4, 0x00].pack('C*')).encoding.should == Encoding::ASCII_8BIT
287
+ MessagePack.unpack([0xc4, 0x01].pack('C*') + 'a').should == "a"
288
+ MessagePack.unpack([0xc4, 0x02].pack('C*') + 'aa').should == "aa"
289
+ end
290
+
291
+ it "msgpack bin 16 type" do
292
+ MessagePack.unpack([0xc5, 0x00, 0x00].pack('C*')).should == ""
293
+ MessagePack.unpack([0xc5, 0x00, 0x00].pack('C*')).encoding.should == Encoding::ASCII_8BIT
294
+ MessagePack.unpack([0xc5, 0x00, 0x01].pack('C*') + 'a').should == "a"
295
+ MessagePack.unpack([0xc5, 0x00, 0x02].pack('C*') + 'aa').should == "aa"
296
+ end
297
+
298
+ it "msgpack bin 32 type" do
299
+ MessagePack.unpack([0xc6, 0x00, 0x00, 0x00, 0x00].pack('C*')).should == ""
300
+ MessagePack.unpack([0xc6, 0x0, 0x00, 0x00, 0x000].pack('C*')).encoding.should == Encoding::ASCII_8BIT
301
+ MessagePack.unpack([0xc6, 0x00, 0x00, 0x00, 0x01].pack('C*') + 'a').should == "a"
302
+ MessagePack.unpack([0xc6, 0x00, 0x00, 0x00, 0x02].pack('C*') + 'aa').should == "aa"
303
+ end
304
+ end
305
+
@@ -0,0 +1,282 @@
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 8" do
87
+ check_raw 2, (1 << 5)
88
+ check_raw 2, (1 << 8)-1
89
+ end
90
+
91
+ it "raw 16" do
92
+ check_raw 3, (1 << 8)
93
+ check_raw 3, (1 << 16)-1
94
+ end
95
+
96
+ it "raw 32" do
97
+ check_raw 5, (1 << 16)
98
+ #check_raw 5, (1 << 32)-1 # memory error
99
+ end
100
+
101
+ it "str encoding is UTF_8" do
102
+ v = pack_unpack('string'.force_encoding(Encoding::UTF_8))
103
+ v.encoding.should == Encoding::UTF_8
104
+ end
105
+
106
+ it "str transcode US-ASCII" do
107
+ v = pack_unpack('string'.force_encoding(Encoding::US_ASCII))
108
+ v.encoding.should == Encoding::UTF_8
109
+ end
110
+
111
+ it "str transcode UTF-16" do
112
+ v = pack_unpack('string'.encode(Encoding::UTF_16))
113
+ v.encoding.should == Encoding::UTF_8
114
+ v.should == 'string'
115
+ end
116
+
117
+ it "str transcode EUC-JP 7bit safe" do
118
+ v = pack_unpack('string'.force_encoding(Encoding::EUC_JP))
119
+ v.encoding.should == Encoding::UTF_8
120
+ v.should == 'string'
121
+ end
122
+
123
+ it "str transcode EUC-JP 7bit unsafe" do
124
+ v = pack_unpack([0xa4, 0xa2].pack('C*').force_encoding(Encoding::EUC_JP))
125
+ v.encoding.should == Encoding::UTF_8
126
+ v.should == "\xE3\x81\x82".force_encoding('UTF-8')
127
+ end
128
+
129
+ it "bin 8" do
130
+ check_bin 2, (1<<8)-1
131
+ end
132
+
133
+ it "bin 16" do
134
+ check_bin 3, (1<<16)-1
135
+ end
136
+
137
+ it "bin 32" do
138
+ check_bin 5, (1<<16)
139
+ end
140
+
141
+ it "bin encoding is ASCII_8BIT" do
142
+ pack_unpack('string'.force_encoding(Encoding::ASCII_8BIT)).encoding.should == Encoding::ASCII_8BIT
143
+ end
144
+
145
+ it "fixarray" do
146
+ check_array 1, 0
147
+ check_array 1, (1 << 4)-1
148
+ end
149
+
150
+ it "array 16" do
151
+ check_array 3, (1 << 4)
152
+ #check_array 3, (1 << 16)-1
153
+ end
154
+
155
+ it "array 32" do
156
+ #check_array 5, (1 << 16)
157
+ #check_array 5, (1 << 32)-1 # memory error
158
+ end
159
+
160
+ it "nil" do
161
+ match nil, "\xc0"
162
+ end
163
+
164
+ it "false" do
165
+ match false, "\xc2"
166
+ end
167
+
168
+ it "true" do
169
+ match true, "\xc3"
170
+ end
171
+
172
+ it "0" do
173
+ match 0, "\x00"
174
+ end
175
+
176
+ it "127" do
177
+ match 127, "\x7f"
178
+ end
179
+
180
+ it "128" do
181
+ match 128, "\xcc\x80"
182
+ end
183
+
184
+ it "256" do
185
+ match 256, "\xcd\x01\x00"
186
+ end
187
+
188
+ it "-1" do
189
+ match -1, "\xff"
190
+ end
191
+
192
+ it "-33" do
193
+ match -33, "\xd0\xdf"
194
+ end
195
+
196
+ it "-129" do
197
+ match -129, "\xd1\xff\x7f"
198
+ end
199
+
200
+ it "{1=>1}" do
201
+ obj = {1=>1}
202
+ match obj, "\x81\x01\x01"
203
+ end
204
+
205
+ it "1.0" do
206
+ match 1.0, "\xcb\x3f\xf0\x00\x00\x00\x00\x00\x00"
207
+ end
208
+
209
+ it "[]" do
210
+ match [], "\x90"
211
+ end
212
+
213
+ it "[0, 1, ..., 14]" do
214
+ obj = (0..14).to_a
215
+ match obj, "\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e"
216
+ end
217
+
218
+ it "[0, 1, ..., 15]" do
219
+ obj = (0..15).to_a
220
+ match obj, "\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
221
+ end
222
+
223
+ it "{}" do
224
+ obj = {}
225
+ match obj, "\x80"
226
+ end
227
+
228
+ ## FIXME
229
+ # it "{0=>0, 1=>1, ..., 14=>14}" do
230
+ # a = (0..14).to_a;
231
+ # 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"
232
+ # end
233
+ #
234
+ # it "{0=>0, 1=>1, ..., 15=>15}" do
235
+ # a = (0..15).to_a;
236
+ # 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"
237
+ # end
238
+
239
+ ## FIXME
240
+ # it "fixmap" do
241
+ # check_map 1, 0
242
+ # check_map 1, (1<<4)-1
243
+ # end
244
+ #
245
+ # it "map 16" do
246
+ # check_map 3, (1<<4)
247
+ # check_map 3, (1<<16)-1
248
+ # end
249
+ #
250
+ # it "map 32" do
251
+ # check_map 5, (1<<16)
252
+ # #check_map 5, (1<<32)-1 # memory error
253
+ # end
254
+
255
+ def check(len, obj)
256
+ raw = obj.to_msgpack.to_s
257
+ raw.length.should == len
258
+ MessagePack.unpack(raw).should == obj
259
+ end
260
+
261
+ def check_raw(overhead, num)
262
+ check num+overhead, (" "*num).force_encoding(Encoding::UTF_8)
263
+ end
264
+
265
+ def check_bin(overhead, num)
266
+ check num+overhead, (" "*num).force_encoding(Encoding::ASCII_8BIT)
267
+ end
268
+
269
+ def check_array(overhead, num)
270
+ check num+overhead, Array.new(num)
271
+ end
272
+
273
+ def match(obj, buf)
274
+ raw = obj.to_msgpack.to_s
275
+ raw.should == buf
276
+ end
277
+
278
+ def pack_unpack(obj)
279
+ MessagePack.unpack(obj.to_msgpack)
280
+ end
281
+ end
282
+