Skip to content

Commit

Permalink
add other BigIntMut api for math.Uint
Browse files Browse the repository at this point in the history
  • Loading branch information
DongLieu committed Oct 25, 2023
1 parent fd93ee7 commit 5983e90
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions math/uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions math/uint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 5983e90

Please sign in to comment.