Skip to content

Commit

Permalink
Rserve::Protocol get_int and get_long uses pack. Spec for test compat…
Browse files Browse the repository at this point in the history
…ibility for old method
  • Loading branch information
clbustos committed Jun 8, 2010
1 parent 08616ee commit f1012e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
26 changes: 12 additions & 14 deletions lib/rserve/protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,11 @@ def new_hdr(ty, len)
# make sure that the buffer is big enough

def get_int(buf, o)
if true or Config::CONFIG['arch']=="x86_64-linux"
buf[o,4].pack("C*").unpack("l")[0]
else
v=((buf[o]&255)|((buf[o+1]&255)<<8)|((buf[o+2]&255)<<16)|((buf[o+3]&255)<<24))
v >= MAX_LONG_SIGNED ? v-MAX_LONG_UNSIGNED : v
end
buf[o,4].pack("C*").unpack("l")[0]
end
def get_int_original(buf,o) # :nodoc:
v=((buf[o]&255)|((buf[o+1]&255)<<8)|((buf[o+2]&255)<<16)|((buf[o+3]&255)<<24))
v >= MAX_LONG_SIGNED ? v-MAX_LONG_UNSIGNED : v
end

# converts bit-wise stored length from a header. "long" format is supported up to 32-bit
Expand All @@ -200,14 +199,13 @@ def get_len(buf, o)
# @param o offset (8 bytes will be used)
# @return long value */
def get_long(buf, o)
if true or Config::CONFIG['arch']=="x86_64-linux"
buf[o,8].pack("CCCCCCCC").unpack("Q")[0]
else
low=(get_int(buf,o))&0xffffffff;
hi=(get_int(buf,o+4))&0xffffffff;
hi<<=32; hi|=low;
hi
end
buf[o,8].pack("CCCCCCCC").unpack("Q")[0]
end
def get_long_original(buf,o) #:nodoc:
low=(get_int(buf,o))&0xffffffff;
hi=(get_int(buf,o+4))&0xffffffff;
hi<<=32; hi|=low;
hi
end

def longBitsToDouble(bits)
Expand Down
5 changes: 5 additions & 0 deletions spec/rserve_protocol_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@
buffer=[0xFF,0x78,0x56,0x34,0x12]
expected=0x12345678
@t.get_int(buffer,1).should==expected
@t.get_int(buffer,1).should==@t.get_int_original(buffer,1)

# Version with errors

buffer=[0xFF,0xFF78,0xFF56,0xFF34,0xFF12]
expected=0x12345678
@t.get_int(buffer,1).should==expected
end



it "get_len method should return correct length from a header" do
cmd=Rserve::Protocol::CMD_login
len=0x12345678
Expand All @@ -61,6 +65,7 @@
buffer=[0xFF,0x78,0x56,0x34,0x12,0x78,0x56,0x34,0x12]
expected=0x1234567812345678
@t.get_long(buffer,1).should==expected
@t.get_long(buffer,1).should==@t.get_long_original(buffer,1)

end
it "set_long method should set correct long(32 bits) for a given buffer" do
Expand Down

0 comments on commit f1012e8

Please sign in to comment.