From ddaa3c5176bc79dcee12361027a6fef4f30f8dfd Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 29 Sep 2020 13:29:10 +0200 Subject: [PATCH] types: tests -> test suites migration (#7405) --- testutil/testdata/test_tx.go | 6 +- types/address_test.go | 4 + types/coin_test.go | 6 +- types/dec_coin_test.go | 155 +++++++------ types/events_test.go | 4 + types/handler_test.go | 21 +- types/int_internal_test.go | 137 +++++++++++ types/int_test.go | 426 ++++++++++++----------------------- types/invariant_test.go | 4 + types/result_test.go | 20 +- types/router_test.go | 4 + types/staking_test.go | 4 + types/store_test.go | 4 + types/tx_msg_test.go | 10 +- types/uint_internal_test.go | 55 +++++ types/uint_test.go | 305 ++++++++++++------------- types/utils_test.go | 70 +++--- 17 files changed, 674 insertions(+), 561 deletions(-) create mode 100644 types/int_internal_test.go create mode 100644 types/uint_internal_test.go diff --git a/testutil/testdata/test_tx.go b/testutil/testdata/test_tx.go index d80daaa03d..72f8d0f00a 100644 --- a/testutil/testdata/test_tx.go +++ b/testutil/testdata/test_tx.go @@ -54,7 +54,11 @@ func (msg *TestMsg) GetSignBytes() []byte { func (msg *TestMsg) GetSigners() []sdk.AccAddress { addrs := make([]sdk.AccAddress, len(msg.Signers)) for i, in := range msg.Signers { - addr, _ := sdk.AccAddressFromBech32(in) + addr, err := sdk.AccAddressFromBech32(in) + if err != nil { + panic(err) + } + addrs[i] = addr } diff --git a/types/address_test.go b/types/address_test.go index 93a59a7a80..9de4bea4f3 100644 --- a/types/address_test.go +++ b/types/address_test.go @@ -26,6 +26,10 @@ func TestAddressTestSuite(t *testing.T) { suite.Run(t, new(addressTestSuite)) } +func (s *addressTestSuite) SetupSuite() { + s.T().Parallel() +} + var invalidStrs = []string{ "hello, world!", "0xAA", diff --git a/types/coin_test.go b/types/coin_test.go index 2ef95d0f41..f3cf0647a7 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -24,6 +24,10 @@ func TestCoinTestSuite(t *testing.T) { suite.Run(t, new(coinTestSuite)) } +func (s *coinTestSuite) SetupSuite() { + s.T().Parallel() +} + // ---------------------------------------------------------------------------- // Coin tests @@ -840,7 +844,7 @@ func (s *coinTestSuite) TestNewCoins() { for _, tt := range tests { if tt.wantPanic { s.Require().Panics(func() { sdk.NewCoins(tt.coins...) }) - return + continue } got := sdk.NewCoins(tt.coins...) s.Require().True(got.IsEqual(tt.want)) diff --git a/types/dec_coin_test.go b/types/dec_coin_test.go index 2554a41707..13ec1b3ecb 100644 --- a/types/dec_coin_test.go +++ b/types/dec_coin_test.go @@ -4,80 +4,92 @@ import ( "strings" "testing" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" sdk "github.com/cosmos/cosmos-sdk/types" ) -func TestNewDecCoin(t *testing.T) { - require.NotPanics(t, func() { +type decCoinTestSuite struct { + suite.Suite +} + +func NewDecCoinTestSuite(t *testing.T) { + suite.Run(t, new(decCoinTestSuite)) +} + +func (s *decCoinTestSuite) SetupSuite() { + s.T().Parallel() +} + +func (s *decCoinTestSuite) TestNewDecCoin() { + s.Require().NotPanics(func() { sdk.NewInt64DecCoin(testDenom1, 5) }) - require.NotPanics(t, func() { + s.Require().NotPanics(func() { sdk.NewInt64DecCoin(testDenom1, 0) }) - require.NotPanics(t, func() { + s.Require().NotPanics(func() { sdk.NewInt64DecCoin(strings.ToUpper(testDenom1), 5) }) - require.Panics(t, func() { + s.Require().Panics(func() { sdk.NewInt64DecCoin(testDenom1, -5) }) } -func TestNewDecCoinFromDec(t *testing.T) { - require.NotPanics(t, func() { +func (s *decCoinTestSuite) TestNewDecCoinFromDec() { + s.Require().NotPanics(func() { sdk.NewDecCoinFromDec(testDenom1, sdk.NewDec(5)) }) - require.NotPanics(t, func() { + s.Require().NotPanics(func() { sdk.NewDecCoinFromDec(testDenom1, sdk.ZeroDec()) }) - require.NotPanics(t, func() { + s.Require().NotPanics(func() { sdk.NewDecCoinFromDec(strings.ToUpper(testDenom1), sdk.NewDec(5)) }) - require.Panics(t, func() { + s.Require().Panics(func() { sdk.NewDecCoinFromDec(testDenom1, sdk.NewDec(-5)) }) } -func TestNewDecCoinFromCoin(t *testing.T) { - require.NotPanics(t, func() { +func (s *decCoinTestSuite) TestNewDecCoinFromCoin() { + s.Require().NotPanics(func() { sdk.NewDecCoinFromCoin(sdk.Coin{testDenom1, sdk.NewInt(5)}) }) - require.NotPanics(t, func() { + s.Require().NotPanics(func() { sdk.NewDecCoinFromCoin(sdk.Coin{testDenom1, sdk.NewInt(0)}) }) - require.NotPanics(t, func() { + s.Require().NotPanics(func() { sdk.NewDecCoinFromCoin(sdk.Coin{strings.ToUpper(testDenom1), sdk.NewInt(5)}) }) - require.Panics(t, func() { + s.Require().Panics(func() { sdk.NewDecCoinFromCoin(sdk.Coin{testDenom1, sdk.NewInt(-5)}) }) } -func TestDecCoinIsPositive(t *testing.T) { +func (s *decCoinTestSuite) TestDecCoinIsPositive() { dc := sdk.NewInt64DecCoin(testDenom1, 5) - require.True(t, dc.IsPositive()) + s.Require().True(dc.IsPositive()) dc = sdk.NewInt64DecCoin(testDenom1, 0) - require.False(t, dc.IsPositive()) + s.Require().False(dc.IsPositive()) } -func TestAddDecCoin(t *testing.T) { +func (s *decCoinTestSuite) TestAddDecCoin() { decCoinA1 := sdk.NewDecCoinFromDec(testDenom1, sdk.NewDecWithPrec(11, 1)) decCoinA2 := sdk.NewDecCoinFromDec(testDenom1, sdk.NewDecWithPrec(22, 1)) decCoinB1 := sdk.NewDecCoinFromDec(testDenom2, sdk.NewDecWithPrec(11, 1)) // regular add res := decCoinA1.Add(decCoinA1) - require.Equal(t, decCoinA2, res, "sum of coins is incorrect") + s.Require().Equal(decCoinA2, res, "sum of coins is incorrect") // bad denom add - require.Panics(t, func() { + s.Require().Panics(func() { decCoinA1.Add(decCoinB1) }, "expected panic on sum of different denoms") } -func TestAddDecCoins(t *testing.T) { +func (s *decCoinTestSuite) TestAddDecCoins() { one := sdk.NewDec(1) zero := sdk.NewDec(0) two := sdk.NewDec(2) @@ -94,11 +106,11 @@ func TestAddDecCoins(t *testing.T) { for tcIndex, tc := range cases { res := tc.inputOne.Add(tc.inputTwo...) - require.Equal(t, tc.expected, res, "sum of coins is incorrect, tc #%d", tcIndex) + s.Require().Equal(tc.expected, res, "sum of coins is incorrect, tc #%d", tcIndex) } } -func TestFilteredZeroDecCoins(t *testing.T) { +func (s *decCoinTestSuite) TestFilteredZeroDecCoins() { cases := []struct { name string input sdk.DecCoins @@ -144,16 +156,13 @@ func TestFilteredZeroDecCoins(t *testing.T) { } for _, tt := range cases { - tt := tt - t.Run(tt.name, func(t *testing.T) { - undertest := sdk.NewDecCoins(tt.input...) - require.Equal(t, tt.expected, undertest.String(), "NewDecCoins must return expected results") - require.Equal(t, tt.original, tt.input.String(), "input must be unmodified and match original") - }) + undertest := sdk.NewDecCoins(tt.input...) + s.Require().Equal(tt.expected, undertest.String(), "NewDecCoins must return expected results") + s.Require().Equal(tt.original, tt.input.String(), "input must be unmodified and match original") } } -func TestIsValid(t *testing.T) { +func (s *decCoinTestSuite) TestIsValid() { tests := []struct { coin sdk.DecCoin expectPass bool @@ -184,14 +193,14 @@ func TestIsValid(t *testing.T) { for _, tc := range tests { tc := tc if tc.expectPass { - require.True(t, tc.coin.IsValid(), tc.msg) + s.Require().True(tc.coin.IsValid(), tc.msg) } else { - require.False(t, tc.coin.IsValid(), tc.msg) + s.Require().False(tc.coin.IsValid(), tc.msg) } } } -func TestSubDecCoin(t *testing.T) { +func (s *decCoinTestSuite) TestSubDecCoin() { tests := []struct { coin sdk.DecCoin expectPass bool @@ -220,14 +229,14 @@ func TestSubDecCoin(t *testing.T) { tc := tc if tc.expectPass { equal := tc.coin.Sub(decCoin) - require.Equal(t, equal, decCoin, tc.msg) + s.Require().Equal(equal, decCoin, tc.msg) } else { - require.Panics(t, func() { tc.coin.Sub(decCoin) }, tc.msg) + s.Require().Panics(func() { tc.coin.Sub(decCoin) }, tc.msg) } } } -func TestSubDecCoins(t *testing.T) { +func (s *decCoinTestSuite) TestSubDecCoins() { tests := []struct { coins sdk.DecCoins expectPass bool @@ -256,14 +265,14 @@ func TestSubDecCoins(t *testing.T) { tc := tc if tc.expectPass { equal := tc.coins.Sub(decCoins) - require.Equal(t, equal, decCoins, tc.msg) + s.Require().Equal(equal, decCoins, tc.msg) } else { - require.Panics(t, func() { tc.coins.Sub(decCoins) }, tc.msg) + s.Require().Panics(func() { tc.coins.Sub(decCoins) }, tc.msg) } } } -func TestSortDecCoins(t *testing.T) { +func (s *decCoinTestSuite) TestSortDecCoins() { good := sdk.DecCoins{ sdk.NewInt64DecCoin("gas", 1), sdk.NewInt64DecCoin("mineral", 1), @@ -292,7 +301,6 @@ func TestSortDecCoins(t *testing.T) { sdk.NewInt64DecCoin("gas", 1), sdk.NewInt64DecCoin("mineral", 1), } - cases := []struct { name string coins sdk.DecCoins @@ -307,13 +315,13 @@ func TestSortDecCoins(t *testing.T) { } for _, tc := range cases { - require.Equal(t, tc.before, tc.coins.IsValid(), "coin validity is incorrect before sorting; %s", tc.name) + s.Require().Equal(tc.before, tc.coins.IsValid(), "coin validity is incorrect before sorting; %s", tc.name) tc.coins.Sort() - require.Equal(t, tc.after, tc.coins.IsValid(), "coin validity is incorrect after sorting; %s", tc.name) + s.Require().Equal(tc.after, tc.coins.IsValid(), "coin validity is incorrect after sorting; %s", tc.name) } } -func TestDecCoinsValidate(t *testing.T) { +func (s *decCoinTestSuite) TestDecCoinsValidate() { testCases := []struct { input sdk.DecCoins expectedPass bool @@ -334,14 +342,14 @@ func TestDecCoinsValidate(t *testing.T) { for i, tc := range testCases { err := tc.input.Validate() if tc.expectedPass { - require.NoError(t, err, "unexpected result for test case #%d, input: %v", i, tc.input) + s.Require().NoError(err, "unexpected result for test case #%d, input: %v", i, tc.input) } else { - require.Error(t, err, "unexpected result for test case #%d, input: %v", i, tc.input) + s.Require().Error(err, "unexpected result for test case #%d, input: %v", i, tc.input) } } } -func TestParseDecCoins(t *testing.T) { +func (s *decCoinTestSuite) TestParseDecCoins() { testCases := []struct { input string expectedResult sdk.DecCoins @@ -382,15 +390,15 @@ func TestParseDecCoins(t *testing.T) { for i, tc := range testCases { res, err := sdk.ParseDecCoins(tc.input) if tc.expectedErr { - require.Error(t, err, "expected error for test case #%d, input: %v", i, tc.input) + s.Require().Error(err, "expected error for test case #%d, input: %v", i, tc.input) } else { - require.NoError(t, err, "unexpected error for test case #%d, input: %v", i, tc.input) - require.Equal(t, tc.expectedResult, res, "unexpected result for test case #%d, input: %v", i, tc.input) + s.Require().NoError(err, "unexpected error for test case #%d, input: %v", i, tc.input) + s.Require().Equal(tc.expectedResult, res, "unexpected result for test case #%d, input: %v", i, tc.input) } } } -func TestDecCoinsString(t *testing.T) { +func (s *decCoinTestSuite) TestDecCoinsString() { testCases := []struct { input sdk.DecCoins expected string @@ -407,11 +415,11 @@ func TestDecCoinsString(t *testing.T) { for i, tc := range testCases { out := tc.input.String() - require.Equal(t, tc.expected, out, "unexpected result for test case #%d, input: %v", i, tc.input) + s.Require().Equal(tc.expected, out, "unexpected result for test case #%d, input: %v", i, tc.input) } } -func TestDecCoinsIntersect(t *testing.T) { +func (s *decCoinTestSuite) TestDecCoinsIntersect() { testCases := []struct { input1 string input2 string @@ -432,17 +440,16 @@ func TestDecCoinsIntersect(t *testing.T) { for i, tc := range testCases { in1, err := sdk.ParseDecCoins(tc.input1) - require.NoError(t, err, "unexpected parse error in %v", i) + s.Require().NoError(err, "unexpected parse error in %v", i) in2, err := sdk.ParseDecCoins(tc.input2) - require.NoError(t, err, "unexpected parse error in %v", i) + s.Require().NoError(err, "unexpected parse error in %v", i) exr, err := sdk.ParseDecCoins(tc.expectedResult) - require.NoError(t, err, "unexpected parse error in %v", i) - - require.True(t, in1.Intersect(in2).IsEqual(exr), "in1.cap(in2) != exr in %v", i) + s.Require().NoError(err, "unexpected parse error in %v", i) + s.Require().True(in1.Intersect(in2).IsEqual(exr), "in1.cap(in2) != exr in %v", i) } } -func TestDecCoinsTruncateDecimal(t *testing.T) { +func (s *decCoinTestSuite) TestDecCoinsTruncateDecimal() { decCoinA := sdk.NewDecCoinFromDec("bar", sdk.MustNewDecFromStr("5.41")) decCoinB := sdk.NewDecCoinFromDec("foo", sdk.MustNewDecFromStr("6.00")) @@ -466,18 +473,18 @@ func TestDecCoinsTruncateDecimal(t *testing.T) { for i, tc := range testCases { truncatedCoins, changeCoins := tc.input.TruncateDecimal() - require.Equal( - t, tc.truncatedCoins, truncatedCoins, + s.Require().Equal( + tc.truncatedCoins, truncatedCoins, "unexpected truncated coins; tc #%d, input: %s", i, tc.input, ) - require.Equal( - t, tc.changeCoins, changeCoins, + s.Require().Equal( + tc.changeCoins, changeCoins, "unexpected change coins; tc #%d, input: %s", i, tc.input, ) } } -func TestDecCoinsQuoDecTruncate(t *testing.T) { +func (s *decCoinTestSuite) TestDecCoinsQuoDecTruncate() { x := sdk.MustNewDecFromStr("1.00") y := sdk.MustNewDecFromStr("10000000000000000000.00") @@ -495,15 +502,15 @@ func TestDecCoinsQuoDecTruncate(t *testing.T) { for i, tc := range testCases { tc := tc if tc.panics { - require.Panics(t, func() { tc.coins.QuoDecTruncate(tc.input) }) + s.Require().Panics(func() { tc.coins.QuoDecTruncate(tc.input) }) } else { res := tc.coins.QuoDecTruncate(tc.input) - require.Equal(t, tc.result, res, "unexpected result; tc #%d, coins: %s, input: %s", i, tc.coins, tc.input) + s.Require().Equal(tc.result, res, "unexpected result; tc #%d, coins: %s, input: %s", i, tc.coins, tc.input) } } } -func TestNewDecCoinsWithIsValid(t *testing.T) { +func (s *decCoinTestSuite) TestNewDecCoinsWithIsValid() { fake1 := append(sdk.NewDecCoins(sdk.NewDecCoin("mytoken", sdk.NewInt(10))), sdk.DecCoin{Denom: "10BTC", Amount: sdk.NewDec(10)}) fake2 := append(sdk.NewDecCoins(sdk.NewDecCoin("mytoken", sdk.NewInt(10))), sdk.DecCoin{Denom: "BTC", Amount: sdk.NewDec(-10)}) @@ -532,16 +539,16 @@ func TestNewDecCoinsWithIsValid(t *testing.T) { for _, tc := range tests { tc := tc if tc.expectPass { - require.True(t, tc.coin.IsValid(), tc.msg) + s.Require().True(tc.coin.IsValid(), tc.msg) } else { - require.False(t, tc.coin.IsValid(), tc.msg) + s.Require().False(tc.coin.IsValid(), tc.msg) } } } -func TestDecCoins_AddDecCoinWithIsValid(t *testing.T) { +func (s *decCoinTestSuite) TestDecCoins_AddDecCoinWithIsValid() { lengthTestDecCoins := sdk.NewDecCoins().Add(sdk.NewDecCoin("mytoken", sdk.NewInt(10))).Add(sdk.DecCoin{Denom: "BTC", Amount: sdk.NewDec(10)}) - require.Equal(t, 2, len(lengthTestDecCoins), "should be 2") + s.Require().Equal(2, len(lengthTestDecCoins), "should be 2") tests := []struct { coin sdk.DecCoins @@ -568,9 +575,9 @@ func TestDecCoins_AddDecCoinWithIsValid(t *testing.T) { for _, tc := range tests { tc := tc if tc.expectPass { - require.True(t, tc.coin.IsValid(), tc.msg) + s.Require().True(tc.coin.IsValid(), tc.msg) } else { - require.False(t, tc.coin.IsValid(), tc.msg) + s.Require().False(tc.coin.IsValid(), tc.msg) } } } diff --git a/types/events_test.go b/types/events_test.go index 6aa6269837..d712e24cd8 100644 --- a/types/events_test.go +++ b/types/events_test.go @@ -18,6 +18,10 @@ func TestEventsTestSuite(t *testing.T) { suite.Run(t, new(eventsTestSuite)) } +func (s *eventsTestSuite) SetupSuite() { + s.T().Parallel() +} + func (s *eventsTestSuite) TestAppendEvents() { e1 := sdk.NewEvent("transfer", sdk.NewAttribute("sender", "foo")) e2 := sdk.NewEvent("transfer", sdk.NewAttribute("sender", "bar")) diff --git a/types/handler_test.go b/types/handler_test.go index 670da2fccb..b71980fd06 100644 --- a/types/handler_test.go +++ b/types/handler_test.go @@ -4,19 +4,30 @@ import ( "testing" "github.com/golang/mock/gomock" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" "github.com/cosmos/cosmos-sdk/tests/mocks" sdk "github.com/cosmos/cosmos-sdk/types" ) -func TestChainAnteDecorators(t *testing.T) { - t.Parallel() +type handlerTestSuite struct { + suite.Suite +} + +func TestHandlerTestSuite(t *testing.T) { + suite.Run(t, new(handlerTestSuite)) +} + +func (s *handlerTestSuite) SetupSuite() { + s.T().Parallel() +} + +func (s *handlerTestSuite) TestChainAnteDecorators() { // test panic - require.Nil(t, sdk.ChainAnteDecorators([]sdk.AnteDecorator{}...)) + s.Require().Nil(sdk.ChainAnteDecorators([]sdk.AnteDecorator{}...)) ctx, tx := sdk.Context{}, sdk.Tx(nil) - mockCtrl := gomock.NewController(t) + mockCtrl := gomock.NewController(s.T()) mockAnteDecorator1 := mocks.NewMockAnteDecorator(mockCtrl) mockAnteDecorator1.EXPECT().AnteHandle(gomock.Eq(ctx), gomock.Eq(tx), true, gomock.Any()).Times(1) sdk.ChainAnteDecorators(mockAnteDecorator1)(ctx, tx, true) //nolint:errcheck diff --git a/types/int_internal_test.go b/types/int_internal_test.go new file mode 100644 index 0000000000..c6b039948e --- /dev/null +++ b/types/int_internal_test.go @@ -0,0 +1,137 @@ +package types + +import ( + "math/big" + "math/rand" + "testing" + + "github.com/stretchr/testify/suite" +) + +type internalIntTestSuite struct { + suite.Suite +} + +func TestInternalIntTestSuite(t *testing.T) { + suite.Run(t, new(internalIntTestSuite)) +} + +func (s *internalIntTestSuite) TestEncodingRandom() { + for i := 0; i < 1000; i++ { + n := rand.Int63() + ni := NewInt(n) + var ri Int + + str, err := ni.Marshal() + s.Require().Nil(err) + err = (&ri).Unmarshal(str) + s.Require().Nil(err) + + s.Require().Equal(ni, ri, "binary mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String()) + s.Require().True(ni.i != ri.i, "pointer addresses are equal; tc #%d", i) + + bz, err := ni.MarshalJSON() + s.Require().Nil(err) + err = (&ri).UnmarshalJSON(bz) + s.Require().Nil(err) + + s.Require().Equal(ni, ri, "json mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String()) + s.Require().True(ni.i != ri.i, "pointer addresses are equal; tc #%d", i) + } + + for i := 0; i < 1000; i++ { + n := rand.Uint64() + ni := NewUint(n) + var ri Uint + + str, err := ni.Marshal() + s.Require().Nil(err) + err = (&ri).Unmarshal(str) + s.Require().Nil(err) + + s.Require().Equal(ni, ri, "binary mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String()) + s.Require().True(ni.i != ri.i, "pointer addresses are equal; tc #%d", i) + + bz, err := ni.MarshalJSON() + s.Require().Nil(err) + err = (&ri).UnmarshalJSON(bz) + s.Require().Nil(err) + + s.Require().Equal(ni, ri, "json mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String()) + s.Require().True(ni.i != ri.i, "pointer addresses are equal; tc #%d", i) + } +} + +func (s *internalIntTestSuite) TestSerializationOverflow() { + bx, _ := new(big.Int).SetString("91888242871839275229946405745257275988696311157297823662689937894645226298583", 10) + x := Int{bx} + y := new(Int) + + bz, err := x.Marshal() + s.Require().NoError(err) + + // require deserialization to fail due to overflow + s.Require().Error(y.Unmarshal(bz)) + + // require JSON deserialization to fail due to overflow + bz, err = x.MarshalJSON() + s.Require().NoError(err) + + s.Require().Error(y.UnmarshalJSON(bz)) +} + +func (s *internalIntTestSuite) TestImmutabilityArithInt() { + size := 500 + + ops := []intOp{ + applyWithRand(Int.Add, (*big.Int).Add), + applyWithRand(Int.Sub, (*big.Int).Sub), + applyWithRand(Int.Mul, (*big.Int).Mul), + applyWithRand(Int.Quo, (*big.Int).Quo), + applyRawWithRand(Int.AddRaw, (*big.Int).Add), + applyRawWithRand(Int.SubRaw, (*big.Int).Sub), + applyRawWithRand(Int.MulRaw, (*big.Int).Mul), + applyRawWithRand(Int.QuoRaw, (*big.Int).Quo), + } + + for i := 0; i < 100; i++ { + uis := make([]Int, size) + bis := make([]*big.Int, size) + + n := rand.Int63() + ui := NewInt(n) + bi := new(big.Int).SetInt64(n) + + for j := 0; j < size; j++ { + op := ops[rand.Intn(len(ops))] + uis[j], bis[j] = op(ui, bi) + } + + for j := 0; j < size; j++ { + 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()) + 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()) + 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()) + } + } +} + +type ( + intOp func(Int, *big.Int) (Int, *big.Int) + bigIntFunc func(*big.Int, *big.Int, *big.Int) *big.Int +) + +func applyWithRand(intFn func(Int, Int) Int, bigIntFn bigIntFunc) intOp { + return func(integer Int, bigInteger *big.Int) (Int, *big.Int) { + r := rand.Int63() + br := new(big.Int).SetInt64(r) + return intFn(integer, NewInt(r)), bigIntFn(new(big.Int), bigInteger, br) + } +} + +func applyRawWithRand(intFn func(Int, int64) Int, bigIntFn bigIntFunc) intOp { + return func(integer Int, bigInteger *big.Int) (Int, *big.Int) { + r := rand.Int63() + br := new(big.Int).SetInt64(r) + return intFn(integer, r), bigIntFn(new(big.Int), bigInteger, br) + } +} diff --git a/types/int_test.go b/types/int_test.go index a4866a97b9..4b26ddb8ad 100644 --- a/types/int_test.go +++ b/types/int_test.go @@ -1,4 +1,4 @@ -package types +package types_test import ( "math/big" @@ -6,108 +6,118 @@ import ( "strconv" "testing" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + + sdk "github.com/cosmos/cosmos-sdk/types" ) -func TestFromInt64(t *testing.T) { - t.Parallel() +type intTestSuite struct { + suite.Suite +} + +func TestIntTestSuite(t *testing.T) { + suite.Run(t, new(intTestSuite)) +} + +func (s *intTestSuite) SetupSuite() { + s.T().Parallel() +} + +func (s *intTestSuite) TestFromInt64() { for n := 0; n < 20; n++ { r := rand.Int63() - require.Equal(t, r, NewInt(r).Int64()) + s.Require().Equal(r, sdk.NewInt(r).Int64()) } } -func TestFromUint64(t *testing.T) { - t.Parallel() +func (s *intTestSuite) TestFromUint64() { for n := 0; n < 20; n++ { r := rand.Uint64() - require.True(t, NewIntFromUint64(r).IsUint64()) - require.Equal(t, r, NewIntFromUint64(r).Uint64()) + s.Require().True(sdk.NewIntFromUint64(r).IsUint64()) + s.Require().Equal(r, sdk.NewIntFromUint64(r).Uint64()) } } -func TestIntPanic(t *testing.T) { - t.Parallel() +func (s *intTestSuite) TestIntPanic() { // Max Int = 2^255-1 = 5.789e+76 // Min Int = -(2^255-1) = -5.789e+76 - require.NotPanics(t, func() { NewIntWithDecimal(1, 76) }) - i1 := NewIntWithDecimal(1, 76) - require.NotPanics(t, func() { NewIntWithDecimal(2, 76) }) - i2 := NewIntWithDecimal(2, 76) - require.NotPanics(t, func() { NewIntWithDecimal(3, 76) }) - i3 := NewIntWithDecimal(3, 76) + s.Require().NotPanics(func() { sdk.NewIntWithDecimal(1, 76) }) + i1 := sdk.NewIntWithDecimal(1, 76) + s.Require().NotPanics(func() { sdk.NewIntWithDecimal(2, 76) }) + i2 := sdk.NewIntWithDecimal(2, 76) + s.Require().NotPanics(func() { sdk.NewIntWithDecimal(3, 76) }) + i3 := sdk.NewIntWithDecimal(3, 76) - require.Panics(t, func() { NewIntWithDecimal(6, 76) }) - require.Panics(t, func() { NewIntWithDecimal(9, 80) }) + s.Require().Panics(func() { sdk.NewIntWithDecimal(6, 76) }) + s.Require().Panics(func() { sdk.NewIntWithDecimal(9, 80) }) // Overflow check - require.NotPanics(t, func() { i1.Add(i1) }) - require.NotPanics(t, func() { i2.Add(i2) }) - require.Panics(t, func() { i3.Add(i3) }) + s.Require().NotPanics(func() { i1.Add(i1) }) + s.Require().NotPanics(func() { i2.Add(i2) }) + s.Require().Panics(func() { i3.Add(i3) }) - require.NotPanics(t, func() { i1.Sub(i1.Neg()) }) - require.NotPanics(t, func() { i2.Sub(i2.Neg()) }) - require.Panics(t, func() { i3.Sub(i3.Neg()) }) + s.Require().NotPanics(func() { i1.Sub(i1.Neg()) }) + s.Require().NotPanics(func() { i2.Sub(i2.Neg()) }) + s.Require().Panics(func() { i3.Sub(i3.Neg()) }) - require.Panics(t, func() { i1.Mul(i1) }) - require.Panics(t, func() { i2.Mul(i2) }) - require.Panics(t, func() { i3.Mul(i3) }) + s.Require().Panics(func() { i1.Mul(i1) }) + s.Require().Panics(func() { i2.Mul(i2) }) + s.Require().Panics(func() { i3.Mul(i3) }) - require.Panics(t, func() { i1.Neg().Mul(i1.Neg()) }) - require.Panics(t, func() { i2.Neg().Mul(i2.Neg()) }) - require.Panics(t, func() { i3.Neg().Mul(i3.Neg()) }) + s.Require().Panics(func() { i1.Neg().Mul(i1.Neg()) }) + s.Require().Panics(func() { i2.Neg().Mul(i2.Neg()) }) + s.Require().Panics(func() { i3.Neg().Mul(i3.Neg()) }) // Underflow check i3n := i3.Neg() - require.NotPanics(t, func() { i3n.Sub(i1) }) - require.NotPanics(t, func() { i3n.Sub(i2) }) - require.Panics(t, func() { i3n.Sub(i3) }) + s.Require().NotPanics(func() { i3n.Sub(i1) }) + s.Require().NotPanics(func() { i3n.Sub(i2) }) + s.Require().Panics(func() { i3n.Sub(i3) }) - require.NotPanics(t, func() { i3n.Add(i1.Neg()) }) - require.NotPanics(t, func() { i3n.Add(i2.Neg()) }) - require.Panics(t, func() { i3n.Add(i3.Neg()) }) + s.Require().NotPanics(func() { i3n.Add(i1.Neg()) }) + s.Require().NotPanics(func() { i3n.Add(i2.Neg()) }) + s.Require().Panics(func() { i3n.Add(i3.Neg()) }) - require.Panics(t, func() { i1.Mul(i1.Neg()) }) - require.Panics(t, func() { i2.Mul(i2.Neg()) }) - require.Panics(t, func() { i3.Mul(i3.Neg()) }) + s.Require().Panics(func() { i1.Mul(i1.Neg()) }) + s.Require().Panics(func() { i2.Mul(i2.Neg()) }) + s.Require().Panics(func() { i3.Mul(i3.Neg()) }) // Bound check - intmax := NewIntFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(255), nil), big.NewInt(1))) + intmax := sdk.NewIntFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(255), nil), big.NewInt(1))) intmin := intmax.Neg() - require.NotPanics(t, func() { intmax.Add(ZeroInt()) }) - require.NotPanics(t, func() { intmin.Sub(ZeroInt()) }) - require.Panics(t, func() { intmax.Add(OneInt()) }) - require.Panics(t, func() { intmin.Sub(OneInt()) }) + s.Require().NotPanics(func() { intmax.Add(sdk.ZeroInt()) }) + s.Require().NotPanics(func() { intmin.Sub(sdk.ZeroInt()) }) + s.Require().Panics(func() { intmax.Add(sdk.OneInt()) }) + s.Require().Panics(func() { intmin.Sub(sdk.OneInt()) }) // Division-by-zero check - require.Panics(t, func() { i1.Quo(NewInt(0)) }) + s.Require().Panics(func() { i1.Quo(sdk.NewInt(0)) }) - require.NotPanics(t, func() { Int{}.BigInt() }) + s.Require().NotPanics(func() { sdk.Int{}.BigInt() }) } // Tests below uses randomness // Since we are using *big.Int as underlying value // and (U/)Int is immutable value(see TestImmutability(U/)Int) // it is safe to use randomness in the tests -func TestIdentInt(t *testing.T) { - t.Parallel() +func (s *intTestSuite) TestIdentInt() { for d := 0; d < 1000; d++ { n := rand.Int63() - i := NewInt(n) + i := sdk.NewInt(n) - ifromstr, ok := NewIntFromString(strconv.FormatInt(n, 10)) - require.True(t, ok) + ifromstr, ok := sdk.NewIntFromString(strconv.FormatInt(n, 10)) + s.Require().True(ok) cases := []int64{ i.Int64(), i.BigInt().Int64(), ifromstr.Int64(), - NewIntFromBigInt(big.NewInt(n)).Int64(), - NewIntWithDecimal(n, 0).Int64(), + sdk.NewIntFromBigInt(big.NewInt(n)).Int64(), + sdk.NewIntWithDecimal(n, 0).Int64(), } for tcnum, tc := range cases { - require.Equal(t, n, tc, "Int is modified during conversion. tc #%d", tcnum) + s.Require().Equal(n, tc, "Int is modified during conversion. tc #%d", tcnum) } } } @@ -126,16 +136,15 @@ func maxint(i1, i2 int64) int64 { return i2 } -func TestArithInt(t *testing.T) { - t.Parallel() +func (s *intTestSuite) TestArithInt() { for d := 0; d < 1000; d++ { n1 := int64(rand.Int31()) - i1 := NewInt(n1) + i1 := sdk.NewInt(n1) n2 := int64(rand.Int31()) - i2 := NewInt(n2) + i2 := sdk.NewInt(n2) cases := []struct { - ires Int + ires sdk.Int nres int64 }{ {i1.Add(i2), n1 + n2}, @@ -146,25 +155,24 @@ func TestArithInt(t *testing.T) { {i1.SubRaw(n2), n1 - n2}, {i1.MulRaw(n2), n1 * n2}, {i1.QuoRaw(n2), n1 / n2}, - {MinInt(i1, i2), minint(n1, n2)}, - {MaxInt(i1, i2), maxint(n1, n2)}, + {sdk.MinInt(i1, i2), minint(n1, n2)}, + {sdk.MaxInt(i1, i2), maxint(n1, n2)}, {i1.Neg(), -n1}, } for tcnum, tc := range cases { - require.Equal(t, tc.nres, tc.ires.Int64(), "Int arithmetic operation does not match with int64 operation. tc #%d", tcnum) + s.Require().Equal(tc.nres, tc.ires.Int64(), "Int arithmetic operation does not match with int64 operation. tc #%d", tcnum) } } } -func TestCompInt(t *testing.T) { - t.Parallel() +func (s *intTestSuite) TestCompInt() { for d := 0; d < 1000; d++ { n1 := int64(rand.Int31()) - i1 := NewInt(n1) + i1 := sdk.NewInt(n1) n2 := int64(rand.Int31()) - i2 := NewInt(n2) + i2 := sdk.NewInt(n2) cases := []struct { ires bool @@ -177,213 +185,97 @@ func TestCompInt(t *testing.T) { } for tcnum, tc := range cases { - require.Equal(t, tc.nres, tc.ires, "Int comparison operation does not match with int64 operation. tc #%d", tcnum) + s.Require().Equal(tc.nres, tc.ires, "Int comparison operation does not match with int64 operation. tc #%d", tcnum) } } } -func minuint(i1, i2 uint64) uint64 { - if i1 < i2 { - return i1 - } - return i2 +func randint() sdk.Int { + return sdk.NewInt(rand.Int63()) } -func maxuint(i1, i2 uint64) uint64 { - if i1 > i2 { - return i1 - } - return i2 -} - -func randint() Int { - return NewInt(rand.Int63()) -} - -func TestImmutabilityAllInt(t *testing.T) { - t.Parallel() - ops := []func(*Int){ - func(i *Int) { _ = i.Add(randint()) }, - func(i *Int) { _ = i.Sub(randint()) }, - func(i *Int) { _ = i.Mul(randint()) }, - func(i *Int) { _ = i.Quo(randint()) }, - func(i *Int) { _ = i.AddRaw(rand.Int63()) }, - func(i *Int) { _ = i.SubRaw(rand.Int63()) }, - func(i *Int) { _ = i.MulRaw(rand.Int63()) }, - func(i *Int) { _ = i.QuoRaw(rand.Int63()) }, - func(i *Int) { _ = i.Neg() }, - func(i *Int) { _ = i.IsZero() }, - func(i *Int) { _ = i.Sign() }, - func(i *Int) { _ = i.Equal(randint()) }, - func(i *Int) { _ = i.GT(randint()) }, - func(i *Int) { _ = i.LT(randint()) }, - func(i *Int) { _ = i.String() }, +func (s *intTestSuite) TestImmutabilityAllInt() { + ops := []func(*sdk.Int){ + func(i *sdk.Int) { _ = i.Add(randint()) }, + func(i *sdk.Int) { _ = i.Sub(randint()) }, + func(i *sdk.Int) { _ = i.Mul(randint()) }, + func(i *sdk.Int) { _ = i.Quo(randint()) }, + func(i *sdk.Int) { _ = i.AddRaw(rand.Int63()) }, + func(i *sdk.Int) { _ = i.SubRaw(rand.Int63()) }, + func(i *sdk.Int) { _ = i.MulRaw(rand.Int63()) }, + func(i *sdk.Int) { _ = i.QuoRaw(rand.Int63()) }, + func(i *sdk.Int) { _ = i.Neg() }, + func(i *sdk.Int) { _ = i.IsZero() }, + func(i *sdk.Int) { _ = i.Sign() }, + func(i *sdk.Int) { _ = i.Equal(randint()) }, + func(i *sdk.Int) { _ = i.GT(randint()) }, + func(i *sdk.Int) { _ = i.LT(randint()) }, + func(i *sdk.Int) { _ = i.String() }, } for i := 0; i < 1000; i++ { n := rand.Int63() - ni := NewInt(n) + ni := sdk.NewInt(n) for opnum, op := range ops { op(&ni) - require.Equal(t, n, ni.Int64(), "Int is modified by operation. tc #%d", opnum) - require.Equal(t, NewInt(n), ni, "Int is modified by operation. tc #%d", opnum) + s.Require().Equal(n, ni.Int64(), "Int is modified by operation. tc #%d", opnum) + s.Require().Equal(sdk.NewInt(n), ni, "Int is modified by operation. tc #%d", opnum) } } } -type intop func(Int, *big.Int) (Int, *big.Int) - -func intarith(uifn func(Int, Int) Int, bifn func(*big.Int, *big.Int, *big.Int) *big.Int) intop { - return func(ui Int, bi *big.Int) (Int, *big.Int) { - r := rand.Int63() - br := new(big.Int).SetInt64(r) - return uifn(ui, NewInt(r)), bifn(new(big.Int), bi, br) - } -} - -func intarithraw(uifn func(Int, int64) Int, bifn func(*big.Int, *big.Int, *big.Int) *big.Int) intop { - return func(ui Int, bi *big.Int) (Int, *big.Int) { - r := rand.Int63() - br := new(big.Int).SetInt64(r) - return uifn(ui, r), bifn(new(big.Int), bi, br) - } -} - -func TestImmutabilityArithInt(t *testing.T) { - t.Parallel() - size := 500 - - ops := []intop{ - intarith(Int.Add, (*big.Int).Add), - intarith(Int.Sub, (*big.Int).Sub), - intarith(Int.Mul, (*big.Int).Mul), - intarith(Int.Quo, (*big.Int).Quo), - intarithraw(Int.AddRaw, (*big.Int).Add), - intarithraw(Int.SubRaw, (*big.Int).Sub), - intarithraw(Int.MulRaw, (*big.Int).Mul), - intarithraw(Int.QuoRaw, (*big.Int).Quo), - } - - for i := 0; i < 100; i++ { - uis := make([]Int, size) - bis := make([]*big.Int, size) - - n := rand.Int63() - ui := NewInt(n) - bi := new(big.Int).SetInt64(n) - - for j := 0; j < size; j++ { - op := ops[rand.Intn(len(ops))] - uis[j], bis[j] = op(ui, bi) - } - - for j := 0; j < size; j++ { - require.Equal(t, 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()) - require.Equal(t, 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()) - require.True(t, uis[j].i != bis[j], "Pointer addresses are equal. tc #%d, Int %s, *big.Int %s", j, uis[j].String(), bis[j].String()) - } - } -} - -func TestEncodingRandom(t *testing.T) { - for i := 0; i < 1000; i++ { - n := rand.Int63() - ni := NewInt(n) - var ri Int - - str, err := ni.Marshal() - require.Nil(t, err) - err = (&ri).Unmarshal(str) - require.Nil(t, err) - - require.Equal(t, ni, ri, "binary mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String()) - require.True(t, ni.i != ri.i, "pointer addresses are equal; tc #%d", i) - - bz, err := ni.MarshalJSON() - require.Nil(t, err) - err = (&ri).UnmarshalJSON(bz) - require.Nil(t, err) - - require.Equal(t, ni, ri, "json mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String()) - require.True(t, ni.i != ri.i, "pointer addresses are equal; tc #%d", i) - } - - for i := 0; i < 1000; i++ { - n := rand.Uint64() - ni := NewUint(n) - var ri Uint - - str, err := ni.Marshal() - require.Nil(t, err) - err = (&ri).Unmarshal(str) - require.Nil(t, err) - - require.Equal(t, ni, ri, "binary mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String()) - require.True(t, ni.i != ri.i, "pointer addresses are equal; tc #%d", i) - - bz, err := ni.MarshalJSON() - require.Nil(t, err) - err = (&ri).UnmarshalJSON(bz) - require.Nil(t, err) - - require.Equal(t, ni, ri, "json mismatch; tc #%d, expected %s, actual %s", i, ni.String(), ri.String()) - require.True(t, ni.i != ri.i, "pointer addresses are equal; tc #%d", i) - } -} - -func TestEncodingTableInt(t *testing.T) { - t.Parallel() - var i Int +func (s *intTestSuite) TestEncodingTableInt() { + var i sdk.Int cases := []struct { - i Int + i sdk.Int jsonBz []byte rawBz []byte }{ { - NewInt(0), + sdk.NewInt(0), []byte("\"0\""), []byte{0x30}, }, { - NewInt(100), + sdk.NewInt(100), []byte("\"100\""), []byte{0x31, 0x30, 0x30}, }, { - NewInt(-100), + sdk.NewInt(-100), []byte("\"-100\""), []byte{0x2d, 0x31, 0x30, 0x30}, }, { - NewInt(51842), + sdk.NewInt(51842), []byte("\"51842\""), []byte{0x35, 0x31, 0x38, 0x34, 0x32}, }, { - NewInt(-51842), + sdk.NewInt(-51842), []byte("\"-51842\""), []byte{0x2d, 0x35, 0x31, 0x38, 0x34, 0x32}, }, { - NewInt(19513368), + sdk.NewInt(19513368), []byte("\"19513368\""), []byte{0x31, 0x39, 0x35, 0x31, 0x33, 0x33, 0x36, 0x38}, }, { - NewInt(-19513368), + sdk.NewInt(-19513368), []byte("\"-19513368\""), []byte{0x2d, 0x31, 0x39, 0x35, 0x31, 0x33, 0x33, 0x36, 0x38}, }, { - NewInt(999999999999), + sdk.NewInt(999999999999), []byte("\"999999999999\""), []byte{0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39}, }, { - NewInt(-999999999999), + sdk.NewInt(-999999999999), []byte("\"-999999999999\""), []byte{0x2d, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39}, }, @@ -391,54 +283,53 @@ func TestEncodingTableInt(t *testing.T) { for tcnum, tc := range cases { bz, err := tc.i.MarshalJSON() - require.Nil(t, err, "Error marshaling Int. tc #%d, err %s", tcnum, err) - require.Equal(t, tc.jsonBz, bz, "Marshaled value is different from exported. tc #%d", tcnum) + s.Require().Nil(err, "Error marshaling Int. tc #%d, err %s", tcnum, err) + s.Require().Equal(tc.jsonBz, bz, "Marshaled value is different from exported. tc #%d", tcnum) err = (&i).UnmarshalJSON(bz) - require.Nil(t, err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err) - require.Equal(t, tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum) + s.Require().Nil(err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err) + s.Require().Equal(tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum) bz, err = tc.i.Marshal() - require.Nil(t, err, "Error marshaling Int. tc #%d, err %s", tcnum, err) - require.Equal(t, tc.rawBz, bz, "Marshaled value is different from exported. tc #%d", tcnum) + s.Require().Nil(err, "Error marshaling Int. tc #%d, err %s", tcnum, err) + s.Require().Equal(tc.rawBz, bz, "Marshaled value is different from exported. tc #%d", tcnum) err = (&i).Unmarshal(bz) - require.Nil(t, err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err) - require.Equal(t, tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum) + s.Require().Nil(err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err) + s.Require().Equal(tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum) } } -func TestEncodingTableUint(t *testing.T) { - t.Parallel() - var i Uint +func (s *intTestSuite) TestEncodingTableUint() { + var i sdk.Uint cases := []struct { - i Uint + i sdk.Uint jsonBz []byte rawBz []byte }{ { - NewUint(0), + sdk.NewUint(0), []byte("\"0\""), []byte{0x30}, }, { - NewUint(100), + sdk.NewUint(100), []byte("\"100\""), []byte{0x31, 0x30, 0x30}, }, { - NewUint(51842), + sdk.NewUint(51842), []byte("\"51842\""), []byte{0x35, 0x31, 0x38, 0x34, 0x32}, }, { - NewUint(19513368), + sdk.NewUint(19513368), []byte("\"19513368\""), []byte{0x31, 0x39, 0x35, 0x31, 0x33, 0x33, 0x36, 0x38}, }, { - NewUint(999999999999), + sdk.NewUint(999999999999), []byte("\"999999999999\""), []byte{0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39}, }, @@ -446,46 +337,24 @@ func TestEncodingTableUint(t *testing.T) { for tcnum, tc := range cases { bz, err := tc.i.MarshalJSON() - require.Nil(t, err, "Error marshaling Int. tc #%d, err %s", tcnum, err) - require.Equal(t, tc.jsonBz, bz, "Marshaled value is different from exported. tc #%d", tcnum) + s.Require().Nil(err, "Error marshaling Int. tc #%d, err %s", tcnum, err) + s.Require().Equal(tc.jsonBz, bz, "Marshaled value is different from exported. tc #%d", tcnum) err = (&i).UnmarshalJSON(bz) - require.Nil(t, err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err) - require.Equal(t, tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum) + s.Require().Nil(err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err) + s.Require().Equal(tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum) bz, err = tc.i.Marshal() - require.Nil(t, err, "Error marshaling Int. tc #%d, err %s", tcnum, err) - require.Equal(t, tc.rawBz, bz, "Marshaled value is different from exported. tc #%d", tcnum) + s.Require().Nil(err, "Error marshaling Int. tc #%d, err %s", tcnum, err) + s.Require().Equal(tc.rawBz, bz, "Marshaled value is different from exported. tc #%d", tcnum) err = (&i).Unmarshal(bz) - require.Nil(t, err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err) - require.Equal(t, tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum) + s.Require().Nil(err, "Error unmarshaling Int. tc #%d, err %s", tcnum, err) + s.Require().Equal(tc.i, i, "Unmarshaled value is different from exported. tc #%d", tcnum) } } -func TestSerializationOverflow(t *testing.T) { - t.Parallel() - bx, _ := new(big.Int).SetString("91888242871839275229946405745257275988696311157297823662689937894645226298583", 10) - x := Int{bx} - y := new(Int) - - bz, err := x.Marshal() - require.NoError(t, err) - - // require deserialization to fail due to overflow - err = y.Unmarshal(bz) - require.Error(t, err) - - // require JSON deserialization to fail due to overflow - bz, err = x.MarshalJSON() - require.NoError(t, err) - - err = y.UnmarshalJSON(bz) - require.Error(t, err) -} - -func TestIntMod(t *testing.T) { - t.Parallel() +func (s *intTestSuite) TestIntMod() { tests := []struct { name string x int64 @@ -500,20 +369,19 @@ func TestIntMod(t *testing.T) { } for _, tt := range tests { - tt := tt - t.Run(tt.name, func(t *testing.T) { - if tt.wantPanic { - require.Panics(t, func() { NewInt(tt.x).Mod(NewInt(tt.y)) }) - require.Panics(t, func() { NewInt(tt.x).ModRaw(tt.y) }) - return - } - require.True(t, NewInt(tt.x).Mod(NewInt(tt.y)).Equal(NewInt(tt.ret))) - require.True(t, NewInt(tt.x).ModRaw(tt.y).Equal(NewInt(tt.ret))) - }) + if tt.wantPanic { + s.Require().Panics(func() { sdk.NewInt(tt.x).Mod(sdk.NewInt(tt.y)) }) + s.Require().Panics(func() { sdk.NewInt(tt.x).ModRaw(tt.y) }) + return + } + s.Require().True(sdk.NewInt(tt.x).Mod(sdk.NewInt(tt.y)).Equal(sdk.NewInt(tt.ret))) + s.Require().True(sdk.NewInt(tt.x).ModRaw(tt.y).Equal(sdk.NewInt(tt.ret))) } } -func TestIntEq(t *testing.T) { - require.True(IntEq(t, ZeroInt(), ZeroInt())) - require.False(IntEq(t, OneInt(), ZeroInt())) +func (s *intTestSuite) TestIntEq() { + _, resp, _, _, _ := sdk.IntEq(s.T(), sdk.ZeroInt(), sdk.ZeroInt()) + s.Require().True(resp) + _, resp, _, _, _ = sdk.IntEq(s.T(), sdk.OneInt(), sdk.ZeroInt()) + s.Require().False(resp) } diff --git a/types/invariant_test.go b/types/invariant_test.go index 8d5f507bb3..dc6fbcf6a9 100644 --- a/types/invariant_test.go +++ b/types/invariant_test.go @@ -16,6 +16,10 @@ func TestInvariantTestSuite(t *testing.T) { suite.Run(t, new(invariantTestSuite)) } +func (s *invariantTestSuite) SetupSuite() { + s.T().Parallel() +} + func (s *invariantTestSuite) TestFormatInvariant() { s.Require().Equal(": invariant\n\n", sdk.FormatInvariant("", "", "")) s.Require().Equal("module: name invariant\nmsg\n", sdk.FormatInvariant("module", "name", "msg")) diff --git a/types/result_test.go b/types/result_test.go index 448d04dc89..44d6602375 100644 --- a/types/result_test.go +++ b/types/result_test.go @@ -15,15 +15,19 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -type resultTestStuite struct { +type resultTestSuite struct { suite.Suite } -func TestRTestStuite(t *testing.T) { - suite.Run(t, new(resultTestStuite)) +func TestResultTestSuite(t *testing.T) { + suite.Run(t, new(resultTestSuite)) } -func (s *resultTestStuite) TestParseABCILog() { +func (s *resultTestSuite) SetupSuite() { + s.T().Parallel() +} + +func (s *resultTestSuite) TestParseABCILog() { logs := `[{"log":"","msg_index":1,"success":true}]` res, err := sdk.ParseABCILogs(logs) @@ -33,7 +37,7 @@ func (s *resultTestStuite) TestParseABCILog() { s.Require().Equal(res[0].MsgIndex, uint32(1)) } -func (s *resultTestStuite) TestABCIMessageLog() { +func (s *resultTestSuite) TestABCIMessageLog() { cdc := codec.NewLegacyAmino() events := sdk.Events{sdk.NewEvent("transfer", sdk.NewAttribute("sender", "foo"))} msgLog := sdk.NewABCIMessageLog(0, "", events) @@ -44,7 +48,7 @@ func (s *resultTestStuite) TestABCIMessageLog() { s.Require().Equal(string(bz), msgLogs.String()) } -func (s *resultTestStuite) TestNewSearchTxsResult() { +func (s *resultTestSuite) TestNewSearchTxsResult() { got := sdk.NewSearchTxsResult(150, 20, 2, 20, []*sdk.TxResponse{}) s.Require().Equal(&sdk.SearchTxsResult{ TotalCount: 150, @@ -56,7 +60,7 @@ func (s *resultTestStuite) TestNewSearchTxsResult() { }, got) } -func (s *resultTestStuite) TestResponseResultTx() { +func (s *resultTestSuite) TestResponseResultTx() { deliverTxResult := abci.ResponseDeliverTx{ Codespace: "codespace", Code: 1, @@ -126,7 +130,7 @@ func (s *resultTestStuite) TestResponseResultTx() { s.Require().Equal((*sdk.TxResponse)(nil), sdk.NewResponseFormatBroadcastTx(nil)) } -func (s *resultTestStuite) TestResponseFormatBroadcastTxCommit() { +func (s *resultTestSuite) TestResponseFormatBroadcastTxCommit() { // test nil s.Require().Equal((*sdk.TxResponse)(nil), sdk.NewResponseFormatBroadcastTxCommit(nil)) diff --git a/types/router_test.go b/types/router_test.go index 79d340e8da..7387e9b945 100644 --- a/types/router_test.go +++ b/types/router_test.go @@ -16,6 +16,10 @@ func TestRouteTestSuite(t *testing.T) { suite.Run(t, new(routeTestSuite)) } +func (s *routeTestSuite) SetupSuite() { + s.T().Parallel() +} + func (s *routeTestSuite) TestNilRoute() { tests := []struct { name string diff --git a/types/staking_test.go b/types/staking_test.go index 6033637891..4ad06cc18b 100644 --- a/types/staking_test.go +++ b/types/staking_test.go @@ -16,6 +16,10 @@ func TestStakingTestSuite(t *testing.T) { suite.Run(t, new(stakingTestSuite)) } +func (s *stakingTestSuite) SetupSuite() { + s.T().Parallel() +} + func (s *stakingTestSuite) TestBondStatus() { s.Require().False(sdk.Unbonded.Equal(sdk.Bonded)) s.Require().False(sdk.Unbonded.Equal(sdk.Unbonding)) diff --git a/types/store_test.go b/types/store_test.go index 84e737570b..02f71bbf41 100644 --- a/types/store_test.go +++ b/types/store_test.go @@ -19,6 +19,10 @@ func TestStoreTestSuite(t *testing.T) { suite.Run(t, new(storeTestSuite)) } +func (s *storeTestSuite) SetupSuite() { + s.T().Parallel() +} + func (s *storeTestSuite) TestPrefixEndBytes() { var testCases = []struct { prefix []byte diff --git a/types/tx_msg_test.go b/types/tx_msg_test.go index 09b7880753..7a55c7d63e 100644 --- a/types/tx_msg_test.go +++ b/types/tx_msg_test.go @@ -9,23 +9,23 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -type msgTestSuite struct { +type testMsgSuite struct { suite.Suite } func TestMsgTestSuite(t *testing.T) { - suite.Run(t, new(msgTestSuite)) + suite.Run(t, new(testMsgSuite)) } -func (s *msgTestSuite) TestTestMsg() { - addr := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} +func (s *testMsgSuite) TestMsg() { + addr := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19} accAddr := sdk.AccAddress(addr) msg := testdata.NewTestMsg(accAddr) s.Require().NotNil(msg) + s.Require().Equal([]sdk.AccAddress{accAddr}, msg.GetSigners()) s.Require().Equal("TestMsg", msg.Route()) s.Require().Equal("Test message", msg.Type()) s.Require().Nil(msg.ValidateBasic()) s.Require().NotPanics(func() { msg.GetSignBytes() }) - s.Require().Equal([]sdk.AccAddress{accAddr}, msg.GetSigners()) } diff --git a/types/uint_internal_test.go b/types/uint_internal_test.go new file mode 100644 index 0000000000..a39869cac7 --- /dev/null +++ b/types/uint_internal_test.go @@ -0,0 +1,55 @@ +package types + +import ( + "math/big" + "math/rand" + "strconv" + "testing" + + "github.com/stretchr/testify/suite" +) + +type uintInternalTestSuite struct { + suite.Suite +} + +func TestUintInternalTestSuite(t *testing.T) { + suite.Run(t, new(uintInternalTestSuite)) +} + +func (s *uintInternalTestSuite) SetupSuite() { + s.T().Parallel() +} + +func (s *uintInternalTestSuite) TestIdentUint() { + for d := 0; d < 1000; d++ { + n := rand.Uint64() + i := NewUint(n) + + ifromstr := NewUintFromString(strconv.FormatUint(n, 10)) + + cases := []uint64{ + i.Uint64(), + i.BigInt().Uint64(), + i.i.Uint64(), + ifromstr.Uint64(), + NewUintFromBigInt(new(big.Int).SetUint64(n)).Uint64(), + } + + for tcnum, tc := range cases { + s.Require().Equal(n, tc, "Uint is modified during conversion. tc #%d", tcnum) + } + } +} + +func (s *uintInternalTestSuite) TestUintSize() { + x := Uint{i: nil} + s.Require().Equal(1, x.Size()) + x = NewUint(0) + s.Require().Equal(1, x.Size()) + x = NewUint(10) + s.Require().Equal(2, x.Size()) + x = NewUint(100) + s.Require().Equal(3, x.Size()) + +} diff --git a/types/uint_test.go b/types/uint_test.go index 5f496b5c83..36705c890a 100644 --- a/types/uint_test.go +++ b/types/uint_test.go @@ -1,112 +1,104 @@ -package types +package types_test import ( "math" "math/big" "math/rand" - "strconv" "testing" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + + sdk "github.com/cosmos/cosmos-sdk/types" ) -func TestUintPanics(t *testing.T) { +type uintTestSuite struct { + suite.Suite +} + +func TestUnitTestSuite(t *testing.T) { + suite.Run(t, new(uintTestSuite)) +} + +func (s *uintTestSuite) SetupSuite() { + s.T().Parallel() +} + +func (s *uintTestSuite) TestUintPanics() { // Max Uint = 1.15e+77 // Min Uint = 0 - u1 := NewUint(0) - u2 := OneUint() + u1 := sdk.NewUint(0) + u2 := sdk.OneUint() - require.Equal(t, uint64(0), u1.Uint64()) - require.Equal(t, uint64(1), u2.Uint64()) + s.Require().Equal(uint64(0), u1.Uint64()) + s.Require().Equal(uint64(1), u2.Uint64()) - require.Panics(t, func() { NewUintFromBigInt(big.NewInt(-5)) }) - require.Panics(t, func() { NewUintFromString("-1") }) - require.NotPanics(t, func() { - require.True(t, NewUintFromString("0").Equal(ZeroUint())) - require.True(t, NewUintFromString("5").Equal(NewUint(5))) + s.Require().Panics(func() { sdk.NewUintFromBigInt(big.NewInt(-5)) }) + s.Require().Panics(func() { sdk.NewUintFromString("-1") }) + s.Require().NotPanics(func() { + s.Require().True(sdk.NewUintFromString("0").Equal(sdk.ZeroUint())) + s.Require().True(sdk.NewUintFromString("5").Equal(sdk.NewUint(5))) }) // Overflow check - require.True(t, u1.Add(u1).Equal(ZeroUint())) - require.True(t, u1.Add(OneUint()).Equal(OneUint())) - require.Equal(t, uint64(0), u1.Uint64()) - require.Equal(t, uint64(1), OneUint().Uint64()) - require.Panics(t, func() { u1.SubUint64(2) }) - require.True(t, u1.SubUint64(0).Equal(ZeroUint())) - require.True(t, u2.Add(OneUint()).Sub(OneUint()).Equal(OneUint())) // i2 == 1 - require.True(t, u2.Add(OneUint()).Mul(NewUint(5)).Equal(NewUint(10))) // i2 == 10 - require.True(t, NewUint(7).Quo(NewUint(2)).Equal(NewUint(3))) - require.True(t, NewUint(0).Quo(NewUint(2)).Equal(ZeroUint())) - require.True(t, NewUint(5).MulUint64(4).Equal(NewUint(20))) - require.True(t, NewUint(5).MulUint64(0).Equal(ZeroUint())) + s.Require().True(u1.Add(u1).Equal(sdk.ZeroUint())) + s.Require().True(u1.Add(sdk.OneUint()).Equal(sdk.OneUint())) + s.Require().Equal(uint64(0), u1.Uint64()) + s.Require().Equal(uint64(1), sdk.OneUint().Uint64()) + s.Require().Panics(func() { u1.SubUint64(2) }) + s.Require().True(u1.SubUint64(0).Equal(sdk.ZeroUint())) + s.Require().True(u2.Add(sdk.OneUint()).Sub(sdk.OneUint()).Equal(sdk.OneUint())) // i2 == 1 + s.Require().True(u2.Add(sdk.OneUint()).Mul(sdk.NewUint(5)).Equal(sdk.NewUint(10))) // i2 == 10 + s.Require().True(sdk.NewUint(7).Quo(sdk.NewUint(2)).Equal(sdk.NewUint(3))) + s.Require().True(sdk.NewUint(0).Quo(sdk.NewUint(2)).Equal(sdk.ZeroUint())) + s.Require().True(sdk.NewUint(5).MulUint64(4).Equal(sdk.NewUint(20))) + s.Require().True(sdk.NewUint(5).MulUint64(0).Equal(sdk.ZeroUint())) - uintmax := NewUintFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil), big.NewInt(1))) - uintmin := ZeroUint() + uintmax := sdk.NewUintFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil), big.NewInt(1))) + uintmin := sdk.ZeroUint() // divs by zero - require.Panics(t, func() { OneUint().Mul(ZeroUint().SubUint64(uint64(1))) }) - require.Panics(t, func() { OneUint().QuoUint64(0) }) - require.Panics(t, func() { OneUint().Quo(ZeroUint()) }) - require.Panics(t, func() { ZeroUint().QuoUint64(0) }) - require.Panics(t, func() { OneUint().Quo(ZeroUint().Sub(OneUint())) }) - require.Panics(t, func() { uintmax.Add(OneUint()) }) - require.Panics(t, func() { uintmax.Incr() }) - require.Panics(t, func() { uintmin.Sub(OneUint()) }) - require.Panics(t, func() { uintmin.Decr() }) + s.Require().Panics(func() { sdk.OneUint().Mul(sdk.ZeroUint().SubUint64(uint64(1))) }) + s.Require().Panics(func() { sdk.OneUint().QuoUint64(0) }) + s.Require().Panics(func() { sdk.OneUint().Quo(sdk.ZeroUint()) }) + s.Require().Panics(func() { sdk.ZeroUint().QuoUint64(0) }) + s.Require().Panics(func() { sdk.OneUint().Quo(sdk.ZeroUint().Sub(sdk.OneUint())) }) + s.Require().Panics(func() { uintmax.Add(sdk.OneUint()) }) + s.Require().Panics(func() { uintmax.Incr() }) + s.Require().Panics(func() { uintmin.Sub(sdk.OneUint()) }) + s.Require().Panics(func() { uintmin.Decr() }) - require.Equal(t, uint64(0), MinUint(ZeroUint(), OneUint()).Uint64()) - require.Equal(t, uint64(1), MaxUint(ZeroUint(), OneUint()).Uint64()) + s.Require().Equal(uint64(0), sdk.MinUint(sdk.ZeroUint(), sdk.OneUint()).Uint64()) + s.Require().Equal(uint64(1), sdk.MaxUint(sdk.ZeroUint(), sdk.OneUint()).Uint64()) // comparison ops - require.True(t, - OneUint().GT(ZeroUint()), + s.Require().True( + sdk.OneUint().GT(sdk.ZeroUint()), ) - require.False(t, - OneUint().LT(ZeroUint()), + s.Require().False( + sdk.OneUint().LT(sdk.ZeroUint()), ) - require.True(t, - OneUint().GTE(ZeroUint()), + s.Require().True( + sdk.OneUint().GTE(sdk.ZeroUint()), ) - require.False(t, - OneUint().LTE(ZeroUint()), + s.Require().False( + sdk.OneUint().LTE(sdk.ZeroUint()), ) - require.False(t, ZeroUint().GT(OneUint())) - require.True(t, ZeroUint().LT(OneUint())) - require.False(t, ZeroUint().GTE(OneUint())) - require.True(t, ZeroUint().LTE(OneUint())) + s.Require().False(sdk.ZeroUint().GT(sdk.OneUint())) + s.Require().True(sdk.ZeroUint().LT(sdk.OneUint())) + s.Require().False(sdk.ZeroUint().GTE(sdk.OneUint())) + s.Require().True(sdk.ZeroUint().LTE(sdk.OneUint())) } -func TestIdentUint(t *testing.T) { - for d := 0; d < 1000; d++ { - n := rand.Uint64() - i := NewUint(n) - - ifromstr := NewUintFromString(strconv.FormatUint(n, 10)) - - cases := []uint64{ - i.Uint64(), - i.BigInt().Uint64(), - i.i.Uint64(), - ifromstr.Uint64(), - NewUintFromBigInt(new(big.Int).SetUint64(n)).Uint64(), - } - - for tcnum, tc := range cases { - require.Equal(t, n, tc, "Uint is modified during conversion. tc #%d", tcnum) - } - } -} - -func TestArithUint(t *testing.T) { +func (s *uintTestSuite) TestArithUint() { for d := 0; d < 1000; d++ { n1 := uint64(rand.Uint32()) - u1 := NewUint(n1) + u1 := sdk.NewUint(n1) n2 := uint64(rand.Uint32()) - u2 := NewUint(n2) + u2 := sdk.NewUint(n2) cases := []struct { - ures Uint + ures sdk.Uint nres uint64 }{ {u1.Add(u2), n1 + n2}, @@ -115,22 +107,22 @@ func TestArithUint(t *testing.T) { {u1.AddUint64(n2), n1 + n2}, {u1.MulUint64(n2), n1 * n2}, {u1.QuoUint64(n2), n1 / n2}, - {MinUint(u1, u2), minuint(n1, n2)}, - {MaxUint(u1, u2), maxuint(n1, n2)}, + {sdk.MinUint(u1, u2), minuint(n1, n2)}, + {sdk.MaxUint(u1, u2), maxuint(n1, n2)}, {u1.Incr(), n1 + 1}, } for tcnum, tc := range cases { - require.Equal(t, tc.nres, tc.ures.Uint64(), "Uint arithmetic operation does not match with uint64 operation. tc #%d", tcnum) + s.Require().Equal(tc.nres, tc.ures.Uint64(), "Uint arithmetic operation does not match with uint64 operation. tc #%d", tcnum) } if n2 > n1 { n1, n2 = n2, n1 - u1, u2 = NewUint(n1), NewUint(n2) + u1, u2 = sdk.NewUint(n1), sdk.NewUint(n2) } subs := []struct { - ures Uint + ures sdk.Uint nres uint64 }{ {u1.Sub(u2), n1 - n2}, @@ -139,17 +131,17 @@ func TestArithUint(t *testing.T) { } for tcnum, tc := range subs { - require.Equal(t, tc.nres, tc.ures.Uint64(), "Uint subtraction does not match with uint64 operation. tc #%d", tcnum) + s.Require().Equal(tc.nres, tc.ures.Uint64(), "Uint subtraction does not match with uint64 operation. tc #%d", tcnum) } } } -func TestCompUint(t *testing.T) { +func (s *uintTestSuite) TestCompUint() { for d := 0; d < 10000; d++ { n1 := rand.Uint64() - i1 := NewUint(n1) + i1 := sdk.NewUint(n1) n2 := rand.Uint64() - i2 := NewUint(n2) + i2 := sdk.NewUint(n2) cases := []struct { ires bool @@ -165,30 +157,30 @@ func TestCompUint(t *testing.T) { } for tcnum, tc := range cases { - require.Equal(t, tc.nres, tc.ires, "Uint comparison operation does not match with uint64 operation. tc #%d", tcnum) + s.Require().Equal(tc.nres, tc.ires, "Uint comparison operation does not match with uint64 operation. tc #%d", tcnum) } } } -func TestImmutabilityAllUint(t *testing.T) { - ops := []func(*Uint){ - func(i *Uint) { _ = i.Add(NewUint(rand.Uint64())) }, - func(i *Uint) { _ = i.Sub(NewUint(rand.Uint64() % i.Uint64())) }, - func(i *Uint) { _ = i.Mul(randuint()) }, - func(i *Uint) { _ = i.Quo(randuint()) }, - func(i *Uint) { _ = i.AddUint64(rand.Uint64()) }, - func(i *Uint) { _ = i.SubUint64(rand.Uint64() % i.Uint64()) }, - func(i *Uint) { _ = i.MulUint64(rand.Uint64()) }, - func(i *Uint) { _ = i.QuoUint64(rand.Uint64()) }, - func(i *Uint) { _ = i.IsZero() }, - func(i *Uint) { _ = i.Equal(randuint()) }, - func(i *Uint) { _ = i.GT(randuint()) }, - func(i *Uint) { _ = i.GTE(randuint()) }, - func(i *Uint) { _ = i.LT(randuint()) }, - func(i *Uint) { _ = i.LTE(randuint()) }, - func(i *Uint) { _ = i.String() }, - func(i *Uint) { _ = i.Incr() }, - func(i *Uint) { +func (s *uintTestSuite) TestImmutabilityAllUint() { + ops := []func(*sdk.Uint){ + func(i *sdk.Uint) { _ = i.Add(sdk.NewUint(rand.Uint64())) }, + func(i *sdk.Uint) { _ = i.Sub(sdk.NewUint(rand.Uint64() % i.Uint64())) }, + func(i *sdk.Uint) { _ = i.Mul(randuint()) }, + func(i *sdk.Uint) { _ = i.Quo(randuint()) }, + func(i *sdk.Uint) { _ = i.AddUint64(rand.Uint64()) }, + func(i *sdk.Uint) { _ = i.SubUint64(rand.Uint64() % i.Uint64()) }, + func(i *sdk.Uint) { _ = i.MulUint64(rand.Uint64()) }, + func(i *sdk.Uint) { _ = i.QuoUint64(rand.Uint64()) }, + func(i *sdk.Uint) { _ = i.IsZero() }, + func(i *sdk.Uint) { _ = i.Equal(randuint()) }, + func(i *sdk.Uint) { _ = i.GT(randuint()) }, + func(i *sdk.Uint) { _ = i.GTE(randuint()) }, + func(i *sdk.Uint) { _ = i.LT(randuint()) }, + func(i *sdk.Uint) { _ = i.LTE(randuint()) }, + func(i *sdk.Uint) { _ = i.String() }, + func(i *sdk.Uint) { _ = i.Incr() }, + func(i *sdk.Uint) { if i.IsZero() { return } @@ -199,103 +191,102 @@ func TestImmutabilityAllUint(t *testing.T) { for i := 0; i < 1000; i++ { n := rand.Uint64() - ni := NewUint(n) + ni := sdk.NewUint(n) for opnum, op := range ops { op(&ni) - require.Equal(t, n, ni.Uint64(), "Uint is modified by operation. #%d", opnum) - require.Equal(t, NewUint(n), ni, "Uint is modified by operation. #%d", opnum) + s.Require().Equal(n, ni.Uint64(), "Uint is modified by operation. #%d", opnum) + s.Require().Equal(sdk.NewUint(n), ni, "Uint is modified by operation. #%d", opnum) } } } -func TestSafeSub(t *testing.T) { +func (s *uintTestSuite) TestSafeSub() { testCases := []struct { - x, y Uint + x, y sdk.Uint expected uint64 panic bool }{ - {NewUint(0), NewUint(0), 0, false}, - {NewUint(10), NewUint(5), 5, false}, - {NewUint(5), NewUint(10), 5, true}, - {NewUint(math.MaxUint64), NewUint(0), math.MaxUint64, false}, + {sdk.NewUint(0), sdk.NewUint(0), 0, false}, + {sdk.NewUint(10), sdk.NewUint(5), 5, false}, + {sdk.NewUint(5), sdk.NewUint(10), 5, true}, + {sdk.NewUint(math.MaxUint64), sdk.NewUint(0), math.MaxUint64, false}, } for i, tc := range testCases { tc := tc if tc.panic { - require.Panics(t, func() { tc.x.Sub(tc.y) }) + s.Require().Panics(func() { tc.x.Sub(tc.y) }) continue } - require.Equal( - t, tc.expected, tc.x.Sub(tc.y).Uint64(), + s.Require().Equal( + tc.expected, tc.x.Sub(tc.y).Uint64(), "invalid subtraction result; x: %s, y: %s, tc: #%d", tc.x, tc.y, i, ) } } -func TestParseUint(t *testing.T) { +func (s *uintTestSuite) TestParseUint() { type args struct { s string } tests := []struct { name string args args - want Uint + want sdk.Uint wantErr bool }{ - {"malformed", args{"malformed"}, Uint{}, true}, - {"empty", args{""}, Uint{}, true}, - {"positive", args{"50"}, NewUint(uint64(50)), false}, - {"negative", args{"-1"}, Uint{}, true}, - {"zero", args{"0"}, ZeroUint(), false}, + {"malformed", args{"malformed"}, sdk.Uint{}, true}, + {"empty", args{""}, sdk.Uint{}, true}, + {"positive", args{"50"}, sdk.NewUint(uint64(50)), false}, + {"negative", args{"-1"}, sdk.Uint{}, true}, + {"zero", args{"0"}, sdk.ZeroUint(), false}, } for _, tt := range tests { - tt := tt - t.Run(tt.name, func(t *testing.T) { - got, err := ParseUint(tt.args.s) - if tt.wantErr { - require.Error(t, err) - return - } - require.NoError(t, err) - require.True(t, got.Equal(tt.want)) - }) + got, err := sdk.ParseUint(tt.args.s) + if tt.wantErr { + s.Require().Error(err) + continue + } + s.Require().NoError(err) + s.Require().True(got.Equal(tt.want)) } } -func randuint() Uint { - return NewUint(rand.Uint64()) +func randuint() sdk.Uint { + return sdk.NewUint(rand.Uint64()) } -func TestRelativePow(t *testing.T) { +func (s *uintTestSuite) TestRelativePow() { tests := []struct { - args []Uint - want Uint + args []sdk.Uint + want sdk.Uint }{ - {[]Uint{ZeroUint(), ZeroUint(), OneUint()}, OneUint()}, - {[]Uint{ZeroUint(), ZeroUint(), NewUint(10)}, NewUint(10)}, - {[]Uint{ZeroUint(), OneUint(), NewUint(10)}, ZeroUint()}, - {[]Uint{NewUint(10), NewUint(2), OneUint()}, NewUint(100)}, - {[]Uint{NewUint(210), NewUint(2), NewUint(100)}, NewUint(441)}, - {[]Uint{NewUint(2100), NewUint(2), NewUint(1000)}, NewUint(4410)}, - {[]Uint{NewUint(1000000001547125958), NewUint(600), NewUint(1000000000000000000)}, NewUint(1000000928276004850)}, + {[]sdk.Uint{sdk.ZeroUint(), sdk.ZeroUint(), sdk.OneUint()}, sdk.OneUint()}, + {[]sdk.Uint{sdk.ZeroUint(), sdk.ZeroUint(), sdk.NewUint(10)}, sdk.NewUint(10)}, + {[]sdk.Uint{sdk.ZeroUint(), sdk.OneUint(), sdk.NewUint(10)}, sdk.ZeroUint()}, + {[]sdk.Uint{sdk.NewUint(10), sdk.NewUint(2), sdk.OneUint()}, sdk.NewUint(100)}, + {[]sdk.Uint{sdk.NewUint(210), sdk.NewUint(2), sdk.NewUint(100)}, sdk.NewUint(441)}, + {[]sdk.Uint{sdk.NewUint(2100), sdk.NewUint(2), sdk.NewUint(1000)}, sdk.NewUint(4410)}, + {[]sdk.Uint{sdk.NewUint(1000000001547125958), sdk.NewUint(600), sdk.NewUint(1000000000000000000)}, sdk.NewUint(1000000928276004850)}, } for i, tc := range tests { - res := RelativePow(tc.args[0], tc.args[1], tc.args[2]) - require.Equal(t, tc.want, res, "unexpected result for test case %d, input: %v, got: %v", i, tc.args, res) + res := sdk.RelativePow(tc.args[0], tc.args[1], tc.args[2]) + s.Require().Equal(tc.want, res, "unexpected result for test case %d, input: %v, got: %v", i, tc.args, res) } } -func TestUintSize(t *testing.T) { - x := Uint{i: nil} - require.Equal(t, 1, x.Size()) - x = NewUint(0) - require.Equal(t, 1, x.Size()) - x = NewUint(10) - require.Equal(t, 2, x.Size()) - x = NewUint(100) - require.Equal(t, 3, x.Size()) - +func minuint(i1, i2 uint64) uint64 { + if i1 < i2 { + return i1 + } + return i2 +} + +func maxuint(i1, i2 uint64) uint64 { + if i1 > i2 { + return i1 + } + return i2 } diff --git a/types/utils_test.go b/types/utils_test.go index 1c451f185b..c04c9c81f3 100644 --- a/types/utils_test.go +++ b/types/utils_test.go @@ -5,12 +5,24 @@ import ( "testing" "time" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" sdk "github.com/cosmos/cosmos-sdk/types" ) -func TestSortJSON(t *testing.T) { +type utilsTestSuite struct { + suite.Suite +} + +func TestUtilsTestSuite(t *testing.T) { + suite.Run(t, new(utilsTestSuite)) +} + +func (s *utilsTestSuite) SetupSuite() { + s.T().Parallel() +} + +func (s *utilsTestSuite) TestSortJSON() { cases := []struct { unsortedJSON string want string @@ -37,19 +49,19 @@ func TestSortJSON(t *testing.T) { tc := tc got, err := sdk.SortJSON([]byte(tc.unsortedJSON)) if tc.wantErr { - require.NotNil(t, err, "tc #%d", tcIndex) - require.Panics(t, func() { sdk.MustSortJSON([]byte(tc.unsortedJSON)) }) + s.Require().NotNil(err, "tc #%d", tcIndex) + s.Require().Panics(func() { sdk.MustSortJSON([]byte(tc.unsortedJSON)) }) } else { - require.Nil(t, err, "tc #%d, err=%s", tcIndex, err) - require.NotPanics(t, func() { sdk.MustSortJSON([]byte(tc.unsortedJSON)) }) - require.Equal(t, got, sdk.MustSortJSON([]byte(tc.unsortedJSON))) + s.Require().Nil(err, "tc #%d, err=%s", tcIndex, err) + s.Require().NotPanics(func() { sdk.MustSortJSON([]byte(tc.unsortedJSON)) }) + s.Require().Equal(got, sdk.MustSortJSON([]byte(tc.unsortedJSON))) } - require.Equal(t, string(got), tc.want) + s.Require().Equal(string(got), tc.want) } } -func TestTimeFormatAndParse(t *testing.T) { +func (s *utilsTestSuite) TestTimeFormatAndParse() { cases := []struct { RFC3339NanoStr string SDKSortableTimeStr string @@ -61,43 +73,39 @@ func TestTimeFormatAndParse(t *testing.T) { for _, tc := range cases { tc := tc timeFromRFC, err := time.Parse(time.RFC3339Nano, tc.RFC3339NanoStr) - require.Nil(t, err) + s.Require().Nil(err) timeFromSDKFormat, err := time.Parse(sdk.SortableTimeFormat, tc.SDKSortableTimeStr) - require.Nil(t, err) + s.Require().Nil(err) - require.True(t, timeFromRFC.Equal(timeFromSDKFormat)) - require.Equal(t, timeFromRFC.Format(sdk.SortableTimeFormat), tc.SDKSortableTimeStr) + s.Require().True(timeFromRFC.Equal(timeFromSDKFormat)) + s.Require().Equal(timeFromRFC.Format(sdk.SortableTimeFormat), tc.SDKSortableTimeStr) } } -func TestCopyBytes(t *testing.T) { - t.Parallel() - require.Nil(t, sdk.CopyBytes(nil)) - require.Equal(t, 0, len(sdk.CopyBytes([]byte{}))) +func (s *utilsTestSuite) TestCopyBytes() { + s.Require().Nil(sdk.CopyBytes(nil)) + s.Require().Equal(0, len(sdk.CopyBytes([]byte{}))) bs := []byte("test") bsCopy := sdk.CopyBytes(bs) - require.True(t, bytes.Equal(bs, bsCopy)) + s.Require().True(bytes.Equal(bs, bsCopy)) } -func TestUint64ToBigEndian(t *testing.T) { - t.Parallel() - require.Equal(t, []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, sdk.Uint64ToBigEndian(uint64(0))) - require.Equal(t, []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa}, sdk.Uint64ToBigEndian(uint64(10))) +func (s *utilsTestSuite) TestUint64ToBigEndian() { + s.Require().Equal([]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, sdk.Uint64ToBigEndian(uint64(0))) + s.Require().Equal([]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa}, sdk.Uint64ToBigEndian(uint64(10))) } -func TestFormatTimeBytes(t *testing.T) { - t.Parallel() +func (s *utilsTestSuite) TestFormatTimeBytes() { tm, err := time.Parse("Jan 2, 2006 at 3:04pm (MST)", "Mar 3, 2020 at 7:54pm (UTC)") - require.NoError(t, err) - require.Equal(t, "2020-03-03T19:54:00.000000000", string(sdk.FormatTimeBytes(tm))) + s.Require().NoError(err) + s.Require().Equal("2020-03-03T19:54:00.000000000", string(sdk.FormatTimeBytes(tm))) } -func TestParseTimeBytes(t *testing.T) { - t.Parallel() +func (s *utilsTestSuite) TestParseTimeBytes() { tm, err := sdk.ParseTimeBytes([]byte("2020-03-03T19:54:00.000000000")) - require.NoError(t, err) - require.True(t, tm.Equal(time.Date(2020, 3, 3, 19, 54, 0, 0, time.UTC))) + s.Require().NoError(err) + s.Require().True(tm.Equal(time.Date(2020, 3, 3, 19, 54, 0, 0, time.UTC))) _, err = sdk.ParseTimeBytes([]byte{}) - require.Error(t, err) + s.Require().Error(err) }