Skip to content

Commit

Permalink
test: improve decimal coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
howeyc committed Apr 24, 2022
1 parent 4989ba7 commit 33bd6db
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions internal/decimal/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ var testCases = []testCase{
"-0.43",
NewFromFloat(-0.43).StringFixedBank(),
},
{
"sub",
"5.12",
NewFromFloat(5.56).Sub(NewFromFloat(0.44)).StringFixedBank(),
},
{
"neg",
"-5.12",
NewFromFloat(5.12).Neg().StringFixedBank(),
},
{
"abs-1",
"5.12",
NewFromFloat(-5.12).Abs().StringFixedBank(),
},
{
"abs-1",
"5.12",
NewFromFloat(5.12).Abs().StringFixedBank(),
},
}

func TestDecimal(t *testing.T) {
Expand All @@ -118,6 +138,42 @@ func TestDecimal(t *testing.T) {
}
}

func TestFloat(t *testing.T) {
d := NewFromFloat(5.56)
f := float64(5.56)
if df, _ := d.Float64(); df != f {
t.Error("Float64 not exact")
}
}

func TestCompare(t *testing.T) {
l := NewFromInt(5)
h := NewFromInt(10)
z := NewFromInt(0)

if !z.IsZero() {
t.Error("zero failed")
}

if h.Cmp(l) != 1 || l.Cmp(h) != -1 || z.Cmp(Zero) != 0 {
t.Error("compare fail")
}
}

func TestSign(t *testing.T) {
n := NewFromInt(-5)
p := NewFromInt(5)
z := NewFromInt(0)

if z.Sign() != 0 {
t.Error("zero failed")
}

if n.Sign() != -1 || p.Sign() != 1 {
t.Error("sign fail")
}
}

var testParseCases = []testCase{
{
"negzero",
Expand Down Expand Up @@ -246,9 +302,19 @@ var testParseCases = []testCase{
},
{
"error-3",
"number too big",
"10000000000000000.56",
},
{
"error-4",
"invalid syntax",
"0.e0",
},
{
"error-badint-1",
`strconv.ParseInt: parsing "1QZ": invalid syntax`,
"1QZ.56",
},
}

func TestStringParse(t *testing.T) {
Expand Down

0 comments on commit 33bd6db

Please sign in to comment.