文章目录
- 目录结构
- 代码示例
- split.go
- split_test.go
- fib.go
- fib_test.go
- 运行结果
目录结构
代码示例
split.go
package split
import "strings"
func Split(s, sep string) []string {
sepNum := strings.Count(s, sep)
r := make([]string, 0, sepNum+1)
index := strings.Index(s, sep)
for index >= 0 {
r = append(r, s[:index])
s = s[index+len(sep):]
index = strings.Index(s, sep)
}
r = append(r, s)
return r
}
split_test.go
package split
import (
"fmt"
"os"
"reflect"
"testing"
)
func TestNormalSplit(t *testing.T) {
get := Split("a:b:c", ":")
want := []string{"a", "b", "c"}
if ok := reflect.DeepEqual(get, want); !ok {
t.Fatalf("期望结果: %v, 实际结果: %v\n", want, get)
}
}
func TestMultiSplit(t *testing.T) {
f := setupT(t)
defer f(t)
get := Split("a::b::c", "::")
want := []string{"a", "b", "c"}
if ok := reflect.DeepEqual(get, want); !ok {
t.Fatalf("期望结果: %v, 实际结果: %v\n", want, get)
} else {
t.Log("pass test...")
}
}
func TestNumSplit(t *testing.T) {
get := Split("1231", "1")
want := []string{"", "23", ""}
if ok := reflect.DeepEqual(get, want); !ok {
t.Fatalf("期望结果: %v, 实际结果: %v\n", want, get)
}
}
func TestSplitGroup(t *testing.T) {
type test struct {
str string
sep string
want []string
}
items := map[string]test{
"normal": test{
str: "a:b:c",
sep: ":",
want: []string{"a", "b", "c"},
},
"multi": test{
str: "a::b::c",
sep: "::",
want: []string{"a", "b", "c"},
},
"num": test{
str: "1231",
sep: "1",
want: []string{"", "23", ""},
},
}
for k, i := range items {
t.Run(k, func(t *testing.T) {
get := Split(i.str, i.sep)
if ok := reflect.DeepEqual(i.want, get); !ok {
t.Fatalf("测试关键字: %v, 期望结果: %v, 实际结果: %v\n", k, i.want, get)
}
})
}
}
func BenchmarkSplit(b *testing.B) {
for i := 0; i < b.N; i++ {
Split("a:b:c", ":")
}
}
func BenchmarkParallelSplit(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Split("1231", "1")
}
})
}
func TestMain(m *testing.M) {
fmt.Println("start test...")
r := m.Run()
fmt.Println("end test...")
os.Exit(r)
}
func setupT(t *testing.T) func(t *testing.T) {
t.Log("start test...")
return func(t *testing.T) {
t.Log("end test...")
}
}
fib.go
package fib
func Fib(n int) int {
if n < 2 {
return n
}
return Fib(n-1) + Fib(n-2)
}
fib_test.go
package fib
import "testing"
func benchmarkFib(b *testing.B, n int) {
for i := 0; i < b.N; i++ {
Fib(n)
}
}
func BenchmarkFib2(b *testing.B) {
benchmarkFib(b, 2)
}
func BenchmarkFib10(b *testing.B) {
benchmarkFib(b, 10)
}
func BenchmarkFib20(b *testing.B) {
benchmarkFib(b, 20)
}
运行结果
split code
xxx@xxx split % go test -v
testMain start test...
=== RUN TestNormalSplit
--- PASS: TestNormalSplit (0.00s)
=== RUN TestMultiSplit
--- PASS: TestMultiSplit (0.00s)
split_test.go:111: setupT start test...
split_test.go:30: pass test...
split_test.go:113: setupT end test...
=== RUN TestNumSplit
--- PASS: TestNumSplit (0.00s)
=== RUN TestSplitGroup
=== RUN TestSplitGroup/normal
=== RUN TestSplitGroup/multi
=== RUN TestSplitGroup/num
--- PASS: TestSplitGroup (0.00s)
--- PASS: TestSplitGroup/normal (0.00s)
--- PASS: TestSplitGroup/multi (0.00s)
--- PASS: TestSplitGroup/num (0.00s)
PASS
testMain end test...
ok xxx/split 0.013s
fib code
xxx@xxx Fib % go test -bench=. -v
goos: darwin
goarch: amd64
pkg: code.oldboy.com/Fib
BenchmarkFib2-4 100000000 10.9 ns/op
BenchmarkFib10-4 2000000 695 ns/op
BenchmarkFib20-4 20000 90912 ns/op
PASS
ok xxx/Fib 5.728s