mirror of https://github.com/golang/go.git
strings: add benchmarks for Replace
This commit is contained in:
parent
f41fdd962d
commit
85a0db545d
|
|
@ -1473,6 +1473,10 @@ var ReplaceTests = []struct {
|
|||
|
||||
func TestReplace(t *testing.T) {
|
||||
for _, tt := range ReplaceTests {
|
||||
allocs := testing.AllocsPerRun(10, func() { Replace(tt.in, tt.old, tt.new, tt.n) })
|
||||
if allocs > 1 {
|
||||
t.Errorf("Replace(%q, %q, %q, %d) allocates %.2f objects", tt.in, tt.old, tt.new, tt.n, allocs)
|
||||
}
|
||||
if s := Replace(tt.in, tt.old, tt.new, tt.n); s != tt.out {
|
||||
t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
|
||||
}
|
||||
|
|
@ -1531,6 +1535,18 @@ func FuzzReplace(f *testing.F) {
|
|||
})
|
||||
}
|
||||
|
||||
func BenchmarkReplace(b *testing.B) {
|
||||
for _, tt := range ReplaceTests {
|
||||
desc := fmt.Sprintf("%q %q %q %d", tt.in, tt.old, tt.new, tt.n)
|
||||
b.Run(desc, func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
for b.Loop() {
|
||||
Replace(tt.in, tt.old, tt.new, tt.n)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
var TitleTests = []struct {
|
||||
in, out string
|
||||
}{
|
||||
|
|
|
|||
Loading…
Reference in New Issue