mochilo 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/pack_test.rb ADDED
@@ -0,0 +1,172 @@
1
+ # encoding: ascii-8bit
2
+ require File.expand_path('../setup', __FILE__)
3
+
4
+ require 'mochilo'
5
+ require 'stringio'
6
+
7
+ class MochiloPackTest < MiniTest::Unit::TestCase
8
+
9
+ OBJECTS = [
10
+ {"hello" => "world"},
11
+ 12345,
12
+ -12345,
13
+ "hey this is a test",
14
+ 0.231
15
+ ]
16
+
17
+ def bytes(a)
18
+ a.bytes.to_a.map { |b| "0x" + b.to_s(16) }.join(' ')
19
+ end
20
+
21
+ def all_objects
22
+ OBJECTS.map{ |obj| Mochilo.pack(obj) }.join
23
+ end
24
+
25
+ def test_simple_pack
26
+ OBJECTS.each do |obj|
27
+ a = Mochilo.pack(obj)
28
+ b = Mochilo.unpack(a)
29
+ assert_equal obj, b
30
+ end
31
+ end
32
+
33
+ def test_pack_nil
34
+ assert_equal "\xC0", Mochilo.pack(nil)
35
+ end
36
+
37
+ def test_pack_false
38
+ assert_equal "\xC2", Mochilo.pack(false)
39
+ end
40
+
41
+ def test_pack_true
42
+ assert_equal "\xC3", Mochilo.pack(true)
43
+ end
44
+
45
+ def xtest_pack_float
46
+ # ruby uses double's internally so we can't reliably
47
+ # test for a float without some hacks
48
+ end
49
+
50
+ def test_pack_double
51
+ assert_equal "\xCB@^\xDC\xCC\xCC\xCC\xCC\xCD", Mochilo.pack(123.45)
52
+ end
53
+
54
+ def test_pack_positive_fixed
55
+ assert_equal "\x15", Mochilo.pack(21)
56
+ end
57
+
58
+ def test_pack_negative_fixed
59
+ assert_equal "\xEB", Mochilo.pack(-21)
60
+ end
61
+
62
+ def test_pack_uint8
63
+ assert_equal "\xCC\xD6", Mochilo.pack(214)
64
+ end
65
+
66
+ def test_pack_uint16
67
+ assert_equal "\xCDS\xE2", Mochilo.pack(21474)
68
+ end
69
+
70
+ def test_pack_uint32
71
+ assert_equal "\xCE\x7F\xFF\xFF\xFF", Mochilo.pack(2147483647)
72
+ end
73
+
74
+ def test_pack_uint64
75
+ assert_equal "\xCF\x00\x00\x00\x04\xFF\xFF\xFF\xFF", Mochilo.pack(21474836479)
76
+ end
77
+
78
+ def test_pack_int8
79
+ assert_equal "\xD0\xDE", Mochilo.pack(-34)
80
+ end
81
+
82
+ def test_pack_int16
83
+ assert_equal "\xD1\xAC\x1E", Mochilo.pack(-21474)
84
+ end
85
+
86
+ def test_pack_int32
87
+ assert_equal "\xD2\x80\x00\x00\x01", Mochilo.pack(-2147483647)
88
+ end
89
+
90
+ def test_pack_int64
91
+ assert_equal "\xD3\xFF\xFF\xFF\xFB\x00\x00\x00\x01", Mochilo.pack(-21474836479)
92
+ end
93
+
94
+ if defined?(Encoding)
95
+ def test_pack_str16
96
+ str = "this is a test".force_encoding('UTF-8')
97
+ assert_equal "\xD8\x00\x0E\x00#{str}", Mochilo.pack(str)
98
+ end
99
+ end
100
+
101
+ def xtest_pack_str32
102
+ # TODO: not sure how to test this without making a massive 66k string
103
+ end
104
+
105
+ def test_pack_fixed_raw
106
+ str = "this is a test"
107
+ assert_equal "\xAE#{str}", Mochilo.pack(str)
108
+ end
109
+
110
+ def test_pack_raw16
111
+ str = ("a"*255)
112
+ assert_equal "\xDA\x00\xFF#{str}", Mochilo.pack(str)
113
+ end
114
+
115
+ def xtest_pack_raw32
116
+ # TODO: not sure how to test this without making a massive 66k string
117
+ end
118
+
119
+ def test_pack_fixed_array
120
+ assert_equal "\x90", Mochilo.pack([])
121
+ assert_equal "\x91\x01", Mochilo.pack([1])
122
+ end
123
+
124
+ def test_pack_array16
125
+ bytes = ("a"*34).bytes.to_a
126
+ assert_equal "\xDC\x00\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Mochilo.pack(bytes)
127
+ end
128
+
129
+ def xtest_pack_array32
130
+ # TODO: not sure how to test this without making a massive 66k item array
131
+ end
132
+
133
+ def test_pack_fixed_map
134
+ assert_equal "\x80", Mochilo.pack({})
135
+ assert_equal "\x81\x01\x02", Mochilo.pack({1 => 2})
136
+ end
137
+
138
+ def test_pack_symbol
139
+ assert_equal "\xD8\x00\x04\x01test", Mochilo.pack(:test)
140
+ assert_equal "\xD4\x04test", Mochilo.pack_unsafe(:test)
141
+ end
142
+
143
+ def test_pack_symbol_size
144
+ too_big = ("a"*0x100).to_sym
145
+ fine = ("a"*0xff).to_sym
146
+
147
+ assert_raises Mochilo::PackError do
148
+ Mochilo.pack_unsafe(too_big)
149
+ end
150
+
151
+ begin
152
+ Mochilo.pack_unsafe(fine)
153
+ rescue Mochilo::PackError => boom
154
+ assert_nil boom, "exception raised, expected nothing"
155
+ end
156
+ end
157
+
158
+ def test_pack_map16
159
+ bytes = ("a"*34).bytes.to_a
160
+ assert_equal "\xDC\x00\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Mochilo.pack(bytes)
161
+ end
162
+
163
+ def test_pack_map32
164
+ # TODO: not sure how to test this without making a massive 66k item hash
165
+ end
166
+
167
+ def test_pack_unsupported_type
168
+ assert_raises Mochilo::PackError do
169
+ Mochilo.pack(Object.new)
170
+ end
171
+ end
172
+ end
data/test/setup.rb ADDED
@@ -0,0 +1,16 @@
1
+ # Basic test environment.
2
+ #
3
+ # This should set up the load path for testing only. Don't require any support libs
4
+ # or gitrpc stuff in here.
5
+
6
+ # blah fuck this
7
+ require 'rubygems' if !defined?(Gem)
8
+ require 'bundler/setup'
9
+
10
+ # bring in minitest
11
+ require 'minitest/autorun'
12
+
13
+ # put lib and test dirs directly on load path
14
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
15
+ $LOAD_PATH.unshift File.expand_path('..', __FILE__)
16
+
@@ -0,0 +1,159 @@
1
+ # encoding: utf-8
2
+ require File.expand_path('../setup', __FILE__)
3
+
4
+ require 'mochilo'
5
+
6
+ class MochiloUnpackTest < MiniTest::Unit::TestCase
7
+ BUFFERS = [
8
+ "\xCC\x80",
9
+ "\xCD\x04\xD2",
10
+ "\xCE\x00\x01\xE2@",
11
+ "\xCF\x00\x00\x00\x01\x00\x00\x00\x00",
12
+ "\xD0\x81",
13
+ "\xD1\xFA\xF7",
14
+ "\xD2\xFF\xFE\x1D\xC0",
15
+ "\xD3\xFF\xFF\xFF\xFF\x00\x00\x00\x00",
16
+ "\xC0",
17
+ "\xC3",
18
+ "\xC2",
19
+ "\xCB@\x93J=p\xA3\xD7\n",
20
+ "\xDA\x00\x01\x61",
21
+ "\xDB\x00\x00\x00\x01\x61",
22
+ "\xDC\x00\x01\241a",
23
+ "\xDD\x00\x00\x00\x01\241a",
24
+ "\xDE\x00\x01\241a\241a",
25
+ "\241\x61",
26
+ "\x91\x00",
27
+ ]
28
+
29
+ def test_simple_unpack
30
+ BUFFERS.each do |buf|
31
+ a = Mochilo.unpack(buf)
32
+ b = Mochilo.pack(a)
33
+ c = Mochilo.unpack(b)
34
+ assert_equal a, c
35
+ end
36
+ end
37
+
38
+ def test_unpack_nil
39
+ assert_equal nil, Mochilo.unpack("\xC0")
40
+ end
41
+
42
+ def test_unpack_true
43
+ assert_equal true, Mochilo.unpack("\xC3")
44
+ end
45
+
46
+ def test_unpack_false
47
+ assert_equal false, Mochilo.unpack("\xC2")
48
+ end
49
+
50
+ def xtest_unpack_float
51
+ # ruby uses double's internally so we can't reliably
52
+ # test for a float without some hacks
53
+ end
54
+
55
+ def test_unpack_double
56
+ assert_equal 123.45, Mochilo.unpack("\xCB@^\xDC\xCC\xCC\xCC\xCC\xCD")
57
+ end
58
+
59
+ def test_unpack_positive_fixed
60
+ assert_equal 21, Mochilo.unpack("\x15")
61
+ end
62
+
63
+ def test_unpack_negative_fixed
64
+ assert_equal -21, Mochilo.unpack("\xEB")
65
+ end
66
+
67
+ def test_unpack_uint8
68
+ assert_equal 214, Mochilo.unpack("\xCC\xD6")
69
+ end
70
+
71
+ def test_unpack_uint16
72
+ assert_equal 21474, Mochilo.unpack("\xCDS\xE2")
73
+ end
74
+
75
+ def test_unpack_uint32
76
+ assert_equal 2147483647, Mochilo.unpack("\xCE\x7F\xFF\xFF\xFF")
77
+ end
78
+
79
+ def test_unpack_uint64
80
+ assert_equal 21474836479, Mochilo.unpack("\xCF\x00\x00\x00\x04\xFF\xFF\xFF\xFF")
81
+ end
82
+
83
+ def test_unpack_int8
84
+ assert_equal -34, Mochilo.unpack("\xD0\xDE")
85
+ end
86
+
87
+ def test_unpack_int16
88
+ assert_equal -21474, Mochilo.unpack("\xD1\xAC\x1E")
89
+ end
90
+
91
+ def test_unpack_int32
92
+ assert_equal -2147483647, Mochilo.unpack("\xD2\x80\x00\x00\x01")
93
+ end
94
+
95
+ def test_unpack_int64
96
+ assert_equal -21474836479, Mochilo.unpack("\xD3\xFF\xFF\xFF\xFB\x00\x00\x00\x01")
97
+ end
98
+
99
+ if defined?(Encoding)
100
+ def test_unpack_str16
101
+ str = "this is a test".force_encoding('UTF-8')
102
+ assert_equal str, Mochilo.unpack("\xD8\x00\x0E\x00#{str}")
103
+ end
104
+ end
105
+
106
+ def xtest_unpack_str32
107
+ # TODO: not sure how to test this without making a massive 66k string
108
+ end
109
+
110
+ def test_unpack_fixed_raw
111
+ str = "this is a test"
112
+ assert_equal str, Mochilo.unpack("\xAE#{str}")
113
+ end
114
+
115
+ def test_unpack_symbol
116
+ assert_raises Mochilo::UnpackError do
117
+ Mochilo.unpack("\xD4\x04test")
118
+ end
119
+
120
+ assert_equal :test, Mochilo.unpack_unsafe("\xD4\x04test")
121
+ end
122
+
123
+ def test_unpack_raw16
124
+ str = ("a"*255)
125
+ assert_equal str, Mochilo.unpack("\xDA\x00\xFF#{str}")
126
+ end
127
+
128
+ def xtest_unpack_raw32
129
+ # TODO: not sure how to test this without making a massive 66k string
130
+ end
131
+
132
+ def test_unpack_fixed_array
133
+ assert_equal [], Mochilo.unpack("\x90")
134
+ assert_equal [1], Mochilo.unpack("\x91\x01")
135
+ end
136
+
137
+ def test_unpack_array16
138
+ bytes = ("a"*34).bytes.to_a
139
+ assert_equal bytes, Mochilo.unpack("\xDC\x00\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
140
+ end
141
+
142
+ def xtest_unpack_array32
143
+ # TODO: not sure how to test this without making a massive 66k item array
144
+ end
145
+
146
+ def test_unpack_fixed_map
147
+ assert_equal({}, Mochilo.unpack("\x80"))
148
+ assert_equal({1 => 2}, Mochilo.unpack("\x81\x01\x02"))
149
+ end
150
+
151
+ def test_unpack_map16
152
+ bytes = ("a"*34).bytes.to_a
153
+ assert_equal bytes, Mochilo.unpack("\xDC\x00\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
154
+ end
155
+
156
+ def test_unpack_map32
157
+ # TODO: not sure how to test this without making a massive 66k item hash
158
+ end
159
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mochilo
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Vicent Martí
8
+ - Brian Lopez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake-compiler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: 0.8.1
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: 0.8.1
28
+ - !ruby/object:Gem::Dependency
29
+ name: minitest
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: 4.1.0
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: 4.1.0
42
+ - !ruby/object:Gem::Dependency
43
+ name: msgpack
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ description:
57
+ email: vicent@github.com seniorlopez@gmail.com
58
+ executables: []
59
+ extensions:
60
+ - ext/mochilo/extconf.rb
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - README.md
67
+ - Rakefile
68
+ - docs/format-spec.md
69
+ - docs/why-banana-pack.md
70
+ - ext/mochilo/buffer.c
71
+ - ext/mochilo/buffer.h
72
+ - ext/mochilo/encodings.h
73
+ - ext/mochilo/extconf.rb
74
+ - ext/mochilo/mochilo.h
75
+ - ext/mochilo/mochilo.rb.c
76
+ - ext/mochilo/mochilo_api.h
77
+ - ext/mochilo/mochilo_pack.c
78
+ - ext/mochilo/mochilo_unpack.c
79
+ - genperf.rb
80
+ - lib/mochilo.rb
81
+ - lib/mochilo/console.rb
82
+ - lib/mochilo/version.rb
83
+ - mochilo.gemspec
84
+ - script/benchmark
85
+ - script/bootstrap
86
+ - script/console
87
+ - script/testsuite
88
+ - test/assets/pulls.json
89
+ - test/pack_test.rb
90
+ - test/setup.rb
91
+ - test/unpack_test.rb
92
+ homepage: http://github.com/brianmario/mochilo
93
+ licenses: []
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options:
97
+ - --charset=UTF-8
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.0.6
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: A ruby library for BananaPack
116
+ test_files: []