bert 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,38 +6,37 @@ module BERT
6
6
  # Returns a BERT
7
7
  def self.encode(ruby)
8
8
  complex_ruby = convert(ruby)
9
- Erlectricity::Encoder.encode(complex_ruby)
9
+ Encode.encode(complex_ruby)
10
10
  end
11
11
 
12
- # Convert Ruby types into corresponding Erlectricity representation
13
- # of BERT complex types.
12
+ # Convert complex Ruby form in simple Ruby form.
14
13
  # +item+ is the Ruby object to convert
15
14
  #
16
15
  # Returns the converted Ruby object
17
16
  def self.convert(item)
18
17
  case item
19
18
  when Hash
20
- pairs = Erl::List[]
21
- item.each_pair { |k, v| pairs << [convert(k), convert(v)] }
22
- [:bert, :dict, pairs]
19
+ pairs = []
20
+ item.each_pair { |k, v| pairs << t[convert(k), convert(v)] }
21
+ t[:bert, :dict, pairs]
23
22
  when Tuple
24
- item.map { |x| convert(x) }
23
+ Tuple.new(item.map { |x| convert(x) })
25
24
  when Array
26
- Erl::List.new(item.map { |x| convert(x) })
25
+ item.map { |x| convert(x) }
27
26
  when nil
28
- [:bert, :nil]
27
+ t[:bert, :nil]
29
28
  when TrueClass
30
- [:bert, :true]
29
+ t[:bert, :true]
31
30
  when FalseClass
32
- [:bert, :false]
31
+ t[:bert, :false]
33
32
  when Time
34
- [:bert, :time, item.to_i / 1_000_000, item.to_i % 1_000_000, item.usec]
33
+ t[:bert, :time, item.to_i / 1_000_000, item.to_i % 1_000_000, item.usec]
35
34
  when Regexp
36
- options = Erl::List[]
35
+ options = []
37
36
  options << :caseless if item.options & Regexp::IGNORECASE > 0
38
37
  options << :extended if item.options & Regexp::EXTENDED > 0
39
38
  options << :multiline if item.options & Regexp::MULTILINE > 0
40
- [:bert, :regex, item.source, options]
39
+ t[:bert, :regex, item.source, options]
41
40
  else
42
41
  item
43
42
  end
@@ -0,0 +1,21 @@
1
+ module BERT
2
+ module Types
3
+ SMALL_INT = 97
4
+ INT = 98
5
+ SMALL_BIGNUM = 110
6
+ LARGE_BIGNUM = 111
7
+ FLOAT = 99
8
+ ATOM = 100
9
+ SMALL_TUPLE = 104
10
+ LARGE_TUPLE = 105
11
+ NIL = 106
12
+ STRING = 107
13
+ LIST = 108
14
+ BIN = 109
15
+ FUN = 117
16
+ NEW_FUN = 112
17
+ MAGIC = 131
18
+ MAX_INT = (1 << 27) -1
19
+ MIN_INT = -(1 << 27)
20
+ end
21
+ end
@@ -1,64 +1,91 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class DecoderTest < Test::Unit::TestCase
4
+ BERT_NIL = [131,104,2,100,0,4,98,101,114,116,100,0,3,110,105,108].pack('c*')
5
+ BERT_TRUE = [131,104,2,100,0,4,98,101,114,116,100,0,4,116,114,117,101].pack('c*')
6
+ BERT_FALSE = [131,104,2,100,0,4,98,101,114,116,100,0,5,102,97,108,115,101].pack('c*')
7
+
4
8
  context "BERT Decoder complex type converter" do
5
9
  should "convert nil" do
6
- before = [:bert, :nil]
7
- after = nil
8
- assert_equal after, BERT::Decoder.convert(before)
10
+ assert_equal nil, BERT::Decoder.decode(BERT_NIL)
9
11
  end
10
12
 
11
13
  should "convert nested nil" do
12
- before = [[:bert, :nil], [[:bert, :nil]]]
13
- after = [nil, [nil]]
14
- assert_equal after, BERT::Decoder.convert(before)
14
+ bert = [131,108,0,0,0,2,104,2,100,0,4,98,101,114,116,100,0,3,110,105,
15
+ 108,108,0,0,0,1,104,2,100,0,4,98,101,114,116,100,0,3,110,105,
16
+ 108,106,106].pack('c*')
17
+ assert_equal [nil, [nil]], BERT::Decoder.decode(bert)
15
18
  end
16
19
 
17
20
  should "convert hashes" do
18
- before = [:bert, :dict, [[:foo, 'bar']]]
21
+ bert = [131,104,3,100,0,4,98,101,114,116,100,0,4,100,105,99,116,108,
22
+ 0,0,0,1,104,2,100,0,3,102,111,111,109,0,0,0,3,98,97,114,
23
+ 106].pack('c*')
19
24
  after = {:foo => 'bar'}
20
- assert_equal after, BERT::Decoder.convert(before)
25
+ assert_equal after, BERT::Decoder.decode(bert)
21
26
  end
22
27
 
23
28
  should "convert empty hashes" do
24
- before = [:bert, :dict, []]
29
+ bert = [131,104,3,100,0,4,98,101,114,116,100,0,4,100,105,99,116,
30
+ 106].pack('c*')
25
31
  after = {}
26
- assert_equal after, BERT::Decoder.convert(before)
32
+ assert_equal after, BERT::Decoder.decode(bert)
27
33
  end
28
34
 
29
35
  should "convert nested hashes" do
30
- before = [:bert, :dict, [[:foo, [:bert, :dict, [[:baz, 'bar']]]]]]
36
+ bert = [131,104,3,100,0,4,98,101,114,116,100,0,4,100,105,99,116,108,0,
37
+ 0,0,1,104,2,100,0,3,102,111,111,104,3,100,0,4,98,101,114,116,
38
+ 100,0,4,100,105,99,116,108,0,0,0,1,104,2,100,0,3,98,97,122,109,
39
+ 0,0,0,3,98,97,114,106,106].pack('c*')
31
40
  after = {:foo => {:baz => 'bar'}}
32
- assert_equal after, BERT::Decoder.convert(before)
41
+ assert_equal after, BERT::Decoder.decode(bert)
33
42
  end
34
43
 
35
44
  should "convert true" do
36
- before = [:bert, true]
37
- after = true
38
- assert_equal after, BERT::Decoder.convert(before)
45
+ assert_equal true, BERT::Decoder.decode(BERT_TRUE)
39
46
  end
40
47
 
41
48
  should "convert false" do
42
- before = [:bert, false]
43
- after = false
44
- assert_equal after, BERT::Decoder.convert(before)
49
+ assert_equal false, BERT::Decoder.decode(BERT_FALSE)
45
50
  end
46
51
 
47
52
  should "convert times" do
48
- before = [:bert, :time, 1254, 976067, 0]
53
+ bert = [131,104,5,100,0,4,98,101,114,116,100,0,4,116,105,109,101,98,0,
54
+ 0,4,230,98,0,14,228,195,97,0].pack('c*')
49
55
  after = Time.at(1254976067)
50
- assert_equal after, BERT::Decoder.convert(before)
56
+ assert_equal after, BERT::Decoder.decode(bert)
51
57
  end
52
58
 
53
59
  should "convert regexen" do
54
- before = [:bert, :regex, '^c(a)t$', [:caseless, :extended]]
60
+ bert = [131,104,4,100,0,4,98,101,114,116,100,0,5,114,101,103,101,120,
61
+ 109,0,0,0,7,94,99,40,97,41,116,36,108,0,0,0,2,100,0,8,99,97,
62
+ 115,101,108,101,115,115,100,0,8,101,120,116,101,110,100,101,
63
+ 100,106].pack('c*')
55
64
  after = /^c(a)t$/ix
56
- assert_equal after, BERT::Decoder.convert(before)
65
+ assert_equal after, BERT::Decoder.decode(bert)
57
66
  end
58
67
 
59
68
  should "leave other stuff alone" do
60
- before = [1, 2.0, [:foo, 'bar']]
61
- assert_equal before, BERT::Decoder.convert(before)
69
+ bert = [131,108,0,0,0,3,97,1,99,50,46,48,48,48,48,48,48,48,48,48,48,48,
70
+ 48,48,48,48,48,48,48,48,48,101,43,48,48,0,0,0,0,0,108,0,0,0,2,
71
+ 100,0,3,102,111,111,109,0,0,0,3,98,97,114,106,106].pack('c*')
72
+ after = [1, 2.0, [:foo, 'bar']]
73
+ assert_equal after, BERT::Decoder.decode(bert)
74
+ end
75
+
76
+ should "handle bignums" do
77
+ bert = [131,110,8,0,0,0,232,137,4,35,199,138].pack('c*')
78
+ assert_equal 10_000_000_000_000_000_000, BERT::Decoder.decode(bert)
79
+ end
80
+
81
+ should "handle bytelists" do
82
+ bert = [131,104,3,100,0,3,102,111,111,107,0,2,97,97,100,0,3,98,97,114].pack('c*')
83
+ assert_equal t[:foo, [97, 97], :bar], BERT::Decoder.decode(bert)
84
+ end
85
+
86
+ should "handle massive binaries" do
87
+ bert = [131,109,0,128,0,0].pack('c*') + ('a' * (8 * 1024 * 1024))
88
+ assert_equal (8 * 1024 * 1024), BERT::Decoder.decode(bert).size
62
89
  end
63
90
  end
64
91
  end
@@ -27,7 +27,7 @@ class EncoderTest < Test::Unit::TestCase
27
27
  should "convert hash to tuple with array of tuples" do
28
28
  arr = BERT::Encoder.convert({:foo => 'bar'})
29
29
  assert arr.is_a?(Array)
30
- assert arr[2].is_a?(Erl::List)
30
+ assert arr[2].is_a?(Array)
31
31
  assert arr[2][0].is_a?(Array)
32
32
  end
33
33
 
@@ -38,13 +38,13 @@ class EncoderTest < Test::Unit::TestCase
38
38
 
39
39
  should "convert array to erl list" do
40
40
  list = BERT::Encoder.convert([1, 2])
41
- assert list.is_a?(Erl::List)
41
+ assert list.is_a?(Array)
42
42
  end
43
43
 
44
44
  should "convert an array in a tuple" do
45
45
  arrtup = BERT::Encoder.convert(t[:foo, [1, 2]])
46
46
  assert arrtup.is_a?(Array)
47
- assert arrtup[1].is_a?(Erl::List)
47
+ assert arrtup[1].is_a?(Array)
48
48
  end
49
49
 
50
50
  should "convert true" do
@@ -74,9 +74,14 @@ class EncoderTest < Test::Unit::TestCase
74
74
  should "properly convert types" do
75
75
  ruby = t[:user, {:name => 'TPW'}, [/cat/i, 9.9], nil, true, false, :true, :false]
76
76
  cruby = BERT::Encoder.convert(ruby)
77
- assert cruby.instance_of?(Array)
77
+ assert cruby.instance_of?(BERT::Tuple)
78
78
  assert cruby[0].instance_of?(Symbol)
79
- assert cruby[1].instance_of?(Array)
79
+ assert cruby[1].instance_of?(BERT::Tuple)
80
+ end
81
+
82
+ should "handle bignums" do
83
+ bert = [131,110,8,0,0,0,232,137,4,35,199,138].pack('c*')
84
+ assert_equal bert, BERT::Encoder.encode(10_000_000_000_000_000_000)
80
85
  end
81
86
 
82
87
  should "leave other stuff alone" do
@@ -5,15 +5,4 @@ require 'shoulda'
5
5
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
6
  $LOAD_PATH.unshift(File.dirname(__FILE__))
7
7
  require 'bert'
8
-
9
- class Test::Unit::TestCase
10
- end
11
-
12
- # So I can easily see which arrays are Erl::List
13
- module Erl
14
- class List
15
- def inspect
16
- "l#{super}"
17
- end
18
- end
19
- end
8
+ puts "Using #{BERT::Decode.impl} implementation."
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bert
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Preston-Werner
@@ -9,19 +9,9 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-19 00:00:00 -07:00
12
+ date: 2009-10-28 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: erlectricity
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 1.1.0
24
- version:
25
15
  - !ruby/object:Gem::Dependency
26
16
  name: thoughtbot-shoulda
27
17
  type: :development
@@ -36,8 +26,8 @@ description: BERT Serializiation for Ruby
36
26
  email: tom@mojombo.com
37
27
  executables: []
38
28
 
39
- extensions: []
40
-
29
+ extensions:
30
+ - ext/bert/c/extconf.rb
41
31
  extra_rdoc_files:
42
32
  - LICENSE
43
33
  - README.md
@@ -49,10 +39,20 @@ files:
49
39
  - README.md
50
40
  - Rakefile
51
41
  - VERSION
42
+ - bench/bench.rb
43
+ - bench/decode_bench.rb
44
+ - bench/encode_bench.rb
45
+ - bench/results.txt
52
46
  - bert.gemspec
47
+ - ext/bert/c/decode.c
48
+ - ext/bert/c/extconf.rb
53
49
  - lib/bert.rb
50
+ - lib/bert/bert.rb
51
+ - lib/bert/decode.rb
54
52
  - lib/bert/decoder.rb
53
+ - lib/bert/encode.rb
55
54
  - lib/bert/encoder.rb
55
+ - lib/bert/types.rb
56
56
  - test/bert_test.rb
57
57
  - test/decoder_test.rb
58
58
  - test/encoder_test.rb
@@ -66,6 +66,7 @@ rdoc_options:
66
66
  - --charset=UTF-8
67
67
  require_paths:
68
68
  - lib
69
+ - ext
69
70
  required_ruby_version: !ruby/object:Gem::Requirement
70
71
  requirements:
71
72
  - - ">="