Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds fix decimal, makefile fixes, DRY fix generator
- Loading branch information
Chris Busbey
committed
Aug 17, 2016
1 parent
d194bff
commit ec8645b
Showing
5 changed files
with
86 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package quickfix | ||
|
||
import "github.com/shopspring/decimal" | ||
|
||
//FIXDecimal is a FIX Float Value that implements an arbitrary precision fixed-point decimal. Implements FieldValue | ||
type FIXDecimal struct { | ||
decimal.Decimal | ||
|
||
//Scale is the number of digits after the decimal point when Writing the field value as a FIX value | ||
Scale int32 | ||
} | ||
|
||
func (d FIXDecimal) Write() []byte { | ||
return []byte(d.Decimal.StringFixed(d.Scale)) | ||
} | ||
|
||
func (d *FIXDecimal) Read(bytes []byte) (err error) { | ||
d.Decimal, err = decimal.NewFromString(string(bytes)) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package quickfix | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/shopspring/decimal" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFIXDecimalWrite(t *testing.T) { | ||
var tests = []struct { | ||
decimal FIXDecimal | ||
expected string | ||
}{ | ||
{decimal: FIXDecimal{Decimal: decimal.New(-1243456, -4), Scale: 4}, expected: "-124.3456"}, | ||
{decimal: FIXDecimal{Decimal: decimal.New(-1243456, -4), Scale: 5}, expected: "-124.34560"}, | ||
{decimal: FIXDecimal{Decimal: decimal.New(-1243456, -4), Scale: 0}, expected: "-124"}, | ||
} | ||
|
||
for _, test := range tests { | ||
b := test.decimal.Write() | ||
assert.Equal(t, test.expected, string(b)) | ||
} | ||
} | ||
|
||
func TestFIXDecimalRead(t *testing.T) { | ||
var tests = []struct { | ||
bytes string | ||
expected decimal.Decimal | ||
expectError bool | ||
}{ | ||
{bytes: "15", expected: decimal.New(15, 0)}, | ||
{bytes: "15.000", expected: decimal.New(15, 0)}, | ||
{bytes: "15.001", expected: decimal.New(15001, -3)}, | ||
{bytes: "-15.001", expected: decimal.New(-15001, -3)}, | ||
{bytes: "blah", expectError: true}, | ||
{bytes: "+200.00", expected: decimal.New(200, 0)}, | ||
} | ||
|
||
for _, test := range tests { | ||
var field FIXDecimal | ||
|
||
err := field.Read([]byte(test.bytes)) | ||
require.Equal(t, test.expectError, err != nil) | ||
|
||
if !test.expectError { | ||
assert.True(t, test.expected.Equals(field.Decimal), "Expected %s got %s", test.expected, field.Decimal) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters