Skip to content

Commit

Permalink
Merge PR cosmos#4303: Fix coins findDup()
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio authored and alexanderbez committed May 8, 2019
1 parent e5e6971 commit 04ed12d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions .pending/bugfixes/sdk/4303-Fix-NewCoins-un
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4303 Fix NewCoins() underlying function for duplicate coins detection.
5 changes: 3 additions & 2 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit 04ed12d

Please sign in to comment.