Fixed byte buffer double encoding problem. Made byte buffer work under Ruby 1.9.

This commit is contained in:
Jim Menard 2008-12-16 07:20:20 -05:00
parent d79551eaa8
commit bce01bbaa6
3 changed files with 39 additions and 4 deletions

View File

@ -49,8 +49,14 @@ class ByteBuffer
@cursor += array.length
end
def put_int(i, offset=nil)
put_array([i].pack(@int_pack_order).split(//).collect{|c| c[0]}, offset)
if RUBY_VERSION >= '1.9'
def put_int(i, offset=nil)
put_array([i].pack(@int_pack_order).split(//).collect{|c| c.bytes.first}, offset)
end
else
def put_int(i, offset=nil)
put_array([i].pack(@int_pack_order).split(//).collect{|c| c[0]}, offset)
end
end
def put_long(i, offset=nil)
@ -64,8 +70,14 @@ class ByteBuffer
end
end
def put_double(d, offset=nil)
put_array([d].pack(@double_pack_order).split(//), offset)
if RUBY_VERSION >= '1.9'
def put_double(d, offset=nil)
put_array([d].pack(@double_pack_order).split(//).collect{|c| c.bytes.first}, offset)
end
else
def put_double(d, offset=nil)
put_array([d].pack(@double_pack_order).split(//).collect{|c| c[0]}, offset)
end
end
# If +size+ == 1, returns one byte. Else returns array of bytes of length

17
tests/test_bson.rb Normal file
View File

@ -0,0 +1,17 @@
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
require 'mongo'
require 'test/unit'
# NOTE: assumes Mongo is running
class BSONTest < Test::Unit::TestCase
def setup
@b = BSON.new
end
def test_object_encoding
doc = {'doc' => {'age' => 41.2, 'name' => 'Spongebob'}}
@b.serialize(doc)
assert_equal doc, @b.deserialize
end
end

View File

@ -32,6 +32,12 @@ class ByteBufferTest < Test::Unit::TestCase
assert_equal 1027, @buf.get_long
end
def test_get_double
@buf.put_double 41.2
@buf.rewind
assert_equal 41.2, @buf.get_double
end
def test_rewrite
@buf.put_int(0)
@buf.rewind