Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhuang2016 committed Jul 16, 2019
1 parent ed7413d commit 99f0072
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions types/mydecimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ var (
// get the zero of MyDecimal with the specified result fraction digits
func zeroMyDecimalWithFrac(frac int8) MyDecimal {
zero := MyDecimal{}
zero.digitsFrac = frac
zero.resultFrac = frac
return zero
}
Expand Down
11 changes: 7 additions & 4 deletions types/mydecimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ func (s *testMyDecimalSuite) TestSub(c *C) {
{"1234500009876.5", ".00012345000098765", "1234500009876.49987654999901235", nil},
{"9999900000000.5", ".555", "9999899999999.945", nil},
{"1111.5551", "1111.555", "0.0001", nil},
{".555", ".555", "0", nil},
{".555", ".555", "0.000", nil},
{"10000000", "1", "9999999", nil},
{"1000001000", ".1", "1000000999.9", nil},
{"1000000000", ".1", "999999999.9", nil},
Expand All @@ -728,6 +728,7 @@ func (s *testMyDecimalSuite) TestSub(c *C) {
{"-123.45", "-12345", "12221.55", nil},
{"-12345", "123.45", "-12468.45", nil},
{"12345", "-123.45", "12468.45", nil},
{"12.12", "12.12", "0.00", nil},
}
for _, tt := range tests {
var a, b, sum MyDecimal
Expand Down Expand Up @@ -759,6 +760,7 @@ func (s *testMyDecimalSuite) TestMul(c *C) {
{"1" + strings.Repeat("0", 60), "1" + strings.Repeat("0", 60), "0", ErrOverflow},
{"0.5999991229316", "0.918755041726043", "0.5512522192246113614062276588", nil},
{"0.5999991229317", "0.918755041726042", "0.5512522192247026369112773314", nil},
{"0.000", "-1", "0.000", nil},
}
for _, tt := range tests {
var a, b, product MyDecimal
Expand Down Expand Up @@ -786,7 +788,7 @@ func (s *testMyDecimalSuite) TestDivMod(c *C) {
{"0", "0", "", ErrDivByZero},
{"-12193185.1853376", "98765.4321", "-123.456000000000000000", nil},
{"121931851853376", "987654321", "123456.000000000", nil},
{"0", "987", "0", nil},
{"0", "987", "0.00000", nil},
{"1", "3", "0.333333333", nil},
{"1.000000000000", "3", "0.333333333333333333", nil},
{"1", "1", "1.000000000", nil},
Expand All @@ -799,7 +801,7 @@ func (s *testMyDecimalSuite) TestDivMod(c *C) {
var a, b, to MyDecimal
a.FromString([]byte(tt.a))
b.FromString([]byte(tt.b))
err := doDivMod(&a, &b, &to, nil, 5)
err := DecimalDiv(&a, &b, &to, 5)
c.Check(err, Equals, tt.err)
if tt.err == ErrDivByZero {
continue
Expand All @@ -816,12 +818,13 @@ func (s *testMyDecimalSuite) TestDivMod(c *C) {
{"99999999999999999999999999999999999999", "3", "0", nil},
{"51", "0.003430", "0.002760", nil},
{"0.0000000001", "1.0", "0.0000000001", nil},
{"0.000", "0.1", "0.000", nil},
}
for _, tt := range tests {
var a, b, to MyDecimal
a.FromString([]byte(tt.a))
b.FromString([]byte(tt.b))
ec := doDivMod(&a, &b, nil, &to, 0)
ec := DecimalMod(&a, &b, &to)
c.Check(ec, Equals, tt.err)
if tt.err == ErrDivByZero {
continue
Expand Down

0 comments on commit 99f0072

Please sign in to comment.