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.
21 func eq(a
, b
[]string) bool {
25 for i
:= 0; i
< len(a
); i
++ {
35 var commas
= "1,2,3,4"
36 var dots
= "1....2....3....4"
38 type IndexTest
struct {
44 var indexTests
= []IndexTest
{
50 {"oofofoofooo", "f", 2},
51 {"oofofoofooo", "foo", 4},
52 {"barfoobarfoo", "foo", 3},
55 {"abcABCabc", "A", 3},
56 // cases with one byte strings - test special case in Index()
64 // test special cases in Index() for short strings
69 {"xab"[:2], "ab", -1},
74 {"xabc"[:3], "abc", -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},
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
{
141 {"oofofoofooo", "f", 7},
142 {"oofofoofooo", "foo", 7},
143 {"barfoobarfoo", "foo", 9},
146 {"abcABCabc", "A", 3},
147 {"abcABCabc", "a", 6},
150 var indexAnyTests
= []IndexTest
{
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
{
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
{
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 {
221 for i
:= n
; i
<= len(s
); i
++ {
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
)
235 s1
[i
] = chars
[rand
.Intn(len(chars
))]
238 for i
:= 0; i
< 50; i
++ {
239 begin
:= rand
.Intn(len(s
) + 1)
240 end
:= begin
+ rand
.Intn(len(s
)+1-begin
)
243 pos
:= rand
.Intn(len(sep
) + 1)
244 sep
= sep
[:pos
] + "A" + sep
[pos
:]
246 want
:= simpleIndex(s
, sep
)
249 t
.Errorf("Index(%s,%s) = %d; want %d", s
, sep
, res
, want
)
256 func TestIndexRune(t
*testing
.T
) {
267 {"foo☺☻☹bar", '☹', 9},
269 {"some_text=some_value", '=', 9},
273 // RuneError should match any invalid UTF-8 byte sequence.
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
)
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 {
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{"☺", "�", "☹"}},
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
)
398 t
.Errorf("Split(%q, %q, %d) = %v; want %v", tt
.s
, tt
.sep
, tt
.n
, a
, tt
.a
)
406 t
.Errorf("Join(Split(%q, %q, %d), %q) = %q", tt
.s
, tt
.sep
, tt
.n
, tt
.sep
, s
)
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
)
437 t
.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt
.s
, tt
.sep
, tt
.n
, a
, tt
.a
)
442 t
.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt
.s
, tt
.sep
, tt
.n
, tt
.sep
, s
)
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 {
458 var fieldstests
= []FieldsTest
{
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
{
480 t
.Errorf("Fields(%q) = %v; want %v", tt
.s
, a
, tt
.a
)
486 var FieldsFuncTests
= []FieldsTest
{
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
)
497 t
.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt
.s
, a
, tt
.a
)
501 pred
:= func(c rune
) bool { return c
== 'X' }
502 for _
, tt
:= range FieldsFuncTests
{
503 a
:= FieldsFunc(tt
.s
, pred
)
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 {
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
{
520 if actual
!= tc
.out
{
521 t
.Errorf("%s(%q) = %q; want %q", funcName
, tc
.in
, actual
, tc
.out
)
526 var upperTests
= []StringTest
{
528 {"ONLYUPPER", "ONLYUPPER"},
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
{
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
{
554 {space
+ "abc" + space
, "abc"},
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"},
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"},
570 func tenRunes(ch rune
) string {
571 r
:= make([]rune
, 10)
578 // User-defined self-inverse mapping function
579 func rot13(r rune
) rune
{
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'
590 func TestMap(t
*testing
.T
) {
591 // Run a couple of awful growth/shrinkage tests
593 // 1. Grow. This triggers two reallocations in Map.
594 maxRune
:= func(rune
) rune
{ return unicode
.MaxRune
}
596 expect
:= tenRunes(unicode
.MaxRune
)
598 t
.Errorf("growing: expected %q got %q", expect
, m
)
602 minRune
:= func(rune
) rune
{ return 'a' }
603 m
= Map(minRune
, tenRunes(unicode
.MaxRune
))
606 t
.Errorf("shrinking: expected %q got %q", expect
, m
)
610 m
= Map(rot13
, "a to zed")
613 t
.Errorf("rot13: expected %q got %q", expect
, m
)
617 m
= Map(rot13
, Map(rot13
, "a to zed"))
620 t
.Errorf("rot13: expected %q got %q", expect
, m
)
624 dropNotLatin
:= func(r rune
) rune
{
625 if unicode
.Is(unicode
.Latin
, r
) {
630 m
= Map(dropNotLatin
, "Hello, 세계")
633 t
.Errorf("drop: expected %q got %q", expect
, m
)
637 identity
:= func(r rune
) rune
{
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
) {
654 m
= Map(replaceNotLatin
, "Hello\255World")
655 expect
= "Hello?World"
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
{
664 return unicode
.MaxRune
665 case unicode
.MaxRune
:
670 s
:= string(utf8
.RuneSelf
) + string(utf8
.MaxRune
)
671 r
:= string(utf8
.MaxRune
) + string(utf8
.RuneSelf
) // reverse of s
674 t
.Errorf("encoding not handled correctly: expected %q got %q", r
, m
)
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
{
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
)
726 t
.Errorf("Upper(upper) is %s not %s", u
, upper
)
728 u
= ToUpperSpecial(unicode
.TurkishCase
, lower
)
730 t
.Errorf("Upper(lower) is %s not %s", u
, upper
)
732 l
:= ToLowerSpecial(unicode
.TurkishCase
, lower
)
734 t
.Errorf("Lower(lower) is %s not %s", l
, lower
)
736 l
= ToLowerSpecial(unicode
.TurkishCase
, upper
)
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 {
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 ", "Ġİ"},
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
{
781 var f
func(string, string) string
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
) {
806 for i
:= 0; i
< b
.N
; i
++ {
807 for _
, tc
:= range trimTests
{
809 var f
func(string, string) string
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 {
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
{
842 return r
!= utf8
.RuneError
847 func not(p predicate
) predicate
{
856 var trimFuncTests
= []struct {
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 {
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
)
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 {
923 for i
, c1
:= range e1
{
927 r1
, _
:= utf8
.DecodeRuneInString(c1
)
928 r2
, _
:= utf8
.DecodeRuneInString(e2
[i
])
930 t
.Errorf("%s diff at %d: U+%04X U+%04X", m
, i
, r1
, r2
)
936 func TestCaseConsistency(t
*testing
.T
) {
937 // Make a string of all the runes.
938 numRunes
:= int(unicode
.MaxRune
+ 1)
942 a
:= make([]rune
, numRunes
)
947 // convert the cases.
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
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 {
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
)
1003 func repeat(s
string, count
int) (err error
) {
1005 if r
:= recover(); r
!= nil {
1006 switch v
:= r
.(type) {
1010 err
= fmt
.Errorf("%s", v
)
1020 // See Issue golang.org/issue/16237
1021 func TestRepeatCatchesOverflow(t
*testing
.T
) {
1022 tests
:= [...]struct {
1027 0: {"--", -2147483647, "negative"},
1028 1: {"", int(^uint(0) >> 1), ""},
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
== "" {
1040 t
.Errorf("#%d panicked %v", i
, err
)
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
) {
1055 for i
, r
:= range a
{
1063 var RunesTests
= []struct {
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
{
1080 if !runesEqual(a
, tt
.out
) {
1081 t
.Errorf("[]rune(%q) = %v; want %v", tt
.in
, a
, tt
.out
)
1085 // can only test reassembly if we didn't lose information
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
1103 b
, e
:= reader
.ReadByte()
1108 t
.Errorf("Reading %q: %s", s
, e
)
1112 // unread and read again
1113 e
= reader
.UnreadByte()
1115 t
.Errorf("Unreading %q: %s", s
, e
)
1118 b1
, e
:= reader
.ReadByte()
1120 t
.Errorf("Reading %q after unreading: %s", s
, e
)
1124 t
.Errorf("Reading %q after unreading: want byte %q, got %q", s
, b
, b1
)
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
)
1143 r
, z
, e
:= reader
.ReadRune()
1148 t
.Errorf("Reading %q: %s", s
, e
)
1152 // unread and read again
1153 e
= reader
.UnreadRune()
1155 t
.Errorf("Unreading %q: %s", s
, e
)
1158 r1
, z1
, e
:= reader
.ReadRune()
1160 t
.Errorf("Reading %q after unreading: %s", s
, e
)
1164 t
.Errorf("Reading %q after unreading: want rune %q, got %q", s
, r
, r1
)
1168 t
.Errorf("Reading %q after unreading: want size %d, got %d", s
, z
, z1
)
1173 t
.Errorf("Reader(%q).ReadRune() produced %q", s
, res
)
1178 var UnreadRuneErrorTests
= []struct {
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
1197 err
:= reader
.UnreadRune()
1199 t
.Errorf("Unreading after %s: expected error", tt
.name
)
1204 var ReplaceTests
= []struct {
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 {
1244 {" aaa aaa aaa ", " Aaa Aaa Aaa "},
1245 {" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
1246 {"123a456", "123a456"},
1247 {"double-blind", "Double-Blind"},
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 {
1265 {"abc", "bc", true},
1266 {"abc", "bcd", false},
1270 // cases to cover code in runtime/asm_amd64.s:indexShortStr
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},
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},
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},
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},
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},
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},
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 {
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 {
1378 {"abc", 'y', false},
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 {
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 {
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},
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>",
1452 x
:= make([]byte, 0, 1<<20)
1454 i
:= rand
.Intn(len(tokens
))
1455 if len(x
)+len(tokens
[i
]) >= 1<<20 {
1458 x
= append(x
, tokens
[i
]...)
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
++ {
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.
1543 switch rand
.Intn(10) {
1547 if i
> 0 && x
[i
-1] == 'x' {
1559 var makeFieldsInputASCII
= func() string {
1560 x
:= make([]byte, 1<<20)
1561 // Input is ~10% space, rest ASCII non-space.
1563 if rand
.Intn(10) == 0 {
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
) {
1583 b
.SetBytes(int64(j
))
1585 for i
:= 0; i
< b
.N
; i
++ {
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
) {
1600 b
.SetBytes(int64(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
++ {
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
++ {
1675 func BenchmarkIndexPeriodic(b
*testing
.B
) {
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
++ {