Skip to content

Commit

Permalink
Add tests for coins
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Brink authored and jaekwon committed Jan 29, 2018
1 parent 102d196 commit 1cc0cf2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
6 changes: 1 addition & 5 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ func (coins Coins) IsValid() bool {
}
}

// Plus combines to sets of coins
//
// TODO: handle empty coins!
// Currently appends an empty coin ...
// Plus combines two sets of coins
func (coins Coins) Plus(coinsB Coins) Coins {
sum := []Coin{}
indexA, indexB := 0, 0
Expand Down Expand Up @@ -110,7 +107,6 @@ func (coins Coins) Plus(coinsB Coins) Coins {
indexB++
}
}
return sum
}

// Negative returns a set of coins with all amount negative
Expand Down
23 changes: 22 additions & 1 deletion types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,28 @@ func TestCoins(t *testing.T) {

}

//Test the parse coin and parse coins functionality
func TestPlusCoins(t *testing.T) {
assert := assert.New(t)

cases := []struct {
inputOne Coins
inputTwo Coins
expected Coins
}{
{Coins{{"A", 1}, {"B", 1}}, Coins{{"A", 1}, {"B", 1}}, Coins{{"A", 2}, {"B", 2}}},
{Coins{{"A", 0}, {"B", 1}}, Coins{{"A", 0}, {"B", 0}}, Coins{{"B", 1}}},
{Coins{{"A", 0}, {"B", 0}}, Coins{{"A", 0}, {"B", 0}}, Coins{}},
{Coins{{"A", 1}, {"B", 0}}, Coins{{"A", -1}, {"B", 0}}, Coins{}},
}

for _, tc := range cases {
res := tc.inputOne.Plus(tc.inputTwo)
assert.True(res.IsValid())
assert.Equal(res, tc.expected)
}
}

//Test the parsing of Coin and Coins
func TestParse(t *testing.T) {

cases := []struct {
Expand Down

0 comments on commit 1cc0cf2

Please sign in to comment.