github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/src/runtime/string_test.go (about) 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. 4 5 package runtime_test 6 7 import ( 8 "runtime" 9 "strings" 10 "testing" 11 ) 12 13 // Strings and slices that don't escape and fit into tmpBuf are stack allocated, 14 // which defeats using AllocsPerRun to test other optimizations. 15 const sizeNoStack = 100 16 17 func BenchmarkCompareStringEqual(b *testing.B) { 18 bytes := []byte("Hello Gophers!") 19 s1, s2 := string(bytes), string(bytes) 20 for i := 0; i < b.N; i++ { 21 if s1 != s2 { 22 b.Fatal("s1 != s2") 23 } 24 } 25 } 26 27 func BenchmarkCompareStringIdentical(b *testing.B) { 28 s1 := "Hello Gophers!" 29 s2 := s1 30 for i := 0; i < b.N; i++ { 31 if s1 != s2 { 32 b.Fatal("s1 != s2") 33 } 34 } 35 } 36 37 func BenchmarkCompareStringSameLength(b *testing.B) { 38 s1 := "Hello Gophers!" 39 s2 := "Hello, Gophers" 40 for i := 0; i < b.N; i++ { 41 if s1 == s2 { 42 b.Fatal("s1 == s2") 43 } 44 } 45 } 46 47 func BenchmarkCompareStringDifferentLength(b *testing.B) { 48 s1 := "Hello Gophers!" 49 s2 := "Hello, Gophers!" 50 for i := 0; i < b.N; i++ { 51 if s1 == s2 { 52 b.Fatal("s1 == s2") 53 } 54 } 55 } 56 57 func BenchmarkCompareStringBigUnaligned(b *testing.B) { 58 bytes := make([]byte, 0, 1<<20) 59 for len(bytes) < 1<<20 { 60 bytes = append(bytes, "Hello Gophers!"...) 61 } 62 s1, s2 := string(bytes), "hello"+string(bytes) 63 for i := 0; i < b.N; i++ { 64 if s1 != s2[len("hello"):] { 65 b.Fatal("s1 != s2") 66 } 67 } 68 b.SetBytes(int64(len(s1))) 69 } 70 71 func BenchmarkCompareStringBig(b *testing.B) { 72 bytes := make([]byte, 0, 1<<20) 73 for len(bytes) < 1<<20 { 74 bytes = append(bytes, "Hello Gophers!"...) 75 } 76 s1, s2 := string(bytes), string(bytes) 77 for i := 0; i < b.N; i++ { 78 if s1 != s2 { 79 b.Fatal("s1 != s2") 80 } 81 } 82 b.SetBytes(int64(len(s1))) 83 } 84 85 func BenchmarkConcatStringAndBytes(b *testing.B) { 86 s1 := []byte("Gophers!") 87 for i := 0; i < b.N; i++ { 88 _ = "Hello " + string(s1) 89 } 90 } 91 92 var stringdata = []struct{ name, data string }{ 93 {"ASCII", "01234567890"}, 94 {"Japanese", "日本語日本語日本語"}, 95 {"MixedLength", "$Ѐࠀက퀀𐀀\U00040000\U0010FFFF"}, 96 } 97 98 func BenchmarkRuneIterate(b *testing.B) { 99 b.Run("range", func(b *testing.B) { 100 for _, sd := range stringdata { 101 b.Run(sd.name, func(b *testing.B) { 102 for i := 0; i < b.N; i++ { 103 for range sd.data { 104 } 105 } 106 }) 107 } 108 }) 109 b.Run("range1", func(b *testing.B) { 110 for _, sd := range stringdata { 111 b.Run(sd.name, func(b *testing.B) { 112 for i := 0; i < b.N; i++ { 113 for _ = range sd.data { 114 } 115 } 116 }) 117 } 118 }) 119 b.Run("range2", func(b *testing.B) { 120 for _, sd := range stringdata { 121 b.Run(sd.name, func(b *testing.B) { 122 for i := 0; i < b.N; i++ { 123 for _, _ = range sd.data { 124 } 125 } 126 }) 127 } 128 }) 129 } 130 131 func BenchmarkArrayEqual(b *testing.B) { 132 a1 := [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16} 133 a2 := [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16} 134 b.ResetTimer() 135 for i := 0; i < b.N; i++ { 136 if a1 != a2 { 137 b.Fatal("not equal") 138 } 139 } 140 } 141 142 func TestStringW(t *testing.T) { 143 strings := []string{ 144 "hello", 145 "a\u5566\u7788b", 146 } 147 148 for _, s := range strings { 149 var b []uint16 150 for _, c := range s { 151 b = append(b, uint16(c)) 152 if c != rune(uint16(c)) { 153 t.Errorf("bad test: stringW can't handle >16 bit runes") 154 } 155 } 156 b = append(b, 0) 157 r := runtime.GostringW(b) 158 if r != s { 159 t.Errorf("gostringW(%v) = %s, want %s", b, r, s) 160 } 161 } 162 } 163 164 func TestLargeStringConcat(t *testing.T) { 165 output := runTestProg(t, "testprog", "stringconcat") 166 want := "panic: " + strings.Repeat("0", 1<<10) + strings.Repeat("1", 1<<10) + 167 strings.Repeat("2", 1<<10) + strings.Repeat("3", 1<<10) 168 if !strings.HasPrefix(output, want) { 169 t.Fatalf("output does not start with %q:\n%s", want, output) 170 } 171 } 172 173 func TestCompareTempString(t *testing.T) { 174 s := strings.Repeat("x", sizeNoStack) 175 b := []byte(s) 176 n := testing.AllocsPerRun(1000, func() { 177 if string(b) != s { 178 t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s) 179 } 180 if string(b) == s { 181 } else { 182 t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s) 183 } 184 }) 185 if n != 0 { 186 t.Fatalf("want 0 allocs, got %v", n) 187 } 188 } 189 190 func TestStringOnStack(t *testing.T) { 191 s := "" 192 for i := 0; i < 3; i++ { 193 s = "a" + s + "b" + s + "c" 194 } 195 196 if want := "aaabcbabccbaabcbabccc"; s != want { 197 t.Fatalf("want: '%v', got '%v'", want, s) 198 } 199 } 200 201 func TestIntString(t *testing.T) { 202 // Non-escaping result of intstring. 203 s := "" 204 for i := 0; i < 4; i++ { 205 s += string(i+'0') + string(i+'0'+1) 206 } 207 if want := "01122334"; s != want { 208 t.Fatalf("want '%v', got '%v'", want, s) 209 } 210 211 // Escaping result of intstring. 212 var a [4]string 213 for i := 0; i < 4; i++ { 214 a[i] = string(i + '0') 215 } 216 s = a[0] + a[1] + a[2] + a[3] 217 if want := "0123"; s != want { 218 t.Fatalf("want '%v', got '%v'", want, s) 219 } 220 } 221 222 func TestIntStringAllocs(t *testing.T) { 223 unknown := '0' 224 n := testing.AllocsPerRun(1000, func() { 225 s1 := string(unknown) 226 s2 := string(unknown + 1) 227 if s1 == s2 { 228 t.Fatalf("bad") 229 } 230 }) 231 if n != 0 { 232 t.Fatalf("want 0 allocs, got %v", n) 233 } 234 } 235 236 func TestRangeStringCast(t *testing.T) { 237 s := strings.Repeat("x", sizeNoStack) 238 n := testing.AllocsPerRun(1000, func() { 239 for i, c := range []byte(s) { 240 if c != s[i] { 241 t.Fatalf("want '%c' at pos %v, got '%c'", s[i], i, c) 242 } 243 } 244 }) 245 if n != 0 { 246 t.Fatalf("want 0 allocs, got %v", n) 247 } 248 } 249 250 func isZeroed(b []byte) bool { 251 for _, x := range b { 252 if x != 0 { 253 return false 254 } 255 } 256 return true 257 } 258 259 func isZeroedR(r []rune) bool { 260 for _, x := range r { 261 if x != 0 { 262 return false 263 } 264 } 265 return true 266 } 267 268 func TestString2Slice(t *testing.T) { 269 // Make sure we don't return slices that expose 270 // an unzeroed section of stack-allocated temp buf 271 // between len and cap. See issue 14232. 272 s := "foož" 273 b := ([]byte)(s) 274 if !isZeroed(b[len(b):cap(b)]) { 275 t.Errorf("extra bytes not zeroed") 276 } 277 r := ([]rune)(s) 278 if !isZeroedR(r[len(r):cap(r)]) { 279 t.Errorf("extra runes not zeroed") 280 } 281 }