Practice code coverage

Signed-off-by: Bruce <weichou1229@gmail.com>
This commit is contained in:
Bruce 2019-02-20 14:52:34 +08:00
parent 01aad3573b
commit b97231eecb
No known key found for this signature in database
GPG Key ID: C715526B381CAF28
2 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,17 @@
package codecoverage
func Size(a int) string {
switch {
case a < 0:
return "negative"
case a == 0:
return "zero"
case a < 10:
return "small"
case a < 100:
return "big"
case a < 1000:
return "huge"
}
return "enormous"
}

View File

@ -0,0 +1,26 @@
package codecoverage
import "testing"
type Test struct {
in int
out string
}
var tests = []Test{
{-1, "negative"},
{0, "zero"},
{5, "small"},
{99, "big"},
{100, "huge"},
{10001, "enormous"},
}
func TestSize(t *testing.T) {
for i, test := range tests {
size := Size(test.in)
if size != test.out {
t.Errorf("#%d: Size(%d)=%s; want %s", i, test.in, size, test.out)
}
}
}