libgo: update to go1.9
[official-gcc.git] / libgo / go / strings / strings_test.go
blob0fddaf0e4a6d01a387915d1227128443d0d015b7
1 // Copyright 2009 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 "bytes"
9 "fmt"
10 "io"
11 "math/rand"
12 "reflect"
13 "runtime"
14 . "strings"
15 "testing"
16 "unicode"
17 "unicode/utf8"
18 "unsafe"
21 func eq(a, b []string) bool {
22 if len(a) != len(b) {
23 return false
25 for i := 0; i < len(a); i++ {
26 if a[i] != b[i] {
27 return false
30 return true
33 var abcd = "abcd"
34 var faces = "☺☻☹"
35 var commas = "1,2,3,4"
36 var dots = "1....2....3....4"
38 type IndexTest struct {
39 s string
40 sep string
41 out int
44 var indexTests = []IndexTest{
45 {"", "", 0},
46 {"", "a", -1},
47 {"", "foo", -1},
48 {"fo", "foo", -1},
49 {"foo", "foo", 0},
50 {"oofofoofooo", "f", 2},
51 {"oofofoofooo", "foo", 4},
52 {"barfoobarfoo", "foo", 3},
53 {"foo", "", 0},
54 {"foo", "o", 1},
55 {"abcABCabc", "A", 3},
56 // cases with one byte strings - test special case in Index()
57 {"", "a", -1},
58 {"x", "a", -1},
59 {"x", "x", 0},
60 {"abc", "a", 0},
61 {"abc", "b", 1},
62 {"abc", "c", 2},
63 {"abc", "x", -1},
64 // test special cases in Index() for short strings
65 {"", "ab", -1},
66 {"bc", "ab", -1},
67 {"ab", "ab", 0},
68 {"xab", "ab", 1},
69 {"xab"[:2], "ab", -1},
70 {"", "abc", -1},
71 {"xbc", "abc", -1},
72 {"abc", "abc", 0},
73 {"xabc", "abc", 1},
74 {"xabc"[:3], "abc", -1},
75 {"xabxc", "abc", -1},
76 {"", "abcd", -1},
77 {"xbcd", "abcd", -1},
78 {"abcd", "abcd", 0},
79 {"xabcd", "abcd", 1},
80 {"xyabcd"[:5], "abcd", -1},
81 {"xbcqq", "abcqq", -1},
82 {"abcqq", "abcqq", 0},
83 {"xabcqq", "abcqq", 1},
84 {"xyabcqq"[:6], "abcqq", -1},
85 {"xabxcqq", "abcqq", -1},
86 {"xabcqxq", "abcqq", -1},
87 {"", "01234567", -1},
88 {"32145678", "01234567", -1},
89 {"01234567", "01234567", 0},
90 {"x01234567", "01234567", 1},
91 {"x0123456x01234567", "01234567", 9},
92 {"xx01234567"[:9], "01234567", -1},
93 {"", "0123456789", -1},
94 {"3214567844", "0123456789", -1},
95 {"0123456789", "0123456789", 0},
96 {"x0123456789", "0123456789", 1},
97 {"x012345678x0123456789", "0123456789", 11},
98 {"xyz0123456789"[:12], "0123456789", -1},
99 {"x01234567x89", "0123456789", -1},
100 {"", "0123456789012345", -1},
101 {"3214567889012345", "0123456789012345", -1},
102 {"0123456789012345", "0123456789012345", 0},
103 {"x0123456789012345", "0123456789012345", 1},
104 {"x012345678901234x0123456789012345", "0123456789012345", 17},
105 {"", "01234567890123456789", -1},
106 {"32145678890123456789", "01234567890123456789", -1},
107 {"01234567890123456789", "01234567890123456789", 0},
108 {"x01234567890123456789", "01234567890123456789", 1},
109 {"x0123456789012345678x01234567890123456789", "01234567890123456789", 21},
110 {"xyz01234567890123456789"[:22], "01234567890123456789", -1},
111 {"", "0123456789012345678901234567890", -1},
112 {"321456788901234567890123456789012345678911", "0123456789012345678901234567890", -1},
113 {"0123456789012345678901234567890", "0123456789012345678901234567890", 0},
114 {"x0123456789012345678901234567890", "0123456789012345678901234567890", 1},
115 {"x012345678901234567890123456789x0123456789012345678901234567890", "0123456789012345678901234567890", 32},
116 {"xyz0123456789012345678901234567890"[:33], "0123456789012345678901234567890", -1},
117 {"", "01234567890123456789012345678901", -1},
118 {"32145678890123456789012345678901234567890211", "01234567890123456789012345678901", -1},
119 {"01234567890123456789012345678901", "01234567890123456789012345678901", 0},
120 {"x01234567890123456789012345678901", "01234567890123456789012345678901", 1},
121 {"x0123456789012345678901234567890x01234567890123456789012345678901", "01234567890123456789012345678901", 33},
122 {"xyz01234567890123456789012345678901"[:34], "01234567890123456789012345678901", -1},
123 {"xxxxxx012345678901234567890123456789012345678901234567890123456789012", "012345678901234567890123456789012345678901234567890123456789012", 6},
124 {"", "0123456789012345678901234567890123456789", -1},
125 {"xx012345678901234567890123456789012345678901234567890123456789012", "0123456789012345678901234567890123456789", 2},
126 {"xx012345678901234567890123456789012345678901234567890123456789012"[:41], "0123456789012345678901234567890123456789", -1},
127 {"xx012345678901234567890123456789012345678901234567890123456789012", "0123456789012345678901234567890123456xxx", -1},
128 {"xx0123456789012345678901234567890123456789012345678901234567890120123456789012345678901234567890123456xxx", "0123456789012345678901234567890123456xxx", 65},
131 var lastIndexTests = []IndexTest{
132 {"", "", 0},
133 {"", "a", -1},
134 {"", "foo", -1},
135 {"fo", "foo", -1},
136 {"foo", "foo", 0},
137 {"foo", "f", 0},
138 {"oofofoofooo", "f", 7},
139 {"oofofoofooo", "foo", 7},
140 {"barfoobarfoo", "foo", 9},
141 {"foo", "", 3},
142 {"foo", "o", 2},
143 {"abcABCabc", "A", 3},
144 {"abcABCabc", "a", 6},
147 var indexAnyTests = []IndexTest{
148 {"", "", -1},
149 {"", "a", -1},
150 {"", "abc", -1},
151 {"a", "", -1},
152 {"a", "a", 0},
153 {"aaa", "a", 0},
154 {"abc", "xyz", -1},
155 {"abc", "xcz", 2},
156 {"ab☺c", "x☺yz", 2},
157 {"a☺b☻c☹d", "cx", len("a☺b☻")},
158 {"a☺b☻c☹d", "uvw☻xyz", len("a☺b")},
159 {"aRegExp*", ".(|)*+?^$[]", 7},
160 {dots + dots + dots, " ", -1},
161 {"012abcba210", "\xffb", 4},
162 {"012\x80bcb\x80210", "\xffb", 3},
165 var lastIndexAnyTests = []IndexTest{
166 {"", "", -1},
167 {"", "a", -1},
168 {"", "abc", -1},
169 {"a", "", -1},
170 {"a", "a", 0},
171 {"aaa", "a", 2},
172 {"abc", "xyz", -1},
173 {"abc", "ab", 1},
174 {"ab☺c", "x☺yz", 2},
175 {"a☺b☻c☹d", "cx", len("a☺b☻")},
176 {"a☺b☻c☹d", "uvw☻xyz", len("a☺b")},
177 {"a.RegExp*", ".(|)*+?^$[]", 8},
178 {dots + dots + dots, " ", -1},
179 {"012abcba210", "\xffb", 6},
180 {"012\x80bcb\x80210", "\xffb", 7},
183 // Execute f on each test case. funcName should be the name of f; it's used
184 // in failure reports.
185 func runIndexTests(t *testing.T, f func(s, sep string) int, funcName string, testCases []IndexTest) {
186 for _, test := range testCases {
187 actual := f(test.s, test.sep)
188 if actual != test.out {
189 t.Errorf("%s(%q,%q) = %v; want %v", funcName, test.s, test.sep, actual, test.out)
194 func TestIndex(t *testing.T) { runIndexTests(t, Index, "Index", indexTests) }
195 func TestLastIndex(t *testing.T) { runIndexTests(t, LastIndex, "LastIndex", lastIndexTests) }
196 func TestIndexAny(t *testing.T) { runIndexTests(t, IndexAny, "IndexAny", indexAnyTests) }
197 func TestLastIndexAny(t *testing.T) { runIndexTests(t, LastIndexAny, "LastIndexAny", lastIndexAnyTests) }
199 func TestLastIndexByte(t *testing.T) {
200 testCases := []IndexTest{
201 {"", "q", -1},
202 {"abcdef", "q", -1},
203 {"abcdefabcdef", "a", len("abcdef")}, // something in the middle
204 {"abcdefabcdef", "f", len("abcdefabcde")}, // last byte
205 {"zabcdefabcdef", "z", 0}, // first byte
206 {"a☺b☻c☹d", "b", len("a☺")}, // non-ascii
208 for _, test := range testCases {
209 actual := LastIndexByte(test.s, test.sep[0])
210 if actual != test.out {
211 t.Errorf("LastIndexByte(%q,%c) = %v; want %v", test.s, test.sep[0], actual, test.out)
216 func simpleIndex(s, sep string) int {
217 n := len(sep)
218 for i := n; i <= len(s); i++ {
219 if s[i-n:i] == sep {
220 return i - n
223 return -1
226 func TestIndexRandom(t *testing.T) {
227 const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
228 for times := 0; times < 10; times++ {
229 for strLen := 5 + rand.Intn(5); strLen < 140; strLen += 10 { // Arbitrary
230 s1 := make([]byte, strLen)
231 for i := range s1 {
232 s1[i] = chars[rand.Intn(len(chars))]
234 s := string(s1)
235 for i := 0; i < 50; i++ {
236 begin := rand.Intn(len(s) + 1)
237 end := begin + rand.Intn(len(s)+1-begin)
238 sep := s[begin:end]
239 if i%4 == 0 {
240 pos := rand.Intn(len(sep) + 1)
241 sep = sep[:pos] + "A" + sep[pos:]
243 want := simpleIndex(s, sep)
244 res := Index(s, sep)
245 if res != want {
246 t.Errorf("Index(%s,%s) = %d; want %d", s, sep, res, want)
253 func TestIndexRune(t *testing.T) {
254 tests := []struct {
255 in string
256 rune rune
257 want int
259 {"", 'a', -1},
260 {"", '☺', -1},
261 {"foo", '☹', -1},
262 {"foo", 'o', 1},
263 {"foo☺bar", '☺', 3},
264 {"foo☺☻☹bar", '☹', 9},
265 {"a A x", 'A', 2},
266 {"some_text=some_value", '=', 9},
267 {"☺a", 'a', 3},
268 {"a☻☺b", '☺', 4},
270 // RuneError should match any invalid UTF-8 byte sequence.
271 {"�", '�', 0},
272 {"\xff", '�', 0},
273 {"☻x�", '�', len("☻x")},
274 {"☻x\xe2\x98", '�', len("☻x")},
275 {"☻x\xe2\x98�", '�', len("☻x")},
276 {"☻x\xe2\x98x", '�', len("☻x")},
278 // Invalid rune values should never match.
279 {"a☺b☻c☹d\xe2\x98\xff\xed\xa0\x80", -1, -1},
280 {"a☺b☻c☹d\xe2\x98\xff\xed\xa0\x80", 0xD800, -1}, // Surrogate pair
281 {"a☺b☻c☹d\xe2\x98\xff\xed\xa0\x80", utf8.MaxRune + 1, -1},
283 for _, tt := range tests {
284 if got := IndexRune(tt.in, tt.rune); got != tt.want {
285 t.Errorf("IndexRune(%q, %d) = %v; want %v", tt.in, tt.rune, got, tt.want)
289 haystack := "test世界"
290 allocs := testing.AllocsPerRun(1000, func() {
291 if i := IndexRune(haystack, 's'); i != 2 {
292 t.Fatalf("'s' at %d; want 2", i)
294 if i := IndexRune(haystack, '世'); i != 4 {
295 t.Fatalf("'世' at %d; want 4", i)
298 if runtime.Compiler == "gccgo" {
299 t.Skip("skipping allocations test for gccgo until escape analysis is enabled")
301 if allocs != 0 && testing.CoverMode() == "" {
302 t.Errorf("expected no allocations, got %f", allocs)
306 const benchmarkString = "some_text=some☺value"
308 func BenchmarkIndexRune(b *testing.B) {
309 if got := IndexRune(benchmarkString, '☺'); got != 14 {
310 b.Fatalf("wrong index: expected 14, got=%d", got)
312 for i := 0; i < b.N; i++ {
313 IndexRune(benchmarkString, '☺')
317 var benchmarkLongString = Repeat(" ", 100) + benchmarkString
319 func BenchmarkIndexRuneLongString(b *testing.B) {
320 if got := IndexRune(benchmarkLongString, '☺'); got != 114 {
321 b.Fatalf("wrong index: expected 114, got=%d", got)
323 for i := 0; i < b.N; i++ {
324 IndexRune(benchmarkLongString, '☺')
328 func BenchmarkIndexRuneFastPath(b *testing.B) {
329 if got := IndexRune(benchmarkString, 'v'); got != 17 {
330 b.Fatalf("wrong index: expected 17, got=%d", got)
332 for i := 0; i < b.N; i++ {
333 IndexRune(benchmarkString, 'v')
337 func BenchmarkIndex(b *testing.B) {
338 if got := Index(benchmarkString, "v"); got != 17 {
339 b.Fatalf("wrong index: expected 17, got=%d", got)
341 for i := 0; i < b.N; i++ {
342 Index(benchmarkString, "v")
346 func BenchmarkLastIndex(b *testing.B) {
347 if got := Index(benchmarkString, "v"); got != 17 {
348 b.Fatalf("wrong index: expected 17, got=%d", got)
350 for i := 0; i < b.N; i++ {
351 LastIndex(benchmarkString, "v")
355 func BenchmarkIndexByte(b *testing.B) {
356 if got := IndexByte(benchmarkString, 'v'); got != 17 {
357 b.Fatalf("wrong index: expected 17, got=%d", got)
359 for i := 0; i < b.N; i++ {
360 IndexByte(benchmarkString, 'v')
364 type SplitTest struct {
365 s string
366 sep string
367 n int
368 a []string
371 var splittests = []SplitTest{
372 {"", "", -1, []string{}},
373 {abcd, "", 2, []string{"a", "bcd"}},
374 {abcd, "", 4, []string{"a", "b", "c", "d"}},
375 {abcd, "", -1, []string{"a", "b", "c", "d"}},
376 {faces, "", -1, []string{"☺", "☻", "☹"}},
377 {faces, "", 3, []string{"☺", "☻", "☹"}},
378 {faces, "", 17, []string{"☺", "☻", "☹"}},
379 {"☺�☹", "", -1, []string{"☺", "�", "☹"}},
380 {abcd, "a", 0, nil},
381 {abcd, "a", -1, []string{"", "bcd"}},
382 {abcd, "z", -1, []string{"abcd"}},
383 {commas, ",", -1, []string{"1", "2", "3", "4"}},
384 {dots, "...", -1, []string{"1", ".2", ".3", ".4"}},
385 {faces, "☹", -1, []string{"☺☻", ""}},
386 {faces, "~", -1, []string{faces}},
387 {"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
388 {"1 2", " ", 3, []string{"1", "2"}},
391 func TestSplit(t *testing.T) {
392 for _, tt := range splittests {
393 a := SplitN(tt.s, tt.sep, tt.n)
394 if !eq(a, tt.a) {
395 t.Errorf("Split(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, a, tt.a)
396 continue
398 if tt.n == 0 {
399 continue
401 s := Join(a, tt.sep)
402 if s != tt.s {
403 t.Errorf("Join(Split(%q, %q, %d), %q) = %q", tt.s, tt.sep, tt.n, tt.sep, s)
405 if tt.n < 0 {
406 b := Split(tt.s, tt.sep)
407 if !reflect.DeepEqual(a, b) {
408 t.Errorf("Split disagrees with SplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
414 var splitaftertests = []SplitTest{
415 {abcd, "a", -1, []string{"a", "bcd"}},
416 {abcd, "z", -1, []string{"abcd"}},
417 {abcd, "", -1, []string{"a", "b", "c", "d"}},
418 {commas, ",", -1, []string{"1,", "2,", "3,", "4"}},
419 {dots, "...", -1, []string{"1...", ".2...", ".3...", ".4"}},
420 {faces, "☹", -1, []string{"☺☻☹", ""}},
421 {faces, "~", -1, []string{faces}},
422 {faces, "", -1, []string{"☺", "☻", "☹"}},
423 {"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}},
424 {"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}},
425 {"1 2", " ", 3, []string{"1 ", "2"}},
426 {"123", "", 2, []string{"1", "23"}},
427 {"123", "", 17, []string{"1", "2", "3"}},
430 func TestSplitAfter(t *testing.T) {
431 for _, tt := range splitaftertests {
432 a := SplitAfterN(tt.s, tt.sep, tt.n)
433 if !eq(a, tt.a) {
434 t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, a, tt.a)
435 continue
437 s := Join(a, "")
438 if s != tt.s {
439 t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
441 if tt.n < 0 {
442 b := SplitAfter(tt.s, tt.sep)
443 if !reflect.DeepEqual(a, b) {
444 t.Errorf("SplitAfter disagrees with SplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
450 type FieldsTest struct {
451 s string
452 a []string
455 var fieldstests = []FieldsTest{
456 {"", []string{}},
457 {" ", []string{}},
458 {" \t ", []string{}},
459 {"\u2000", []string{}},
460 {" abc ", []string{"abc"}},
461 {"1 2 3 4", []string{"1", "2", "3", "4"}},
462 {"1 2 3 4", []string{"1", "2", "3", "4"}},
463 {"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
464 {"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
465 {"\u2000\u2001\u2002", []string{}},
466 {"\n\t\n", []string{"™", "™"}},
467 {"\n\u20001™2\u2000 \u2001 ™", []string{"1™2", "™"}},
468 {"\n1\uFFFD \uFFFD2\u20003\uFFFD4", []string{"1\uFFFD", "\uFFFD2", "3\uFFFD4"}},
469 {"1\xFF\u2000\xFF2\xFF \xFF", []string{"1\xFF", "\xFF2\xFF", "\xFF"}},
470 {faces, []string{faces}},
473 func TestFields(t *testing.T) {
474 for _, tt := range fieldstests {
475 a := Fields(tt.s)
476 if !eq(a, tt.a) {
477 t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a)
478 continue
483 var FieldsFuncTests = []FieldsTest{
484 {"", []string{}},
485 {"XX", []string{}},
486 {"XXhiXXX", []string{"hi"}},
487 {"aXXbXXXcX", []string{"a", "b", "c"}},
490 func TestFieldsFunc(t *testing.T) {
491 for _, tt := range fieldstests {
492 a := FieldsFunc(tt.s, unicode.IsSpace)
493 if !eq(a, tt.a) {
494 t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a)
495 continue
498 pred := func(c rune) bool { return c == 'X' }
499 for _, tt := range FieldsFuncTests {
500 a := FieldsFunc(tt.s, pred)
501 if !eq(a, tt.a) {
502 t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
507 // Test case for any function which accepts and returns a single string.
508 type StringTest struct {
509 in, out string
512 // Execute f on each test case. funcName should be the name of f; it's used
513 // in failure reports.
514 func runStringTests(t *testing.T, f func(string) string, funcName string, testCases []StringTest) {
515 for _, tc := range testCases {
516 actual := f(tc.in)
517 if actual != tc.out {
518 t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out)
523 var upperTests = []StringTest{
524 {"", ""},
525 {"abc", "ABC"},
526 {"AbC123", "ABC123"},
527 {"azAZ09_", "AZAZ09_"},
528 {"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
531 var lowerTests = []StringTest{
532 {"", ""},
533 {"abc", "abc"},
534 {"AbC123", "abc123"},
535 {"azAZ09_", "azaz09_"},
536 {"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
539 const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
541 var trimSpaceTests = []StringTest{
542 {"", ""},
543 {"abc", "abc"},
544 {space + "abc" + space, "abc"},
545 {" ", ""},
546 {" \t\r\n \t\t\r\r\n\n ", ""},
547 {" \t\r\n x\t\t\r\r\n\n ", "x"},
548 {" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
549 {"1 \t\r\n2", "1 \t\r\n2"},
550 {" x\x80", "x\x80"},
551 {" x\xc0", "x\xc0"},
552 {"x \xc0\xc0 ", "x \xc0\xc0"},
553 {"x \xc0", "x \xc0"},
554 {"x \xc0 ", "x \xc0"},
555 {"x \xc0\xc0 ", "x \xc0\xc0"},
556 {"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"},
557 {"x ☺ ", "x ☺"},
560 func tenRunes(ch rune) string {
561 r := make([]rune, 10)
562 for i := range r {
563 r[i] = ch
565 return string(r)
568 // User-defined self-inverse mapping function
569 func rot13(r rune) rune {
570 step := rune(13)
571 if r >= 'a' && r <= 'z' {
572 return ((r - 'a' + step) % 26) + 'a'
574 if r >= 'A' && r <= 'Z' {
575 return ((r - 'A' + step) % 26) + 'A'
577 return r
580 func TestMap(t *testing.T) {
581 // Run a couple of awful growth/shrinkage tests
582 a := tenRunes('a')
583 // 1. Grow. This triggers two reallocations in Map.
584 maxRune := func(rune) rune { return unicode.MaxRune }
585 m := Map(maxRune, a)
586 expect := tenRunes(unicode.MaxRune)
587 if m != expect {
588 t.Errorf("growing: expected %q got %q", expect, m)
591 // 2. Shrink
592 minRune := func(rune) rune { return 'a' }
593 m = Map(minRune, tenRunes(unicode.MaxRune))
594 expect = a
595 if m != expect {
596 t.Errorf("shrinking: expected %q got %q", expect, m)
599 // 3. Rot13
600 m = Map(rot13, "a to zed")
601 expect = "n gb mrq"
602 if m != expect {
603 t.Errorf("rot13: expected %q got %q", expect, m)
606 // 4. Rot13^2
607 m = Map(rot13, Map(rot13, "a to zed"))
608 expect = "a to zed"
609 if m != expect {
610 t.Errorf("rot13: expected %q got %q", expect, m)
613 // 5. Drop
614 dropNotLatin := func(r rune) rune {
615 if unicode.Is(unicode.Latin, r) {
616 return r
618 return -1
620 m = Map(dropNotLatin, "Hello, 세계")
621 expect = "Hello"
622 if m != expect {
623 t.Errorf("drop: expected %q got %q", expect, m)
626 // 6. Identity
627 identity := func(r rune) rune {
628 return r
630 orig := "Input string that we expect not to be copied."
631 m = Map(identity, orig)
632 if (*reflect.StringHeader)(unsafe.Pointer(&orig)).Data !=
633 (*reflect.StringHeader)(unsafe.Pointer(&m)).Data {
634 t.Error("unexpected copy during identity map")
637 // 7. Handle invalid UTF-8 sequence
638 replaceNotLatin := func(r rune) rune {
639 if unicode.Is(unicode.Latin, r) {
640 return r
642 return '?'
644 m = Map(replaceNotLatin, "Hello\255World")
645 expect = "Hello?World"
646 if m != expect {
647 t.Errorf("replace invalid sequence: expected %q got %q", expect, m)
651 func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }
653 func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTests) }
655 func BenchmarkMapNoChanges(b *testing.B) {
656 identity := func(r rune) rune {
657 return r
659 for i := 0; i < b.N; i++ {
660 Map(identity, "Some string that won't be modified.")
664 func TestSpecialCase(t *testing.T) {
665 lower := "abcçdefgğhıijklmnoöprsştuüvyz"
666 upper := "ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ"
667 u := ToUpperSpecial(unicode.TurkishCase, upper)
668 if u != upper {
669 t.Errorf("Upper(upper) is %s not %s", u, upper)
671 u = ToUpperSpecial(unicode.TurkishCase, lower)
672 if u != upper {
673 t.Errorf("Upper(lower) is %s not %s", u, upper)
675 l := ToLowerSpecial(unicode.TurkishCase, lower)
676 if l != lower {
677 t.Errorf("Lower(lower) is %s not %s", l, lower)
679 l = ToLowerSpecial(unicode.TurkishCase, upper)
680 if l != lower {
681 t.Errorf("Lower(upper) is %s not %s", l, lower)
685 func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) }
687 var trimTests = []struct {
688 f string
689 in, arg, out string
691 {"Trim", "abba", "a", "bb"},
692 {"Trim", "abba", "ab", ""},
693 {"TrimLeft", "abba", "ab", ""},
694 {"TrimRight", "abba", "ab", ""},
695 {"TrimLeft", "abba", "a", "bba"},
696 {"TrimRight", "abba", "a", "abb"},
697 {"Trim", "<tag>", "<>", "tag"},
698 {"Trim", "* listitem", " *", "listitem"},
699 {"Trim", `"quote"`, `"`, "quote"},
700 {"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
701 {"Trim", "\x80test\xff", "\xff", "test"},
702 {"Trim", " Ġ ", " ", "Ġ"},
703 {"Trim", " Ġİ0", "0 ", "Ġİ"},
704 //empty string tests
705 {"Trim", "abba", "", "abba"},
706 {"Trim", "", "123", ""},
707 {"Trim", "", "", ""},
708 {"TrimLeft", "abba", "", "abba"},
709 {"TrimLeft", "", "123", ""},
710 {"TrimLeft", "", "", ""},
711 {"TrimRight", "abba", "", "abba"},
712 {"TrimRight", "", "123", ""},
713 {"TrimRight", "", "", ""},
714 {"TrimRight", "☺\xc0", "☺", "☺\xc0"},
715 {"TrimPrefix", "aabb", "a", "abb"},
716 {"TrimPrefix", "aabb", "b", "aabb"},
717 {"TrimSuffix", "aabb", "a", "aabb"},
718 {"TrimSuffix", "aabb", "b", "aab"},
721 func TestTrim(t *testing.T) {
722 for _, tc := range trimTests {
723 name := tc.f
724 var f func(string, string) string
725 switch name {
726 case "Trim":
727 f = Trim
728 case "TrimLeft":
729 f = TrimLeft
730 case "TrimRight":
731 f = TrimRight
732 case "TrimPrefix":
733 f = TrimPrefix
734 case "TrimSuffix":
735 f = TrimSuffix
736 default:
737 t.Errorf("Undefined trim function %s", name)
739 actual := f(tc.in, tc.arg)
740 if actual != tc.out {
741 t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
746 func BenchmarkTrim(b *testing.B) {
747 b.ReportAllocs()
749 for i := 0; i < b.N; i++ {
750 for _, tc := range trimTests {
751 name := tc.f
752 var f func(string, string) string
753 switch name {
754 case "Trim":
755 f = Trim
756 case "TrimLeft":
757 f = TrimLeft
758 case "TrimRight":
759 f = TrimRight
760 case "TrimPrefix":
761 f = TrimPrefix
762 case "TrimSuffix":
763 f = TrimSuffix
764 default:
765 b.Errorf("Undefined trim function %s", name)
767 actual := f(tc.in, tc.arg)
768 if actual != tc.out {
769 b.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
775 type predicate struct {
776 f func(rune) bool
777 name string
780 var isSpace = predicate{unicode.IsSpace, "IsSpace"}
781 var isDigit = predicate{unicode.IsDigit, "IsDigit"}
782 var isUpper = predicate{unicode.IsUpper, "IsUpper"}
783 var isValidRune = predicate{
784 func(r rune) bool {
785 return r != utf8.RuneError
787 "IsValidRune",
790 func not(p predicate) predicate {
791 return predicate{
792 func(r rune) bool {
793 return !p.f(r)
795 "not " + p.name,
799 var trimFuncTests = []struct {
800 f predicate
801 in, out string
803 {isSpace, space + " hello " + space, "hello"},
804 {isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
805 {isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
806 {not(isSpace), "hello" + space + "hello", space},
807 {not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"},
808 {isValidRune, "ab\xc0a\xc0cd", "\xc0a\xc0"},
809 {not(isValidRune), "\xc0a\xc0", "a"},
812 func TestTrimFunc(t *testing.T) {
813 for _, tc := range trimFuncTests {
814 actual := TrimFunc(tc.in, tc.f.f)
815 if actual != tc.out {
816 t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.f.name, actual, tc.out)
821 var indexFuncTests = []struct {
822 in string
823 f predicate
824 first, last int
826 {"", isValidRune, -1, -1},
827 {"abc", isDigit, -1, -1},
828 {"0123", isDigit, 0, 3},
829 {"a1b", isDigit, 1, 1},
830 {space, isSpace, 0, len(space) - 3}, // last rune in space is 3 bytes
831 {"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit, 0, 18},
832 {"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34},
833 {"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12},
835 // tests of invalid UTF-8
836 {"\x801", isDigit, 1, 1},
837 {"\x80abc", isDigit, -1, -1},
838 {"\xc0a\xc0", isValidRune, 1, 1},
839 {"\xc0a\xc0", not(isValidRune), 0, 2},
840 {"\xc0\xc0", not(isValidRune), 0, 4},
841 {"\xc0\xc0\xc0", not(isValidRune), 0, 5},
842 {"ab\xc0a\xc0cd", not(isValidRune), 2, 4},
843 {"a\xe0\x80cd", not(isValidRune), 1, 2},
844 {"\x80\x80\x80\x80", not(isValidRune), 0, 3},
847 func TestIndexFunc(t *testing.T) {
848 for _, tc := range indexFuncTests {
849 first := IndexFunc(tc.in, tc.f.f)
850 if first != tc.first {
851 t.Errorf("IndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, first, tc.first)
853 last := LastIndexFunc(tc.in, tc.f.f)
854 if last != tc.last {
855 t.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, last, tc.last)
860 func equal(m string, s1, s2 string, t *testing.T) bool {
861 if s1 == s2 {
862 return true
864 e1 := Split(s1, "")
865 e2 := Split(s2, "")
866 for i, c1 := range e1 {
867 if i >= len(e2) {
868 break
870 r1, _ := utf8.DecodeRuneInString(c1)
871 r2, _ := utf8.DecodeRuneInString(e2[i])
872 if r1 != r2 {
873 t.Errorf("%s diff at %d: U+%04X U+%04X", m, i, r1, r2)
876 return false
879 func TestCaseConsistency(t *testing.T) {
880 // Make a string of all the runes.
881 numRunes := int(unicode.MaxRune + 1)
882 if testing.Short() {
883 numRunes = 1000
885 a := make([]rune, numRunes)
886 for i := range a {
887 a[i] = rune(i)
889 s := string(a)
890 // convert the cases.
891 upper := ToUpper(s)
892 lower := ToLower(s)
894 // Consistency checks
895 if n := utf8.RuneCountInString(upper); n != numRunes {
896 t.Error("rune count wrong in upper:", n)
898 if n := utf8.RuneCountInString(lower); n != numRunes {
899 t.Error("rune count wrong in lower:", n)
901 if !equal("ToUpper(upper)", ToUpper(upper), upper, t) {
902 t.Error("ToUpper(upper) consistency fail")
904 if !equal("ToLower(lower)", ToLower(lower), lower, t) {
905 t.Error("ToLower(lower) consistency fail")
908 These fail because of non-one-to-oneness of the data, such as multiple
909 upper case 'I' mapping to 'i'. We comment them out but keep them for
910 interest.
911 For instance: CAPITAL LETTER I WITH DOT ABOVE:
912 unicode.ToUpper(unicode.ToLower('\u0130')) != '\u0130'
914 if !equal("ToUpper(lower)", ToUpper(lower), upper, t) {
915 t.Error("ToUpper(lower) consistency fail");
917 if !equal("ToLower(upper)", ToLower(upper), lower, t) {
918 t.Error("ToLower(upper) consistency fail");
923 var RepeatTests = []struct {
924 in, out string
925 count int
927 {"", "", 0},
928 {"", "", 1},
929 {"", "", 2},
930 {"-", "", 0},
931 {"-", "-", 1},
932 {"-", "----------", 10},
933 {"abc ", "abc abc abc ", 3},
936 func TestRepeat(t *testing.T) {
937 for _, tt := range RepeatTests {
938 a := Repeat(tt.in, tt.count)
939 if !equal("Repeat(s)", a, tt.out, t) {
940 t.Errorf("Repeat(%v, %d) = %v; want %v", tt.in, tt.count, a, tt.out)
941 continue
946 func repeat(s string, count int) (err error) {
947 defer func() {
948 if r := recover(); r != nil {
949 switch v := r.(type) {
950 case error:
951 err = v
952 default:
953 err = fmt.Errorf("%s", v)
958 Repeat(s, count)
960 return
963 // See Issue golang.org/issue/16237
964 func TestRepeatCatchesOverflow(t *testing.T) {
965 tests := [...]struct {
966 s string
967 count int
968 errStr string
970 0: {"--", -2147483647, "negative"},
971 1: {"", int(^uint(0) >> 1), ""},
972 2: {"-", 10, ""},
973 3: {"gopher", 0, ""},
974 4: {"-", -1, "negative"},
975 5: {"--", -102, "negative"},
976 6: {string(make([]byte, 255)), int((^uint(0))/255 + 1), "overflow"},
979 for i, tt := range tests {
980 err := repeat(tt.s, tt.count)
981 if tt.errStr == "" {
982 if err != nil {
983 t.Errorf("#%d panicked %v", i, err)
985 continue
988 if err == nil || !Contains(err.Error(), tt.errStr) {
989 t.Errorf("#%d expected %q got %q", i, tt.errStr, err)
994 func runesEqual(a, b []rune) bool {
995 if len(a) != len(b) {
996 return false
998 for i, r := range a {
999 if r != b[i] {
1000 return false
1003 return true
1006 var RunesTests = []struct {
1007 in string
1008 out []rune
1009 lossy bool
1011 {"", []rune{}, false},
1012 {" ", []rune{32}, false},
1013 {"ABC", []rune{65, 66, 67}, false},
1014 {"abc", []rune{97, 98, 99}, false},
1015 {"\u65e5\u672c\u8a9e", []rune{26085, 26412, 35486}, false},
1016 {"ab\x80c", []rune{97, 98, 0xFFFD, 99}, true},
1017 {"ab\xc0c", []rune{97, 98, 0xFFFD, 99}, true},
1020 func TestRunes(t *testing.T) {
1021 for _, tt := range RunesTests {
1022 a := []rune(tt.in)
1023 if !runesEqual(a, tt.out) {
1024 t.Errorf("[]rune(%q) = %v; want %v", tt.in, a, tt.out)
1025 continue
1027 if !tt.lossy {
1028 // can only test reassembly if we didn't lose information
1029 s := string(a)
1030 if s != tt.in {
1031 t.Errorf("string([]rune(%q)) = %x; want %x", tt.in, s, tt.in)
1037 func TestReadByte(t *testing.T) {
1038 testStrings := []string{"", abcd, faces, commas}
1039 for _, s := range testStrings {
1040 reader := NewReader(s)
1041 if e := reader.UnreadByte(); e == nil {
1042 t.Errorf("Unreading %q at beginning: expected error", s)
1044 var res bytes.Buffer
1045 for {
1046 b, e := reader.ReadByte()
1047 if e == io.EOF {
1048 break
1050 if e != nil {
1051 t.Errorf("Reading %q: %s", s, e)
1052 break
1054 res.WriteByte(b)
1055 // unread and read again
1056 e = reader.UnreadByte()
1057 if e != nil {
1058 t.Errorf("Unreading %q: %s", s, e)
1059 break
1061 b1, e := reader.ReadByte()
1062 if e != nil {
1063 t.Errorf("Reading %q after unreading: %s", s, e)
1064 break
1066 if b1 != b {
1067 t.Errorf("Reading %q after unreading: want byte %q, got %q", s, b, b1)
1068 break
1071 if res.String() != s {
1072 t.Errorf("Reader(%q).ReadByte() produced %q", s, res.String())
1077 func TestReadRune(t *testing.T) {
1078 testStrings := []string{"", abcd, faces, commas}
1079 for _, s := range testStrings {
1080 reader := NewReader(s)
1081 if e := reader.UnreadRune(); e == nil {
1082 t.Errorf("Unreading %q at beginning: expected error", s)
1084 res := ""
1085 for {
1086 r, z, e := reader.ReadRune()
1087 if e == io.EOF {
1088 break
1090 if e != nil {
1091 t.Errorf("Reading %q: %s", s, e)
1092 break
1094 res += string(r)
1095 // unread and read again
1096 e = reader.UnreadRune()
1097 if e != nil {
1098 t.Errorf("Unreading %q: %s", s, e)
1099 break
1101 r1, z1, e := reader.ReadRune()
1102 if e != nil {
1103 t.Errorf("Reading %q after unreading: %s", s, e)
1104 break
1106 if r1 != r {
1107 t.Errorf("Reading %q after unreading: want rune %q, got %q", s, r, r1)
1108 break
1110 if z1 != z {
1111 t.Errorf("Reading %q after unreading: want size %d, got %d", s, z, z1)
1112 break
1115 if res != s {
1116 t.Errorf("Reader(%q).ReadRune() produced %q", s, res)
1121 var UnreadRuneErrorTests = []struct {
1122 name string
1123 f func(*Reader)
1125 {"Read", func(r *Reader) { r.Read([]byte{0}) }},
1126 {"ReadByte", func(r *Reader) { r.ReadByte() }},
1127 {"UnreadRune", func(r *Reader) { r.UnreadRune() }},
1128 {"Seek", func(r *Reader) { r.Seek(0, io.SeekCurrent) }},
1129 {"WriteTo", func(r *Reader) { r.WriteTo(&bytes.Buffer{}) }},
1132 func TestUnreadRuneError(t *testing.T) {
1133 for _, tt := range UnreadRuneErrorTests {
1134 reader := NewReader("0123456789")
1135 if _, _, err := reader.ReadRune(); err != nil {
1136 // should not happen
1137 t.Fatal(err)
1139 tt.f(reader)
1140 err := reader.UnreadRune()
1141 if err == nil {
1142 t.Errorf("Unreading after %s: expected error", tt.name)
1147 var ReplaceTests = []struct {
1148 in string
1149 old, new string
1150 n int
1151 out string
1153 {"hello", "l", "L", 0, "hello"},
1154 {"hello", "l", "L", -1, "heLLo"},
1155 {"hello", "x", "X", -1, "hello"},
1156 {"", "x", "X", -1, ""},
1157 {"radar", "r", "<r>", -1, "<r>ada<r>"},
1158 {"", "", "<>", -1, "<>"},
1159 {"banana", "a", "<>", -1, "b<>n<>n<>"},
1160 {"banana", "a", "<>", 1, "b<>nana"},
1161 {"banana", "a", "<>", 1000, "b<>n<>n<>"},
1162 {"banana", "an", "<>", -1, "b<><>a"},
1163 {"banana", "ana", "<>", -1, "b<>na"},
1164 {"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"},
1165 {"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
1166 {"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
1167 {"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
1168 {"banana", "", "<>", 1, "<>banana"},
1169 {"banana", "a", "a", -1, "banana"},
1170 {"banana", "a", "a", 1, "banana"},
1171 {"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"},
1174 func TestReplace(t *testing.T) {
1175 for _, tt := range ReplaceTests {
1176 if s := Replace(tt.in, tt.old, tt.new, tt.n); s != tt.out {
1177 t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
1182 var TitleTests = []struct {
1183 in, out string
1185 {"", ""},
1186 {"a", "A"},
1187 {" aaa aaa aaa ", " Aaa Aaa Aaa "},
1188 {" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
1189 {"123a456", "123a456"},
1190 {"double-blind", "Double-Blind"},
1191 {"ÿøû", "Ÿøû"},
1192 {"with_underscore", "With_underscore"},
1193 {"unicode \xe2\x80\xa8 line separator", "Unicode \xe2\x80\xa8 Line Separator"},
1196 func TestTitle(t *testing.T) {
1197 for _, tt := range TitleTests {
1198 if s := Title(tt.in); s != tt.out {
1199 t.Errorf("Title(%q) = %q, want %q", tt.in, s, tt.out)
1204 var ContainsTests = []struct {
1205 str, substr string
1206 expected bool
1208 {"abc", "bc", true},
1209 {"abc", "bcd", false},
1210 {"abc", "", true},
1211 {"", "a", false},
1213 // cases to cover code in runtime/asm_amd64.s:indexShortStr
1214 // 2-byte needle
1215 {"xxxxxx", "01", false},
1216 {"01xxxx", "01", true},
1217 {"xx01xx", "01", true},
1218 {"xxxx01", "01", true},
1219 {"01xxxxx"[1:], "01", false},
1220 {"xxxxx01"[:6], "01", false},
1221 // 3-byte needle
1222 {"xxxxxxx", "012", false},
1223 {"012xxxx", "012", true},
1224 {"xx012xx", "012", true},
1225 {"xxxx012", "012", true},
1226 {"012xxxxx"[1:], "012", false},
1227 {"xxxxx012"[:7], "012", false},
1228 // 4-byte needle
1229 {"xxxxxxxx", "0123", false},
1230 {"0123xxxx", "0123", true},
1231 {"xx0123xx", "0123", true},
1232 {"xxxx0123", "0123", true},
1233 {"0123xxxxx"[1:], "0123", false},
1234 {"xxxxx0123"[:8], "0123", false},
1235 // 5-7-byte needle
1236 {"xxxxxxxxx", "01234", false},
1237 {"01234xxxx", "01234", true},
1238 {"xx01234xx", "01234", true},
1239 {"xxxx01234", "01234", true},
1240 {"01234xxxxx"[1:], "01234", false},
1241 {"xxxxx01234"[:9], "01234", false},
1242 // 8-byte needle
1243 {"xxxxxxxxxxxx", "01234567", false},
1244 {"01234567xxxx", "01234567", true},
1245 {"xx01234567xx", "01234567", true},
1246 {"xxxx01234567", "01234567", true},
1247 {"01234567xxxxx"[1:], "01234567", false},
1248 {"xxxxx01234567"[:12], "01234567", false},
1249 // 9-15-byte needle
1250 {"xxxxxxxxxxxxx", "012345678", false},
1251 {"012345678xxxx", "012345678", true},
1252 {"xx012345678xx", "012345678", true},
1253 {"xxxx012345678", "012345678", true},
1254 {"012345678xxxxx"[1:], "012345678", false},
1255 {"xxxxx012345678"[:13], "012345678", false},
1256 // 16-byte needle
1257 {"xxxxxxxxxxxxxxxxxxxx", "0123456789ABCDEF", false},
1258 {"0123456789ABCDEFxxxx", "0123456789ABCDEF", true},
1259 {"xx0123456789ABCDEFxx", "0123456789ABCDEF", true},
1260 {"xxxx0123456789ABCDEF", "0123456789ABCDEF", true},
1261 {"0123456789ABCDEFxxxxx"[1:], "0123456789ABCDEF", false},
1262 {"xxxxx0123456789ABCDEF"[:20], "0123456789ABCDEF", false},
1263 // 17-31-byte needle
1264 {"xxxxxxxxxxxxxxxxxxxxx", "0123456789ABCDEFG", false},
1265 {"0123456789ABCDEFGxxxx", "0123456789ABCDEFG", true},
1266 {"xx0123456789ABCDEFGxx", "0123456789ABCDEFG", true},
1267 {"xxxx0123456789ABCDEFG", "0123456789ABCDEFG", true},
1268 {"0123456789ABCDEFGxxxxx"[1:], "0123456789ABCDEFG", false},
1269 {"xxxxx0123456789ABCDEFG"[:21], "0123456789ABCDEFG", false},
1271 // partial match cases
1272 {"xx01x", "012", false}, // 3
1273 {"xx0123x", "01234", false}, // 5-7
1274 {"xx01234567x", "012345678", false}, // 9-15
1275 {"xx0123456789ABCDEFx", "0123456789ABCDEFG", false}, // 17-31, issue 15679
1278 func TestContains(t *testing.T) {
1279 for _, ct := range ContainsTests {
1280 if Contains(ct.str, ct.substr) != ct.expected {
1281 t.Errorf("Contains(%s, %s) = %v, want %v",
1282 ct.str, ct.substr, !ct.expected, ct.expected)
1287 var ContainsAnyTests = []struct {
1288 str, substr string
1289 expected bool
1291 {"", "", false},
1292 {"", "a", false},
1293 {"", "abc", false},
1294 {"a", "", false},
1295 {"a", "a", true},
1296 {"aaa", "a", true},
1297 {"abc", "xyz", false},
1298 {"abc", "xcz", true},
1299 {"a☺b☻c☹d", "uvw☻xyz", true},
1300 {"aRegExp*", ".(|)*+?^$[]", true},
1301 {dots + dots + dots, " ", false},
1304 func TestContainsAny(t *testing.T) {
1305 for _, ct := range ContainsAnyTests {
1306 if ContainsAny(ct.str, ct.substr) != ct.expected {
1307 t.Errorf("ContainsAny(%s, %s) = %v, want %v",
1308 ct.str, ct.substr, !ct.expected, ct.expected)
1313 var ContainsRuneTests = []struct {
1314 str string
1315 r rune
1316 expected bool
1318 {"", 'a', false},
1319 {"a", 'a', true},
1320 {"aaa", 'a', true},
1321 {"abc", 'y', false},
1322 {"abc", 'c', true},
1323 {"a☺b☻c☹d", 'x', false},
1324 {"a☺b☻c☹d", '☻', true},
1325 {"aRegExp*", '*', true},
1328 func TestContainsRune(t *testing.T) {
1329 for _, ct := range ContainsRuneTests {
1330 if ContainsRune(ct.str, ct.r) != ct.expected {
1331 t.Errorf("ContainsRune(%q, %q) = %v, want %v",
1332 ct.str, ct.r, !ct.expected, ct.expected)
1337 var EqualFoldTests = []struct {
1338 s, t string
1339 out bool
1341 {"abc", "abc", true},
1342 {"ABcd", "ABcd", true},
1343 {"123abc", "123ABC", true},
1344 {"αβδ", "ΑΒΔ", true},
1345 {"abc", "xyz", false},
1346 {"abc", "XYZ", false},
1347 {"abcdefghijk", "abcdefghijX", false},
1348 {"abcdefghijk", "abcdefghij\u212A", true},
1349 {"abcdefghijK", "abcdefghij\u212A", true},
1350 {"abcdefghijkz", "abcdefghij\u212Ay", false},
1351 {"abcdefghijKz", "abcdefghij\u212Ay", false},
1354 func TestEqualFold(t *testing.T) {
1355 for _, tt := range EqualFoldTests {
1356 if out := EqualFold(tt.s, tt.t); out != tt.out {
1357 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.s, tt.t, out, tt.out)
1359 if out := EqualFold(tt.t, tt.s); out != tt.out {
1360 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.t, tt.s, out, tt.out)
1365 var CountTests = []struct {
1366 s, sep string
1367 num int
1369 {"", "", 1},
1370 {"", "notempty", 0},
1371 {"notempty", "", 9},
1372 {"smaller", "not smaller", 0},
1373 {"12345678987654321", "6", 2},
1374 {"611161116", "6", 3},
1375 {"notequal", "NotEqual", 0},
1376 {"equal", "equal", 1},
1377 {"abc1231231123q", "123", 3},
1378 {"11111", "11", 2},
1381 func TestCount(t *testing.T) {
1382 for _, tt := range CountTests {
1383 if num := Count(tt.s, tt.sep); num != tt.num {
1384 t.Errorf("Count(\"%s\", \"%s\") = %d, want %d", tt.s, tt.sep, num, tt.num)
1389 func makeBenchInputHard() string {
1390 tokens := [...]string{
1391 "<a>", "<p>", "<b>", "<strong>",
1392 "</a>", "</p>", "</b>", "</strong>",
1393 "hello", "world",
1395 x := make([]byte, 0, 1<<20)
1396 for {
1397 i := rand.Intn(len(tokens))
1398 if len(x)+len(tokens[i]) >= 1<<20 {
1399 break
1401 x = append(x, tokens[i]...)
1403 return string(x)
1406 var benchInputHard = makeBenchInputHard()
1408 func benchmarkIndexHard(b *testing.B, sep string) {
1409 for i := 0; i < b.N; i++ {
1410 Index(benchInputHard, sep)
1414 func benchmarkLastIndexHard(b *testing.B, sep string) {
1415 for i := 0; i < b.N; i++ {
1416 LastIndex(benchInputHard, sep)
1420 func benchmarkCountHard(b *testing.B, sep string) {
1421 for i := 0; i < b.N; i++ {
1422 Count(benchInputHard, sep)
1426 func BenchmarkIndexHard1(b *testing.B) { benchmarkIndexHard(b, "<>") }
1427 func BenchmarkIndexHard2(b *testing.B) { benchmarkIndexHard(b, "</pre>") }
1428 func BenchmarkIndexHard3(b *testing.B) { benchmarkIndexHard(b, "<b>hello world</b>") }
1429 func BenchmarkIndexHard4(b *testing.B) {
1430 benchmarkIndexHard(b, "<pre><b>hello</b><strong>world</strong></pre>")
1433 func BenchmarkLastIndexHard1(b *testing.B) { benchmarkLastIndexHard(b, "<>") }
1434 func BenchmarkLastIndexHard2(b *testing.B) { benchmarkLastIndexHard(b, "</pre>") }
1435 func BenchmarkLastIndexHard3(b *testing.B) { benchmarkLastIndexHard(b, "<b>hello world</b>") }
1437 func BenchmarkCountHard1(b *testing.B) { benchmarkCountHard(b, "<>") }
1438 func BenchmarkCountHard2(b *testing.B) { benchmarkCountHard(b, "</pre>") }
1439 func BenchmarkCountHard3(b *testing.B) { benchmarkCountHard(b, "<b>hello world</b>") }
1441 var benchInputTorture = Repeat("ABC", 1<<10) + "123" + Repeat("ABC", 1<<10)
1442 var benchNeedleTorture = Repeat("ABC", 1<<10+1)
1444 func BenchmarkIndexTorture(b *testing.B) {
1445 for i := 0; i < b.N; i++ {
1446 Index(benchInputTorture, benchNeedleTorture)
1450 func BenchmarkCountTorture(b *testing.B) {
1451 for i := 0; i < b.N; i++ {
1452 Count(benchInputTorture, benchNeedleTorture)
1456 func BenchmarkCountTortureOverlapping(b *testing.B) {
1457 A := Repeat("ABC", 1<<20)
1458 B := Repeat("ABC", 1<<10)
1459 for i := 0; i < b.N; i++ {
1460 Count(A, B)
1464 func BenchmarkCountByte(b *testing.B) {
1465 indexSizes := []int{10, 32, 4 << 10, 4 << 20, 64 << 20}
1466 benchStr := Repeat(benchmarkString,
1467 (indexSizes[len(indexSizes)-1]+len(benchmarkString)-1)/len(benchmarkString))
1468 benchFunc := func(b *testing.B, benchStr string) {
1469 b.SetBytes(int64(len(benchStr)))
1470 for i := 0; i < b.N; i++ {
1471 Count(benchStr, "=")
1474 for _, size := range indexSizes {
1475 b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
1476 benchFunc(b, benchStr[:size])
1482 var makeFieldsInput = func() string {
1483 x := make([]byte, 1<<20)
1484 // Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
1485 for i := range x {
1486 switch rand.Intn(10) {
1487 case 0:
1488 x[i] = ' '
1489 case 1:
1490 if i > 0 && x[i-1] == 'x' {
1491 copy(x[i-1:], "χ")
1492 break
1494 fallthrough
1495 default:
1496 x[i] = 'x'
1499 return string(x)
1502 var makeFieldsInputASCII = func() string {
1503 x := make([]byte, 1<<20)
1504 // Input is ~10% space, rest ASCII non-space.
1505 for i := range x {
1506 if rand.Intn(10) == 0 {
1507 x[i] = ' '
1508 } else {
1509 x[i] = 'x'
1512 return string(x)
1515 var stringdata = []struct{ name, data string }{
1516 {"ASCII", makeFieldsInputASCII()},
1517 {"Mixed", makeFieldsInput()},
1520 func BenchmarkFields(b *testing.B) {
1521 for _, sd := range stringdata {
1522 b.Run(sd.name, func(b *testing.B) {
1523 for j := 1 << 4; j <= 1<<20; j <<= 4 {
1524 b.Run(fmt.Sprintf("%d", j), func(b *testing.B) {
1525 b.ReportAllocs()
1526 b.SetBytes(int64(j))
1527 data := sd.data[:j]
1528 for i := 0; i < b.N; i++ {
1529 Fields(data)
1537 func BenchmarkFieldsFunc(b *testing.B) {
1538 for _, sd := range stringdata {
1539 b.Run(sd.name, func(b *testing.B) {
1540 for j := 1 << 4; j <= 1<<20; j <<= 4 {
1541 b.Run(fmt.Sprintf("%d", j), func(b *testing.B) {
1542 b.ReportAllocs()
1543 b.SetBytes(int64(j))
1544 data := sd.data[:j]
1545 for i := 0; i < b.N; i++ {
1546 FieldsFunc(data, unicode.IsSpace)
1554 func BenchmarkSplitEmptySeparator(b *testing.B) {
1555 for i := 0; i < b.N; i++ {
1556 Split(benchInputHard, "")
1560 func BenchmarkSplitSingleByteSeparator(b *testing.B) {
1561 for i := 0; i < b.N; i++ {
1562 Split(benchInputHard, "/")
1566 func BenchmarkSplitMultiByteSeparator(b *testing.B) {
1567 for i := 0; i < b.N; i++ {
1568 Split(benchInputHard, "hello")
1572 func BenchmarkSplitNSingleByteSeparator(b *testing.B) {
1573 for i := 0; i < b.N; i++ {
1574 SplitN(benchInputHard, "/", 10)
1578 func BenchmarkSplitNMultiByteSeparator(b *testing.B) {
1579 for i := 0; i < b.N; i++ {
1580 SplitN(benchInputHard, "hello", 10)
1584 func BenchmarkRepeat(b *testing.B) {
1585 for i := 0; i < b.N; i++ {
1586 Repeat("-", 80)
1590 func BenchmarkIndexAnyASCII(b *testing.B) {
1591 x := Repeat("#", 4096) // Never matches set
1592 cs := "0123456789abcdef"
1593 for k := 1; k <= 4096; k <<= 4 {
1594 for j := 1; j <= 16; j <<= 1 {
1595 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
1596 for i := 0; i < b.N; i++ {
1597 IndexAny(x[:k], cs[:j])
1604 func BenchmarkTrimASCII(b *testing.B) {
1605 cs := "0123456789abcdef"
1606 for k := 1; k <= 4096; k <<= 4 {
1607 for j := 1; j <= 16; j <<= 1 {
1608 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
1609 x := Repeat(cs[:j], k) // Always matches set
1610 for i := 0; i < b.N; i++ {
1611 Trim(x[:k], cs[:j])