测试
func TestNonrepeatingsubstr(t *testing.T) {
tests := []struct {
s string
ans int
}{
// Normal cases
{"jack006", 5},
{"aadewr", 5},
{"wsxdswws", 4},
// Edge cases
{"", 0},
{"b", 1},
{"aaaaaaaaaaaaaaaaaaaa", 1},
// chinese support
{"这里是慕课网", 6},
{"黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花", 8},
}
for _, tt := range tests {
actual, _ := lengthOfNonRepeatingSubStr(tt.s)
if actual != tt.ans {
t.Errorf("Got %d for input %s, want %d", actual, tt.s, tt.ans)
}
}
}
代码覆盖率测试
- 运行方式 Run with cover
- 命令行 go test -cover .
- 命令行 go tool -coverprofile=c.out
- 命令行 go tool cover -html=c.out
- 命令行 go tool cover -func=c.out
性能测试
func BenchmarkSubstr(b *testing.B) {
s := "黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花"
ans := 8
for i := 0; i < b.N; i++ {
actual, _ := lengthOfNonRepeatingSubStr(s)
if actual != ans {
b.Errorf("Got %d for input %s, want %d", actual, s, ans)
}
}
}
代码覆盖
- 使用IDE查看代码覆盖率
- 使用 go test 获取覆盖报告
- 使用 go tool cover 查看代码覆盖报告
性能测试
文档
- 用注释写文档
- 在测试中加入Example
- 使用go doc / godoc 来查看/生成文档
网友评论