Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix types/findDup() #4303

Merged
merged 1 commit into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
}
}