Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Commit

Permalink
Fix encoding of VARINTs with value 0
Browse files Browse the repository at this point in the history
  • Loading branch information
ndrwdn authored and iconara committed Jun 26, 2014
1 parent 17e3fbc commit 63271fb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/cql/protocol/cql_byte_buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,14 @@ def append_long(n)
def append_varint(n)
num = n
bytes = []
until num == 0
bytes << (num & 0xff)
num = num >> 8
break if num == -1
if num == 0
bytes << 0
else
until num == 0
bytes << (num & 0xff)
num = num >> 8
break if num == -1
end
end
append(bytes.reverse.pack(Formats::BYTES_FORMAT))
end
Expand Down
10 changes: 10 additions & 0 deletions spec/cql/protocol/cql_byte_buffer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,11 @@ module Protocol
end

describe '#append_varint' do
it 'encodes zero' do
buffer.append_varint(0)
buffer.should eql_bytes("\x00")
end

it 'encodes a variable length integer' do
buffer.append_varint(1231312312331283012830129382342342412123)
buffer.should eql_bytes("\x03\x9EV \x15\f\x03\x9DK\x18\xCDI\\$?\a[")
Expand Down Expand Up @@ -843,6 +848,11 @@ module Protocol
buffer.should eql_bytes("\x00\x00\x00\x12\r'\xFDI\xAD\x80f\x11g\xDCfV\xAA")
end

it 'encodes a 0.0 BigDecimal' do
buffer.append_decimal(BigDecimal.new('0.0'))
buffer.should eql_bytes("\x00\x00\x00\x01\x00")
end

it 'appends to the buffer' do
buffer << "\x99"
buffer.append_decimal(BigDecimal.new('1042342234234.123423435647768234'))
Expand Down

0 comments on commit 63271fb

Please sign in to comment.