github.com/Finschia/finschia-sdk@v0.48.1/types/int_internal_test.go (about) 1 package types 2 3 import ( 4 "math/big" 5 "math/rand" 6 "testing" 7 8 "github.com/stretchr/testify/suite" 9 ) 10 11 type internalIntTestSuite struct { 12 suite.Suite 13 } 14 15 func TestInternalIntTestSuite(t *testing.T) { 16 suite.Run(t, new(internalIntTestSuite)) 17 } 18 19 func (s *internalIntTestSuite) TestEncodingRandom() { 20 for i := 0; i < 1000; i++ { 21 n := rand.Int63() 22 ni := NewInt(n) 23 var ri Int 24 25 str, err := ni.Marshal() 26 s.Require().Nil(err) 27 err = (&ri).Unmarshal(str) 28 s.Require().Nil(err) 29 30 s.Require().Equal(ni, ri, "binary mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String()) 31 s.Require().True(ni.i != ri.i, "pointer addresses are equal; tc #%d", i) 32 33 bz, err := ni.MarshalJSON() 34 s.Require().Nil(err) 35 err = (&ri).UnmarshalJSON(bz) 36 s.Require().Nil(err) 37 38 s.Require().Equal(ni, ri, "json mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String()) 39 s.Require().True(ni.i != ri.i, "pointer addresses are equal; tc #%d", i) 40 } 41 42 for i := 0; i < 1000; i++ { 43 n := rand.Uint64() 44 ni := NewUint(n) 45 var ri Uint 46 47 str, err := ni.Marshal() 48 s.Require().Nil(err) 49 err = (&ri).Unmarshal(str) 50 s.Require().Nil(err) 51 52 s.Require().Equal(ni, ri, "binary mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String()) 53 s.Require().True(ni.i != ri.i, "pointer addresses are equal; tc #%d", i) 54 55 bz, err := ni.MarshalJSON() 56 s.Require().Nil(err) 57 err = (&ri).UnmarshalJSON(bz) 58 s.Require().Nil(err) 59 60 s.Require().Equal(ni, ri, "json mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String()) 61 s.Require().True(ni.i != ri.i, "pointer addresses are equal; tc #%d", i) 62 } 63 } 64 65 func (s *internalIntTestSuite) TestSerializationOverflow() { 66 bx, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007913129639936", 10) 67 x := Int{bx} 68 y := new(Int) 69 70 bz, err := x.Marshal() 71 s.Require().NoError(err) 72 73 // require deserialization to fail due to overflow 74 s.Require().Error(y.Unmarshal(bz)) 75 76 // require JSON deserialization to fail due to overflow 77 bz, err = x.MarshalJSON() 78 s.Require().NoError(err) 79 80 s.Require().Error(y.UnmarshalJSON(bz)) 81 } 82 83 func (s *internalIntTestSuite) TestDeserializeMaxERC20() { 84 bx, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007913129639935", 10) 85 x := Int{bx} 86 y := new(Int) 87 88 bz, err := x.Marshal() 89 s.Require().NoError(err) 90 91 // require deserialization to be successful 92 s.Require().NoError(y.Unmarshal(bz)) 93 94 // require JSON deserialization to succeed 95 bz, err = x.MarshalJSON() 96 s.Require().NoError(err) 97 98 s.Require().NoError(y.UnmarshalJSON(bz)) 99 } 100 101 func (s *internalIntTestSuite) TestImmutabilityArithInt() { 102 size := 500 103 104 ops := []intOp{ 105 applyWithRand(Int.Add, (*big.Int).Add), 106 applyWithRand(Int.Sub, (*big.Int).Sub), 107 applyWithRand(Int.Mul, (*big.Int).Mul), 108 applyWithRand(Int.Quo, (*big.Int).Quo), 109 applyRawWithRand(Int.AddRaw, (*big.Int).Add), 110 applyRawWithRand(Int.SubRaw, (*big.Int).Sub), 111 applyRawWithRand(Int.MulRaw, (*big.Int).Mul), 112 applyRawWithRand(Int.QuoRaw, (*big.Int).Quo), 113 } 114 115 for i := 0; i < 100; i++ { 116 uis := make([]Int, size) 117 bis := make([]*big.Int, size) 118 119 n := rand.Int63() 120 ui := NewInt(n) 121 bi := new(big.Int).SetInt64(n) 122 123 for j := 0; j < size; j++ { 124 op := ops[rand.Intn(len(ops))] 125 uis[j], bis[j] = op(ui, bi) 126 } 127 128 for j := 0; j < size; j++ { 129 s.Require().Equal(0, bis[j].Cmp(uis[j].BigInt()), "Int is different from *big.Int. tc #%d, Int %s, *big.Int %s", j, uis[j].String(), bis[j].String()) 130 s.Require().Equal(NewIntFromBigInt(bis[j]), uis[j], "Int is different from *big.Int. tc #%d, Int %s, *big.Int %s", j, uis[j].String(), bis[j].String()) 131 s.Require().True(uis[j].i != bis[j], "Pointer addresses are equal. tc #%d, Int %s, *big.Int %s", j, uis[j].String(), bis[j].String()) 132 } 133 } 134 } 135 136 type ( 137 intOp func(Int, *big.Int) (Int, *big.Int) 138 bigIntFunc func(*big.Int, *big.Int, *big.Int) *big.Int 139 ) 140 141 func applyWithRand(intFn func(Int, Int) Int, bigIntFn bigIntFunc) intOp { 142 return func(integer Int, bigInteger *big.Int) (Int, *big.Int) { 143 r := rand.Int63() 144 br := new(big.Int).SetInt64(r) 145 return intFn(integer, NewInt(r)), bigIntFn(new(big.Int), bigInteger, br) 146 } 147 } 148 149 func applyRawWithRand(intFn func(Int, int64) Int, bigIntFn bigIntFunc) intOp { 150 return func(integer Int, bigInteger *big.Int) (Int, *big.Int) { 151 r := rand.Int63() 152 br := new(big.Int).SetInt64(r) 153 return intFn(integer, r), bigIntFn(new(big.Int), bigInteger, br) 154 } 155 }