Add tests for coins

This commit is contained in:
Adrian Brink 2018-01-23 13:10:06 +01:00 committed by Jae Kwon
parent 102d196204
commit 1cc0cf236c
2 changed files with 23 additions and 6 deletions

View File

@ -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
@ -110,7 +107,6 @@ func (coins Coins) Plus(coinsB Coins) Coins {
indexB++
}
}
return sum
}
// Negative returns a set of coins with all amount negative

View File

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