course/test/spilt_string_test.go

41 lines
775 B
Go

package Splitstring
import (
"reflect"
"testing"
)
//单元测试,测试每个函数的功能是否正常
func TestSplit(t *testing.T) {
got := Split("babcbef", "b")
want := []string{"", "a", "c", "ef"}
if !reflect.DeepEqual(want, got) {
t.Errorf("want:%v, but got:%v\n", want, got)
}
}
func TestSplit2(t *testing.T) {
got := Split("a:b:c", ":")
want := []string{"a", "b", "c"}
if !reflect.DeepEqual(want, got) {
t.Errorf("want:%v, but got:%v\n", want, got)
}
}
func TestSplit3(t *testing.T) {
got := Split("abcef", "bc")
want := []string{"a", "ef"}
if !reflect.DeepEqual(want, got) {
t.Fatalf("want:%v, but got:%v\n", want, got)
}
}
//基准测试
func BenchmarkSplit(b *testing.B) {
for i := 0; i < b.N; i++ {
Split("a:b:c:d:e:f", ":")
}
}