[x/simulation] RandSubsetCoins() should return sorted coinset (#5373)

Closes: #5372
This commit is contained in:
Alessio Treglia 2019-12-06 16:25:25 +01:00 committed by GitHub
parent 3c2ace9510
commit 72ff13eb97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 1 deletions

View File

@ -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.

View File

@ -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
}