Clarified ByteBuffer.get behavior: get() returns one byte, get(1) returns array of length one.

This commit is contained in:
Jim Menard 2008-12-18 16:14:56 -05:00
parent 8aed3abda6
commit 80475ff198
2 changed files with 24 additions and 4 deletions

View File

@ -97,13 +97,15 @@ class ByteBuffer
end
end
# If +size+ == 1, returns one byte. Else returns array of bytes of length
# +size+.
def get(len=1)
# If +size+ == nil, returns one byte. Else returns array of bytes of length
# # +size+.
def get(len=nil)
one_byte = len.nil?
len ||= 1
check_read_length(len)
start = @cursor
@cursor += len
if len == 1
if one_byte
@buf[start]
else
@buf[start, len]

View File

@ -8,6 +8,24 @@ class ByteBufferTest < Test::Unit::TestCase
@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