libgo: update to Go 1.10.3 release
[official-gcc.git] / libgo / go / strings / strings_test.go
blob92122dbf9b22eb1cbb55a155ee814ea3dd412b2d
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},
129 // test fallback to Rabin-Karp.
130 {"oxoxoxoxoxoxoxoxoxoxoxoy", "oy", 22},
131 {"oxoxoxoxoxoxoxoxoxoxoxox", "oy", -1},
134 var lastIndexTests = []IndexTest{
135 {"", "", 0},
136 {"", "a", -1},
137 {"", "foo", -1},
138 {"fo", "foo", -1},
139 {"foo", "foo", 0},
140 {"foo", "f", 0},
141 {"oofofoofooo", "f", 7},
142 {"oofofoofooo", "foo", 7},
143 {"barfoobarfoo", "foo", 9},
144 {"foo", "", 3},
145 {"foo", "o", 2},
146 {"abcABCabc", "A", 3},
147 {"abcABCabc", "a", 6},
150 var indexAnyTests = []IndexTest{
151 {"", "", -1},
152 {"", "a", -1},
153 {"", "abc", -1},
154 {"a", "", -1},
155 {"a", "a", 0},
156 {"aaa", "a", 0},
157 {"abc", "xyz", -1},
158 {"abc", "xcz", 2},
159 {"ab☺c", "x☺yz", 2},
160 {"a☺b☻c☹d", "cx", len("a☺b☻")},
161 {"a☺b☻c☹d", "uvw☻xyz", len("a☺b")},
162 {"aRegExp*", ".(|)*+?^$[]", 7},
163 {dots + dots + dots, " ", -1},
164 {"012abcba210", "\xffb", 4},
165 {"012\x80bcb\x80210", "\xffb", 3},
168 var lastIndexAnyTests = []IndexTest{
169 {"", "", -1},
170 {"", "a", -1},
171 {"", "abc", -1},
172 {"a", "", -1},
173 {"a", "a", 0},
174 {"aaa", "a", 2},
175 {"abc", "xyz", -1},
176 {"abc", "ab", 1},
177 {"ab☺c", "x☺yz", 2},
178 {"a☺b☻c☹d", "cx", len("a☺b☻")},
179 {"a☺b☻c☹d", "uvw☻xyz", len("a☺b")},
180 {"a.RegExp*", ".(|)*+?^$[]", 8},
181 {dots + dots + dots, " ", -1},
182 {"012abcba210", "\xffb", 6},
183 {"012\x80bcb\x80210", "\xffb", 7},
186 // Execute f on each test case. funcName should be the name of f; it's used
187 // in failure reports.
188 func runIndexTests(t *testing.T, f func(s, sep string) int, funcName string, testCases []IndexTest) {
189 for _, test := range testCases {
190 actual := f(test.s, test.sep)
191 if actual != test.out {
192 t.Errorf("%s(%q,%q) = %v; want %v", funcName, test.s, test.sep, actual, test.out)
197 func TestIndex(t *testing.T) { runIndexTests(t, Index, "Index", indexTests) }
198 func TestLastIndex(t *testing.T) { runIndexTests(t, LastIndex, "LastIndex", lastIndexTests) }
199 func TestIndexAny(t *testing.T) { runIndexTests(t, IndexAny, "IndexAny", indexAnyTests) }
200 func TestLastIndexAny(t *testing.T) { runIndexTests(t, LastIndexAny, "LastIndexAny", lastIndexAnyTests) }
202 func TestLastIndexByte(t *testing.T) {
203 testCases := []IndexTest{
204 {"", "q", -1},
205 {"abcdef", "q", -1},
206 {"abcdefabcdef", "a", len("abcdef")}, // something in the middle
207 {"abcdefabcdef", "f", len("abcdefabcde")}, // last byte
208 {"zabcdefabcdef", "z", 0}, // first byte
209 {"a☺b☻c☹d", "b", len("a☺")}, // non-ascii
211 for _, test := range testCases {
212 actual := LastIndexByte(test.s, test.sep[0])
213 if actual != test.out {
214 t.Errorf("LastIndexByte(%q,%c) = %v; want %v", test.s, test.sep[0], actual, test.out)
219 func simpleIndex(s, sep string) int {
220 n := len(sep)
221 for i := n; i <= len(s); i++ {
222 if s[i-n:i] == sep {
223 return i - n
226 return -1
229 func TestIndexRandom(t *testing.T) {
230 const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
231 for times := 0; times < 10; times++ {
232 for strLen := 5 + rand.Intn(5); strLen < 140; strLen += 10 { // Arbitrary
233 s1 := make([]byte, strLen)
234 for i := range s1 {
235 s1[i] = chars[rand.Intn(len(chars))]
237 s := string(s1)
238 for i := 0; i < 50; i++ {
239 begin := rand.Intn(len(s) + 1)
240 end := begin + rand.Intn(len(s)+1-begin)
241 sep := s[begin:end]
242 if i%4 == 0 {
243 pos := rand.Intn(len(sep) + 1)
244 sep = sep[:pos] + "A" + sep[pos:]
246 want := simpleIndex(s, sep)
247 res := Index(s, sep)
248 if res != want {
249 t.Errorf("Index(%s,%s) = %d; want %d", s, sep, res, want)
256 func TestIndexRune(t *testing.T) {
257 tests := []struct {
258 in string
259 rune rune
260 want int
262 {"", 'a', -1},
263 {"", '☺', -1},
264 {"foo", '☹', -1},
265 {"foo", 'o', 1},
266 {"foo☺bar", '☺', 3},
267 {"foo☺☻☹bar", '☹', 9},
268 {"a A x", 'A', 2},
269 {"some_text=some_value", '=', 9},
270 {"☺a", 'a', 3},
271 {"a☻☺b", '☺', 4},
273 // RuneError should match any invalid UTF-8 byte sequence.
274 {"�", '�', 0},
275 {"\xff", '�', 0},
276 {"☻x�", '�', len("☻x")},
277 {"☻x\xe2\x98", '�', len("☻x")},
278 {"☻x\xe2\x98�", '�', len("☻x")},
279 {"☻x\xe2\x98x", '�', len("☻x")},
281 // Invalid rune values should never match.
282 {"a☺b☻c☹d\xe2\x98\xff\xed\xa0\x80", -1, -1},
283 {"a☺b☻c☹d\xe2\x98\xff\xed\xa0\x80", 0xD800, -1}, // Surrogate pair
284 {"a☺b☻c☹d\xe2\x98\xff\xed\xa0\x80", utf8.MaxRune + 1, -1},
286 for _, tt := range tests {
287 if got := IndexRune(tt.in, tt.rune); got != tt.want {
288 t.Errorf("IndexRune(%q, %d) = %v; want %v", tt.in, tt.rune, got, tt.want)
292 haystack := "test世界"
293 allocs := testing.AllocsPerRun(1000, func() {
294 if i := IndexRune(haystack, 's'); i != 2 {
295 t.Fatalf("'s' at %d; want 2", i)
297 if i := IndexRune(haystack, '世'); i != 4 {
298 t.Fatalf("'世' at %d; want 4", i)
301 if runtime.Compiler == "gccgo" {
302 t.Skip("skipping allocations test for gccgo until escape analysis is enabled")
304 if allocs != 0 && testing.CoverMode() == "" {
305 t.Errorf("expected no allocations, got %f", allocs)
309 const benchmarkString = "some_text=some☺value"
311 func BenchmarkIndexRune(b *testing.B) {
312 if got := IndexRune(benchmarkString, '☺'); got != 14 {
313 b.Fatalf("wrong index: expected 14, got=%d", got)
315 for i := 0; i < b.N; i++ {
316 IndexRune(benchmarkString, '☺')
320 var benchmarkLongString = Repeat(" ", 100) + benchmarkString
322 func BenchmarkIndexRuneLongString(b *testing.B) {
323 if got := IndexRune(benchmarkLongString, '☺'); got != 114 {
324 b.Fatalf("wrong index: expected 114, got=%d", got)
326 for i := 0; i < b.N; i++ {
327 IndexRune(benchmarkLongString, '☺')
331 func BenchmarkIndexRuneFastPath(b *testing.B) {
332 if got := IndexRune(benchmarkString, 'v'); got != 17 {
333 b.Fatalf("wrong index: expected 17, got=%d", got)
335 for i := 0; i < b.N; i++ {
336 IndexRune(benchmarkString, 'v')
340 func BenchmarkIndex(b *testing.B) {
341 if got := Index(benchmarkString, "v"); got != 17 {
342 b.Fatalf("wrong index: expected 17, got=%d", got)
344 for i := 0; i < b.N; i++ {
345 Index(benchmarkString, "v")
349 func BenchmarkLastIndex(b *testing.B) {
350 if got := Index(benchmarkString, "v"); got != 17 {
351 b.Fatalf("wrong index: expected 17, got=%d", got)
353 for i := 0; i < b.N; i++ {
354 LastIndex(benchmarkString, "v")
358 func BenchmarkIndexByte(b *testing.B) {
359 if got := IndexByte(benchmarkString, 'v'); got != 17 {
360 b.Fatalf("wrong index: expected 17, got=%d", got)
362 for i := 0; i < b.N; i++ {
363 IndexByte(benchmarkString, 'v')
367 type SplitTest struct {
368 s string
369 sep string
370 n int
371 a []string
374 var splittests = []SplitTest{
375 {"", "", -1, []string{}},
376 {abcd, "", 2, []string{"a", "bcd"}},
377 {abcd, "", 4, []string{"a", "b", "c", "d"}},
378 {abcd, "", -1, []string{"a", "b", "c", "d"}},
379 {faces, "", -1, []string{"☺", "☻", "☹"}},
380 {faces, "", 3, []string{"☺", "☻", "☹"}},
381 {faces, "", 17, []string{"☺", "☻", "☹"}},
382 {"☺�☹", "", -1, []string{"☺", "�", "☹"}},
383 {abcd, "a", 0, nil},
384 {abcd, "a", -1, []string{"", "bcd"}},
385 {abcd, "z", -1, []string{"abcd"}},
386 {commas, ",", -1, []string{"1", "2", "3", "4"}},
387 {dots, "...", -1, []string{"1", ".2", ".3", ".4"}},
388 {faces, "☹", -1, []string{"☺☻", ""}},
389 {faces, "~", -1, []string{faces}},
390 {"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
391 {"1 2", " ", 3, []string{"1", "2"}},
394 func TestSplit(t *testing.T) {
395 for _, tt := range splittests {
396 a := SplitN(tt.s, tt.sep, tt.n)
397 if !eq(a, tt.a) {
398 t.Errorf("Split(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, a, tt.a)
399 continue
401 if tt.n == 0 {
402 continue
404 s := Join(a, tt.sep)
405 if s != tt.s {
406 t.Errorf("Join(Split(%q, %q, %d), %q) = %q", tt.s, tt.sep, tt.n, tt.sep, s)
408 if tt.n < 0 {
409 b := Split(tt.s, tt.sep)
410 if !reflect.DeepEqual(a, b) {
411 t.Errorf("Split disagrees with SplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
417 var splitaftertests = []SplitTest{
418 {abcd, "a", -1, []string{"a", "bcd"}},
419 {abcd, "z", -1, []string{"abcd"}},
420 {abcd, "", -1, []string{"a", "b", "c", "d"}},
421 {commas, ",", -1, []string{"1,", "2,", "3,", "4"}},
422 {dots, "...", -1, []string{"1...", ".2...", ".3...", ".4"}},
423 {faces, "☹", -1, []string{"☺☻☹", ""}},
424 {faces, "~", -1, []string{faces}},
425 {faces, "", -1, []string{"☺", "☻", "☹"}},
426 {"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}},
427 {"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}},
428 {"1 2", " ", 3, []string{"1 ", "2"}},
429 {"123", "", 2, []string{"1", "23"}},
430 {"123", "", 17, []string{"1", "2", "3"}},
433 func TestSplitAfter(t *testing.T) {
434 for _, tt := range splitaftertests {
435 a := SplitAfterN(tt.s, tt.sep, tt.n)
436 if !eq(a, tt.a) {
437 t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, a, tt.a)
438 continue
440 s := Join(a, "")
441 if s != tt.s {
442 t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
444 if tt.n < 0 {
445 b := SplitAfter(tt.s, tt.sep)
446 if !reflect.DeepEqual(a, b) {
447 t.Errorf("SplitAfter disagrees with SplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
453 type FieldsTest struct {
454 s string
455 a []string
458 var fieldstests = []FieldsTest{
459 {"", []string{}},
460 {" ", []string{}},
461 {" \t ", []string{}},
462 {"\u2000", []string{}},
463 {" abc ", []string{"abc"}},
464 {"1 2 3 4", []string{"1", "2", "3", "4"}},
465 {"1 2 3 4", []string{"1", "2", "3", "4"}},
466 {"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
467 {"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
468 {"\u2000\u2001\u2002", []string{}},
469 {"\n\t\n", []string{"™", "™"}},
470 {"\n\u20001™2\u2000 \u2001 ™", []string{"1™2", "™"}},
471 {"\n1\uFFFD \uFFFD2\u20003\uFFFD4", []string{"1\uFFFD", "\uFFFD2", "3\uFFFD4"}},
472 {"1\xFF\u2000\xFF2\xFF \xFF", []string{"1\xFF", "\xFF2\xFF", "\xFF"}},
473 {faces, []string{faces}},
476 func TestFields(t *testing.T) {
477 for _, tt := range fieldstests {
478 a := Fields(tt.s)
479 if !eq(a, tt.a) {
480 t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a)
481 continue
486 var FieldsFuncTests = []FieldsTest{
487 {"", []string{}},
488 {"XX", []string{}},
489 {"XXhiXXX", []string{"hi"}},
490 {"aXXbXXXcX", []string{"a", "b", "c"}},
493 func TestFieldsFunc(t *testing.T) {
494 for _, tt := range fieldstests {
495 a := FieldsFunc(tt.s, unicode.IsSpace)
496 if !eq(a, tt.a) {
497 t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a)
498 continue
501 pred := func(c rune) bool { return c == 'X' }
502 for _, tt := range FieldsFuncTests {
503 a := FieldsFunc(tt.s, pred)
504 if !eq(a, tt.a) {
505 t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
510 // Test case for any function which accepts and returns a single string.
511 type StringTest struct {
512 in, out string
515 // Execute f on each test case. funcName should be the name of f; it's used
516 // in failure reports.
517 func runStringTests(t *testing.T, f func(string) string, funcName string, testCases []StringTest) {
518 for _, tc := range testCases {
519 actual := f(tc.in)
520 if actual != tc.out {
521 t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out)
526 var upperTests = []StringTest{
527 {"", ""},
528 {"ONLYUPPER", "ONLYUPPER"},
529 {"abc", "ABC"},
530 {"AbC123", "ABC123"},
531 {"azAZ09_", "AZAZ09_"},
532 {"longStrinGwitHmixofsmaLLandcAps", "LONGSTRINGWITHMIXOFSMALLANDCAPS"},
533 {"long\u0250string\u0250with\u0250nonascii\u2C6Fchars", "LONG\u2C6FSTRING\u2C6FWITH\u2C6FNONASCII\u2C6FCHARS"},
534 {"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
535 {"a\u0080\U0010FFFF", "A\u0080\U0010FFFF"}, // test utf8.RuneSelf and utf8.MaxRune
538 var lowerTests = []StringTest{
539 {"", ""},
540 {"abc", "abc"},
541 {"AbC123", "abc123"},
542 {"azAZ09_", "azaz09_"},
543 {"longStrinGwitHmixofsmaLLandcAps", "longstringwithmixofsmallandcaps"},
544 {"LONG\u2C6FSTRING\u2C6FWITH\u2C6FNONASCII\u2C6FCHARS", "long\u0250string\u0250with\u0250nonascii\u0250chars"},
545 {"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
546 {"A\u0080\U0010FFFF", "a\u0080\U0010FFFF"}, // test utf8.RuneSelf and utf8.MaxRune
549 const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
551 var trimSpaceTests = []StringTest{
552 {"", ""},
553 {"abc", "abc"},
554 {space + "abc" + space, "abc"},
555 {" ", ""},
556 {" \t\r\n \t\t\r\r\n\n ", ""},
557 {" \t\r\n x\t\t\r\r\n\n ", "x"},
558 {" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
559 {"1 \t\r\n2", "1 \t\r\n2"},
560 {" x\x80", "x\x80"},
561 {" x\xc0", "x\xc0"},
562 {"x \xc0\xc0 ", "x \xc0\xc0"},
563 {"x \xc0", "x \xc0"},
564 {"x \xc0 ", "x \xc0"},
565 {"x \xc0\xc0 ", "x \xc0\xc0"},
566 {"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"},
567 {"x ☺ ", "x ☺"},
570 func tenRunes(ch rune) string {
571 r := make([]rune, 10)
572 for i := range r {
573 r[i] = ch
575 return string(r)
578 // User-defined self-inverse mapping function
579 func rot13(r rune) rune {
580 step := rune(13)
581 if r >= 'a' && r <= 'z' {
582 return ((r - 'a' + step) % 26) + 'a'
584 if r >= 'A' && r <= 'Z' {
585 return ((r - 'A' + step) % 26) + 'A'
587 return r
590 func TestMap(t *testing.T) {
591 // Run a couple of awful growth/shrinkage tests
592 a := tenRunes('a')
593 // 1. Grow. This triggers two reallocations in Map.
594 maxRune := func(rune) rune { return unicode.MaxRune }
595 m := Map(maxRune, a)
596 expect := tenRunes(unicode.MaxRune)
597 if m != expect {
598 t.Errorf("growing: expected %q got %q", expect, m)
601 // 2. Shrink
602 minRune := func(rune) rune { return 'a' }
603 m = Map(minRune, tenRunes(unicode.MaxRune))
604 expect = a
605 if m != expect {
606 t.Errorf("shrinking: expected %q got %q", expect, m)
609 // 3. Rot13
610 m = Map(rot13, "a to zed")
611 expect = "n gb mrq"
612 if m != expect {
613 t.Errorf("rot13: expected %q got %q", expect, m)
616 // 4. Rot13^2
617 m = Map(rot13, Map(rot13, "a to zed"))
618 expect = "a to zed"
619 if m != expect {
620 t.Errorf("rot13: expected %q got %q", expect, m)
623 // 5. Drop
624 dropNotLatin := func(r rune) rune {
625 if unicode.Is(unicode.Latin, r) {
626 return r
628 return -1
630 m = Map(dropNotLatin, "Hello, 세계")
631 expect = "Hello"
632 if m != expect {
633 t.Errorf("drop: expected %q got %q", expect, m)
636 // 6. Identity
637 identity := func(r rune) rune {
638 return r
640 orig := "Input string that we expect not to be copied."
641 m = Map(identity, orig)
642 if (*reflect.StringHeader)(unsafe.Pointer(&orig)).Data !=
643 (*reflect.StringHeader)(unsafe.Pointer(&m)).Data {
644 t.Error("unexpected copy during identity map")
647 // 7. Handle invalid UTF-8 sequence
648 replaceNotLatin := func(r rune) rune {
649 if unicode.Is(unicode.Latin, r) {
650 return r
652 return '?'
654 m = Map(replaceNotLatin, "Hello\255World")
655 expect = "Hello?World"
656 if m != expect {
657 t.Errorf("replace invalid sequence: expected %q got %q", expect, m)
660 // 8. Check utf8.RuneSelf and utf8.MaxRune encoding
661 encode := func(r rune) rune {
662 switch r {
663 case utf8.RuneSelf:
664 return unicode.MaxRune
665 case unicode.MaxRune:
666 return utf8.RuneSelf
668 return r
670 s := string(utf8.RuneSelf) + string(utf8.MaxRune)
671 r := string(utf8.MaxRune) + string(utf8.RuneSelf) // reverse of s
672 m = Map(encode, s)
673 if m != r {
674 t.Errorf("encoding not handled correctly: expected %q got %q", r, m)
676 m = Map(encode, r)
677 if m != s {
678 t.Errorf("encoding not handled correctly: expected %q got %q", s, m)
682 func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }
684 func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTests) }
686 func BenchmarkToUpper(b *testing.B) {
687 for _, tc := range upperTests {
688 b.Run(tc.in, func(b *testing.B) {
689 for i := 0; i < b.N; i++ {
690 actual := ToUpper(tc.in)
691 if actual != tc.out {
692 b.Errorf("ToUpper(%q) = %q; want %q", tc.in, actual, tc.out)
699 func BenchmarkToLower(b *testing.B) {
700 for _, tc := range lowerTests {
701 b.Run(tc.in, func(b *testing.B) {
702 for i := 0; i < b.N; i++ {
703 actual := ToLower(tc.in)
704 if actual != tc.out {
705 b.Errorf("ToLower(%q) = %q; want %q", tc.in, actual, tc.out)
712 func BenchmarkMapNoChanges(b *testing.B) {
713 identity := func(r rune) rune {
714 return r
716 for i := 0; i < b.N; i++ {
717 Map(identity, "Some string that won't be modified.")
721 func TestSpecialCase(t *testing.T) {
722 lower := "abcçdefgğhıijklmnoöprsştuüvyz"
723 upper := "ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ"
724 u := ToUpperSpecial(unicode.TurkishCase, upper)
725 if u != upper {
726 t.Errorf("Upper(upper) is %s not %s", u, upper)
728 u = ToUpperSpecial(unicode.TurkishCase, lower)
729 if u != upper {
730 t.Errorf("Upper(lower) is %s not %s", u, upper)
732 l := ToLowerSpecial(unicode.TurkishCase, lower)
733 if l != lower {
734 t.Errorf("Lower(lower) is %s not %s", l, lower)
736 l = ToLowerSpecial(unicode.TurkishCase, upper)
737 if l != lower {
738 t.Errorf("Lower(upper) is %s not %s", l, lower)
742 func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) }
744 var trimTests = []struct {
745 f string
746 in, arg, out string
748 {"Trim", "abba", "a", "bb"},
749 {"Trim", "abba", "ab", ""},
750 {"TrimLeft", "abba", "ab", ""},
751 {"TrimRight", "abba", "ab", ""},
752 {"TrimLeft", "abba", "a", "bba"},
753 {"TrimRight", "abba", "a", "abb"},
754 {"Trim", "<tag>", "<>", "tag"},
755 {"Trim", "* listitem", " *", "listitem"},
756 {"Trim", `"quote"`, `"`, "quote"},
757 {"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
758 {"Trim", "\x80test\xff", "\xff", "test"},
759 {"Trim", " Ġ ", " ", "Ġ"},
760 {"Trim", " Ġİ0", "0 ", "Ġİ"},
761 //empty string tests
762 {"Trim", "abba", "", "abba"},
763 {"Trim", "", "123", ""},
764 {"Trim", "", "", ""},
765 {"TrimLeft", "abba", "", "abba"},
766 {"TrimLeft", "", "123", ""},
767 {"TrimLeft", "", "", ""},
768 {"TrimRight", "abba", "", "abba"},
769 {"TrimRight", "", "123", ""},
770 {"TrimRight", "", "", ""},
771 {"TrimRight", "☺\xc0", "☺", "☺\xc0"},
772 {"TrimPrefix", "aabb", "a", "abb"},
773 {"TrimPrefix", "aabb", "b", "aabb"},
774 {"TrimSuffix", "aabb", "a", "aabb"},
775 {"TrimSuffix", "aabb", "b", "aab"},
778 func TestTrim(t *testing.T) {
779 for _, tc := range trimTests {
780 name := tc.f
781 var f func(string, string) string
782 switch name {
783 case "Trim":
784 f = Trim
785 case "TrimLeft":
786 f = TrimLeft
787 case "TrimRight":
788 f = TrimRight
789 case "TrimPrefix":
790 f = TrimPrefix
791 case "TrimSuffix":
792 f = TrimSuffix
793 default:
794 t.Errorf("Undefined trim function %s", name)
796 actual := f(tc.in, tc.arg)
797 if actual != tc.out {
798 t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
803 func BenchmarkTrim(b *testing.B) {
804 b.ReportAllocs()
806 for i := 0; i < b.N; i++ {
807 for _, tc := range trimTests {
808 name := tc.f
809 var f func(string, string) string
810 switch name {
811 case "Trim":
812 f = Trim
813 case "TrimLeft":
814 f = TrimLeft
815 case "TrimRight":
816 f = TrimRight
817 case "TrimPrefix":
818 f = TrimPrefix
819 case "TrimSuffix":
820 f = TrimSuffix
821 default:
822 b.Errorf("Undefined trim function %s", name)
824 actual := f(tc.in, tc.arg)
825 if actual != tc.out {
826 b.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
832 type predicate struct {
833 f func(rune) bool
834 name string
837 var isSpace = predicate{unicode.IsSpace, "IsSpace"}
838 var isDigit = predicate{unicode.IsDigit, "IsDigit"}
839 var isUpper = predicate{unicode.IsUpper, "IsUpper"}
840 var isValidRune = predicate{
841 func(r rune) bool {
842 return r != utf8.RuneError
844 "IsValidRune",
847 func not(p predicate) predicate {
848 return predicate{
849 func(r rune) bool {
850 return !p.f(r)
852 "not " + p.name,
856 var trimFuncTests = []struct {
857 f predicate
858 in, out string
860 {isSpace, space + " hello " + space, "hello"},
861 {isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
862 {isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
863 {not(isSpace), "hello" + space + "hello", space},
864 {not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"},
865 {isValidRune, "ab\xc0a\xc0cd", "\xc0a\xc0"},
866 {not(isValidRune), "\xc0a\xc0", "a"},
869 func TestTrimFunc(t *testing.T) {
870 for _, tc := range trimFuncTests {
871 actual := TrimFunc(tc.in, tc.f.f)
872 if actual != tc.out {
873 t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.f.name, actual, tc.out)
878 var indexFuncTests = []struct {
879 in string
880 f predicate
881 first, last int
883 {"", isValidRune, -1, -1},
884 {"abc", isDigit, -1, -1},
885 {"0123", isDigit, 0, 3},
886 {"a1b", isDigit, 1, 1},
887 {space, isSpace, 0, len(space) - 3}, // last rune in space is 3 bytes
888 {"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit, 0, 18},
889 {"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34},
890 {"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12},
892 // tests of invalid UTF-8
893 {"\x801", isDigit, 1, 1},
894 {"\x80abc", isDigit, -1, -1},
895 {"\xc0a\xc0", isValidRune, 1, 1},
896 {"\xc0a\xc0", not(isValidRune), 0, 2},
897 {"\xc0\xc0", not(isValidRune), 0, 4},
898 {"\xc0\xc0\xc0", not(isValidRune), 0, 5},
899 {"ab\xc0a\xc0cd", not(isValidRune), 2, 4},
900 {"a\xe0\x80cd", not(isValidRune), 1, 2},
901 {"\x80\x80\x80\x80", not(isValidRune), 0, 3},
904 func TestIndexFunc(t *testing.T) {
905 for _, tc := range indexFuncTests {
906 first := IndexFunc(tc.in, tc.f.f)
907 if first != tc.first {
908 t.Errorf("IndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, first, tc.first)
910 last := LastIndexFunc(tc.in, tc.f.f)
911 if last != tc.last {
912 t.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, last, tc.last)
917 func equal(m string, s1, s2 string, t *testing.T) bool {
918 if s1 == s2 {
919 return true
921 e1 := Split(s1, "")
922 e2 := Split(s2, "")
923 for i, c1 := range e1 {
924 if i >= len(e2) {
925 break
927 r1, _ := utf8.DecodeRuneInString(c1)
928 r2, _ := utf8.DecodeRuneInString(e2[i])
929 if r1 != r2 {
930 t.Errorf("%s diff at %d: U+%04X U+%04X", m, i, r1, r2)
933 return false
936 func TestCaseConsistency(t *testing.T) {
937 // Make a string of all the runes.
938 numRunes := int(unicode.MaxRune + 1)
939 if testing.Short() {
940 numRunes = 1000
942 a := make([]rune, numRunes)
943 for i := range a {
944 a[i] = rune(i)
946 s := string(a)
947 // convert the cases.
948 upper := ToUpper(s)
949 lower := ToLower(s)
951 // Consistency checks
952 if n := utf8.RuneCountInString(upper); n != numRunes {
953 t.Error("rune count wrong in upper:", n)
955 if n := utf8.RuneCountInString(lower); n != numRunes {
956 t.Error("rune count wrong in lower:", n)
958 if !equal("ToUpper(upper)", ToUpper(upper), upper, t) {
959 t.Error("ToUpper(upper) consistency fail")
961 if !equal("ToLower(lower)", ToLower(lower), lower, t) {
962 t.Error("ToLower(lower) consistency fail")
965 These fail because of non-one-to-oneness of the data, such as multiple
966 upper case 'I' mapping to 'i'. We comment them out but keep them for
967 interest.
968 For instance: CAPITAL LETTER I WITH DOT ABOVE:
969 unicode.ToUpper(unicode.ToLower('\u0130')) != '\u0130'
971 if !equal("ToUpper(lower)", ToUpper(lower), upper, t) {
972 t.Error("ToUpper(lower) consistency fail");
974 if !equal("ToLower(upper)", ToLower(upper), lower, t) {
975 t.Error("ToLower(upper) consistency fail");
980 var RepeatTests = []struct {
981 in, out string
982 count int
984 {"", "", 0},
985 {"", "", 1},
986 {"", "", 2},
987 {"-", "", 0},
988 {"-", "-", 1},
989 {"-", "----------", 10},
990 {"abc ", "abc abc abc ", 3},
993 func TestRepeat(t *testing.T) {
994 for _, tt := range RepeatTests {
995 a := Repeat(tt.in, tt.count)
996 if !equal("Repeat(s)", a, tt.out, t) {
997 t.Errorf("Repeat(%v, %d) = %v; want %v", tt.in, tt.count, a, tt.out)
998 continue
1003 func repeat(s string, count int) (err error) {
1004 defer func() {
1005 if r := recover(); r != nil {
1006 switch v := r.(type) {
1007 case error:
1008 err = v
1009 default:
1010 err = fmt.Errorf("%s", v)
1015 Repeat(s, count)
1017 return
1020 // See Issue golang.org/issue/16237
1021 func TestRepeatCatchesOverflow(t *testing.T) {
1022 tests := [...]struct {
1023 s string
1024 count int
1025 errStr string
1027 0: {"--", -2147483647, "negative"},
1028 1: {"", int(^uint(0) >> 1), ""},
1029 2: {"-", 10, ""},
1030 3: {"gopher", 0, ""},
1031 4: {"-", -1, "negative"},
1032 5: {"--", -102, "negative"},
1033 6: {string(make([]byte, 255)), int((^uint(0))/255 + 1), "overflow"},
1036 for i, tt := range tests {
1037 err := repeat(tt.s, tt.count)
1038 if tt.errStr == "" {
1039 if err != nil {
1040 t.Errorf("#%d panicked %v", i, err)
1042 continue
1045 if err == nil || !Contains(err.Error(), tt.errStr) {
1046 t.Errorf("#%d expected %q got %q", i, tt.errStr, err)
1051 func runesEqual(a, b []rune) bool {
1052 if len(a) != len(b) {
1053 return false
1055 for i, r := range a {
1056 if r != b[i] {
1057 return false
1060 return true
1063 var RunesTests = []struct {
1064 in string
1065 out []rune
1066 lossy bool
1068 {"", []rune{}, false},
1069 {" ", []rune{32}, false},
1070 {"ABC", []rune{65, 66, 67}, false},
1071 {"abc", []rune{97, 98, 99}, false},
1072 {"\u65e5\u672c\u8a9e", []rune{26085, 26412, 35486}, false},
1073 {"ab\x80c", []rune{97, 98, 0xFFFD, 99}, true},
1074 {"ab\xc0c", []rune{97, 98, 0xFFFD, 99}, true},
1077 func TestRunes(t *testing.T) {
1078 for _, tt := range RunesTests {
1079 a := []rune(tt.in)
1080 if !runesEqual(a, tt.out) {
1081 t.Errorf("[]rune(%q) = %v; want %v", tt.in, a, tt.out)
1082 continue
1084 if !tt.lossy {
1085 // can only test reassembly if we didn't lose information
1086 s := string(a)
1087 if s != tt.in {
1088 t.Errorf("string([]rune(%q)) = %x; want %x", tt.in, s, tt.in)
1094 func TestReadByte(t *testing.T) {
1095 testStrings := []string{"", abcd, faces, commas}
1096 for _, s := range testStrings {
1097 reader := NewReader(s)
1098 if e := reader.UnreadByte(); e == nil {
1099 t.Errorf("Unreading %q at beginning: expected error", s)
1101 var res bytes.Buffer
1102 for {
1103 b, e := reader.ReadByte()
1104 if e == io.EOF {
1105 break
1107 if e != nil {
1108 t.Errorf("Reading %q: %s", s, e)
1109 break
1111 res.WriteByte(b)
1112 // unread and read again
1113 e = reader.UnreadByte()
1114 if e != nil {
1115 t.Errorf("Unreading %q: %s", s, e)
1116 break
1118 b1, e := reader.ReadByte()
1119 if e != nil {
1120 t.Errorf("Reading %q after unreading: %s", s, e)
1121 break
1123 if b1 != b {
1124 t.Errorf("Reading %q after unreading: want byte %q, got %q", s, b, b1)
1125 break
1128 if res.String() != s {
1129 t.Errorf("Reader(%q).ReadByte() produced %q", s, res.String())
1134 func TestReadRune(t *testing.T) {
1135 testStrings := []string{"", abcd, faces, commas}
1136 for _, s := range testStrings {
1137 reader := NewReader(s)
1138 if e := reader.UnreadRune(); e == nil {
1139 t.Errorf("Unreading %q at beginning: expected error", s)
1141 res := ""
1142 for {
1143 r, z, e := reader.ReadRune()
1144 if e == io.EOF {
1145 break
1147 if e != nil {
1148 t.Errorf("Reading %q: %s", s, e)
1149 break
1151 res += string(r)
1152 // unread and read again
1153 e = reader.UnreadRune()
1154 if e != nil {
1155 t.Errorf("Unreading %q: %s", s, e)
1156 break
1158 r1, z1, e := reader.ReadRune()
1159 if e != nil {
1160 t.Errorf("Reading %q after unreading: %s", s, e)
1161 break
1163 if r1 != r {
1164 t.Errorf("Reading %q after unreading: want rune %q, got %q", s, r, r1)
1165 break
1167 if z1 != z {
1168 t.Errorf("Reading %q after unreading: want size %d, got %d", s, z, z1)
1169 break
1172 if res != s {
1173 t.Errorf("Reader(%q).ReadRune() produced %q", s, res)
1178 var UnreadRuneErrorTests = []struct {
1179 name string
1180 f func(*Reader)
1182 {"Read", func(r *Reader) { r.Read([]byte{0}) }},
1183 {"ReadByte", func(r *Reader) { r.ReadByte() }},
1184 {"UnreadRune", func(r *Reader) { r.UnreadRune() }},
1185 {"Seek", func(r *Reader) { r.Seek(0, io.SeekCurrent) }},
1186 {"WriteTo", func(r *Reader) { r.WriteTo(&bytes.Buffer{}) }},
1189 func TestUnreadRuneError(t *testing.T) {
1190 for _, tt := range UnreadRuneErrorTests {
1191 reader := NewReader("0123456789")
1192 if _, _, err := reader.ReadRune(); err != nil {
1193 // should not happen
1194 t.Fatal(err)
1196 tt.f(reader)
1197 err := reader.UnreadRune()
1198 if err == nil {
1199 t.Errorf("Unreading after %s: expected error", tt.name)
1204 var ReplaceTests = []struct {
1205 in string
1206 old, new string
1207 n int
1208 out string
1210 {"hello", "l", "L", 0, "hello"},
1211 {"hello", "l", "L", -1, "heLLo"},
1212 {"hello", "x", "X", -1, "hello"},
1213 {"", "x", "X", -1, ""},
1214 {"radar", "r", "<r>", -1, "<r>ada<r>"},
1215 {"", "", "<>", -1, "<>"},
1216 {"banana", "a", "<>", -1, "b<>n<>n<>"},
1217 {"banana", "a", "<>", 1, "b<>nana"},
1218 {"banana", "a", "<>", 1000, "b<>n<>n<>"},
1219 {"banana", "an", "<>", -1, "b<><>a"},
1220 {"banana", "ana", "<>", -1, "b<>na"},
1221 {"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"},
1222 {"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
1223 {"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
1224 {"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
1225 {"banana", "", "<>", 1, "<>banana"},
1226 {"banana", "a", "a", -1, "banana"},
1227 {"banana", "a", "a", 1, "banana"},
1228 {"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"},
1231 func TestReplace(t *testing.T) {
1232 for _, tt := range ReplaceTests {
1233 if s := Replace(tt.in, tt.old, tt.new, tt.n); s != tt.out {
1234 t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
1239 var TitleTests = []struct {
1240 in, out string
1242 {"", ""},
1243 {"a", "A"},
1244 {" aaa aaa aaa ", " Aaa Aaa Aaa "},
1245 {" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
1246 {"123a456", "123a456"},
1247 {"double-blind", "Double-Blind"},
1248 {"ÿøû", "Ÿøû"},
1249 {"with_underscore", "With_underscore"},
1250 {"unicode \xe2\x80\xa8 line separator", "Unicode \xe2\x80\xa8 Line Separator"},
1253 func TestTitle(t *testing.T) {
1254 for _, tt := range TitleTests {
1255 if s := Title(tt.in); s != tt.out {
1256 t.Errorf("Title(%q) = %q, want %q", tt.in, s, tt.out)
1261 var ContainsTests = []struct {
1262 str, substr string
1263 expected bool
1265 {"abc", "bc", true},
1266 {"abc", "bcd", false},
1267 {"abc", "", true},
1268 {"", "a", false},
1270 // cases to cover code in runtime/asm_amd64.s:indexShortStr
1271 // 2-byte needle
1272 {"xxxxxx", "01", false},
1273 {"01xxxx", "01", true},
1274 {"xx01xx", "01", true},
1275 {"xxxx01", "01", true},
1276 {"01xxxxx"[1:], "01", false},
1277 {"xxxxx01"[:6], "01", false},
1278 // 3-byte needle
1279 {"xxxxxxx", "012", false},
1280 {"012xxxx", "012", true},
1281 {"xx012xx", "012", true},
1282 {"xxxx012", "012", true},
1283 {"012xxxxx"[1:], "012", false},
1284 {"xxxxx012"[:7], "012", false},
1285 // 4-byte needle
1286 {"xxxxxxxx", "0123", false},
1287 {"0123xxxx", "0123", true},
1288 {"xx0123xx", "0123", true},
1289 {"xxxx0123", "0123", true},
1290 {"0123xxxxx"[1:], "0123", false},
1291 {"xxxxx0123"[:8], "0123", false},
1292 // 5-7-byte needle
1293 {"xxxxxxxxx", "01234", false},
1294 {"01234xxxx", "01234", true},
1295 {"xx01234xx", "01234", true},
1296 {"xxxx01234", "01234", true},
1297 {"01234xxxxx"[1:], "01234", false},
1298 {"xxxxx01234"[:9], "01234", false},
1299 // 8-byte needle
1300 {"xxxxxxxxxxxx", "01234567", false},
1301 {"01234567xxxx", "01234567", true},
1302 {"xx01234567xx", "01234567", true},
1303 {"xxxx01234567", "01234567", true},
1304 {"01234567xxxxx"[1:], "01234567", false},
1305 {"xxxxx01234567"[:12], "01234567", false},
1306 // 9-15-byte needle
1307 {"xxxxxxxxxxxxx", "012345678", false},
1308 {"012345678xxxx", "012345678", true},
1309 {"xx012345678xx", "012345678", true},
1310 {"xxxx012345678", "012345678", true},
1311 {"012345678xxxxx"[1:], "012345678", false},
1312 {"xxxxx012345678"[:13], "012345678", false},
1313 // 16-byte needle
1314 {"xxxxxxxxxxxxxxxxxxxx", "0123456789ABCDEF", false},
1315 {"0123456789ABCDEFxxxx", "0123456789ABCDEF", true},
1316 {"xx0123456789ABCDEFxx", "0123456789ABCDEF", true},
1317 {"xxxx0123456789ABCDEF", "0123456789ABCDEF", true},
1318 {"0123456789ABCDEFxxxxx"[1:], "0123456789ABCDEF", false},
1319 {"xxxxx0123456789ABCDEF"[:20], "0123456789ABCDEF", false},
1320 // 17-31-byte needle
1321 {"xxxxxxxxxxxxxxxxxxxxx", "0123456789ABCDEFG", false},
1322 {"0123456789ABCDEFGxxxx", "0123456789ABCDEFG", true},
1323 {"xx0123456789ABCDEFGxx", "0123456789ABCDEFG", true},
1324 {"xxxx0123456789ABCDEFG", "0123456789ABCDEFG", true},
1325 {"0123456789ABCDEFGxxxxx"[1:], "0123456789ABCDEFG", false},
1326 {"xxxxx0123456789ABCDEFG"[:21], "0123456789ABCDEFG", false},
1328 // partial match cases
1329 {"xx01x", "012", false}, // 3
1330 {"xx0123x", "01234", false}, // 5-7
1331 {"xx01234567x", "012345678", false}, // 9-15
1332 {"xx0123456789ABCDEFx", "0123456789ABCDEFG", false}, // 17-31, issue 15679
1335 func TestContains(t *testing.T) {
1336 for _, ct := range ContainsTests {
1337 if Contains(ct.str, ct.substr) != ct.expected {
1338 t.Errorf("Contains(%s, %s) = %v, want %v",
1339 ct.str, ct.substr, !ct.expected, ct.expected)
1344 var ContainsAnyTests = []struct {
1345 str, substr string
1346 expected bool
1348 {"", "", false},
1349 {"", "a", false},
1350 {"", "abc", false},
1351 {"a", "", false},
1352 {"a", "a", true},
1353 {"aaa", "a", true},
1354 {"abc", "xyz", false},
1355 {"abc", "xcz", true},
1356 {"a☺b☻c☹d", "uvw☻xyz", true},
1357 {"aRegExp*", ".(|)*+?^$[]", true},
1358 {dots + dots + dots, " ", false},
1361 func TestContainsAny(t *testing.T) {
1362 for _, ct := range ContainsAnyTests {
1363 if ContainsAny(ct.str, ct.substr) != ct.expected {
1364 t.Errorf("ContainsAny(%s, %s) = %v, want %v",
1365 ct.str, ct.substr, !ct.expected, ct.expected)
1370 var ContainsRuneTests = []struct {
1371 str string
1372 r rune
1373 expected bool
1375 {"", 'a', false},
1376 {"a", 'a', true},
1377 {"aaa", 'a', true},
1378 {"abc", 'y', false},
1379 {"abc", 'c', true},
1380 {"a☺b☻c☹d", 'x', false},
1381 {"a☺b☻c☹d", '☻', true},
1382 {"aRegExp*", '*', true},
1385 func TestContainsRune(t *testing.T) {
1386 for _, ct := range ContainsRuneTests {
1387 if ContainsRune(ct.str, ct.r) != ct.expected {
1388 t.Errorf("ContainsRune(%q, %q) = %v, want %v",
1389 ct.str, ct.r, !ct.expected, ct.expected)
1394 var EqualFoldTests = []struct {
1395 s, t string
1396 out bool
1398 {"abc", "abc", true},
1399 {"ABcd", "ABcd", true},
1400 {"123abc", "123ABC", true},
1401 {"αβδ", "ΑΒΔ", true},
1402 {"abc", "xyz", false},
1403 {"abc", "XYZ", false},
1404 {"abcdefghijk", "abcdefghijX", false},
1405 {"abcdefghijk", "abcdefghij\u212A", true},
1406 {"abcdefghijK", "abcdefghij\u212A", true},
1407 {"abcdefghijkz", "abcdefghij\u212Ay", false},
1408 {"abcdefghijKz", "abcdefghij\u212Ay", false},
1411 func TestEqualFold(t *testing.T) {
1412 for _, tt := range EqualFoldTests {
1413 if out := EqualFold(tt.s, tt.t); out != tt.out {
1414 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.s, tt.t, out, tt.out)
1416 if out := EqualFold(tt.t, tt.s); out != tt.out {
1417 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.t, tt.s, out, tt.out)
1422 var CountTests = []struct {
1423 s, sep string
1424 num int
1426 {"", "", 1},
1427 {"", "notempty", 0},
1428 {"notempty", "", 9},
1429 {"smaller", "not smaller", 0},
1430 {"12345678987654321", "6", 2},
1431 {"611161116", "6", 3},
1432 {"notequal", "NotEqual", 0},
1433 {"equal", "equal", 1},
1434 {"abc1231231123q", "123", 3},
1435 {"11111", "11", 2},
1438 func TestCount(t *testing.T) {
1439 for _, tt := range CountTests {
1440 if num := Count(tt.s, tt.sep); num != tt.num {
1441 t.Errorf("Count(\"%s\", \"%s\") = %d, want %d", tt.s, tt.sep, num, tt.num)
1446 func makeBenchInputHard() string {
1447 tokens := [...]string{
1448 "<a>", "<p>", "<b>", "<strong>",
1449 "</a>", "</p>", "</b>", "</strong>",
1450 "hello", "world",
1452 x := make([]byte, 0, 1<<20)
1453 for {
1454 i := rand.Intn(len(tokens))
1455 if len(x)+len(tokens[i]) >= 1<<20 {
1456 break
1458 x = append(x, tokens[i]...)
1460 return string(x)
1463 var benchInputHard = makeBenchInputHard()
1465 func benchmarkIndexHard(b *testing.B, sep string) {
1466 for i := 0; i < b.N; i++ {
1467 Index(benchInputHard, sep)
1471 func benchmarkLastIndexHard(b *testing.B, sep string) {
1472 for i := 0; i < b.N; i++ {
1473 LastIndex(benchInputHard, sep)
1477 func benchmarkCountHard(b *testing.B, sep string) {
1478 for i := 0; i < b.N; i++ {
1479 Count(benchInputHard, sep)
1483 func BenchmarkIndexHard1(b *testing.B) { benchmarkIndexHard(b, "<>") }
1484 func BenchmarkIndexHard2(b *testing.B) { benchmarkIndexHard(b, "</pre>") }
1485 func BenchmarkIndexHard3(b *testing.B) { benchmarkIndexHard(b, "<b>hello world</b>") }
1486 func BenchmarkIndexHard4(b *testing.B) {
1487 benchmarkIndexHard(b, "<pre><b>hello</b><strong>world</strong></pre>")
1490 func BenchmarkLastIndexHard1(b *testing.B) { benchmarkLastIndexHard(b, "<>") }
1491 func BenchmarkLastIndexHard2(b *testing.B) { benchmarkLastIndexHard(b, "</pre>") }
1492 func BenchmarkLastIndexHard3(b *testing.B) { benchmarkLastIndexHard(b, "<b>hello world</b>") }
1494 func BenchmarkCountHard1(b *testing.B) { benchmarkCountHard(b, "<>") }
1495 func BenchmarkCountHard2(b *testing.B) { benchmarkCountHard(b, "</pre>") }
1496 func BenchmarkCountHard3(b *testing.B) { benchmarkCountHard(b, "<b>hello world</b>") }
1498 var benchInputTorture = Repeat("ABC", 1<<10) + "123" + Repeat("ABC", 1<<10)
1499 var benchNeedleTorture = Repeat("ABC", 1<<10+1)
1501 func BenchmarkIndexTorture(b *testing.B) {
1502 for i := 0; i < b.N; i++ {
1503 Index(benchInputTorture, benchNeedleTorture)
1507 func BenchmarkCountTorture(b *testing.B) {
1508 for i := 0; i < b.N; i++ {
1509 Count(benchInputTorture, benchNeedleTorture)
1513 func BenchmarkCountTortureOverlapping(b *testing.B) {
1514 A := Repeat("ABC", 1<<20)
1515 B := Repeat("ABC", 1<<10)
1516 for i := 0; i < b.N; i++ {
1517 Count(A, B)
1521 func BenchmarkCountByte(b *testing.B) {
1522 indexSizes := []int{10, 32, 4 << 10, 4 << 20, 64 << 20}
1523 benchStr := Repeat(benchmarkString,
1524 (indexSizes[len(indexSizes)-1]+len(benchmarkString)-1)/len(benchmarkString))
1525 benchFunc := func(b *testing.B, benchStr string) {
1526 b.SetBytes(int64(len(benchStr)))
1527 for i := 0; i < b.N; i++ {
1528 Count(benchStr, "=")
1531 for _, size := range indexSizes {
1532 b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
1533 benchFunc(b, benchStr[:size])
1539 var makeFieldsInput = func() string {
1540 x := make([]byte, 1<<20)
1541 // Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
1542 for i := range x {
1543 switch rand.Intn(10) {
1544 case 0:
1545 x[i] = ' '
1546 case 1:
1547 if i > 0 && x[i-1] == 'x' {
1548 copy(x[i-1:], "χ")
1549 break
1551 fallthrough
1552 default:
1553 x[i] = 'x'
1556 return string(x)
1559 var makeFieldsInputASCII = func() string {
1560 x := make([]byte, 1<<20)
1561 // Input is ~10% space, rest ASCII non-space.
1562 for i := range x {
1563 if rand.Intn(10) == 0 {
1564 x[i] = ' '
1565 } else {
1566 x[i] = 'x'
1569 return string(x)
1572 var stringdata = []struct{ name, data string }{
1573 {"ASCII", makeFieldsInputASCII()},
1574 {"Mixed", makeFieldsInput()},
1577 func BenchmarkFields(b *testing.B) {
1578 for _, sd := range stringdata {
1579 b.Run(sd.name, func(b *testing.B) {
1580 for j := 1 << 4; j <= 1<<20; j <<= 4 {
1581 b.Run(fmt.Sprintf("%d", j), func(b *testing.B) {
1582 b.ReportAllocs()
1583 b.SetBytes(int64(j))
1584 data := sd.data[:j]
1585 for i := 0; i < b.N; i++ {
1586 Fields(data)
1594 func BenchmarkFieldsFunc(b *testing.B) {
1595 for _, sd := range stringdata {
1596 b.Run(sd.name, func(b *testing.B) {
1597 for j := 1 << 4; j <= 1<<20; j <<= 4 {
1598 b.Run(fmt.Sprintf("%d", j), func(b *testing.B) {
1599 b.ReportAllocs()
1600 b.SetBytes(int64(j))
1601 data := sd.data[:j]
1602 for i := 0; i < b.N; i++ {
1603 FieldsFunc(data, unicode.IsSpace)
1611 func BenchmarkSplitEmptySeparator(b *testing.B) {
1612 for i := 0; i < b.N; i++ {
1613 Split(benchInputHard, "")
1617 func BenchmarkSplitSingleByteSeparator(b *testing.B) {
1618 for i := 0; i < b.N; i++ {
1619 Split(benchInputHard, "/")
1623 func BenchmarkSplitMultiByteSeparator(b *testing.B) {
1624 for i := 0; i < b.N; i++ {
1625 Split(benchInputHard, "hello")
1629 func BenchmarkSplitNSingleByteSeparator(b *testing.B) {
1630 for i := 0; i < b.N; i++ {
1631 SplitN(benchInputHard, "/", 10)
1635 func BenchmarkSplitNMultiByteSeparator(b *testing.B) {
1636 for i := 0; i < b.N; i++ {
1637 SplitN(benchInputHard, "hello", 10)
1641 func BenchmarkRepeat(b *testing.B) {
1642 for i := 0; i < b.N; i++ {
1643 Repeat("-", 80)
1647 func BenchmarkIndexAnyASCII(b *testing.B) {
1648 x := Repeat("#", 4096) // Never matches set
1649 cs := "0123456789abcdef"
1650 for k := 1; k <= 4096; k <<= 4 {
1651 for j := 1; j <= 16; j <<= 1 {
1652 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
1653 for i := 0; i < b.N; i++ {
1654 IndexAny(x[:k], cs[:j])
1661 func BenchmarkTrimASCII(b *testing.B) {
1662 cs := "0123456789abcdef"
1663 for k := 1; k <= 4096; k <<= 4 {
1664 for j := 1; j <= 16; j <<= 1 {
1665 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
1666 x := Repeat(cs[:j], k) // Always matches set
1667 for i := 0; i < b.N; i++ {
1668 Trim(x[:k], cs[:j])
1675 func BenchmarkIndexPeriodic(b *testing.B) {
1676 key := "aa"
1677 for _, skip := range [...]int{2, 4, 8, 16, 32, 64} {
1678 b.Run(fmt.Sprintf("IndexPeriodic%d", skip), func(b *testing.B) {
1679 s := Repeat("a"+Repeat(" ", skip-1), 1<<16/skip)
1680 for i := 0; i < b.N; i++ {
1681 Index(s, key)