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
This commit is contained in:
ivahaev 2018-01-27 02:25:43 +05:00 committed by Victor Quinn
parent a76a8bf60b
commit e3482495ff
2 changed files with 9 additions and 0 deletions

View file

@ -278,6 +278,7 @@ func (d Decimal) Sub(d2 Decimal) Decimal {
// Neg returns -d. // Neg returns -d.
func (d Decimal) Neg() Decimal { func (d Decimal) Neg() Decimal {
d.ensureInitialized()
val := new(big.Int).Neg(d.value) val := new(big.Int).Neg(d.value)
return Decimal{ return Decimal{
value: val, value: val,

View file

@ -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) { func TestDecimal_Mul(t *testing.T) {
type Inp struct { type Inp struct {
a string a string