summaryrefslogtreecommitdiff
path: root/src/strings/strings_test.go
diff options
context:
space:
mode:
authorJohn Potocny <johnp@vividcortex.com>2015-03-25 21:08:04 -0400
committerIan Lance Taylor <iant@golang.org>2015-03-31 00:40:55 +0000
commit6262192cd0fb98d6bb80752de70ae33fc10dc33e (patch)
tree92abd988571dfdde55510c10864f28432d1d78b9 /src/strings/strings_test.go
parentc45751e8a50d6050c6090d654364d69599b2e97a (diff)
downloadgo-git-6262192cd0fb98d6bb80752de70ae33fc10dc33e.tar.gz
strings: Add benchmark test for trim function
The strings.Trim function and variants allocate memory on the heap when creating a function to pass into TrimFunc. Add a benchmark to document the behavior; an issue will be submitted to address this behavior in the compiler if possible. Change-Id: I8b66721f077951f7e7b8cf3cf346fac27a9b68c0 Reviewed-on: https://go-review.googlesource.com/8200 Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/strings/strings_test.go')
-rw-r--r--src/strings/strings_test.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/strings/strings_test.go b/src/strings/strings_test.go
index 7bb81ef3ca..ee0c260753 100644
--- a/src/strings/strings_test.go
+++ b/src/strings/strings_test.go
@@ -569,6 +569,35 @@ func TestTrim(t *testing.T) {
}
}
+func BenchmarkTrim(b *testing.B) {
+ b.ReportAllocs()
+
+ for i := 0; i < b.N; i++ {
+ for _, tc := range trimTests {
+ name := tc.f
+ var f func(string, string) string
+ switch name {
+ case "Trim":
+ f = Trim
+ case "TrimLeft":
+ f = TrimLeft
+ case "TrimRight":
+ f = TrimRight
+ case "TrimPrefix":
+ f = TrimPrefix
+ case "TrimSuffix":
+ f = TrimSuffix
+ default:
+ b.Errorf("Undefined trim function %s", name)
+ }
+ actual := f(tc.in, tc.arg)
+ if actual != tc.out {
+ b.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
+ }
+ }
+ }
+}
+
type predicate struct {
f func(rune) bool
name string