feat(math): add mutative api for Uint.BigInt() (#18247)

This commit is contained in:
Đông Liều 2023-11-07 15:35:17 +07:00 committed by GitHub
parent fb9dadc00f
commit a6eea3c5ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View File

@ -38,6 +38,7 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j
### Features
* [#18247](https://github.com/cosmos/cosmos-sdk/pull/18247) Add mutative api for Uint.BigInt()
* [#17803](https://github.com/cosmos/cosmos-sdk/pull/17803) Add mutative api for Int.BigInt()
### Bug Fixes

View File

@ -21,6 +21,14 @@ func (u Uint) BigInt() *big.Int {
return new(big.Int).Set(u.i)
}
// BigInt converts Uint to big.Int, mutative the input
func (u Uint) BigIntMut() *big.Int {
if u.IsNil() {
return nil
}
return u.i
}
// IsNil returns true if Uint is uninitialized
func (u Uint) IsNil() bool {
return u.i == nil

View File

@ -99,6 +99,26 @@ func (s *uintTestSuite) TestIsNil() {
s.Require().True(sdkmath.Uint{}.IsNil())
}
func (s *uintTestSuite) TestConvertToBigIntMutativeForUint() {
r := big.NewInt(30)
i := sdkmath.NewUintFromBigInt(r)
// Compare value of BigInt & BigIntMut
s.Require().Equal(i.BigInt(), i.BigIntMut())
// Modify BigIntMut() pointer and ensure i.BigIntMut() & i.BigInt() change
p1 := i.BigIntMut()
p1.SetInt64(40)
s.Require().Equal(big.NewInt(40), i.BigIntMut())
s.Require().Equal(big.NewInt(40), i.BigInt())
// Modify big.Int() pointer and ensure i.BigIntMut() & i.BigInt() don't change
p2 := i.BigInt()
p2.SetInt64(50)
s.Require().NotEqual(big.NewInt(50), i.BigIntMut())
s.Require().NotEqual(big.NewInt(50), i.BigInt())
}
func (s *uintTestSuite) TestArithUint() {
for d := 0; d < 1000; d++ {
n1 := uint64(rand.Uint32())