Merge PR #4303: Fix coins findDup()

This commit is contained in:
Alessio Treglia 2019-05-08 14:51:30 +01:00 committed by Alexander Bezobchuk
parent e5e6971558
commit 04ed12dd2a
3 changed files with 32 additions and 2 deletions

View File

@ -0,0 +1 @@
#4303 Fix NewCoins() underlying function for duplicate coins detection.

View File

@ -646,11 +646,12 @@ func findDup(coins Coins) int {
return -1
}
prevDenom := coins[0]
prevDenom := coins[0].Denom
for i := 1; i < len(coins); i++ {
if coins[i] == prevDenom {
if coins[i].Denom == prevDenom {
return i
}
prevDenom = coins[i].Denom
}
return -1

View File

@ -593,3 +593,31 @@ func TestCoinsIsAnyGT(t *testing.T) {
require.False(t, Coins{twoBtc, twoAtom, threeEth}.IsAnyGT(Coins{fiveAtom, sixEth}))
require.False(t, Coins{twoAtom, sixEth}.IsAnyGT(Coins{twoBtc, fiveAtom}))
}
func TestFindDup(t *testing.T) {
abc := NewInt64Coin("abc", 10)
def := NewInt64Coin("def", 10)
ghi := NewInt64Coin("ghi", 10)
type args struct {
coins Coins
}
tests := []struct {
name string
args args
want int
}{
{"empty", args{NewCoins()}, -1},
{"one coin", args{NewCoins(NewInt64Coin("xyz", 10))}, -1},
{"no dups", args{Coins{abc, def, ghi}}, -1},
{"dup at first position", args{Coins{abc, abc, def}}, 1},
{"dup after first position", args{Coins{abc, def, def}}, 2},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := findDup(tt.args.coins); got != tt.want {
t.Errorf("findDup() = %v, want %v", got, tt.want)
}
})
}
}