1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.ß
14 var emptyString
string
16 func TestClone(t
*testing
.T
) {
17 var cloneTests
= []string{
20 strings
.Repeat("a", 42)[:0],
22 strings
.Repeat("a", 42),
24 for _
, input
:= range cloneTests
{
25 clone
:= strings
.Clone(input
)
27 t
.Errorf("Clone(%q) = %q; want %q", input
, clone
, input
)
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
)
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
)
43 func BenchmarkClone(b
*testing
.B
) {
44 var str
= strings
.Repeat("a", 42)
46 for i
:= 0; i
< b
.N
; i
++ {
47 stringSink
= strings
.Clone(str
)