aboutsummaryrefslogtreecommitdiffstats
path: root/decimal_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'decimal_test.go')
-rw-r--r--decimal_test.go103
1 files changed, 70 insertions, 33 deletions
diff --git a/decimal_test.go b/decimal_test.go
index fa57083..33e344c 100644
--- a/decimal_test.go
+++ b/decimal_test.go
@@ -14,28 +14,35 @@ import (
"time"
)
-var testTable = map[float64]string{
- 3.141592653589793: "3.141592653589793",
- 3: "3",
- 1234567890123456: "1234567890123456",
- 1234567890123456000: "1234567890123456000",
- 1234.567890123456: "1234.567890123456",
- .1234567890123456: "0.1234567890123456",
- 0: "0",
- .1111111111111110: "0.111111111111111",
- .1111111111111111: "0.1111111111111111",
- .1111111111111119: "0.1111111111111119",
- .000000000000000001: "0.000000000000000001",
- .000000000000000002: "0.000000000000000002",
- .000000000000000003: "0.000000000000000003",
- .000000000000000005: "0.000000000000000005",
- .000000000000000008: "0.000000000000000008",
- .1000000000000001: "0.1000000000000001",
- .1000000000000002: "0.1000000000000002",
- .1000000000000003: "0.1000000000000003",
- .1000000000000005: "0.1000000000000005",
- .1000000000000008: "0.1000000000000008",
- 1e25: "10000000000000000000000000",
+type testEnt struct {
+ float float64
+ short string
+ exact string
+}
+
+var testTable = []*testEnt{
+ {3.141592653589793, "3.141592653589793", ""},
+ {3, "3", ""},
+ {1234567890123456, "1234567890123456", ""},
+ {1234567890123456000, "1234567890123456000", ""},
+ {1234.567890123456, "1234.567890123456", ""},
+ {.1234567890123456, "0.1234567890123456", ""},
+ {0, "0", ""},
+ {.1111111111111110, "0.111111111111111", ""},
+ {.1111111111111111, "0.1111111111111111", ""},
+ {.1111111111111119, "0.1111111111111119", ""},
+ {.000000000000000001, "0.000000000000000001", ""},
+ {.000000000000000002, "0.000000000000000002", ""},
+ {.000000000000000003, "0.000000000000000003", ""},
+ {.000000000000000005, "0.000000000000000005", ""},
+ {.000000000000000008, "0.000000000000000008", ""},
+ {.1000000000000001, "0.1000000000000001", ""},
+ {.1000000000000002, "0.1000000000000002", ""},
+ {.1000000000000003, "0.1000000000000003", ""},
+ {.1000000000000005, "0.1000000000000005", ""},
+ {.1000000000000008, "0.1000000000000008", ""},
+ {1e25, "10000000000000000000000000", ""},
+ {math.MaxInt64, strconv.FormatInt(math.MaxInt64, 10), ""},
}
var testTableScientificNotation = map[string]string{
@@ -54,12 +61,23 @@ var testTableScientificNotation = map[string]string{
}
func init() {
+ for _, s := range testTable {
+ s.exact = strconv.FormatFloat(s.float, 'f', 300, 64)
+ if strings.ContainsRune(s.exact, '.') {
+ s.exact = strings.TrimRight(s.exact, "0")
+ s.exact = strings.TrimRight(s.exact, ".")
+ }
+ }
+
// add negatives
- for f, s := range testTable {
- if f > 0 {
- testTable[-f] = "-" + s
+ withNeg := testTable[:]
+ for _, s := range testTable {
+ if s.float > 0 {
+ withNeg = append(withNeg, &testEnt{-s.float, "-" + s.short, "-" + s.exact})
}
}
+ testTable = withNeg
+
for e, s := range testTableScientificNotation {
if string(e[0]) != "-" && s != "0" {
testTableScientificNotation["-"+e] = "-" + s
@@ -68,8 +86,9 @@ func init() {
}
func TestNewFromFloat(t *testing.T) {
- for f, s := range testTable {
- d := NewFromFloat(f)
+ for _, x := range testTable {
+ s := x.exact
+ d := NewFromFloat(x.float)
if d.String() != s {
t.Errorf("expected %s, got %s (%s, %d)",
s, d.String(),
@@ -92,7 +111,20 @@ func TestNewFromFloat(t *testing.T) {
}
func TestNewFromString(t *testing.T) {
- for _, s := range testTable {
+ for _, x := range testTable {
+ s := x.short
+ d, err := NewFromString(s)
+ if err != nil {
+ t.Errorf("error while parsing %s", s)
+ } else if d.String() != s {
+ t.Errorf("expected %s, got %s (%s, %d)",
+ s, d.String(),
+ d.value.String(), d.exp)
+ }
+ }
+
+ for _, x := range testTable {
+ s := x.exact
d, err := NewFromString(s)
if err != nil {
t.Errorf("error while parsing %s", s)
@@ -289,7 +321,8 @@ func TestNewFromBigIntWithExponent(t *testing.T) {
}
func TestJSON(t *testing.T) {
- for _, s := range testTable {
+ for _, x := range testTable {
+ s := x.short
var doc struct {
Amount Decimal `json:"amount"`
}
@@ -358,7 +391,8 @@ func TestBadJSON(t *testing.T) {
}
func TestNullDecimalJSON(t *testing.T) {
- for _, s := range testTable {
+ for _, x := range testTable {
+ s := x.short
var doc struct {
Amount NullDecimal `json:"amount"`
}
@@ -450,7 +484,8 @@ func TestNullDecimalBadJSON(t *testing.T) {
}
func TestXML(t *testing.T) {
- for _, s := range testTable {
+ for _, x := range testTable {
+ s := x.short
var doc struct {
XMLName xml.Name `xml:"account"`
Amount Decimal `xml:"amount"`
@@ -2035,7 +2070,8 @@ func TestNullDecimal_Value(t *testing.T) {
}
func TestBinary(t *testing.T) {
- for x := range testTable {
+ for _, y := range testTable {
+ x := y.float
// Create the decimal
d1 := NewFromFloat(x)
@@ -2070,7 +2106,8 @@ func slicesEqual(a, b []byte) bool {
}
func TestGobEncode(t *testing.T) {
- for x := range testTable {
+ for _, y := range testTable {
+ x := y.float
d1 := NewFromFloat(x)
b1, err := d1.GobEncode()