* da.po, sv.po: Update.
[official-gcc.git] / libgo / go / runtime / string_test.go
blob64abd57d28c84a3484c25878a71b26b393592410
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.
5 package runtime_test
7 import (
8 "strings"
9 "testing"
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")
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")
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")
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")
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"):] {
60 b.Fatal("s1 != s2")
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++ {
73 if s1 != s2 {
74 b.Fatal("s1 != s2")
77 b.SetBytes(int64(len(s1)))
80 func BenchmarkRuneIterate(b *testing.B) {
81 bytes := make([]byte, 100)
82 for i := range bytes {
83 bytes[i] = byte('A')
85 s := string(bytes)
86 for i := 0; i < b.N; i++ {
87 for range s {
92 func BenchmarkRuneIterate2(b *testing.B) {
93 bytes := make([]byte, 100)
94 for i := range bytes {
95 bytes[i] = byte('A')
97 s := string(bytes)
98 for i := 0; i < b.N; i++ {
99 for range s {
105 func TestStringW(t *testing.T) {
106 strings := []string{
107 "hello",
108 "a\u5566\u7788b",
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")
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)
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++ {
142 b[i] = 'a'
144 _ = runtime.Gostringnocopy(&b[0])
145 newmax := *runtime.Maxstring
146 if newmax != max+9 {
147 t.Errorf("want %d, got %d", max+9, newmax)
152 func TestCompareTempString(t *testing.T) {
153 s := "foo"
154 b := []byte(s)
155 n := testing.AllocsPerRun(1000, func() {
156 if string(b) != s {
157 t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
159 if string(b) == s {
160 } else {
161 t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
164 if n != 0 {
165 t.Fatalf("want 0 allocs, got %v", n)
169 func TestStringOnStack(t *testing.T) {
170 s := ""
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.
182 s := ""
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.
191 var a [4]string
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) {
202 unknown := '0'
203 n := testing.AllocsPerRun(1000, func() {
204 s1 := string(unknown)
205 s2 := string(unknown + 1)
206 if s1 == s2 {
207 t.Fatalf("bad")
210 if n != 0 {
211 t.Fatalf("want 0 allocs, got %v", n)
215 func TestRangeStringCast(t *testing.T) {
216 s := "abc"
217 n := testing.AllocsPerRun(1000, func() {
218 for i, c := range []byte(s) {
219 if c != s[i] {
220 t.Fatalf("want '%c' at pos %v, got '%c'", s[i], i, c)
224 if n != 0 {
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.
233 s := "foož"
234 b := ([]byte)(s)
235 if cap(b) != 5 {
236 t.Errorf("want cap of 5, got %d", cap(b))
238 r := ([]rune)(s)
239 if cap(r) != 4 {
240 t.Errorf("want cap of 4, got %d", cap(r))