1 // Copyright 2012 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.
12 func BenchmarkCompareStringEqual(b
*testing
.B
) {
13 bytes
:= []byte("Hello Gophers!")
14 s1
, s2
:= string(bytes
), string(bytes
)
15 for i
:= 0; i
< b
.N
; i
++ {
22 func BenchmarkCompareStringIdentical(b
*testing
.B
) {
23 s1
:= "Hello Gophers!"
25 for i
:= 0; i
< b
.N
; i
++ {
32 func BenchmarkCompareStringSameLength(b
*testing
.B
) {
33 s1
:= "Hello Gophers!"
34 s2
:= "Hello, Gophers"
35 for i
:= 0; i
< b
.N
; i
++ {
42 func BenchmarkCompareStringDifferentLength(b
*testing
.B
) {
43 s1
:= "Hello Gophers!"
44 s2
:= "Hello, Gophers!"
45 for i
:= 0; i
< b
.N
; i
++ {
52 func BenchmarkCompareStringBigUnaligned(b
*testing
.B
) {
53 bytes
:= make([]byte, 0, 1<<20)
54 for len(bytes
) < 1<<20 {
55 bytes
= append(bytes
, "Hello Gophers!"...)
57 s1
, s2
:= string(bytes
), "hello"+string(bytes
)
58 for i
:= 0; i
< b
.N
; i
++ {
59 if s1
!= s2
[len("hello"):] {
63 b
.SetBytes(int64(len(s1
)))
66 func BenchmarkCompareStringBig(b
*testing
.B
) {
67 bytes
:= make([]byte, 0, 1<<20)
68 for len(bytes
) < 1<<20 {
69 bytes
= append(bytes
, "Hello Gophers!"...)
71 s1
, s2
:= string(bytes
), string(bytes
)
72 for i
:= 0; i
< b
.N
; i
++ {
77 b
.SetBytes(int64(len(s1
)))
80 func BenchmarkRuneIterate(b
*testing
.B
) {
81 bytes
:= make([]byte, 100)
82 for i
:= range bytes
{
86 for i
:= 0; i
< b
.N
; i
++ {
92 func BenchmarkRuneIterate2(b
*testing
.B
) {
93 bytes
:= make([]byte, 100)
94 for i
:= range bytes
{
98 for i
:= 0; i
< b
.N
; i
++ {
105 func TestStringW(t *testing.T) {
111 for _, s := range strings {
113 for _, c := range s {
114 b = append(b, uint16(c))
115 if c != rune(uint16(c)) {
116 t.Errorf("bad test: stringW can't handle >16 bit runes")
120 r := runtime.GostringW(b)
122 t.Errorf("gostringW(%v) = %s, want %s", b, r, s)
128 func TestLargeStringConcat(t
*testing
.T
) {
129 output
:= runTestProg(t
, "testprog", "stringconcat")
130 want
:= "panic: " + strings
.Repeat("0", 1<<10) + strings
.Repeat("1", 1<<10) +
131 strings
.Repeat("2", 1<<10) + strings
.Repeat("3", 1<<10)
132 if !strings
.HasPrefix(output
, want
) {
133 t
.Fatalf("output does not start with %q:\n%s", want
, output
)
138 func TestGostringnocopy(t *testing.T) {
139 max := *runtime.Maxstring
140 b := make([]byte, max+10)
141 for i := uintptr(0); i < max+9; i++ {
144 _ = runtime.Gostringnocopy(&b[0])
145 newmax := *runtime.Maxstring
147 t.Errorf("want %d, got %d", max+9, newmax)
152 func TestCompareTempString(t
*testing
.T
) {
155 n
:= testing
.AllocsPerRun(1000, func() {
157 t
.Fatalf("strings are not equal: '%v' and '%v'", string(b
), s
)
161 t
.Fatalf("strings are not equal: '%v' and '%v'", string(b
), s
)
165 t
.Fatalf("want 0 allocs, got %v", n
)
169 func TestStringOnStack(t
*testing
.T
) {
171 for i
:= 0; i
< 3; i
++ {
172 s
= "a" + s
+ "b" + s
+ "c"
175 if want
:= "aaabcbabccbaabcbabccc"; s
!= want
{
176 t
.Fatalf("want: '%v', got '%v'", want
, s
)
180 func TestIntString(t
*testing
.T
) {
181 // Non-escaping result of intstring.
183 for i
:= 0; i
< 4; i
++ {
184 s
+= string(i
+'0') + string(i
+'0'+1)
186 if want
:= "01122334"; s
!= want
{
187 t
.Fatalf("want '%v', got '%v'", want
, s
)
190 // Escaping result of intstring.
192 for i
:= 0; i
< 4; i
++ {
193 a
[i
] = string(i
+ '0')
195 s
= a
[0] + a
[1] + a
[2] + a
[3]
196 if want
:= "0123"; s
!= want
{
197 t
.Fatalf("want '%v', got '%v'", want
, s
)
201 func TestIntStringAllocs(t
*testing
.T
) {
203 n
:= testing
.AllocsPerRun(1000, func() {
204 s1
:= string(unknown
)
205 s2
:= string(unknown
+ 1)
211 t
.Fatalf("want 0 allocs, got %v", n
)
215 func TestRangeStringCast(t
*testing
.T
) {
217 n
:= testing
.AllocsPerRun(1000, func() {
218 for i
, c
:= range []byte(s
) {
220 t
.Fatalf("want '%c' at pos %v, got '%c'", s
[i
], i
, c
)
225 t
.Fatalf("want 0 allocs, got %v", n
)
229 func TestString2Slice(t
*testing
.T
) {
230 // Make sure we don't return slices that expose
231 // an unzeroed section of stack-allocated temp buf
232 // between len and cap. See issue 14232.
236 t
.Errorf("want cap of 5, got %d", cap(b
))
240 t
.Errorf("want cap of 4, got %d", cap(r
))