mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-23 04:40:49 +01:00
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:
parent
a76a8bf60b
commit
e3482495ff
2 changed files with 9 additions and 0 deletions
|
@ -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,
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue