Permalink
Browse files

fix bignum encoding. closes gh-1.

  • Loading branch information...
1 parent 9529687 commit 843a50c1a5f26f809f66e462b8299da75b43bc0d @mojombo committed Oct 28, 2009
Showing with 13 additions and 3 deletions.
  1. +3 −3 lib/bert/encode.rb
  2. +5 −0 test/decoder_test.rb
  3. +5 −0 test/encoder_test.rb
View
6 lib/bert/encode.rb
@@ -97,10 +97,10 @@ def write_bignum(num)
def write_bignum_guts(num)
write_1 (num >= 0 ? 0 : 1)
num = num.abs
- i = 0
- while (rem = (num >> i * 8) % (256)) != 0
+ while num != 0
+ rem = num % 256
write_1 rem
- i += 1
+ num = num >> 8
end
end
View
5 test/decoder_test.rb
@@ -73,6 +73,11 @@ class DecoderTest < Test::Unit::TestCase
assert_equal after, BERT::Decoder.decode(bert)
end
+ should "handle bignums" do
+ bert = [131,110,8,0,0,0,232,137,4,35,199,138].pack('c*')
+ assert_equal 10_000_000_000_000_000_000, BERT::Decoder.decode(bert)
+ end
+
should "handle bytelists" do
bert = [131,104,3,100,0,3,102,111,111,107,0,2,97,97,100,0,3,98,97,114].pack('c*')
assert_equal t[:foo, [97, 97], :bar], BERT::Decoder.decode(bert)
View
5 test/encoder_test.rb
@@ -79,6 +79,11 @@ class EncoderTest < Test::Unit::TestCase
assert cruby[1].instance_of?(BERT::Tuple)
end
+ should "handle bignums" do
+ bert = [131,110,8,0,0,0,232,137,4,35,199,138].pack('c*')
+ assert_equal bert, BERT::Encoder.encode(10_000_000_000_000_000_000)
+ end
+
should "leave other stuff alone" do
before = [1, 2.0, [:foo, 'bar']]
assert_equal before, BERT::Encoder.convert(before)

0 comments on commit 843a50c

Please sign in to comment.