[x/simulation] RandSubsetCoins() should return sorted coinset (#5373)
Closes: #5372
This commit is contained in:
parent
3c2ace9510
commit
72ff13eb97
|
@ -122,7 +122,7 @@ func RandSubsetCoins(r *rand.Rand, coins sdk.Coins) sdk.Coins {
|
|||
}
|
||||
subset = append(subset, sdk.NewCoin(c.Denom, amt))
|
||||
}
|
||||
return subset
|
||||
return subset.Sort()
|
||||
}
|
||||
|
||||
// DeriveRand derives a new Rand deterministically from another random source.
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package simulation_test
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/simulation"
|
||||
)
|
||||
|
||||
func TestRandSubsetCoins(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
r *rand.Rand
|
||||
coins sdk.Coins
|
||||
}{
|
||||
{"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")},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := simulation.RandSubsetCoins(tt.r, tt.coins)
|
||||
gotStringRep := got.String()
|
||||
sortedStringRep := got.Sort().String()
|
||||
require.Equal(t, gotStringRep, sortedStringRep)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func mustParseCoins(s string) sdk.Coins {
|
||||
coins, err := sdk.ParseCoins(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return coins
|
||||
}
|
Loading…
Reference in New Issue