Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse numbers as signed values. #70

Merged
merged 2 commits into from
Oct 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/dbf/column/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def underscored_name
def type_cast_methods # nodoc
{
'N' => :unpack_number,
'I' => :unpack_unsigned_long,
'I' => :unpack_signed_long,
'F' => :unpack_float,
'Y' => :unpack_currency,
'D' => :decode_date,
Expand Down Expand Up @@ -111,11 +111,11 @@ def unpack_number(value) # nodoc
end

def unpack_currency(value) # nodoc
(unpack_unsigned_long(value) / 10_000.0).to_f
(unpack_signed_long(value) / 10_000.0).to_f
end

def unpack_unsigned_long(value) # nodoc
value.unpack('V')[0]
def unpack_signed_long(value) # nodoc
value.unpack('l<')[0]
end

def unpack_float(value) # nodoc
Expand Down
39 changes: 39 additions & 0 deletions spec/dbf/column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@
expect(column.type_cast(value)).to be_a(Fixnum)
expect(column.type_cast(value)).to eq 135
end

it 'supports negative Fixnum' do
value = '-135'
column = DBF::Column::Dbase.new table, "ColumnName", "N", 3, 0
expect(column.type_cast(value)).to be_a(Fixnum)
expect(column.type_cast(value)).to eq -135
end
end

context 'and more than 0 decimals' do
Expand All @@ -67,6 +74,13 @@
expect(column.type_cast(value)).to be_a(Float)
expect(column.type_cast(value)).to eq 13.5
end

it 'supports negative Float' do
value = '-13.5'
column = DBF::Column::Dbase.new table, "ColumnName", "N", 2, 1
expect(column.type_cast(value)).to be_a(Float)
expect(column.type_cast(value)).to eq -13.5
end
end
end

Expand All @@ -84,6 +98,13 @@
expect(column.type_cast(value)).to be_a(Float)
expect(column.type_cast(value)).to eq 135.0
end

it 'supports negative Float' do
value = '-135'
column = DBF::Column::Dbase.new table, "ColumnName", "F", 3, 0
expect(column.type_cast(value)).to be_a(Float)
expect(column.type_cast(value)).to eq -135.0
end
end

context "with type B (binary)" do
Expand All @@ -99,6 +120,12 @@
expect(column.type_cast("\xEC\x51\xB8\x1E\x85\x6B\x31\x40")).to be_a(Float)
expect(column.type_cast("\xEC\x51\xB8\x1E\x85\x6B\x31\x40")).to eq 17.42
end

it 'supports negative binary' do
column = DBF::Column::Dbase.new table, "ColumnName", "B", 1, 2
expect(column.type_cast("\x00\x00\x00\x00\x00\xC0\x65\xC0")).to be_a(Float)
expect(column.type_cast("\x00\x00\x00\x00\x00\xC0\x65\xC0")).to eq -174.0
end
end
end

Expand All @@ -116,6 +143,13 @@
expect(column.type_cast(value)).to be_a(Fixnum)
expect(column.type_cast(value)).to eq 96643
end

it "supports negative Fixnum" do
value = "\x24\xE1\xFF\xFF\xFF\xFF\xFF\xFF"
column = DBF::Column::Dbase.new table, "ColumnName", "I", 3, 0
expect(column.type_cast(value)).to be_a(Fixnum)
expect(column.type_cast(value)).to eq -7900
end
end

context 'with type L (logical/boolean)' do
Expand Down Expand Up @@ -234,6 +268,11 @@
expect(column.type_cast(" \xBF\x02\x00\x00\x00\x00\x00")).to eq 18.0
end

it 'supports negative currency' do
expect(column.type_cast("\xFC\xF0\xF0\xFE\xFF\xFF\xFF\xFF")).to be_a(Float)
expect(column.type_cast("\xFC\xF0\xF0\xFE\xFF\xFF\xFF\xFF")).to eq -1776.41
end

context 'and 0 length' do
it 'returns nil' do
column = DBF::Column::Dbase.new table, "ColumnName", "Y", 0, 0
Expand Down