nayutaya-msgpack-pure 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -5,6 +5,4 @@ msgpack_pureは、MessagePackのPure Rubyによる実装です。
5
5
 
6
6
  ## インストール
7
7
 
8
- ↓まだgemパッケージは作っていないため、下記はできません
9
-
10
8
  $ gem install nayutaya-msgpack_pure
@@ -7,12 +7,14 @@ require "msgpack_pure/unpacker"
7
7
  module MessagePackPure
8
8
  def self.pack(value)
9
9
  io = StringIO.new
10
- Packer.write(io, value)
10
+ packer = Packer.new(io)
11
+ packer.write(value)
11
12
  return io.string
12
13
  end
13
14
 
14
15
  def self.unpack(binary)
15
16
  io = StringIO.new(binary, "r")
16
- return Unpacker.read(io)
17
+ unpacker = Unpacker.new(io)
18
+ return unpacker.read
17
19
  end
18
20
  end
@@ -4,189 +4,198 @@
4
4
  # http://msgpack.sourceforge.jp/spec
5
5
 
6
6
  module MessagePackPure
7
- module Packer
7
+ class Packer
8
8
  end
9
9
  end
10
10
 
11
- module MessagePackPure::Packer
12
- def self.write(io, value)
11
+ class MessagePackPure::Packer
12
+ def initialize(io)
13
+ @io = io
14
+ end
15
+
16
+ attr_reader :io
17
+
18
+ def write(value)
13
19
  case value
14
- when Integer then self.write_integer(io, value)
15
- when NilClass then self.write_nil(io)
16
- when TrueClass then self.write_true(io)
17
- when FalseClass then self.write_false(io)
18
- when Float then self.write_float(io, value)
19
- when String then self.write_string(io, value)
20
- when Array then self.write_array(io, value)
21
- when Hash then self.write_hash(io, value)
20
+ when Integer then self.write_integer(value)
21
+ when NilClass then self.write_nil
22
+ when TrueClass then self.write_true
23
+ when FalseClass then self.write_false
24
+ when Float then self.write_float(value)
25
+ when String then self.write_string(value)
26
+ when Array then self.write_array(value)
27
+ when Hash then self.write_hash(value)
22
28
  else raise("unknown type")
23
29
  end
24
- return io
30
+
31
+ return @io
25
32
  end
26
33
 
27
- def self.write_integer(io, num)
34
+ protected
35
+
36
+ def write_integer(num)
28
37
  case num
29
38
  when (-0x20..0x7F)
30
39
  # positive fixnum, negative fixnum
31
- io.write(self.pack_int8(num))
40
+ @io.write(self.pack_int8(num))
32
41
  when (0x00..0xFF)
33
42
  # uint8
34
- io.write("\xCC")
35
- io.write(self.pack_uint8(num))
43
+ @io.write("\xCC")
44
+ @io.write(self.pack_uint8(num))
36
45
  when (-0x80..0x7F)
37
46
  # int8
38
- io.write("\xD0")
39
- io.write(self.pack_int8(num))
47
+ @io.write("\xD0")
48
+ @io.write(self.pack_int8(num))
40
49
  when (0x0000..0xFFFF)
41
50
  # uint16
42
- io.write("\xCD")
43
- io.write(self.pack_uint16(num))
51
+ @io.write("\xCD")
52
+ @io.write(self.pack_uint16(num))
44
53
  when (-0x8000..0x7FFF)
45
54
  # int16
46
- io.write("\xD1")
47
- io.write(self.pack_int16(num))
55
+ @io.write("\xD1")
56
+ @io.write(self.pack_int16(num))
48
57
  when (0x00000000..0xFFFFFFFF)
49
58
  # uint32
50
- io.write("\xCE")
51
- io.write(self.pack_uint32(num))
59
+ @io.write("\xCE")
60
+ @io.write(self.pack_uint32(num))
52
61
  when (-0x80000000..0x7FFFFFFF)
53
62
  # int32
54
- io.write("\xD2")
55
- io.write(self.pack_int32(num))
63
+ @io.write("\xD2")
64
+ @io.write(self.pack_int32(num))
56
65
  when (0x0000000000000000..0xFFFFFFFFFFFFFFFF)
57
66
  # uint64
58
- io.write("\xCF")
59
- io.write(self.pack_uint64(num))
67
+ @io.write("\xCF")
68
+ @io.write(self.pack_uint64(num))
60
69
  when (-0x8000000000000000..0x7FFFFFFFFFFFFFFF)
61
70
  # int64
62
- io.write("\xD3")
63
- io.write(self.pack_int64(num))
71
+ @io.write("\xD3")
72
+ @io.write(self.pack_int64(num))
64
73
  else
65
74
  raise("invalid integer")
66
75
  end
67
76
  end
68
77
 
69
- def self.write_nil(io)
70
- io.write("\xC0")
78
+ def write_nil
79
+ @io.write("\xC0")
71
80
  end
72
81
 
73
- def self.write_true(io)
74
- io.write("\xC3")
82
+ def write_true
83
+ @io.write("\xC3")
75
84
  end
76
85
 
77
- def self.write_false(io)
78
- io.write("\xC2")
86
+ def write_false
87
+ @io.write("\xC2")
79
88
  end
80
89
 
81
- def self.write_float(io, value)
82
- io.write("\xCB")
83
- io.write(self.pack_double(value))
90
+ def write_float(value)
91
+ @io.write("\xCB")
92
+ @io.write(self.pack_double(value))
84
93
  end
85
94
 
86
- def self.write_string(io, value)
95
+ def write_string(value)
87
96
  case value.size
88
97
  when (0x00..0x1F)
89
98
  # fixraw
90
- io.write(self.pack_uint8(0b10100000 + value.size))
91
- io.write(value)
99
+ @io.write(self.pack_uint8(0b10100000 + value.size))
100
+ @io.write(value)
92
101
  when (0x0000..0xFFFF)
93
102
  # raw16
94
- io.write("\xDA")
95
- io.write(self.pack_uint16(value.size))
96
- io.write(value)
103
+ @io.write("\xDA")
104
+ @io.write(self.pack_uint16(value.size))
105
+ @io.write(value)
97
106
  when (0x00000000..0xFFFFFFFF)
98
107
  # raw32
99
- io.write("\xDB")
100
- io.write(self.pack_uint32(value.size))
101
- io.write(value)
108
+ @io.write("\xDB")
109
+ @io.write(self.pack_uint32(value.size))
110
+ @io.write(value)
102
111
  else
103
112
  raise("invalid length")
104
113
  end
105
114
  end
106
115
 
107
- def self.write_array(io, value)
116
+ def write_array(value)
108
117
  case value.size
109
118
  when (0x00..0x0F)
110
119
  # fixarray
111
- io.write(self.pack_uint8(0b10010000 + value.size))
120
+ @io.write(self.pack_uint8(0b10010000 + value.size))
112
121
  when (0x0000..0xFFFF)
113
122
  # array16
114
- io.write("\xDC")
115
- io.write(self.pack_uint16(value.size))
123
+ @io.write("\xDC")
124
+ @io.write(self.pack_uint16(value.size))
116
125
  when (0x00000000..0xFFFFFFFF)
117
126
  # array32
118
- io.write("\xDD")
119
- io.write(self.pack_uint32(value.size))
127
+ @io.write("\xDD")
128
+ @io.write(self.pack_uint32(value.size))
120
129
  else
121
130
  raise("invalid length")
122
131
  end
123
132
 
124
133
  value.each { |item|
125
- self.write(io, item)
134
+ self.write(item)
126
135
  }
127
136
  end
128
137
 
129
- def self.write_hash(io, value)
138
+ def write_hash(value)
130
139
  case value.size
131
140
  when (0x00..0x0F)
132
141
  # fixmap
133
- io.write(self.pack_uint8(0b10000000 + value.size))
142
+ @io.write(self.pack_uint8(0b10000000 + value.size))
134
143
  when (0x0000..0xFFFF)
135
144
  # map16
136
- io.write("\xDE")
137
- io.write(self.pack_uint16(value.size))
145
+ @io.write("\xDE")
146
+ @io.write(self.pack_uint16(value.size))
138
147
  when (0x00000000..0xFFFFFFFF)
139
148
  # map32
140
- io.write("\xDF")
141
- io.write(self.pack_uint32(value.size))
149
+ @io.write("\xDF")
150
+ @io.write(self.pack_uint32(value.size))
142
151
  else
143
152
  raise("invalid length")
144
153
  end
145
154
 
146
155
  value.sort_by { |key, value| key }.each { |key, value|
147
- self.write(io, key)
148
- self.write(io, value)
156
+ self.write(key)
157
+ self.write(value)
149
158
  }
150
159
  end
151
160
 
152
- def self.pack_uint8(value)
161
+ def pack_uint8(value)
153
162
  return [value].pack("C")
154
163
  end
155
164
 
156
- def self.pack_int8(value)
165
+ def pack_int8(value)
157
166
  return [value].pack("c")
158
167
  end
159
168
 
160
- def self.pack_uint16(value)
169
+ def pack_uint16(value)
161
170
  return [value].pack("n")
162
171
  end
163
172
 
164
- def self.pack_int16(value)
173
+ def pack_int16(value)
165
174
  value += (2 ** 16) if value < 0
166
175
  return self.pack_uint16(value)
167
176
  end
168
177
 
169
- def self.pack_uint32(value)
178
+ def pack_uint32(value)
170
179
  return [value].pack("N")
171
180
  end
172
181
 
173
- def self.pack_int32(value)
182
+ def pack_int32(value)
174
183
  value += (2 ** 32) if value < 0
175
184
  return self.pack_uint32(value)
176
185
  end
177
186
 
178
- def self.pack_uint64(value)
187
+ def pack_uint64(value)
179
188
  high = (value >> 32)
180
189
  low = (value & 0xFFFFFFFF)
181
190
  return self.pack_uint32(high) + self.pack_uint32(low)
182
191
  end
183
192
 
184
- def self.pack_int64(value)
193
+ def pack_int64(value)
185
194
  value += (2 ** 64) if value < 0
186
195
  return self.pack_uint64(value)
187
196
  end
188
197
 
189
- def self.pack_double(value)
198
+ def pack_double(value)
190
199
  return [value].pack("G")
191
200
  end
192
201
  end
@@ -4,13 +4,19 @@
4
4
  # http://msgpack.sourceforge.jp/spec
5
5
 
6
6
  module MessagePackPure
7
- module Unpacker
7
+ class Unpacker
8
8
  end
9
9
  end
10
10
 
11
- module MessagePackPure::Unpacker
12
- def self.read(io)
13
- type = self.unpack_uint8(io)
11
+ class MessagePackPure::Unpacker
12
+ def initialize(io)
13
+ @io = io
14
+ end
15
+
16
+ attr_reader :io
17
+
18
+ def read
19
+ type = self.unpack_uint8
14
20
 
15
21
  case
16
22
  when (type & 0b10000000) == 0b00000000 # positive fixnum
@@ -19,13 +25,13 @@ module MessagePackPure::Unpacker
19
25
  return (type & 0b00011111) - (2 ** 5)
20
26
  when (type & 0b11100000) == 0b10100000 # fixraw
21
27
  size = (type & 0b00011111)
22
- return io.read(size)
28
+ return @io.read(size)
23
29
  when (type & 0b11110000) == 0b10010000 # fixarray
24
30
  size = (type & 0b00001111)
25
- return self.read_array(io, size)
31
+ return self.read_array(size)
26
32
  when (type & 0b11110000) == 0b10000000 # fixmap
27
33
  size = (type & 0b00001111)
28
- return self.read_hash(io, size)
34
+ return self.read_hash(size)
29
35
  end
30
36
 
31
37
  case type
@@ -36,101 +42,101 @@ module MessagePackPure::Unpacker
36
42
  when 0xC3 # true
37
43
  return true
38
44
  when 0xCA # float
39
- return self.unpack_float(io)
45
+ return self.unpack_float
40
46
  when 0xCB # double
41
- return self.unpack_double(io)
47
+ return self.unpack_double
42
48
  when 0xCC # uint8
43
- return self.unpack_uint8(io)
49
+ return self.unpack_uint8
44
50
  when 0xCD # uint16
45
- return self.unpack_uint16(io)
51
+ return self.unpack_uint16
46
52
  when 0xCE # uint32
47
- return self.unpack_uint32(io)
53
+ return self.unpack_uint32
48
54
  when 0xCF # uint64
49
- return self.unpack_uint64(io)
55
+ return self.unpack_uint64
50
56
  when 0xD0 # int8
51
- return self.unpack_int8(io)
57
+ return self.unpack_int8
52
58
  when 0xD1 # int16
53
- return self.unpack_int16(io)
59
+ return self.unpack_int16
54
60
  when 0xD2 # int32
55
- return self.unpack_int32(io)
61
+ return self.unpack_int32
56
62
  when 0xD3 # int64
57
- return self.unpack_int64(io)
63
+ return self.unpack_int64
58
64
  when 0xDA # raw16
59
- size = self.unpack_uint16(io)
60
- return io.read(size)
65
+ size = self.unpack_uint16
66
+ return @io.read(size)
61
67
  when 0xDB # raw32
62
- size = self.unpack_uint32(io)
63
- return io.read(size)
68
+ size = self.unpack_uint32
69
+ return @io.read(size)
64
70
  when 0xDC # array16
65
- size = self.unpack_uint16(io)
66
- return self.read_array(io, size)
71
+ size = self.unpack_uint16
72
+ return self.read_array(size)
67
73
  when 0xDD # array32
68
- size = self.unpack_uint32(io)
69
- return self.read_array(io, size)
74
+ size = self.unpack_uint32
75
+ return self.read_array(size)
70
76
  when 0xDE # map16
71
- size = self.unpack_uint16(io)
72
- return self.read_hash(io, size)
77
+ size = self.unpack_uint16
78
+ return self.read_hash(size)
73
79
  when 0xDF # map32
74
- size = self.unpack_uint32(io)
75
- return self.read_hash(io, size)
80
+ size = self.unpack_uint32
81
+ return self.read_hash(size)
76
82
  else
77
83
  raise("Unknown Type -- #{'0x%02X' % type}")
78
84
  end
79
85
  end
80
86
 
81
- def self.read_array(io, size)
82
- return size.times.map { self.read(io) }
87
+ def read_array(size)
88
+ return size.times.map { self.read }
83
89
  end
84
90
 
85
- def self.read_hash(io, size)
91
+ def read_hash(size)
86
92
  return size.times.inject({}) { |memo,|
87
- memo[self.read(io)] = self.read(io)
93
+ memo[self.read] = self.read
88
94
  memo
89
95
  }
90
96
  end
91
97
 
92
- def self.unpack_uint8(io)
93
- return io.read(1).unpack("C")[0]
98
+ def unpack_uint8
99
+ return @io.read(1).unpack("C")[0]
94
100
  end
95
101
 
96
- def self.unpack_int8(io)
97
- return io.read(1).unpack("c")[0]
102
+ def unpack_int8
103
+ return @io.read(1).unpack("c")[0]
98
104
  end
99
105
 
100
- def self.unpack_uint16(io)
101
- return io.read(2).unpack("n")[0]
106
+ def unpack_uint16
107
+ return @io.read(2).unpack("n")[0]
102
108
  end
103
109
 
104
- def self.unpack_int16(io)
105
- num = self.unpack_uint16(io)
110
+ def unpack_int16
111
+ num = self.unpack_uint16
106
112
  return (num < 2 ** 15 ? num : num - (2 ** 16))
107
113
  end
108
114
 
109
- def self.unpack_uint32(io)
110
- return io.read(4).unpack("N")[0]
115
+ def unpack_uint32
116
+ return @io.read(4).unpack("N")[0]
111
117
  end
112
118
 
113
- def self.unpack_int32(io)
114
- num = self.unpack_uint32(io)
119
+ def unpack_int32
120
+ num = self.unpack_uint32
115
121
  return (num < 2 ** 31 ? num : num - (2 ** 32))
116
122
  end
117
123
 
118
- def self.unpack_uint64(io)
119
- high = self.unpack_uint32(io)
120
- low = self.unpack_uint32(io)
124
+ def unpack_uint64
125
+ high = self.unpack_uint32
126
+ low = self.unpack_uint32
121
127
  return (high << 32) + low
122
128
  end
123
129
 
124
- def self.unpack_int64(io)
125
- num = self.unpack_uint64(io)
130
+ def unpack_int64
131
+ num = self.unpack_uint64
126
132
  return (num < 2 ** 63 ? num : num - (2 ** 64))
127
133
  end
128
134
 
129
- def self.unpack_float(io)
130
- return io.read(4).unpack("g")[0]
135
+ def unpack_float
136
+ return @io.read(4).unpack("g")[0]
131
137
  end
132
138
 
133
- def self.unpack_double(io)
134
- return io.read(8).unpack("G")[0]
139
+ def unpack_double
140
+ return @io.read(8).unpack("G")[0]
135
141
  end
136
142
  end
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
2
 
3
3
  module MessagePackPure
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
@@ -5,8 +5,8 @@ Gem::Specification.new do |s|
5
5
  s.required_ruby_version = Gem::Requirement.new(">= 1.8.6")
6
6
 
7
7
  s.name = "nayutaya-msgpack-pure"
8
- s.version = "0.0.1"
9
- s.date = "2010-03-08"
8
+ s.version = "0.0.2"
9
+ s.date = "2010-03-09"
10
10
 
11
11
  s.authors = ["Yuya Kato"]
12
12
  s.email = "yuyakato@gmail.com"
@@ -7,187 +7,193 @@ require "msgpack_pure/packer"
7
7
 
8
8
  class PackerTest < Test::Unit::TestCase
9
9
  def setup
10
- @module = MessagePackPure::Packer
10
+ @klass = MessagePackPure::Packer
11
+ end
12
+
13
+ def test_initialize
14
+ io = StringIO.new
15
+ packer = @klass.new(io)
16
+ assert_same(io, packer.io)
11
17
  end
12
18
 
13
19
  def test_write__positive_fixnum
14
- assert_equal("\x00", @module.write(sio, 0x00).string)
15
- assert_equal("\x01", @module.write(sio, 0x01).string)
16
- assert_equal("\x7F", @module.write(sio, 0x7F).string)
20
+ assert_equal("\x00", @klass.new(sio).write(0x00).string)
21
+ assert_equal("\x01", @klass.new(sio).write(0x01).string)
22
+ assert_equal("\x7F", @klass.new(sio).write(0x7F).string)
17
23
  end
18
24
 
19
25
  def test_write__negative_fixnum
20
- assert_equal("\xFF", @module.write(sio, -0x01).string)
21
- assert_equal("\xE0", @module.write(sio, -0x20).string)
26
+ assert_equal("\xFF", @klass.new(sio).write(-0x01).string)
27
+ assert_equal("\xE0", @klass.new(sio).write(-0x20).string)
22
28
  end
23
29
 
24
30
  def test_write__uint8
25
- assert_equal("\xCC\x80", @module.write(sio, 0x80).string)
26
- assert_equal("\xCC\xFF", @module.write(sio, 0xFF).string)
31
+ assert_equal("\xCC\x80", @klass.new(sio).write(0x80).string)
32
+ assert_equal("\xCC\xFF", @klass.new(sio).write(0xFF).string)
27
33
  end
28
34
 
29
35
  def test_write__uint16
30
- assert_equal("\xCD\x01\x00", @module.write(sio, 0x0100).string)
31
- assert_equal("\xCD\xFF\xFF", @module.write(sio, 0xFFFF).string)
36
+ assert_equal("\xCD\x01\x00", @klass.new(sio).write(0x0100).string)
37
+ assert_equal("\xCD\xFF\xFF", @klass.new(sio).write(0xFFFF).string)
32
38
  end
33
39
 
34
40
  def test_write__uint32
35
- assert_equal("\xCE\x00\x01\x00\x00", @module.write(sio, 0x00010000).string)
36
- assert_equal("\xCE\xFF\xFF\xFF\xFF", @module.write(sio, 0xFFFFFFFF).string)
41
+ assert_equal("\xCE\x00\x01\x00\x00", @klass.new(sio).write(0x00010000).string)
42
+ assert_equal("\xCE\xFF\xFF\xFF\xFF", @klass.new(sio).write(0xFFFFFFFF).string)
37
43
  end
38
44
 
39
45
  def test_write__uint64
40
- assert_equal("\xCF\x00\x00\x00\x01\x00\x00\x00\x00", @module.write(sio, 0x0000000100000000).string)
41
- assert_equal("\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", @module.write(sio, 0xFFFFFFFFFFFFFFFF).string)
46
+ assert_equal("\xCF\x00\x00\x00\x01\x00\x00\x00\x00", @klass.new(sio).write(0x0000000100000000).string)
47
+ assert_equal("\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", @klass.new(sio).write(0xFFFFFFFFFFFFFFFF).string)
42
48
  end
43
49
 
44
50
  def test_write__int8
45
- assert_equal("\xD0\xDF", @module.write(sio, -0x21).string)
46
- assert_equal("\xD0\x80", @module.write(sio, -0x80).string)
51
+ assert_equal("\xD0\xDF", @klass.new(sio).write(-0x21).string)
52
+ assert_equal("\xD0\x80", @klass.new(sio).write(-0x80).string)
47
53
  end
48
54
 
49
55
  def test_write__int16
50
- assert_equal("\xD1\xFF\x7F", @module.write(sio, -0x0081).string)
51
- assert_equal("\xD1\x80\x00", @module.write(sio, -0x8000).string)
56
+ assert_equal("\xD1\xFF\x7F", @klass.new(sio).write(-0x0081).string)
57
+ assert_equal("\xD1\x80\x00", @klass.new(sio).write(-0x8000).string)
52
58
  end
53
59
 
54
60
  def test_write__int32
55
- assert_equal("\xD2\xFF\xFF\x7F\xFF", @module.write(sio, -0x00008001).string)
56
- assert_equal("\xD2\x80\x00\x00\x00", @module.write(sio, -0x80000000).string)
61
+ assert_equal("\xD2\xFF\xFF\x7F\xFF", @klass.new(sio).write(-0x00008001).string)
62
+ assert_equal("\xD2\x80\x00\x00\x00", @klass.new(sio).write(-0x80000000).string)
57
63
  end
58
64
 
59
65
  def test_write__int64
60
- assert_equal("\xD3\xFF\xFF\xFF\xFF\x7F\xFF\xFF\xFF", @module.write(sio, -0x0000000080000001).string)
61
- assert_equal("\xD3\x80\x00\x00\x00\x00\x00\x00\x00", @module.write(sio, -0x8000000000000000).string)
66
+ assert_equal("\xD3\xFF\xFF\xFF\xFF\x7F\xFF\xFF\xFF", @klass.new(sio).write(-0x0000000080000001).string)
67
+ assert_equal("\xD3\x80\x00\x00\x00\x00\x00\x00\x00", @klass.new(sio).write(-0x8000000000000000).string)
62
68
  end
63
69
 
64
70
  def test_write__nil
65
- assert_equal("\xC0", @module.write(sio, nil).string)
71
+ assert_equal("\xC0", @klass.new(sio).write(nil).string)
66
72
  end
67
73
 
68
74
  def test_write__true
69
- assert_equal("\xC3", @module.write(sio, true).string)
75
+ assert_equal("\xC3", @klass.new(sio).write(true).string)
70
76
  end
71
77
 
72
78
  def test_write__false
73
- assert_equal("\xC2", @module.write(sio, false).string)
79
+ assert_equal("\xC2", @klass.new(sio).write(false).string)
74
80
  end
75
81
 
76
82
  def test_write__float
77
- assert_equal("\xCB\x00\x00\x00\x00\x00\x00\x00\x00", @module.write(sio, +0.0).string)
78
- assert_equal("\xCB\x3F\xE0\x00\x00\x00\x00\x00\x00", @module.write(sio, +0.5).string)
79
- assert_equal("\xCB\xBF\xE0\x00\x00\x00\x00\x00\x00", @module.write(sio, -0.5).string)
83
+ assert_equal("\xCB\x00\x00\x00\x00\x00\x00\x00\x00", @klass.new(sio).write(+0.0).string)
84
+ assert_equal("\xCB\x3F\xE0\x00\x00\x00\x00\x00\x00", @klass.new(sio).write(+0.5).string)
85
+ assert_equal("\xCB\xBF\xE0\x00\x00\x00\x00\x00\x00", @klass.new(sio).write(-0.5).string)
80
86
  end
81
87
 
82
88
  def test_write__fixraw
83
- assert_equal("\xA0", @module.write(sio, "").string)
84
- assert_equal("\xA3ABC", @module.write(sio, "ABC").string)
89
+ assert_equal("\xA0", @klass.new(sio).write("").string)
90
+ assert_equal("\xA3ABC", @klass.new(sio).write("ABC").string)
85
91
  assert_equal(
86
92
  "\xBF" + "A" * 31,
87
- @module.write(sio, "A" * 31).string)
93
+ @klass.new(sio).write("A" * 31).string)
88
94
  end
89
95
 
90
96
  def test_write__raw16
91
97
  assert_equal(
92
98
  "\xDA\x00\x20" + "A" * 0x0020,
93
- @module.write(sio, "A" * 0x0020).string)
99
+ @klass.new(sio).write("A" * 0x0020).string)
94
100
  assert_equal(
95
101
  "\xDA\xFF\xFF" + "A" * 0xFFFF,
96
- @module.write(sio, "A" * 0xFFFF).string)
102
+ @klass.new(sio).write("A" * 0xFFFF).string)
97
103
  end
98
104
 
99
105
  def test_write__raw32
100
106
  assert_equal(
101
107
  "\xDB\x00\x01\x00\x00" + "A" * 0x00010000,
102
- @module.write(sio, "A" * 0x00010000).string)
108
+ @klass.new(sio).write("A" * 0x00010000).string)
103
109
  end
104
110
 
105
111
  def test_write__fixarray
106
- assert_equal("\x90", @module.write(sio, []).string)
107
- assert_equal("\x93\x00\x01\x02", @module.write(sio, [0, 1, 2]).string)
112
+ assert_equal("\x90", @klass.new(sio).write([]).string)
113
+ assert_equal("\x93\x00\x01\x02", @klass.new(sio).write([0, 1, 2]).string)
108
114
 
109
- io = StringIO.new("\x9F", "a+")
115
+ packer = @klass.new(StringIO.new("\x9F", "a+"))
110
116
  array = 0x0F.times.map { |i|
111
- @module.write(io, i)
117
+ packer.write(i)
112
118
  i
113
119
  }
114
- assert_equal(io.string, @module.write(sio, array).string)
120
+ assert_equal(packer.io.string, @klass.new(sio).write(array).string)
115
121
  end
116
122
 
117
123
  def test_write__array16_min
118
- io = StringIO.new("\xDC\x00\x10", "a+")
124
+ packer = @klass.new(StringIO.new("\xDC\x00\x10", "a+"))
119
125
  array = 0x0010.times.map { |i|
120
- @module.write(io, i)
126
+ packer.write(i)
121
127
  i
122
128
  }
123
- assert_equal(io.string, @module.write(sio, array).string)
129
+ assert_equal(packer.io.string, @klass.new(sio).write(array).string)
124
130
  end
125
131
 
126
132
  def test_write__array16_max
127
- io = StringIO.new("\xDC\xFF\xFF", "a+")
133
+ packer = @klass.new(StringIO.new("\xDC\xFF\xFF", "a+"))
128
134
  array = 0xFFFF.times.map { |i|
129
- @module.write(io, i)
135
+ packer.write(i)
130
136
  i
131
137
  }
132
- assert_equal(io.string, @module.write(sio, array).string)
138
+ assert_equal(packer.io.string, @klass.new(sio).write(array).string)
133
139
  end
134
140
 
135
141
  def test_write__array32_min
136
- io = StringIO.new("\xDD\x00\x01\x00\x00", "a+")
142
+ packer = @klass.new(StringIO.new("\xDD\x00\x01\x00\x00", "a+"))
137
143
  array = 0x00010000.times.map { |i|
138
- @module.write(io, i)
144
+ packer.write(i)
139
145
  i
140
146
  }
141
- assert_equal(io.string, @module.write(sio, array).string)
147
+ assert_equal(packer.io.string, @klass.new(sio).write(array).string)
142
148
  end
143
149
 
144
150
  def test_write__fixmap
145
- assert_equal("\x80", @module.write(sio, {}).string)
151
+ assert_equal("\x80", @klass.new(sio).write({}).string)
146
152
  assert_equal(
147
153
  "\x82\x00\x01\x02\x03",
148
- @module.write(sio, {0 => 1, 2 => 3}).string)
154
+ @klass.new(sio).write({0 => 1, 2 => 3}).string)
149
155
 
150
- io = StringIO.new("\x8F", "a+")
156
+ packer = @klass.new(StringIO.new("\x8F", "a+"))
151
157
  hash = 0x0F.times.inject({}) { |memo, i|
152
- @module.write(io, i)
153
- @module.write(io, 0)
158
+ packer.write(i)
159
+ packer.write(0)
154
160
  memo[i] = 0
155
161
  memo
156
162
  }
157
- assert_equal(io.string, @module.write(sio, hash).string)
163
+ assert_equal(packer.io.string, @klass.new(sio).write(hash).string)
158
164
  end
159
165
 
160
166
  def test_write__map16_min
161
- io = StringIO.new("\xDE\x00\x10", "a+")
167
+ packer = @klass.new(StringIO.new("\xDE\x00\x10", "a+"))
162
168
  hash = 0x0010.times.inject({}) { |memo, i|
163
- @module.write(io, i)
164
- @module.write(io, 0)
169
+ packer.write(i)
170
+ packer.write(0)
165
171
  memo[i] = 0
166
172
  memo
167
173
  }
168
- assert_equal(io.string, @module.write(sio, hash).string)
174
+ assert_equal(packer.io.string, @klass.new(sio).write(hash).string)
169
175
  end
170
176
 
171
177
  def test_write__map16_max
172
- io = StringIO.new("\xDE\xFF\xFF", "a+")
178
+ packer = @klass.new(StringIO.new("\xDE\xFF\xFF", "a+"))
173
179
  hash = 0xFFFF.times.inject({}) { |memo, i|
174
- @module.write(io, i)
175
- @module.write(io, 0)
180
+ packer.write(i)
181
+ packer.write(0)
176
182
  memo[i] = 0
177
183
  memo
178
184
  }
179
- assert_equal(io.string, @module.write(sio, hash).string)
185
+ assert_equal(packer.io.string, @klass.new(sio).write(hash).string)
180
186
  end
181
187
 
182
188
  def test_write__map32_min
183
- io = StringIO.new("\xDF\x00\x01\x00\x00", "a+")
189
+ packer = @klass.new(StringIO.new("\xDF\x00\x01\x00\x00", "a+"))
184
190
  hash = 0x00010000.times.inject({}) { |memo, i|
185
- @module.write(io, i)
186
- @module.write(io, 0)
191
+ packer.write(i)
192
+ packer.write(0)
187
193
  memo[i] = 0
188
194
  memo
189
195
  }
190
- assert_equal(io.string, @module.write(sio, hash).string)
196
+ assert_equal(packer.io.string, @klass.new(sio).write(hash).string)
191
197
  end
192
198
 
193
199
  private
@@ -7,7 +7,13 @@ require "msgpack_pure/unpacker"
7
7
 
8
8
  class UnpackerTest < Test::Unit::TestCase
9
9
  def setup
10
- @module = MessagePackPure::Unpacker
10
+ @klass = MessagePackPure::Unpacker
11
+ end
12
+
13
+ def test_initialize
14
+ io = StringIO.new
15
+ unpacker = @klass.new(io)
16
+ assert_same(io, unpacker.io)
11
17
  end
12
18
 
13
19
  def test_read__positive_fixnum
@@ -127,7 +133,7 @@ class UnpackerTest < Test::Unit::TestCase
127
133
  i
128
134
  }
129
135
  io.rewind
130
- assert_equal(array, @module.read(io))
136
+ assert_equal(array, @klass.new(io).read)
131
137
  end
132
138
 
133
139
  def test_read__array16
@@ -140,7 +146,7 @@ class UnpackerTest < Test::Unit::TestCase
140
146
  i
141
147
  }
142
148
  io.rewind
143
- assert_equal(array, @module.read(io))
149
+ assert_equal(array, @klass.new(io).read)
144
150
  end
145
151
 
146
152
  def test_read__array32
@@ -153,7 +159,7 @@ class UnpackerTest < Test::Unit::TestCase
153
159
  i
154
160
  }
155
161
  io.rewind
156
- assert_equal(array, @module.read(io))
162
+ assert_equal(array, @klass.new(io).read)
157
163
  end
158
164
 
159
165
  def test_read__fixmap
@@ -170,7 +176,7 @@ class UnpackerTest < Test::Unit::TestCase
170
176
  memo
171
177
  }
172
178
  io.rewind
173
- assert_equal(hash, @module.read(io))
179
+ assert_equal(hash, @klass.new(io).read)
174
180
  end
175
181
 
176
182
  def test_read__map16
@@ -187,7 +193,7 @@ class UnpackerTest < Test::Unit::TestCase
187
193
  memo
188
194
  }
189
195
  io.rewind
190
- assert_equal(hash, @module.read(io))
196
+ assert_equal(hash, @klass.new(io).read)
191
197
  end
192
198
 
193
199
  def test_read__map32
@@ -204,12 +210,12 @@ class UnpackerTest < Test::Unit::TestCase
204
210
  memo
205
211
  }
206
212
  io.rewind
207
- assert_equal(hash, @module.read(io))
213
+ assert_equal(hash, @klass.new(io).read)
208
214
  end
209
215
 
210
216
  private
211
217
 
212
218
  def read(binary)
213
- return @module.read(StringIO.new(binary, "r"))
219
+ return @klass.new(StringIO.new(binary, "r")).read
214
220
  end
215
221
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nayutaya-msgpack-pure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuya Kato
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-08 00:00:00 +09:00
12
+ date: 2010-03-09 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15