Require target lra in gcc.dg/pr108095.c
[official-gcc.git] / libgo / go / strings / strings_test.go
blob6855ab4f235f0e02ceb61d52a6a602bd9d57d8d8
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 "strconv"
15 . "strings"
16 "testing"
17 "unicode"
18 "unicode/utf8"
19 "unsafe"
22 func eq(a, b []string) bool {
23 if len(a) != len(b) {
24 return false
26 for i := 0; i < len(a); i++ {
27 if a[i] != b[i] {
28 return false
31 return true
34 var abcd = "abcd"
35 var faces = "☺☻☹"
36 var commas = "1,2,3,4"
37 var dots = "1....2....3....4"
39 type IndexTest struct {
40 s string
41 sep string
42 out int
45 var indexTests = []IndexTest{
46 {"", "", 0},
47 {"", "a", -1},
48 {"", "foo", -1},
49 {"fo", "foo", -1},
50 {"foo", "foo", 0},
51 {"oofofoofooo", "f", 2},
52 {"oofofoofooo", "foo", 4},
53 {"barfoobarfoo", "foo", 3},
54 {"foo", "", 0},
55 {"foo", "o", 1},
56 {"abcABCabc", "A", 3},
57 // cases with one byte strings - test special case in Index()
58 {"", "a", -1},
59 {"x", "a", -1},
60 {"x", "x", 0},
61 {"abc", "a", 0},
62 {"abc", "b", 1},
63 {"abc", "c", 2},
64 {"abc", "x", -1},
65 // test special cases in Index() for short strings
66 {"", "ab", -1},
67 {"bc", "ab", -1},
68 {"ab", "ab", 0},
69 {"xab", "ab", 1},
70 {"xab"[:2], "ab", -1},
71 {"", "abc", -1},
72 {"xbc", "abc", -1},
73 {"abc", "abc", 0},
74 {"xabc", "abc", 1},
75 {"xabc"[:3], "abc", -1},
76 {"xabxc", "abc", -1},
77 {"", "abcd", -1},
78 {"xbcd", "abcd", -1},
79 {"abcd", "abcd", 0},
80 {"xabcd", "abcd", 1},
81 {"xyabcd"[:5], "abcd", -1},
82 {"xbcqq", "abcqq", -1},
83 {"abcqq", "abcqq", 0},
84 {"xabcqq", "abcqq", 1},
85 {"xyabcqq"[:6], "abcqq", -1},
86 {"xabxcqq", "abcqq", -1},
87 {"xabcqxq", "abcqq", -1},
88 {"", "01234567", -1},
89 {"32145678", "01234567", -1},
90 {"01234567", "01234567", 0},
91 {"x01234567", "01234567", 1},
92 {"x0123456x01234567", "01234567", 9},
93 {"xx01234567"[:9], "01234567", -1},
94 {"", "0123456789", -1},
95 {"3214567844", "0123456789", -1},
96 {"0123456789", "0123456789", 0},
97 {"x0123456789", "0123456789", 1},
98 {"x012345678x0123456789", "0123456789", 11},
99 {"xyz0123456789"[:12], "0123456789", -1},
100 {"x01234567x89", "0123456789", -1},
101 {"", "0123456789012345", -1},
102 {"3214567889012345", "0123456789012345", -1},
103 {"0123456789012345", "0123456789012345", 0},
104 {"x0123456789012345", "0123456789012345", 1},
105 {"x012345678901234x0123456789012345", "0123456789012345", 17},
106 {"", "01234567890123456789", -1},
107 {"32145678890123456789", "01234567890123456789", -1},
108 {"01234567890123456789", "01234567890123456789", 0},
109 {"x01234567890123456789", "01234567890123456789", 1},
110 {"x0123456789012345678x01234567890123456789", "01234567890123456789", 21},
111 {"xyz01234567890123456789"[:22], "01234567890123456789", -1},
112 {"", "0123456789012345678901234567890", -1},
113 {"321456788901234567890123456789012345678911", "0123456789012345678901234567890", -1},
114 {"0123456789012345678901234567890", "0123456789012345678901234567890", 0},
115 {"x0123456789012345678901234567890", "0123456789012345678901234567890", 1},
116 {"x012345678901234567890123456789x0123456789012345678901234567890", "0123456789012345678901234567890", 32},
117 {"xyz0123456789012345678901234567890"[:33], "0123456789012345678901234567890", -1},
118 {"", "01234567890123456789012345678901", -1},
119 {"32145678890123456789012345678901234567890211", "01234567890123456789012345678901", -1},
120 {"01234567890123456789012345678901", "01234567890123456789012345678901", 0},
121 {"x01234567890123456789012345678901", "01234567890123456789012345678901", 1},
122 {"x0123456789012345678901234567890x01234567890123456789012345678901", "01234567890123456789012345678901", 33},
123 {"xyz01234567890123456789012345678901"[:34], "01234567890123456789012345678901", -1},
124 {"xxxxxx012345678901234567890123456789012345678901234567890123456789012", "012345678901234567890123456789012345678901234567890123456789012", 6},
125 {"", "0123456789012345678901234567890123456789", -1},
126 {"xx012345678901234567890123456789012345678901234567890123456789012", "0123456789012345678901234567890123456789", 2},
127 {"xx012345678901234567890123456789012345678901234567890123456789012"[:41], "0123456789012345678901234567890123456789", -1},
128 {"xx012345678901234567890123456789012345678901234567890123456789012", "0123456789012345678901234567890123456xxx", -1},
129 {"xx0123456789012345678901234567890123456789012345678901234567890120123456789012345678901234567890123456xxx", "0123456789012345678901234567890123456xxx", 65},
130 // test fallback to Rabin-Karp.
131 {"oxoxoxoxoxoxoxoxoxoxoxoy", "oy", 22},
132 {"oxoxoxoxoxoxoxoxoxoxoxox", "oy", -1},
135 var lastIndexTests = []IndexTest{
136 {"", "", 0},
137 {"", "a", -1},
138 {"", "foo", -1},
139 {"fo", "foo", -1},
140 {"foo", "foo", 0},
141 {"foo", "f", 0},
142 {"oofofoofooo", "f", 7},
143 {"oofofoofooo", "foo", 7},
144 {"barfoobarfoo", "foo", 9},
145 {"foo", "", 3},
146 {"foo", "o", 2},
147 {"abcABCabc", "A", 3},
148 {"abcABCabc", "a", 6},
151 var indexAnyTests = []IndexTest{
152 {"", "", -1},
153 {"", "a", -1},
154 {"", "abc", -1},
155 {"a", "", -1},
156 {"a", "a", 0},
157 {"\x80", "\xffb", 0},
158 {"aaa", "a", 0},
159 {"abc", "xyz", -1},
160 {"abc", "xcz", 2},
161 {"ab☺c", "x☺yz", 2},
162 {"a☺b☻c☹d", "cx", len("a☺b☻")},
163 {"a☺b☻c☹d", "uvw☻xyz", len("a☺b")},
164 {"aRegExp*", ".(|)*+?^$[]", 7},
165 {dots + dots + dots, " ", -1},
166 {"012abcba210", "\xffb", 4},
167 {"012\x80bcb\x80210", "\xffb", 3},
168 {"0123456\xcf\x80abc", "\xcfb\x80", 10},
171 var lastIndexAnyTests = []IndexTest{
172 {"", "", -1},
173 {"", "a", -1},
174 {"", "abc", -1},
175 {"a", "", -1},
176 {"a", "a", 0},
177 {"\x80", "\xffb", 0},
178 {"aaa", "a", 2},
179 {"abc", "xyz", -1},
180 {"abc", "ab", 1},
181 {"ab☺c", "x☺yz", 2},
182 {"a☺b☻c☹d", "cx", len("a☺b☻")},
183 {"a☺b☻c☹d", "uvw☻xyz", len("a☺b")},
184 {"a.RegExp*", ".(|)*+?^$[]", 8},
185 {dots + dots + dots, " ", -1},
186 {"012abcba210", "\xffb", 6},
187 {"012\x80bcb\x80210", "\xffb", 7},
188 {"0123456\xcf\x80abc", "\xcfb\x80", 10},
191 // Execute f on each test case. funcName should be the name of f; it's used
192 // in failure reports.
193 func runIndexTests(t *testing.T, f func(s, sep string) int, funcName string, testCases []IndexTest) {
194 for _, test := range testCases {
195 actual := f(test.s, test.sep)
196 if actual != test.out {
197 t.Errorf("%s(%q,%q) = %v; want %v", funcName, test.s, test.sep, actual, test.out)
202 func TestIndex(t *testing.T) { runIndexTests(t, Index, "Index", indexTests) }
203 func TestLastIndex(t *testing.T) { runIndexTests(t, LastIndex, "LastIndex", lastIndexTests) }
204 func TestIndexAny(t *testing.T) { runIndexTests(t, IndexAny, "IndexAny", indexAnyTests) }
205 func TestLastIndexAny(t *testing.T) {
206 runIndexTests(t, LastIndexAny, "LastIndexAny", lastIndexAnyTests)
209 func TestIndexByte(t *testing.T) {
210 for _, tt := range indexTests {
211 if len(tt.sep) != 1 {
212 continue
214 pos := IndexByte(tt.s, tt.sep[0])
215 if pos != tt.out {
216 t.Errorf(`IndexByte(%q, %q) = %v; want %v`, tt.s, tt.sep[0], pos, tt.out)
221 func TestLastIndexByte(t *testing.T) {
222 testCases := []IndexTest{
223 {"", "q", -1},
224 {"abcdef", "q", -1},
225 {"abcdefabcdef", "a", len("abcdef")}, // something in the middle
226 {"abcdefabcdef", "f", len("abcdefabcde")}, // last byte
227 {"zabcdefabcdef", "z", 0}, // first byte
228 {"a☺b☻c☹d", "b", len("a☺")}, // non-ascii
230 for _, test := range testCases {
231 actual := LastIndexByte(test.s, test.sep[0])
232 if actual != test.out {
233 t.Errorf("LastIndexByte(%q,%c) = %v; want %v", test.s, test.sep[0], actual, test.out)
238 func simpleIndex(s, sep string) int {
239 n := len(sep)
240 for i := n; i <= len(s); i++ {
241 if s[i-n:i] == sep {
242 return i - n
245 return -1
248 func TestIndexRandom(t *testing.T) {
249 const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
250 for times := 0; times < 10; times++ {
251 for strLen := 5 + rand.Intn(5); strLen < 140; strLen += 10 { // Arbitrary
252 s1 := make([]byte, strLen)
253 for i := range s1 {
254 s1[i] = chars[rand.Intn(len(chars))]
256 s := string(s1)
257 for i := 0; i < 50; i++ {
258 begin := rand.Intn(len(s) + 1)
259 end := begin + rand.Intn(len(s)+1-begin)
260 sep := s[begin:end]
261 if i%4 == 0 {
262 pos := rand.Intn(len(sep) + 1)
263 sep = sep[:pos] + "A" + sep[pos:]
265 want := simpleIndex(s, sep)
266 res := Index(s, sep)
267 if res != want {
268 t.Errorf("Index(%s,%s) = %d; want %d", s, sep, res, want)
275 func TestIndexRune(t *testing.T) {
276 tests := []struct {
277 in string
278 rune rune
279 want int
281 {"", 'a', -1},
282 {"", '☺', -1},
283 {"foo", '☹', -1},
284 {"foo", 'o', 1},
285 {"foo☺bar", '☺', 3},
286 {"foo☺☻☹bar", '☹', 9},
287 {"a A x", 'A', 2},
288 {"some_text=some_value", '=', 9},
289 {"☺a", 'a', 3},
290 {"a☻☺b", '☺', 4},
292 // RuneError should match any invalid UTF-8 byte sequence.
293 {"�", '�', 0},
294 {"\xff", '�', 0},
295 {"☻x�", '�', len("☻x")},
296 {"☻x\xe2\x98", '�', len("☻x")},
297 {"☻x\xe2\x98�", '�', len("☻x")},
298 {"☻x\xe2\x98x", '�', len("☻x")},
300 // Invalid rune values should never match.
301 {"a☺b☻c☹d\xe2\x98\xff\xed\xa0\x80", -1, -1},
302 {"a☺b☻c☹d\xe2\x98\xff\xed\xa0\x80", 0xD800, -1}, // Surrogate pair
303 {"a☺b☻c☹d\xe2\x98\xff\xed\xa0\x80", utf8.MaxRune + 1, -1},
305 for _, tt := range tests {
306 if got := IndexRune(tt.in, tt.rune); got != tt.want {
307 t.Errorf("IndexRune(%q, %d) = %v; want %v", tt.in, tt.rune, got, tt.want)
311 haystack := "test世界"
312 allocs := testing.AllocsPerRun(1000, func() {
313 if i := IndexRune(haystack, 's'); i != 2 {
314 t.Fatalf("'s' at %d; want 2", i)
316 if i := IndexRune(haystack, '世'); i != 4 {
317 t.Fatalf("'世' at %d; want 4", i)
320 if runtime.Compiler == "gccgo" {
321 t.Skip("skipping allocations test for gccgo until escape analysis is enabled")
323 if allocs != 0 && testing.CoverMode() == "" {
324 t.Errorf("expected no allocations, got %f", allocs)
328 const benchmarkString = "some_text=some☺value"
330 func BenchmarkIndexRune(b *testing.B) {
331 if got := IndexRune(benchmarkString, '☺'); got != 14 {
332 b.Fatalf("wrong index: expected 14, got=%d", got)
334 for i := 0; i < b.N; i++ {
335 IndexRune(benchmarkString, '☺')
339 var benchmarkLongString = Repeat(" ", 100) + benchmarkString
341 func BenchmarkIndexRuneLongString(b *testing.B) {
342 if got := IndexRune(benchmarkLongString, '☺'); got != 114 {
343 b.Fatalf("wrong index: expected 114, got=%d", got)
345 for i := 0; i < b.N; i++ {
346 IndexRune(benchmarkLongString, '☺')
350 func BenchmarkIndexRuneFastPath(b *testing.B) {
351 if got := IndexRune(benchmarkString, 'v'); got != 17 {
352 b.Fatalf("wrong index: expected 17, got=%d", got)
354 for i := 0; i < b.N; i++ {
355 IndexRune(benchmarkString, 'v')
359 func BenchmarkIndex(b *testing.B) {
360 if got := Index(benchmarkString, "v"); got != 17 {
361 b.Fatalf("wrong index: expected 17, got=%d", got)
363 for i := 0; i < b.N; i++ {
364 Index(benchmarkString, "v")
368 func BenchmarkLastIndex(b *testing.B) {
369 if got := Index(benchmarkString, "v"); got != 17 {
370 b.Fatalf("wrong index: expected 17, got=%d", got)
372 for i := 0; i < b.N; i++ {
373 LastIndex(benchmarkString, "v")
377 func BenchmarkIndexByte(b *testing.B) {
378 if got := IndexByte(benchmarkString, 'v'); got != 17 {
379 b.Fatalf("wrong index: expected 17, got=%d", got)
381 for i := 0; i < b.N; i++ {
382 IndexByte(benchmarkString, 'v')
386 type SplitTest struct {
387 s string
388 sep string
389 n int
390 a []string
393 var splittests = []SplitTest{
394 {"", "", -1, []string{}},
395 {abcd, "", 2, []string{"a", "bcd"}},
396 {abcd, "", 4, []string{"a", "b", "c", "d"}},
397 {abcd, "", -1, []string{"a", "b", "c", "d"}},
398 {faces, "", -1, []string{"☺", "☻", "☹"}},
399 {faces, "", 3, []string{"☺", "☻", "☹"}},
400 {faces, "", 17, []string{"☺", "☻", "☹"}},
401 {"☺�☹", "", -1, []string{"☺", "�", "☹"}},
402 {abcd, "a", 0, nil},
403 {abcd, "a", -1, []string{"", "bcd"}},
404 {abcd, "z", -1, []string{"abcd"}},
405 {commas, ",", -1, []string{"1", "2", "3", "4"}},
406 {dots, "...", -1, []string{"1", ".2", ".3", ".4"}},
407 {faces, "☹", -1, []string{"☺☻", ""}},
408 {faces, "~", -1, []string{faces}},
409 {"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
410 {"1 2", " ", 3, []string{"1", "2"}},
413 func TestSplit(t *testing.T) {
414 for _, tt := range splittests {
415 a := SplitN(tt.s, tt.sep, tt.n)
416 if !eq(a, tt.a) {
417 t.Errorf("Split(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, a, tt.a)
418 continue
420 if tt.n == 0 {
421 continue
423 s := Join(a, tt.sep)
424 if s != tt.s {
425 t.Errorf("Join(Split(%q, %q, %d), %q) = %q", tt.s, tt.sep, tt.n, tt.sep, s)
427 if tt.n < 0 {
428 b := Split(tt.s, tt.sep)
429 if !reflect.DeepEqual(a, b) {
430 t.Errorf("Split disagrees with SplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
436 var splitaftertests = []SplitTest{
437 {abcd, "a", -1, []string{"a", "bcd"}},
438 {abcd, "z", -1, []string{"abcd"}},
439 {abcd, "", -1, []string{"a", "b", "c", "d"}},
440 {commas, ",", -1, []string{"1,", "2,", "3,", "4"}},
441 {dots, "...", -1, []string{"1...", ".2...", ".3...", ".4"}},
442 {faces, "☹", -1, []string{"☺☻☹", ""}},
443 {faces, "~", -1, []string{faces}},
444 {faces, "", -1, []string{"☺", "☻", "☹"}},
445 {"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}},
446 {"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}},
447 {"1 2", " ", 3, []string{"1 ", "2"}},
448 {"123", "", 2, []string{"1", "23"}},
449 {"123", "", 17, []string{"1", "2", "3"}},
452 func TestSplitAfter(t *testing.T) {
453 for _, tt := range splitaftertests {
454 a := SplitAfterN(tt.s, tt.sep, tt.n)
455 if !eq(a, tt.a) {
456 t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, a, tt.a)
457 continue
459 s := Join(a, "")
460 if s != tt.s {
461 t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
463 if tt.n < 0 {
464 b := SplitAfter(tt.s, tt.sep)
465 if !reflect.DeepEqual(a, b) {
466 t.Errorf("SplitAfter disagrees with SplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
472 type FieldsTest struct {
473 s string
474 a []string
477 var fieldstests = []FieldsTest{
478 {"", []string{}},
479 {" ", []string{}},
480 {" \t ", []string{}},
481 {"\u2000", []string{}},
482 {" abc ", []string{"abc"}},
483 {"1 2 3 4", []string{"1", "2", "3", "4"}},
484 {"1 2 3 4", []string{"1", "2", "3", "4"}},
485 {"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
486 {"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
487 {"\u2000\u2001\u2002", []string{}},
488 {"\n\t\n", []string{"™", "™"}},
489 {"\n\u20001™2\u2000 \u2001 ™", []string{"1™2", "™"}},
490 {"\n1\uFFFD \uFFFD2\u20003\uFFFD4", []string{"1\uFFFD", "\uFFFD2", "3\uFFFD4"}},
491 {"1\xFF\u2000\xFF2\xFF \xFF", []string{"1\xFF", "\xFF2\xFF", "\xFF"}},
492 {faces, []string{faces}},
495 func TestFields(t *testing.T) {
496 for _, tt := range fieldstests {
497 a := Fields(tt.s)
498 if !eq(a, tt.a) {
499 t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a)
500 continue
505 var FieldsFuncTests = []FieldsTest{
506 {"", []string{}},
507 {"XX", []string{}},
508 {"XXhiXXX", []string{"hi"}},
509 {"aXXbXXXcX", []string{"a", "b", "c"}},
512 func TestFieldsFunc(t *testing.T) {
513 for _, tt := range fieldstests {
514 a := FieldsFunc(tt.s, unicode.IsSpace)
515 if !eq(a, tt.a) {
516 t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a)
517 continue
520 pred := func(c rune) bool { return c == 'X' }
521 for _, tt := range FieldsFuncTests {
522 a := FieldsFunc(tt.s, pred)
523 if !eq(a, tt.a) {
524 t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
529 // Test case for any function which accepts and returns a single string.
530 type StringTest struct {
531 in, out string
534 // Execute f on each test case. funcName should be the name of f; it's used
535 // in failure reports.
536 func runStringTests(t *testing.T, f func(string) string, funcName string, testCases []StringTest) {
537 for _, tc := range testCases {
538 actual := f(tc.in)
539 if actual != tc.out {
540 t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out)
545 var upperTests = []StringTest{
546 {"", ""},
547 {"ONLYUPPER", "ONLYUPPER"},
548 {"abc", "ABC"},
549 {"AbC123", "ABC123"},
550 {"azAZ09_", "AZAZ09_"},
551 {"longStrinGwitHmixofsmaLLandcAps", "LONGSTRINGWITHMIXOFSMALLANDCAPS"},
552 {"long\u0250string\u0250with\u0250nonascii\u2C6Fchars", "LONG\u2C6FSTRING\u2C6FWITH\u2C6FNONASCII\u2C6FCHARS"},
553 {"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
554 {"a\u0080\U0010FFFF", "A\u0080\U0010FFFF"}, // test utf8.RuneSelf and utf8.MaxRune
557 var lowerTests = []StringTest{
558 {"", ""},
559 {"abc", "abc"},
560 {"AbC123", "abc123"},
561 {"azAZ09_", "azaz09_"},
562 {"longStrinGwitHmixofsmaLLandcAps", "longstringwithmixofsmallandcaps"},
563 {"LONG\u2C6FSTRING\u2C6FWITH\u2C6FNONASCII\u2C6FCHARS", "long\u0250string\u0250with\u0250nonascii\u0250chars"},
564 {"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
565 {"A\u0080\U0010FFFF", "a\u0080\U0010FFFF"}, // test utf8.RuneSelf and utf8.MaxRune
568 const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
570 var trimSpaceTests = []StringTest{
571 {"", ""},
572 {"abc", "abc"},
573 {space + "abc" + space, "abc"},
574 {" ", ""},
575 {" \t\r\n \t\t\r\r\n\n ", ""},
576 {" \t\r\n x\t\t\r\r\n\n ", "x"},
577 {" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
578 {"1 \t\r\n2", "1 \t\r\n2"},
579 {" x\x80", "x\x80"},
580 {" x\xc0", "x\xc0"},
581 {"x \xc0\xc0 ", "x \xc0\xc0"},
582 {"x \xc0", "x \xc0"},
583 {"x \xc0 ", "x \xc0"},
584 {"x \xc0\xc0 ", "x \xc0\xc0"},
585 {"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"},
586 {"x ☺ ", "x ☺"},
589 func tenRunes(ch rune) string {
590 r := make([]rune, 10)
591 for i := range r {
592 r[i] = ch
594 return string(r)
597 // User-defined self-inverse mapping function
598 func rot13(r rune) rune {
599 step := rune(13)
600 if r >= 'a' && r <= 'z' {
601 return ((r - 'a' + step) % 26) + 'a'
603 if r >= 'A' && r <= 'Z' {
604 return ((r - 'A' + step) % 26) + 'A'
606 return r
609 func TestMap(t *testing.T) {
610 // Run a couple of awful growth/shrinkage tests
611 a := tenRunes('a')
612 // 1. Grow. This triggers two reallocations in Map.
613 maxRune := func(rune) rune { return unicode.MaxRune }
614 m := Map(maxRune, a)
615 expect := tenRunes(unicode.MaxRune)
616 if m != expect {
617 t.Errorf("growing: expected %q got %q", expect, m)
620 // 2. Shrink
621 minRune := func(rune) rune { return 'a' }
622 m = Map(minRune, tenRunes(unicode.MaxRune))
623 expect = a
624 if m != expect {
625 t.Errorf("shrinking: expected %q got %q", expect, m)
628 // 3. Rot13
629 m = Map(rot13, "a to zed")
630 expect = "n gb mrq"
631 if m != expect {
632 t.Errorf("rot13: expected %q got %q", expect, m)
635 // 4. Rot13^2
636 m = Map(rot13, Map(rot13, "a to zed"))
637 expect = "a to zed"
638 if m != expect {
639 t.Errorf("rot13: expected %q got %q", expect, m)
642 // 5. Drop
643 dropNotLatin := func(r rune) rune {
644 if unicode.Is(unicode.Latin, r) {
645 return r
647 return -1
649 m = Map(dropNotLatin, "Hello, 세계")
650 expect = "Hello"
651 if m != expect {
652 t.Errorf("drop: expected %q got %q", expect, m)
655 // 6. Identity
656 identity := func(r rune) rune {
657 return r
659 orig := "Input string that we expect not to be copied."
660 m = Map(identity, orig)
661 if (*reflect.StringHeader)(unsafe.Pointer(&orig)).Data !=
662 (*reflect.StringHeader)(unsafe.Pointer(&m)).Data {
663 t.Error("unexpected copy during identity map")
666 // 7. Handle invalid UTF-8 sequence
667 replaceNotLatin := func(r rune) rune {
668 if unicode.Is(unicode.Latin, r) {
669 return r
671 return utf8.RuneError
673 m = Map(replaceNotLatin, "Hello\255World")
674 expect = "Hello\uFFFDWorld"
675 if m != expect {
676 t.Errorf("replace invalid sequence: expected %q got %q", expect, m)
679 // 8. Check utf8.RuneSelf and utf8.MaxRune encoding
680 encode := func(r rune) rune {
681 switch r {
682 case utf8.RuneSelf:
683 return unicode.MaxRune
684 case unicode.MaxRune:
685 return utf8.RuneSelf
687 return r
689 s := string(rune(utf8.RuneSelf)) + string(utf8.MaxRune)
690 r := string(utf8.MaxRune) + string(rune(utf8.RuneSelf)) // reverse of s
691 m = Map(encode, s)
692 if m != r {
693 t.Errorf("encoding not handled correctly: expected %q got %q", r, m)
695 m = Map(encode, r)
696 if m != s {
697 t.Errorf("encoding not handled correctly: expected %q got %q", s, m)
700 // 9. Check mapping occurs in the front, middle and back
701 trimSpaces := func(r rune) rune {
702 if unicode.IsSpace(r) {
703 return -1
705 return r
707 m = Map(trimSpaces, " abc 123 ")
708 expect = "abc123"
709 if m != expect {
710 t.Errorf("trimSpaces: expected %q got %q", expect, m)
714 func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }
716 func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTests) }
718 var toValidUTF8Tests = []struct {
719 in string
720 repl string
721 out string
723 {"", "\uFFFD", ""},
724 {"abc", "\uFFFD", "abc"},
725 {"\uFDDD", "\uFFFD", "\uFDDD"},
726 {"a\xffb", "\uFFFD", "a\uFFFDb"},
727 {"a\xffb\uFFFD", "X", "aXb\uFFFD"},
728 {"a☺\xffb☺\xC0\xAFc☺\xff", "", "a☺b☺c☺"},
729 {"a☺\xffb☺\xC0\xAFc☺\xff", "日本語", "a☺日本語b☺日本語c☺日本語"},
730 {"\xC0\xAF", "\uFFFD", "\uFFFD"},
731 {"\xE0\x80\xAF", "\uFFFD", "\uFFFD"},
732 {"\xed\xa0\x80", "abc", "abc"},
733 {"\xed\xbf\xbf", "\uFFFD", "\uFFFD"},
734 {"\xF0\x80\x80\xaf", "☺", "☺"},
735 {"\xF8\x80\x80\x80\xAF", "\uFFFD", "\uFFFD"},
736 {"\xFC\x80\x80\x80\x80\xAF", "\uFFFD", "\uFFFD"},
739 func TestToValidUTF8(t *testing.T) {
740 for _, tc := range toValidUTF8Tests {
741 got := ToValidUTF8(tc.in, tc.repl)
742 if got != tc.out {
743 t.Errorf("ToValidUTF8(%q, %q) = %q; want %q", tc.in, tc.repl, got, tc.out)
748 func BenchmarkToUpper(b *testing.B) {
749 for _, tc := range upperTests {
750 b.Run(tc.in, func(b *testing.B) {
751 for i := 0; i < b.N; i++ {
752 actual := ToUpper(tc.in)
753 if actual != tc.out {
754 b.Errorf("ToUpper(%q) = %q; want %q", tc.in, actual, tc.out)
761 func BenchmarkToLower(b *testing.B) {
762 for _, tc := range lowerTests {
763 b.Run(tc.in, func(b *testing.B) {
764 for i := 0; i < b.N; i++ {
765 actual := ToLower(tc.in)
766 if actual != tc.out {
767 b.Errorf("ToLower(%q) = %q; want %q", tc.in, actual, tc.out)
774 func BenchmarkMapNoChanges(b *testing.B) {
775 identity := func(r rune) rune {
776 return r
778 for i := 0; i < b.N; i++ {
779 Map(identity, "Some string that won't be modified.")
783 func TestSpecialCase(t *testing.T) {
784 lower := "abcçdefgğhıijklmnoöprsştuüvyz"
785 upper := "ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ"
786 u := ToUpperSpecial(unicode.TurkishCase, upper)
787 if u != upper {
788 t.Errorf("Upper(upper) is %s not %s", u, upper)
790 u = ToUpperSpecial(unicode.TurkishCase, lower)
791 if u != upper {
792 t.Errorf("Upper(lower) is %s not %s", u, upper)
794 l := ToLowerSpecial(unicode.TurkishCase, lower)
795 if l != lower {
796 t.Errorf("Lower(lower) is %s not %s", l, lower)
798 l = ToLowerSpecial(unicode.TurkishCase, upper)
799 if l != lower {
800 t.Errorf("Lower(upper) is %s not %s", l, lower)
804 func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) }
806 var trimTests = []struct {
807 f string
808 in, arg, out string
810 {"Trim", "abba", "a", "bb"},
811 {"Trim", "abba", "ab", ""},
812 {"TrimLeft", "abba", "ab", ""},
813 {"TrimRight", "abba", "ab", ""},
814 {"TrimLeft", "abba", "a", "bba"},
815 {"TrimLeft", "abba", "b", "abba"},
816 {"TrimRight", "abba", "a", "abb"},
817 {"TrimRight", "abba", "b", "abba"},
818 {"Trim", "<tag>", "<>", "tag"},
819 {"Trim", "* listitem", " *", "listitem"},
820 {"Trim", `"quote"`, `"`, "quote"},
821 {"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
822 {"Trim", "\x80test\xff", "\xff", "test"},
823 {"Trim", " Ġ ", " ", "Ġ"},
824 {"Trim", " Ġİ0", "0 ", "Ġİ"},
825 //empty string tests
826 {"Trim", "abba", "", "abba"},
827 {"Trim", "", "123", ""},
828 {"Trim", "", "", ""},
829 {"TrimLeft", "abba", "", "abba"},
830 {"TrimLeft", "", "123", ""},
831 {"TrimLeft", "", "", ""},
832 {"TrimRight", "abba", "", "abba"},
833 {"TrimRight", "", "123", ""},
834 {"TrimRight", "", "", ""},
835 {"TrimRight", "☺\xc0", "☺", "☺\xc0"},
836 {"TrimPrefix", "aabb", "a", "abb"},
837 {"TrimPrefix", "aabb", "b", "aabb"},
838 {"TrimSuffix", "aabb", "a", "aabb"},
839 {"TrimSuffix", "aabb", "b", "aab"},
842 func TestTrim(t *testing.T) {
843 for _, tc := range trimTests {
844 name := tc.f
845 var f func(string, string) string
846 switch name {
847 case "Trim":
848 f = Trim
849 case "TrimLeft":
850 f = TrimLeft
851 case "TrimRight":
852 f = TrimRight
853 case "TrimPrefix":
854 f = TrimPrefix
855 case "TrimSuffix":
856 f = TrimSuffix
857 default:
858 t.Errorf("Undefined trim function %s", name)
860 actual := f(tc.in, tc.arg)
861 if actual != tc.out {
862 t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
867 func BenchmarkTrim(b *testing.B) {
868 b.ReportAllocs()
870 for i := 0; i < b.N; i++ {
871 for _, tc := range trimTests {
872 name := tc.f
873 var f func(string, string) string
874 switch name {
875 case "Trim":
876 f = Trim
877 case "TrimLeft":
878 f = TrimLeft
879 case "TrimRight":
880 f = TrimRight
881 case "TrimPrefix":
882 f = TrimPrefix
883 case "TrimSuffix":
884 f = TrimSuffix
885 default:
886 b.Errorf("Undefined trim function %s", name)
888 actual := f(tc.in, tc.arg)
889 if actual != tc.out {
890 b.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
896 func BenchmarkToValidUTF8(b *testing.B) {
897 tests := []struct {
898 name string
899 input string
901 {"Valid", "typical"},
902 {"InvalidASCII", "foo\xffbar"},
903 {"InvalidNonASCII", "日本語\xff日本語"},
905 replacement := "\uFFFD"
906 b.ResetTimer()
907 for _, test := range tests {
908 b.Run(test.name, func(b *testing.B) {
909 for i := 0; i < b.N; i++ {
910 ToValidUTF8(test.input, replacement)
916 type predicate struct {
917 f func(rune) bool
918 name string
921 var isSpace = predicate{unicode.IsSpace, "IsSpace"}
922 var isDigit = predicate{unicode.IsDigit, "IsDigit"}
923 var isUpper = predicate{unicode.IsUpper, "IsUpper"}
924 var isValidRune = predicate{
925 func(r rune) bool {
926 return r != utf8.RuneError
928 "IsValidRune",
931 func not(p predicate) predicate {
932 return predicate{
933 func(r rune) bool {
934 return !p.f(r)
936 "not " + p.name,
940 var trimFuncTests = []struct {
941 f predicate
942 in string
943 trimOut string
944 leftOut string
945 rightOut string
947 {isSpace, space + " hello " + space,
948 "hello",
949 "hello " + space,
950 space + " hello"},
951 {isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51",
952 "hello",
953 "hello34\u0e50\u0e51",
954 "\u0e50\u0e5212hello"},
955 {isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F",
956 "hello",
957 "helloEF\u2C6F\u2C6FGH\u2C6F\u2C6F",
958 "\u2C6F\u2C6F\u2C6F\u2C6FABCDhello"},
959 {not(isSpace), "hello" + space + "hello",
960 space,
961 space + "hello",
962 "hello" + space},
963 {not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo",
964 "\u0e50\u0e521234\u0e50\u0e51",
965 "\u0e50\u0e521234\u0e50\u0e51helo",
966 "hello\u0e50\u0e521234\u0e50\u0e51"},
967 {isValidRune, "ab\xc0a\xc0cd",
968 "\xc0a\xc0",
969 "\xc0a\xc0cd",
970 "ab\xc0a\xc0"},
971 {not(isValidRune), "\xc0a\xc0",
972 "a",
973 "a\xc0",
974 "\xc0a"},
975 {isSpace, "",
978 ""},
979 {isSpace, " ",
982 ""},
985 func TestTrimFunc(t *testing.T) {
986 for _, tc := range trimFuncTests {
987 trimmers := []struct {
988 name string
989 trim func(s string, f func(r rune) bool) string
990 out string
992 {"TrimFunc", TrimFunc, tc.trimOut},
993 {"TrimLeftFunc", TrimLeftFunc, tc.leftOut},
994 {"TrimRightFunc", TrimRightFunc, tc.rightOut},
996 for _, trimmer := range trimmers {
997 actual := trimmer.trim(tc.in, tc.f.f)
998 if actual != trimmer.out {
999 t.Errorf("%s(%q, %q) = %q; want %q", trimmer.name, tc.in, tc.f.name, actual, trimmer.out)
1005 var indexFuncTests = []struct {
1006 in string
1007 f predicate
1008 first, last int
1010 {"", isValidRune, -1, -1},
1011 {"abc", isDigit, -1, -1},
1012 {"0123", isDigit, 0, 3},
1013 {"a1b", isDigit, 1, 1},
1014 {space, isSpace, 0, len(space) - 3}, // last rune in space is 3 bytes
1015 {"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit, 0, 18},
1016 {"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34},
1017 {"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12},
1019 // tests of invalid UTF-8
1020 {"\x801", isDigit, 1, 1},
1021 {"\x80abc", isDigit, -1, -1},
1022 {"\xc0a\xc0", isValidRune, 1, 1},
1023 {"\xc0a\xc0", not(isValidRune), 0, 2},
1024 {"\xc0\xc0", not(isValidRune), 0, 4},
1025 {"\xc0\xc0\xc0", not(isValidRune), 0, 5},
1026 {"ab\xc0a\xc0cd", not(isValidRune), 2, 4},
1027 {"a\xe0\x80cd", not(isValidRune), 1, 2},
1028 {"\x80\x80\x80\x80", not(isValidRune), 0, 3},
1031 func TestIndexFunc(t *testing.T) {
1032 for _, tc := range indexFuncTests {
1033 first := IndexFunc(tc.in, tc.f.f)
1034 if first != tc.first {
1035 t.Errorf("IndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, first, tc.first)
1037 last := LastIndexFunc(tc.in, tc.f.f)
1038 if last != tc.last {
1039 t.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, last, tc.last)
1044 func equal(m string, s1, s2 string, t *testing.T) bool {
1045 if s1 == s2 {
1046 return true
1048 e1 := Split(s1, "")
1049 e2 := Split(s2, "")
1050 for i, c1 := range e1 {
1051 if i >= len(e2) {
1052 break
1054 r1, _ := utf8.DecodeRuneInString(c1)
1055 r2, _ := utf8.DecodeRuneInString(e2[i])
1056 if r1 != r2 {
1057 t.Errorf("%s diff at %d: U+%04X U+%04X", m, i, r1, r2)
1060 return false
1063 func TestCaseConsistency(t *testing.T) {
1064 // Make a string of all the runes.
1065 numRunes := int(unicode.MaxRune + 1)
1066 if testing.Short() {
1067 numRunes = 1000
1069 a := make([]rune, numRunes)
1070 for i := range a {
1071 a[i] = rune(i)
1073 s := string(a)
1074 // convert the cases.
1075 upper := ToUpper(s)
1076 lower := ToLower(s)
1078 // Consistency checks
1079 if n := utf8.RuneCountInString(upper); n != numRunes {
1080 t.Error("rune count wrong in upper:", n)
1082 if n := utf8.RuneCountInString(lower); n != numRunes {
1083 t.Error("rune count wrong in lower:", n)
1085 if !equal("ToUpper(upper)", ToUpper(upper), upper, t) {
1086 t.Error("ToUpper(upper) consistency fail")
1088 if !equal("ToLower(lower)", ToLower(lower), lower, t) {
1089 t.Error("ToLower(lower) consistency fail")
1092 These fail because of non-one-to-oneness of the data, such as multiple
1093 upper case 'I' mapping to 'i'. We comment them out but keep them for
1094 interest.
1095 For instance: CAPITAL LETTER I WITH DOT ABOVE:
1096 unicode.ToUpper(unicode.ToLower('\u0130')) != '\u0130'
1098 if !equal("ToUpper(lower)", ToUpper(lower), upper, t) {
1099 t.Error("ToUpper(lower) consistency fail");
1101 if !equal("ToLower(upper)", ToLower(upper), lower, t) {
1102 t.Error("ToLower(upper) consistency fail");
1107 var RepeatTests = []struct {
1108 in, out string
1109 count int
1111 {"", "", 0},
1112 {"", "", 1},
1113 {"", "", 2},
1114 {"-", "", 0},
1115 {"-", "-", 1},
1116 {"-", "----------", 10},
1117 {"abc ", "abc abc abc ", 3},
1120 func TestRepeat(t *testing.T) {
1121 for _, tt := range RepeatTests {
1122 a := Repeat(tt.in, tt.count)
1123 if !equal("Repeat(s)", a, tt.out, t) {
1124 t.Errorf("Repeat(%v, %d) = %v; want %v", tt.in, tt.count, a, tt.out)
1125 continue
1130 func repeat(s string, count int) (err error) {
1131 defer func() {
1132 if r := recover(); r != nil {
1133 switch v := r.(type) {
1134 case error:
1135 err = v
1136 default:
1137 err = fmt.Errorf("%s", v)
1142 Repeat(s, count)
1144 return
1147 // See Issue golang.org/issue/16237
1148 func TestRepeatCatchesOverflow(t *testing.T) {
1149 tests := [...]struct {
1150 s string
1151 count int
1152 errStr string
1154 0: {"--", -2147483647, "negative"},
1155 1: {"", int(^uint(0) >> 1), ""},
1156 2: {"-", 10, ""},
1157 3: {"gopher", 0, ""},
1158 4: {"-", -1, "negative"},
1159 5: {"--", -102, "negative"},
1160 6: {string(make([]byte, 255)), int((^uint(0))/255 + 1), "overflow"},
1163 for i, tt := range tests {
1164 err := repeat(tt.s, tt.count)
1165 if tt.errStr == "" {
1166 if err != nil {
1167 t.Errorf("#%d panicked %v", i, err)
1169 continue
1172 if err == nil || !Contains(err.Error(), tt.errStr) {
1173 t.Errorf("#%d expected %q got %q", i, tt.errStr, err)
1178 func runesEqual(a, b []rune) bool {
1179 if len(a) != len(b) {
1180 return false
1182 for i, r := range a {
1183 if r != b[i] {
1184 return false
1187 return true
1190 var RunesTests = []struct {
1191 in string
1192 out []rune
1193 lossy bool
1195 {"", []rune{}, false},
1196 {" ", []rune{32}, false},
1197 {"ABC", []rune{65, 66, 67}, false},
1198 {"abc", []rune{97, 98, 99}, false},
1199 {"\u65e5\u672c\u8a9e", []rune{26085, 26412, 35486}, false},
1200 {"ab\x80c", []rune{97, 98, 0xFFFD, 99}, true},
1201 {"ab\xc0c", []rune{97, 98, 0xFFFD, 99}, true},
1204 func TestRunes(t *testing.T) {
1205 for _, tt := range RunesTests {
1206 a := []rune(tt.in)
1207 if !runesEqual(a, tt.out) {
1208 t.Errorf("[]rune(%q) = %v; want %v", tt.in, a, tt.out)
1209 continue
1211 if !tt.lossy {
1212 // can only test reassembly if we didn't lose information
1213 s := string(a)
1214 if s != tt.in {
1215 t.Errorf("string([]rune(%q)) = %x; want %x", tt.in, s, tt.in)
1221 func TestReadByte(t *testing.T) {
1222 testStrings := []string{"", abcd, faces, commas}
1223 for _, s := range testStrings {
1224 reader := NewReader(s)
1225 if e := reader.UnreadByte(); e == nil {
1226 t.Errorf("Unreading %q at beginning: expected error", s)
1228 var res bytes.Buffer
1229 for {
1230 b, e := reader.ReadByte()
1231 if e == io.EOF {
1232 break
1234 if e != nil {
1235 t.Errorf("Reading %q: %s", s, e)
1236 break
1238 res.WriteByte(b)
1239 // unread and read again
1240 e = reader.UnreadByte()
1241 if e != nil {
1242 t.Errorf("Unreading %q: %s", s, e)
1243 break
1245 b1, e := reader.ReadByte()
1246 if e != nil {
1247 t.Errorf("Reading %q after unreading: %s", s, e)
1248 break
1250 if b1 != b {
1251 t.Errorf("Reading %q after unreading: want byte %q, got %q", s, b, b1)
1252 break
1255 if res.String() != s {
1256 t.Errorf("Reader(%q).ReadByte() produced %q", s, res.String())
1261 func TestReadRune(t *testing.T) {
1262 testStrings := []string{"", abcd, faces, commas}
1263 for _, s := range testStrings {
1264 reader := NewReader(s)
1265 if e := reader.UnreadRune(); e == nil {
1266 t.Errorf("Unreading %q at beginning: expected error", s)
1268 res := ""
1269 for {
1270 r, z, e := reader.ReadRune()
1271 if e == io.EOF {
1272 break
1274 if e != nil {
1275 t.Errorf("Reading %q: %s", s, e)
1276 break
1278 res += string(r)
1279 // unread and read again
1280 e = reader.UnreadRune()
1281 if e != nil {
1282 t.Errorf("Unreading %q: %s", s, e)
1283 break
1285 r1, z1, e := reader.ReadRune()
1286 if e != nil {
1287 t.Errorf("Reading %q after unreading: %s", s, e)
1288 break
1290 if r1 != r {
1291 t.Errorf("Reading %q after unreading: want rune %q, got %q", s, r, r1)
1292 break
1294 if z1 != z {
1295 t.Errorf("Reading %q after unreading: want size %d, got %d", s, z, z1)
1296 break
1299 if res != s {
1300 t.Errorf("Reader(%q).ReadRune() produced %q", s, res)
1305 var UnreadRuneErrorTests = []struct {
1306 name string
1307 f func(*Reader)
1309 {"Read", func(r *Reader) { r.Read([]byte{0}) }},
1310 {"ReadByte", func(r *Reader) { r.ReadByte() }},
1311 {"UnreadRune", func(r *Reader) { r.UnreadRune() }},
1312 {"Seek", func(r *Reader) { r.Seek(0, io.SeekCurrent) }},
1313 {"WriteTo", func(r *Reader) { r.WriteTo(&bytes.Buffer{}) }},
1316 func TestUnreadRuneError(t *testing.T) {
1317 for _, tt := range UnreadRuneErrorTests {
1318 reader := NewReader("0123456789")
1319 if _, _, err := reader.ReadRune(); err != nil {
1320 // should not happen
1321 t.Fatal(err)
1323 tt.f(reader)
1324 err := reader.UnreadRune()
1325 if err == nil {
1326 t.Errorf("Unreading after %s: expected error", tt.name)
1331 var ReplaceTests = []struct {
1332 in string
1333 old, new string
1334 n int
1335 out string
1337 {"hello", "l", "L", 0, "hello"},
1338 {"hello", "l", "L", -1, "heLLo"},
1339 {"hello", "x", "X", -1, "hello"},
1340 {"", "x", "X", -1, ""},
1341 {"radar", "r", "<r>", -1, "<r>ada<r>"},
1342 {"", "", "<>", -1, "<>"},
1343 {"banana", "a", "<>", -1, "b<>n<>n<>"},
1344 {"banana", "a", "<>", 1, "b<>nana"},
1345 {"banana", "a", "<>", 1000, "b<>n<>n<>"},
1346 {"banana", "an", "<>", -1, "b<><>a"},
1347 {"banana", "ana", "<>", -1, "b<>na"},
1348 {"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"},
1349 {"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
1350 {"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
1351 {"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
1352 {"banana", "", "<>", 1, "<>banana"},
1353 {"banana", "a", "a", -1, "banana"},
1354 {"banana", "a", "a", 1, "banana"},
1355 {"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"},
1358 func TestReplace(t *testing.T) {
1359 for _, tt := range ReplaceTests {
1360 if s := Replace(tt.in, tt.old, tt.new, tt.n); s != tt.out {
1361 t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
1363 if tt.n == -1 {
1364 s := ReplaceAll(tt.in, tt.old, tt.new)
1365 if s != tt.out {
1366 t.Errorf("ReplaceAll(%q, %q, %q) = %q, want %q", tt.in, tt.old, tt.new, s, tt.out)
1372 var TitleTests = []struct {
1373 in, out string
1375 {"", ""},
1376 {"a", "A"},
1377 {" aaa aaa aaa ", " Aaa Aaa Aaa "},
1378 {" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
1379 {"123a456", "123a456"},
1380 {"double-blind", "Double-Blind"},
1381 {"ÿøû", "Ÿøû"},
1382 {"with_underscore", "With_underscore"},
1383 {"unicode \xe2\x80\xa8 line separator", "Unicode \xe2\x80\xa8 Line Separator"},
1386 func TestTitle(t *testing.T) {
1387 for _, tt := range TitleTests {
1388 if s := Title(tt.in); s != tt.out {
1389 t.Errorf("Title(%q) = %q, want %q", tt.in, s, tt.out)
1394 var ContainsTests = []struct {
1395 str, substr string
1396 expected bool
1398 {"abc", "bc", true},
1399 {"abc", "bcd", false},
1400 {"abc", "", true},
1401 {"", "a", false},
1403 // cases to cover code in runtime/asm_amd64.s:indexShortStr
1404 // 2-byte needle
1405 {"xxxxxx", "01", false},
1406 {"01xxxx", "01", true},
1407 {"xx01xx", "01", true},
1408 {"xxxx01", "01", true},
1409 {"01xxxxx"[1:], "01", false},
1410 {"xxxxx01"[:6], "01", false},
1411 // 3-byte needle
1412 {"xxxxxxx", "012", false},
1413 {"012xxxx", "012", true},
1414 {"xx012xx", "012", true},
1415 {"xxxx012", "012", true},
1416 {"012xxxxx"[1:], "012", false},
1417 {"xxxxx012"[:7], "012", false},
1418 // 4-byte needle
1419 {"xxxxxxxx", "0123", false},
1420 {"0123xxxx", "0123", true},
1421 {"xx0123xx", "0123", true},
1422 {"xxxx0123", "0123", true},
1423 {"0123xxxxx"[1:], "0123", false},
1424 {"xxxxx0123"[:8], "0123", false},
1425 // 5-7-byte needle
1426 {"xxxxxxxxx", "01234", false},
1427 {"01234xxxx", "01234", true},
1428 {"xx01234xx", "01234", true},
1429 {"xxxx01234", "01234", true},
1430 {"01234xxxxx"[1:], "01234", false},
1431 {"xxxxx01234"[:9], "01234", false},
1432 // 8-byte needle
1433 {"xxxxxxxxxxxx", "01234567", false},
1434 {"01234567xxxx", "01234567", true},
1435 {"xx01234567xx", "01234567", true},
1436 {"xxxx01234567", "01234567", true},
1437 {"01234567xxxxx"[1:], "01234567", false},
1438 {"xxxxx01234567"[:12], "01234567", false},
1439 // 9-15-byte needle
1440 {"xxxxxxxxxxxxx", "012345678", false},
1441 {"012345678xxxx", "012345678", true},
1442 {"xx012345678xx", "012345678", true},
1443 {"xxxx012345678", "012345678", true},
1444 {"012345678xxxxx"[1:], "012345678", false},
1445 {"xxxxx012345678"[:13], "012345678", false},
1446 // 16-byte needle
1447 {"xxxxxxxxxxxxxxxxxxxx", "0123456789ABCDEF", false},
1448 {"0123456789ABCDEFxxxx", "0123456789ABCDEF", true},
1449 {"xx0123456789ABCDEFxx", "0123456789ABCDEF", true},
1450 {"xxxx0123456789ABCDEF", "0123456789ABCDEF", true},
1451 {"0123456789ABCDEFxxxxx"[1:], "0123456789ABCDEF", false},
1452 {"xxxxx0123456789ABCDEF"[:20], "0123456789ABCDEF", false},
1453 // 17-31-byte needle
1454 {"xxxxxxxxxxxxxxxxxxxxx", "0123456789ABCDEFG", false},
1455 {"0123456789ABCDEFGxxxx", "0123456789ABCDEFG", true},
1456 {"xx0123456789ABCDEFGxx", "0123456789ABCDEFG", true},
1457 {"xxxx0123456789ABCDEFG", "0123456789ABCDEFG", true},
1458 {"0123456789ABCDEFGxxxxx"[1:], "0123456789ABCDEFG", false},
1459 {"xxxxx0123456789ABCDEFG"[:21], "0123456789ABCDEFG", false},
1461 // partial match cases
1462 {"xx01x", "012", false}, // 3
1463 {"xx0123x", "01234", false}, // 5-7
1464 {"xx01234567x", "012345678", false}, // 9-15
1465 {"xx0123456789ABCDEFx", "0123456789ABCDEFG", false}, // 17-31, issue 15679
1468 func TestContains(t *testing.T) {
1469 for _, ct := range ContainsTests {
1470 if Contains(ct.str, ct.substr) != ct.expected {
1471 t.Errorf("Contains(%s, %s) = %v, want %v",
1472 ct.str, ct.substr, !ct.expected, ct.expected)
1477 var ContainsAnyTests = []struct {
1478 str, substr string
1479 expected bool
1481 {"", "", false},
1482 {"", "a", false},
1483 {"", "abc", false},
1484 {"a", "", false},
1485 {"a", "a", true},
1486 {"aaa", "a", true},
1487 {"abc", "xyz", false},
1488 {"abc", "xcz", true},
1489 {"a☺b☻c☹d", "uvw☻xyz", true},
1490 {"aRegExp*", ".(|)*+?^$[]", true},
1491 {dots + dots + dots, " ", false},
1494 func TestContainsAny(t *testing.T) {
1495 for _, ct := range ContainsAnyTests {
1496 if ContainsAny(ct.str, ct.substr) != ct.expected {
1497 t.Errorf("ContainsAny(%s, %s) = %v, want %v",
1498 ct.str, ct.substr, !ct.expected, ct.expected)
1503 var ContainsRuneTests = []struct {
1504 str string
1505 r rune
1506 expected bool
1508 {"", 'a', false},
1509 {"a", 'a', true},
1510 {"aaa", 'a', true},
1511 {"abc", 'y', false},
1512 {"abc", 'c', true},
1513 {"a☺b☻c☹d", 'x', false},
1514 {"a☺b☻c☹d", '☻', true},
1515 {"aRegExp*", '*', true},
1518 func TestContainsRune(t *testing.T) {
1519 for _, ct := range ContainsRuneTests {
1520 if ContainsRune(ct.str, ct.r) != ct.expected {
1521 t.Errorf("ContainsRune(%q, %q) = %v, want %v",
1522 ct.str, ct.r, !ct.expected, ct.expected)
1527 var EqualFoldTests = []struct {
1528 s, t string
1529 out bool
1531 {"abc", "abc", true},
1532 {"ABcd", "ABcd", true},
1533 {"123abc", "123ABC", true},
1534 {"αβδ", "ΑΒΔ", true},
1535 {"abc", "xyz", false},
1536 {"abc", "XYZ", false},
1537 {"abcdefghijk", "abcdefghijX", false},
1538 {"abcdefghijk", "abcdefghij\u212A", true},
1539 {"abcdefghijK", "abcdefghij\u212A", true},
1540 {"abcdefghijkz", "abcdefghij\u212Ay", false},
1541 {"abcdefghijKz", "abcdefghij\u212Ay", false},
1542 {"1", "2", false},
1543 {"utf-8", "US-ASCII", false},
1546 func TestEqualFold(t *testing.T) {
1547 for _, tt := range EqualFoldTests {
1548 if out := EqualFold(tt.s, tt.t); out != tt.out {
1549 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.s, tt.t, out, tt.out)
1551 if out := EqualFold(tt.t, tt.s); out != tt.out {
1552 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.t, tt.s, out, tt.out)
1557 func BenchmarkEqualFold(b *testing.B) {
1558 for i := 0; i < b.N; i++ {
1559 for _, tt := range EqualFoldTests {
1560 if out := EqualFold(tt.s, tt.t); out != tt.out {
1561 b.Fatal("wrong result")
1567 var CountTests = []struct {
1568 s, sep string
1569 num int
1571 {"", "", 1},
1572 {"", "notempty", 0},
1573 {"notempty", "", 9},
1574 {"smaller", "not smaller", 0},
1575 {"12345678987654321", "6", 2},
1576 {"611161116", "6", 3},
1577 {"notequal", "NotEqual", 0},
1578 {"equal", "equal", 1},
1579 {"abc1231231123q", "123", 3},
1580 {"11111", "11", 2},
1583 func TestCount(t *testing.T) {
1584 for _, tt := range CountTests {
1585 if num := Count(tt.s, tt.sep); num != tt.num {
1586 t.Errorf("Count(%q, %q) = %d, want %d", tt.s, tt.sep, num, tt.num)
1591 var cutTests = []struct {
1592 s, sep string
1593 before, after string
1594 found bool
1596 {"abc", "b", "a", "c", true},
1597 {"abc", "a", "", "bc", true},
1598 {"abc", "c", "ab", "", true},
1599 {"abc", "abc", "", "", true},
1600 {"abc", "", "", "abc", true},
1601 {"abc", "d", "abc", "", false},
1602 {"", "d", "", "", false},
1603 {"", "", "", "", true},
1606 func TestCut(t *testing.T) {
1607 for _, tt := range cutTests {
1608 if before, after, found := Cut(tt.s, tt.sep); before != tt.before || after != tt.after || found != tt.found {
1609 t.Errorf("Cut(%q, %q) = %q, %q, %v, want %q, %q, %v", tt.s, tt.sep, before, after, found, tt.before, tt.after, tt.found)
1614 func makeBenchInputHard() string {
1615 tokens := [...]string{
1616 "<a>", "<p>", "<b>", "<strong>",
1617 "</a>", "</p>", "</b>", "</strong>",
1618 "hello", "world",
1620 x := make([]byte, 0, 1<<20)
1621 for {
1622 i := rand.Intn(len(tokens))
1623 if len(x)+len(tokens[i]) >= 1<<20 {
1624 break
1626 x = append(x, tokens[i]...)
1628 return string(x)
1631 var benchInputHard = makeBenchInputHard()
1633 func benchmarkIndexHard(b *testing.B, sep string) {
1634 for i := 0; i < b.N; i++ {
1635 Index(benchInputHard, sep)
1639 func benchmarkLastIndexHard(b *testing.B, sep string) {
1640 for i := 0; i < b.N; i++ {
1641 LastIndex(benchInputHard, sep)
1645 func benchmarkCountHard(b *testing.B, sep string) {
1646 for i := 0; i < b.N; i++ {
1647 Count(benchInputHard, sep)
1651 func BenchmarkIndexHard1(b *testing.B) { benchmarkIndexHard(b, "<>") }
1652 func BenchmarkIndexHard2(b *testing.B) { benchmarkIndexHard(b, "</pre>") }
1653 func BenchmarkIndexHard3(b *testing.B) { benchmarkIndexHard(b, "<b>hello world</b>") }
1654 func BenchmarkIndexHard4(b *testing.B) {
1655 benchmarkIndexHard(b, "<pre><b>hello</b><strong>world</strong></pre>")
1658 func BenchmarkLastIndexHard1(b *testing.B) { benchmarkLastIndexHard(b, "<>") }
1659 func BenchmarkLastIndexHard2(b *testing.B) { benchmarkLastIndexHard(b, "</pre>") }
1660 func BenchmarkLastIndexHard3(b *testing.B) { benchmarkLastIndexHard(b, "<b>hello world</b>") }
1662 func BenchmarkCountHard1(b *testing.B) { benchmarkCountHard(b, "<>") }
1663 func BenchmarkCountHard2(b *testing.B) { benchmarkCountHard(b, "</pre>") }
1664 func BenchmarkCountHard3(b *testing.B) { benchmarkCountHard(b, "<b>hello world</b>") }
1666 var benchInputTorture = Repeat("ABC", 1<<10) + "123" + Repeat("ABC", 1<<10)
1667 var benchNeedleTorture = Repeat("ABC", 1<<10+1)
1669 func BenchmarkIndexTorture(b *testing.B) {
1670 for i := 0; i < b.N; i++ {
1671 Index(benchInputTorture, benchNeedleTorture)
1675 func BenchmarkCountTorture(b *testing.B) {
1676 for i := 0; i < b.N; i++ {
1677 Count(benchInputTorture, benchNeedleTorture)
1681 func BenchmarkCountTortureOverlapping(b *testing.B) {
1682 A := Repeat("ABC", 1<<20)
1683 B := Repeat("ABC", 1<<10)
1684 for i := 0; i < b.N; i++ {
1685 Count(A, B)
1689 func BenchmarkCountByte(b *testing.B) {
1690 indexSizes := []int{10, 32, 4 << 10, 4 << 20, 64 << 20}
1691 benchStr := Repeat(benchmarkString,
1692 (indexSizes[len(indexSizes)-1]+len(benchmarkString)-1)/len(benchmarkString))
1693 benchFunc := func(b *testing.B, benchStr string) {
1694 b.SetBytes(int64(len(benchStr)))
1695 for i := 0; i < b.N; i++ {
1696 Count(benchStr, "=")
1699 for _, size := range indexSizes {
1700 b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
1701 benchFunc(b, benchStr[:size])
1707 var makeFieldsInput = func() string {
1708 x := make([]byte, 1<<20)
1709 // Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
1710 for i := range x {
1711 switch rand.Intn(10) {
1712 case 0:
1713 x[i] = ' '
1714 case 1:
1715 if i > 0 && x[i-1] == 'x' {
1716 copy(x[i-1:], "χ")
1717 break
1719 fallthrough
1720 default:
1721 x[i] = 'x'
1724 return string(x)
1727 var makeFieldsInputASCII = func() string {
1728 x := make([]byte, 1<<20)
1729 // Input is ~10% space, rest ASCII non-space.
1730 for i := range x {
1731 if rand.Intn(10) == 0 {
1732 x[i] = ' '
1733 } else {
1734 x[i] = 'x'
1737 return string(x)
1740 var stringdata = []struct{ name, data string }{
1741 {"ASCII", makeFieldsInputASCII()},
1742 {"Mixed", makeFieldsInput()},
1745 func BenchmarkFields(b *testing.B) {
1746 for _, sd := range stringdata {
1747 b.Run(sd.name, func(b *testing.B) {
1748 for j := 1 << 4; j <= 1<<20; j <<= 4 {
1749 b.Run(fmt.Sprintf("%d", j), func(b *testing.B) {
1750 b.ReportAllocs()
1751 b.SetBytes(int64(j))
1752 data := sd.data[:j]
1753 for i := 0; i < b.N; i++ {
1754 Fields(data)
1762 func BenchmarkFieldsFunc(b *testing.B) {
1763 for _, sd := range stringdata {
1764 b.Run(sd.name, func(b *testing.B) {
1765 for j := 1 << 4; j <= 1<<20; j <<= 4 {
1766 b.Run(fmt.Sprintf("%d", j), func(b *testing.B) {
1767 b.ReportAllocs()
1768 b.SetBytes(int64(j))
1769 data := sd.data[:j]
1770 for i := 0; i < b.N; i++ {
1771 FieldsFunc(data, unicode.IsSpace)
1779 func BenchmarkSplitEmptySeparator(b *testing.B) {
1780 for i := 0; i < b.N; i++ {
1781 Split(benchInputHard, "")
1785 func BenchmarkSplitSingleByteSeparator(b *testing.B) {
1786 for i := 0; i < b.N; i++ {
1787 Split(benchInputHard, "/")
1791 func BenchmarkSplitMultiByteSeparator(b *testing.B) {
1792 for i := 0; i < b.N; i++ {
1793 Split(benchInputHard, "hello")
1797 func BenchmarkSplitNSingleByteSeparator(b *testing.B) {
1798 for i := 0; i < b.N; i++ {
1799 SplitN(benchInputHard, "/", 10)
1803 func BenchmarkSplitNMultiByteSeparator(b *testing.B) {
1804 for i := 0; i < b.N; i++ {
1805 SplitN(benchInputHard, "hello", 10)
1809 func BenchmarkRepeat(b *testing.B) {
1810 s := "0123456789"
1811 for _, n := range []int{5, 10} {
1812 for _, c := range []int{1, 2, 6} {
1813 b.Run(fmt.Sprintf("%dx%d", n, c), func(b *testing.B) {
1814 for i := 0; i < b.N; i++ {
1815 Repeat(s[:n], c)
1822 func BenchmarkIndexAnyASCII(b *testing.B) {
1823 x := Repeat("#", 2048) // Never matches set
1824 cs := "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz"
1825 for k := 1; k <= 2048; k <<= 4 {
1826 for j := 1; j <= 64; j <<= 1 {
1827 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
1828 for i := 0; i < b.N; i++ {
1829 IndexAny(x[:k], cs[:j])
1836 func BenchmarkIndexAnyUTF8(b *testing.B) {
1837 x := Repeat("#", 2048) // Never matches set
1838 cs := "你好世界, hello world. 你好世界, hello world. 你好世界, hello world."
1839 for k := 1; k <= 2048; k <<= 4 {
1840 for j := 1; j <= 64; j <<= 1 {
1841 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
1842 for i := 0; i < b.N; i++ {
1843 IndexAny(x[:k], cs[:j])
1850 func BenchmarkLastIndexAnyASCII(b *testing.B) {
1851 x := Repeat("#", 2048) // Never matches set
1852 cs := "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz"
1853 for k := 1; k <= 2048; k <<= 4 {
1854 for j := 1; j <= 64; j <<= 1 {
1855 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
1856 for i := 0; i < b.N; i++ {
1857 LastIndexAny(x[:k], cs[:j])
1864 func BenchmarkLastIndexAnyUTF8(b *testing.B) {
1865 x := Repeat("#", 2048) // Never matches set
1866 cs := "你好世界, hello world. 你好世界, hello world. 你好世界, hello world."
1867 for k := 1; k <= 2048; k <<= 4 {
1868 for j := 1; j <= 64; j <<= 1 {
1869 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
1870 for i := 0; i < b.N; i++ {
1871 LastIndexAny(x[:k], cs[:j])
1878 func BenchmarkTrimASCII(b *testing.B) {
1879 cs := "0123456789abcdef"
1880 for k := 1; k <= 4096; k <<= 4 {
1881 for j := 1; j <= 16; j <<= 1 {
1882 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
1883 x := Repeat(cs[:j], k) // Always matches set
1884 for i := 0; i < b.N; i++ {
1885 Trim(x[:k], cs[:j])
1892 func BenchmarkTrimByte(b *testing.B) {
1893 x := " the quick brown fox "
1894 for i := 0; i < b.N; i++ {
1895 Trim(x, " ")
1899 func BenchmarkIndexPeriodic(b *testing.B) {
1900 key := "aa"
1901 for _, skip := range [...]int{2, 4, 8, 16, 32, 64} {
1902 b.Run(fmt.Sprintf("IndexPeriodic%d", skip), func(b *testing.B) {
1903 s := Repeat("a"+Repeat(" ", skip-1), 1<<16/skip)
1904 for i := 0; i < b.N; i++ {
1905 Index(s, key)
1911 func BenchmarkJoin(b *testing.B) {
1912 vals := []string{"red", "yellow", "pink", "green", "purple", "orange", "blue"}
1913 for l := 0; l <= len(vals); l++ {
1914 b.Run(strconv.Itoa(l), func(b *testing.B) {
1915 b.ReportAllocs()
1916 vals := vals[:l]
1917 for i := 0; i < b.N; i++ {
1918 Join(vals, " and ")
1924 func BenchmarkTrimSpace(b *testing.B) {
1925 tests := []struct{ name, input string }{
1926 {"NoTrim", "typical"},
1927 {"ASCII", " foo bar "},
1928 {"SomeNonASCII", " \u2000\t\r\n x\t\t\r\r\ny\n \u3000 "},
1929 {"JustNonASCII", "\u2000\u2000\u2000☺☺☺☺\u3000\u3000\u3000"},
1931 for _, test := range tests {
1932 b.Run(test.name, func(b *testing.B) {
1933 for i := 0; i < b.N; i++ {
1934 TrimSpace(test.input)
1940 var stringSink string
1942 func BenchmarkReplaceAll(b *testing.B) {
1943 b.ReportAllocs()
1944 for i := 0; i < b.N; i++ {
1945 stringSink = ReplaceAll("banana", "a", "<>")