mongo-ruby-driver/test/mongo_bson/byte_buffer_test.rb
Hongli Lai (Phusion) b9de2eaa5c When possible, have BSON::ByteBuffer store the underlying binary data as a binary String instead of an array.
This dramatically improves the performance of BSON::ByteBuffer.new(a_string).
On Ruby 1.9.2 it is about 130 times faster.
2010-09-09 15:59:17 -04:00

93 lines
1.9 KiB
Ruby

require './test/test_helper'
class ByteBufferTest < Test::Unit::TestCase
include BSON
def setup
@buf = ByteBuffer.new
end
def test_nil_get_returns_one_byte
@buf.put_array([1, 2, 3, 4])
@buf.rewind
assert_equal 1, @buf.get
end
def test_one_get_returns_array_length_one
@buf.put_array([1, 2, 3, 4])
@buf.rewind
assert_equal [1], @buf.get(1)
end
def test_zero_get_returns_empty_array
@buf.put_array([1, 2, 3, 4])
@buf.rewind
assert_equal [], @buf.get(0)
end
def test_empty
assert_equal 0, @buf.length
end
def test_length
@buf.put_int 3
assert_equal 4, @buf.length
end
def test_default_order
assert_equal :little_endian, @buf.order
end
def test_long_length
@buf.put_long 1027
assert_equal 8, @buf.length
end
def test_get_long
@buf.put_long 1027
@buf.rewind
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
@buf.put_int(1027)
assert_equal 4, @buf.length
@buf.rewind
assert_equal 1027, @buf.get_int
assert_equal 4, @buf.position
end
def test_prepend_byte_buffer
@buf.put_int(4)
new_buf = ByteBuffer.new([5, 0, 0, 0])
@buf.prepend!(new_buf)
assert_equal [5, 0, 0, 0, 4, 0, 0, 0], @buf.to_a
end
def test_append_byte_buffer
@buf.put_int(4)
new_buf = ByteBuffer.new([5, 0, 0, 0])
@buf.append!(new_buf)
assert_equal [4, 0, 0, 0, 5, 0, 0, 0], @buf.to_a
end
def test_binary_string_input
str = "abcd"
str.force_encoding('binary') if str.respond_to?(:force_encoding)
@buf = ByteBuffer.new(str)
assert_equal "abcd", @buf.to_s
assert_equal [97, 98, 99, 100], @buf.to_a
@buf.put_int(0)
assert_equal [97, 98, 99, 100, 0, 0, 0, 0], @buf.to_a
end
end