diff options
author | ivahaev <ivahaev@gmail.com> | 2018-01-27 05:25:43 +0800 |
---|---|---|
committer | Victor Quinn <mail@victorquinn.com> | 2018-01-27 05:25:43 +0800 |
commit | e3482495ff4cba75613e4177ed79825c890058a9 (patch) | |
tree | dddd632dac4e6d51eacd3a628bc87eb9ed38240b | |
parent | a76a8bf60b90042d6a0bfdbc50d7a6dae67a8ee8 (diff) | |
download | dexon-decimal-e3482495ff4cba75613e4177ed79825c890058a9.tar.gz dexon-decimal-e3482495ff4cba75613e4177ed79825c890058a9.tar.zst dexon-decimal-e3482495ff4cba75613e4177ed79825c890058a9.zip |
Added ensureInitialized() to the Neg() method (#80)
* Added ensureInitialized to the Neg() method,
because it can panic when d.value is nil
* TestDecimal_NegFromEmpty added
-rw-r--r-- | decimal.go | 1 | ||||
-rw-r--r-- | decimal_test.go | 8 |
2 files changed, 9 insertions, 0 deletions
@@ -278,6 +278,7 @@ func (d Decimal) Sub(d2 Decimal) Decimal { // Neg returns -d. func (d Decimal) Neg() Decimal { + d.ensureInitialized() val := new(big.Int).Neg(d.value) return Decimal{ value: val, diff --git a/decimal_test.go b/decimal_test.go index 7979218..384f8a5 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -936,6 +936,14 @@ func TestDecimal_Neg(t *testing.T) { } } +func TestDecimal_NegFromEmpty(t *testing.T) { + a := Decimal{} + b := a.Neg() + if b.String() != "0" { + t.Errorf("expected %s, got %s", "0", b) + } +} + func TestDecimal_Mul(t *testing.T) { type Inp struct { a string |