Require target lra in gcc.dg/pr108095.c
[official-gcc.git] / libgo / go / strings / clone_test.go
bloba9ba8add2355245a532f57b7cefa93998b3ee4fa
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.ß
5 package strings_test
7 import (
8 "reflect"
9 "strings"
10 "testing"
11 "unsafe"
14 var emptyString string
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),
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)
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)
45 b.ReportAllocs()
46 for i := 0; i < b.N; i++ {
47 stringSink = strings.Clone(str)