1 Star 0 Fork 43

未名/go_sexy

forked from 烈冰/go_sexy 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main_test.go 2.59 KB
一键复制 编辑 原始数据 按行查看 历史
烈冰 提交于 2015-01-23 13:03 . 增加一个benchmark
// main_test
package main
import (
"net/url"
"testing"
)
type toAbsTest struct {
pageURL, href, result string
}
var toAbsTestData = []toAbsTest{
{"http://blog.hoday.cn", "?page=1", "http://blog.hoday.cn/?page=1"},
{"http://blog.hoday.cn", "/tag/coding.html", "http://blog.hoday.cn/tag/coding.html"},
{"http://blog.hoday.cn", "tag/coding.html", "http://blog.hoday.cn/tag/coding.html"},
{"http://blog.hoday.cn", "http://blog.hoday.cn/post/java.html", "http://blog.hoday.cn/post/java.html"},
{"http://blog.hoday.cn", "../tag/coding.html", "http://blog.hoday.cn/tag/coding.html"},
{"http://blog.hoday.cn/index.html", "?page=1", "http://blog.hoday.cn/index.html?page=1"},
{"http://blog.hoday.cn/index.html", "/tag/coding.html", "http://blog.hoday.cn/tag/coding.html"},
{"http://blog.hoday.cn/index.html", "tag/coding.html", "http://blog.hoday.cn/tag/coding.html"},
{"http://blog.hoday.cn/index.html", "http://blog.hoday.cn/post/java.html", "http://blog.hoday.cn/post/java.html"},
{"http://blog.hoday.cn/index.html", "../tag/coding.html", "http://blog.hoday.cn/tag/coding.html"},
{"http://blog.hoday.cn/post/golang.html", "?page=1", "http://blog.hoday.cn/post/golang.html?page=1"},
{"http://blog.hoday.cn/post/golang.html", "/tag/coding.html", "http://blog.hoday.cn/tag/coding.html"},
{"http://blog.hoday.cn/post/golang.html", "tag/coding.html", "http://blog.hoday.cn/post/tag/coding.html"},
{"http://blog.hoday.cn/post/golang.html", "http://blog.hoday.cn/post/java.html", "http://blog.hoday.cn/post/java.html"},
{"http://blog.hoday.cn/post/golang.html", "../tag/coding.html", "http://blog.hoday.cn/tag/coding.html"},
}
var toAbsBenchmark = []string{"?page=1", "/tag/coding.html", "tag/coding.html","http://blog.hoday.cn/post/java.html","../tag/coding.html"}
func TestToAbs(t *testing.T) {
for _, test := range toAbsTestData {
p, err := url.Parse(test.pageURL)
if err != nil {
t.Fatalf("url.Parse(%q) %q", test.pageURL, err) //调用Fatal后会中断当前的测试函数
}
if u := toAbs(p, test.href); u.String() != test.result {
t.Errorf("toAbs(%q, %q) = %q, 期望值 %q", p, test.href, u, test.result) //调用Error不会中断
}
}
}
func BenchmarkToAbs(b *testing.B) {
b.StopTimer()
pageURL := "http://blog.hoday.cn/post/golang.html"
p, err := url.Parse(pageURL)
if err != nil {
b.Fatalf("url.Parse(%q) %q", pageURL, err) //调用Fatal后会中断当前的测试函数
}
b.StartTimer()
for i := 0; i < b.N; i++ {
for _, val := range toAbsBenchmark{
if u := toAbs(p, val); u == nil {
b.Fatalf("toAbs(%q, %q) 出错", p, val) //调用Error不会中断
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/phpu/go_sexy.git
git@gitee.com:phpu/go_sexy.git
phpu
go_sexy
go_sexy
master

搜索帮助