Skip to content

Commit

Permalink
test(types/simulation): add unit tests for the code in the package ty…
Browse files Browse the repository at this point in the history
…pes/simulation (cosmos#19996)
  • Loading branch information
EmilGeorgiev authored Apr 18, 2024
1 parent 90a0114 commit 84a9c8a
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 5 deletions.
3 changes: 2 additions & 1 deletion types/simulation/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func TestRandomFees(t *testing.T) {
wantErr bool
}{
{"0 coins", sdk.Coins{}, true, false},
{"0 coins", sdk.NewCoins(sdk.NewInt64Coin("aaa", 10), sdk.NewInt64Coin("bbb", 5)), false, false},
{"2 coins", sdk.NewCoins(sdk.NewInt64Coin("aaa", 10), sdk.NewInt64Coin("bbb", 5)), false, false},
{"1 coin with 0 amount", sdk.Coins{sdk.NewInt64Coin("ccc", 0)}, true, true},
}
for _, tt := range tests {
tt := tt
Expand Down
148 changes: 144 additions & 4 deletions types/simulation/rand_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/stretchr/testify/require"

"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/simulation"
)
Expand All @@ -21,6 +23,8 @@ func TestRandSubsetCoins(t *testing.T) {
{"seed=1", rand.New(rand.NewSource(1)), mustParseCoins("100stake,2testtoken")},
{"seed=50", rand.New(rand.NewSource(50)), mustParseCoins("100stake,2testtoken")},
{"seed=99", rand.New(rand.NewSource(99)), mustParseCoins("100stake,2testtoken")},
{"zero coins", rand.New(rand.NewSource(99)), sdk.Coins{}},
{"too small amount", rand.New(rand.NewSource(99)), sdk.Coins{sdk.Coin{Denom: "aaa", Amount: math.NewInt(0)}}},
}
for _, tt := range tests {
tt := tt
Expand All @@ -46,11 +50,147 @@ func TestRandStringOfLength(t *testing.T) {
{"1_000_000-size", 1_000_000, 1_000_000},
}
for _, tt := range tests {
tt := tt
tc := tt
t.Run(tc.name, func(t *testing.T) {
got := simulation.RandStringOfLength(r, tc.n)
require.Equal(t, tc.want, len(got))
})
}
}

t.Run(tt.name, func(t *testing.T) {
got := simulation.RandStringOfLength(r, tt.n)
require.Equal(t, tt.want, len(got))
func TestRandPositiveInt(t *testing.T) {
t.Parallel()
tests := []struct {
name string
r *rand.Rand
max math.Int
wantErr bool
}{
{"max number is 1", rand.New(rand.NewSource(1)), math.NewInt(1), false},
{"max number is 5", rand.New(rand.NewSource(50)), math.NewInt(5), false},
{"negative max number", rand.New(rand.NewSource(99)), math.NewInt(-10), true},
{"too small max number", rand.New(rand.NewSource(50)), math.NewInt(0), true},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
got, err := simulation.RandPositiveInt(tc.r, tc.max)
if tc.wantErr {
require.NotNil(t, err)
return
}
require.Greater(t, got.Int64(), int64(0))
require.LessOrEqual(t, got.Int64(), tc.max.Int64())
})
}
}

func TestRandomAmount(t *testing.T) {
t.Parallel()
tests := []struct {
name string
r *rand.Rand
max math.Int
}{
{"max number is 1", rand.New(rand.NewSource(1)), math.NewInt(1)},
{"max number is 5", rand.New(rand.NewSource(50)), math.NewInt(5)},
{"negative max number", rand.New(rand.NewSource(99)), math.NewInt(-10)},
{"too small max number", rand.New(rand.NewSource(50)), math.NewInt(0)},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
got := simulation.RandomAmount(tc.r, tc.max)
if tc.max.Int64() < 0 {
require.Equal(t, got.Int64(), int64(0))
return
}
require.GreaterOrEqual(t, got.Int64(), int64(0))
require.LessOrEqual(t, got.Int64(), tc.max.Int64())
})
}
}

func TestRandomDecAmount(t *testing.T) {
t.Parallel()
tests := []struct {
name string
r *rand.Rand
max math.LegacyDec
exp string
}{
{"max number is 1000", rand.New(rand.NewSource(1)), math.LegacyNewDec(1000), "1000.000000000000000000"},
{"max number is 531234567", rand.New(rand.NewSource(50)), math.LegacyNewDec(531234567), "531234567.000000000000000000"},
{"negative max number", rand.New(rand.NewSource(99)), math.LegacyNewDec(-10), "0.000000000000000000"},
{"max number is zero", rand.New(rand.NewSource(50)), math.LegacyNewDec(0), "0.000000000000000000"},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
got := simulation.RandomDecAmount(tc.r, tc.max)
require.Equal(t, got.String(), tc.exp)
})
}
}

func TestRandomTimestamp(t *testing.T) {
t.Parallel()
tests := []struct {
name string
r *rand.Rand
exp string
}{
{"rand is 0", rand.New(rand.NewSource(0)), "2083-05-23 03:26:06 +0000 UTC"},
{"rand is greater than 1", rand.New(rand.NewSource(1)), "2126-08-31 20:37:51 +0000 UTC"},
{"rand is negative", rand.New(rand.NewSource(-1)), "2179-08-09 15:02:17 +0000 UTC"},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
got := simulation.RandTimestamp(tc.r)
require.Equal(t, got.UTC().String(), tc.exp)
})
}
}

func TestRandomIntBetween(t *testing.T) {
t.Parallel()
tests := []struct {
name string
r *rand.Rand
min int
max int
}{
{"random int between 0 and 10", rand.New(rand.NewSource(0)), 0, 10},
{"random int between 11 ans 1000", rand.New(rand.NewSource(1)), 11, 1000},
{"random int between 10001 and 100000", rand.New(rand.NewSource(-1)), 1001, 10000},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
got := simulation.RandIntBetween(tc.r, tc.min, tc.max)
require.GreaterOrEqual(t, got, tc.min)
require.LessOrEqual(t, got, tc.max)
})
}
}

func TestDeriveRand(t *testing.T) {
t.Parallel()
tests := []struct {
name string
r *rand.Rand
exp int64
}{
{"seed equal to zero", rand.New(rand.NewSource(0)), 8759604767892952359},
{"seed positive number", rand.New(rand.NewSource(1)), 4609759310771376844},
{"seed negative number", rand.New(rand.NewSource(-2)), 7949885017563827825},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
got := simulation.DeriveRand(tc.r)
require.Equal(t, got.Int63(), tc.exp)
})
}
}
Expand Down

0 comments on commit 84a9c8a

Please sign in to comment.