Source file
src/strings/clone_test.go
1
2
3
4
5 package strings_test
6
7 import (
8 "strings"
9 "testing"
10 "unsafe"
11 )
12
13 var emptyString string
14
15 func TestClone(t *testing.T) {
16 var cloneTests = []string{
17 "",
18 strings.Clone(""),
19 strings.Repeat("a", 42)[:0],
20 "short",
21 strings.Repeat("a", 42),
22 }
23 for _, input := range cloneTests {
24 clone := strings.Clone(input)
25 if clone != input {
26 t.Errorf("Clone(%q) = %q; want %q", input, clone, input)
27 }
28
29 if len(input) != 0 && unsafe.StringData(clone) == unsafe.StringData(input) {
30 t.Errorf("Clone(%q) return value should not reference inputs backing memory.", input)
31 }
32
33 if len(input) == 0 && unsafe.StringData(clone) != unsafe.StringData(emptyString) {
34 t.Errorf("Clone(%#v) return value should be equal to empty string.", unsafe.StringData(input))
35 }
36 }
37 }
38
39 func BenchmarkClone(b *testing.B) {
40 var str = strings.Repeat("a", 42)
41 b.ReportAllocs()
42 for i := 0; i < b.N; i++ {
43 stringSink = strings.Clone(str)
44 }
45 }
46
View as plain text