Merge PR #7084: types: fix panic on Int.BigInt()
This commit is contained in:
@@ -208,6 +208,10 @@ func (d Dec) Abs() Dec { return Dec{new(big.Int).Abs(d.i)} } // absolut
|
||||
|
||||
// BigInt returns a copy of the underlying big.Int.
|
||||
func (d Dec) BigInt() *big.Int {
|
||||
if d.IsNil() {
|
||||
return nil
|
||||
}
|
||||
|
||||
copy := new(big.Int)
|
||||
return copy.Set(d.i)
|
||||
}
|
||||
|
||||
@@ -76,9 +76,17 @@ type Int struct {
|
||||
|
||||
// BigInt converts Int to big.Int
|
||||
func (i Int) BigInt() *big.Int {
|
||||
if i.IsNil() {
|
||||
return nil
|
||||
}
|
||||
return new(big.Int).Set(i.i)
|
||||
}
|
||||
|
||||
// IsNil returns true if Int is uninitialized
|
||||
func (i Int) IsNil() bool {
|
||||
return i.i == nil
|
||||
}
|
||||
|
||||
// NewInt constructs Int from int64
|
||||
func NewInt(n int64) Int {
|
||||
return Int{big.NewInt(n)}
|
||||
|
||||
@@ -81,6 +81,8 @@ func TestIntPanic(t *testing.T) {
|
||||
|
||||
// Division-by-zero check
|
||||
require.Panics(t, func() { i1.Quo(NewInt(0)) })
|
||||
|
||||
require.NotPanics(t, func() { Int{}.BigInt() })
|
||||
}
|
||||
|
||||
// Tests below uses randomness
|
||||
|
||||
Reference in New Issue
Block a user