github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gofrontend/libgo/go/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 "strings" 9 "testing" 10 ) 11 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++ { 16 if s1 != s2 { 17 b.Fatal("s1 != s2") 18 } 19 } 20 } 21 22 func BenchmarkCompareStringIdentical(b *testing.B) { 23 s1 := "Hello Gophers!" 24 s2 := s1 25 for i := 0; i < b.N; i++ { 26 if s1 != s2 { 27 b.Fatal("s1 != s2") 28 } 29 } 30 } 31 32 func BenchmarkCompareStringSameLength(b *testing.B) { 33 s1 := "Hello Gophers!" 34 s2 := "Hello, Gophers" 35 for i := 0; i < b.N; i++ { 36 if s1 == s2 { 37 b.Fatal("s1 == s2") 38 } 39 } 40 } 41 42 func BenchmarkCompareStringDifferentLength(b *testing.B) { 43 s1 := "Hello Gophers!" 44 s2 := "Hello, Gophers!" 45 for i := 0; i < b.N; i++ { 46 if s1 == s2 { 47 b.Fatal("s1 == s2") 48 } 49 } 50 } 51 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!"...) 56 } 57 s1, s2 := string(bytes), "hello"+string(bytes) 58 for i := 0; i < b.N; i++ { 59 if s1 != s2[len("hello"):] { 60 b.Fatal("s1 != s2") 61 } 62 } 63 b.SetBytes(int64(len(s1))) 64 } 65 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!"...) 70 } 71 s1, s2 := string(bytes), string(bytes) 72 for i := 0; i < b.N; i++ { 73 if s1 != s2 { 74 b.Fatal("s1 != s2") 75 } 76 } 77 b.SetBytes(int64(len(s1))) 78 } 79 80 func BenchmarkRuneIterate(b *testing.B) { 81 bytes := make([]byte, 100) 82 for i := range bytes { 83 bytes[i] = byte('A') 84 } 85 s := string(bytes) 86 for i := 0; i < b.N; i++ { 87 for range s { 88 } 89 } 90 } 91 92 func BenchmarkRuneIterate2(b *testing.B) { 93 bytes := make([]byte, 100) 94 for i := range bytes { 95 bytes[i] = byte('A') 96 } 97 s := string(bytes) 98 for i := 0; i < b.N; i++ { 99 for range s { 100 } 101 } 102 } 103 104 /* 105 func TestStringW(t *testing.T) { 106 strings := []string{ 107 "hello", 108 "a\u5566\u7788b", 109 } 110 111 for _, s := range strings { 112 var b []uint16 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") 117 } 118 } 119 b = append(b, 0) 120 r := runtime.GostringW(b) 121 if r != s { 122 t.Errorf("gostringW(%v) = %s, want %s", b, r, s) 123 } 124 } 125 } 126 */ 127 128 func TestLargeStringConcat(t *testing.T) { 129 output := executeTest(t, largeStringConcatSource, nil) 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) 134 } 135 } 136 137 var largeStringConcatSource = ` 138 package main 139 import "strings" 140 func main() { 141 s0 := strings.Repeat("0", 1<<10) 142 s1 := strings.Repeat("1", 1<<10) 143 s2 := strings.Repeat("2", 1<<10) 144 s3 := strings.Repeat("3", 1<<10) 145 s := s0 + s1 + s2 + s3 146 panic(s) 147 } 148 ` 149 150 /* 151 func TestGostringnocopy(t *testing.T) { 152 max := *runtime.Maxstring 153 b := make([]byte, max+10) 154 for i := uintptr(0); i < max+9; i++ { 155 b[i] = 'a' 156 } 157 _ = runtime.Gostringnocopy(&b[0]) 158 newmax := *runtime.Maxstring 159 if newmax != max+9 { 160 t.Errorf("want %d, got %d", max+9, newmax) 161 } 162 } 163 */ 164 165 func TestCompareTempString(t *testing.T) { 166 s := "foo" 167 b := []byte(s) 168 n := testing.AllocsPerRun(1000, func() { 169 if string(b) != s { 170 t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s) 171 } 172 if string(b) == s { 173 } else { 174 t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s) 175 } 176 }) 177 if n != 0 { 178 t.Fatalf("want 0 allocs, got %v", n) 179 } 180 } 181 182 func TestStringOnStack(t *testing.T) { 183 s := "" 184 for i := 0; i < 3; i++ { 185 s = "a" + s + "b" + s + "c" 186 } 187 188 if want := "aaabcbabccbaabcbabccc"; s != want { 189 t.Fatalf("want: '%v', got '%v'", want, s) 190 } 191 } 192 193 func TestIntString(t *testing.T) { 194 // Non-escaping result of intstring. 195 s := "" 196 for i := 0; i < 4; i++ { 197 s += string(i+'0') + string(i+'0'+1) 198 } 199 if want := "01122334"; s != want { 200 t.Fatalf("want '%v', got '%v'", want, s) 201 } 202 203 // Escaping result of intstring. 204 var a [4]string 205 for i := 0; i < 4; i++ { 206 a[i] = string(i + '0') 207 } 208 s = a[0] + a[1] + a[2] + a[3] 209 if want := "0123"; s != want { 210 t.Fatalf("want '%v', got '%v'", want, s) 211 } 212 } 213 214 func TestIntStringAllocs(t *testing.T) { 215 unknown := '0' 216 n := testing.AllocsPerRun(1000, func() { 217 s1 := string(unknown) 218 s2 := string(unknown + 1) 219 if s1 == s2 { 220 t.Fatalf("bad") 221 } 222 }) 223 if n != 0 { 224 t.Fatalf("want 0 allocs, got %v", n) 225 } 226 } 227 228 func TestRangeStringCast(t *testing.T) { 229 s := "abc" 230 n := testing.AllocsPerRun(1000, func() { 231 for i, c := range []byte(s) { 232 if c != s[i] { 233 t.Fatalf("want '%c' at pos %v, got '%c'", s[i], i, c) 234 } 235 } 236 }) 237 if n != 0 { 238 t.Fatalf("want 0 allocs, got %v", n) 239 } 240 }