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