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.
20 func eq(a
, b
[]string) bool {
24 for i
:= 0; i
< len(a
); i
++ {
32 func sliceOfString(s
[][]byte) []string {
33 result
:= make([]string, len(s
))
40 // For ease of reading, the test cases use strings that are converted to byte
41 // slices before invoking the functions.
45 var commas
= "1,2,3,4"
46 var dots
= "1....2....3....4"
48 type BinOpTest
struct {
54 func TestEqual(t
*testing
.T
) {
55 for _
, tt
:= range compareTests
{
56 eql
:= Equal(tt
.a
, tt
.b
)
57 if eql
!= (tt
.i
== 0) {
58 t
.Errorf(`Equal(%q, %q) = %v`, tt
.a
, tt
.b
, eql
)
60 eql
= EqualPortable(tt
.a
, tt
.b
)
61 if eql
!= (tt
.i
== 0) {
62 t
.Errorf(`EqualPortable(%q, %q) = %v`, tt
.a
, tt
.b
, eql
)
67 func TestEqualExhaustive(t
*testing
.T
) {
72 a
:= make([]byte, size
)
73 b
:= make([]byte, size
)
74 b_init
:= make([]byte, size
)
75 // randomish but deterministic data
76 for i
:= 0; i
< size
; i
++ {
78 b_init
[i
] = byte(23*i
+ 100)
81 for len := 0; len <= size
; len++ {
82 for x
:= 0; x
<= size
-len; x
++ {
83 for y
:= 0; y
<= size
-len; y
++ {
85 copy(b
[y
:y
+len], a
[x
:x
+len])
86 if !Equal(a
[x
:x
+len], b
[y
:y
+len]) ||
!Equal(b
[y
:y
+len], a
[x
:x
+len]) {
87 t
.Errorf("Equal(%d, %d, %d) = false", len, x
, y
)
94 // make sure Equal returns false for minimally different strings. The data
95 // is all zeros except for a single one in one location.
96 func TestNotEqual(t
*testing
.T
) {
101 a
:= make([]byte, size
)
102 b
:= make([]byte, size
)
104 for len := 0; len <= size
; len++ {
105 for x
:= 0; x
<= size
-len; x
++ {
106 for y
:= 0; y
<= size
-len; y
++ {
107 for diffpos
:= x
; diffpos
< x
+len; diffpos
++ {
109 if Equal(a
[x
:x
+len], b
[y
:y
+len]) ||
Equal(b
[y
:y
+len], a
[x
:x
+len]) {
110 t
.Errorf("NotEqual(%d, %d, %d, %d) = true", len, x
, y
, diffpos
)
119 var indexTests
= []BinOpTest
{
126 {"oofofoofooo", "f", 2},
127 {"oofofoofooo", "foo", 4},
128 {"barfoobarfoo", "foo", 3},
131 {"abcABCabc", "A", 3},
132 // cases with one byte strings - test IndexByte and special case in Index()
140 {"barfoobarfooyyyzzzyyyzzzyyyzzzyyyxxxzzzyyy", "x", 33},
141 {"foofyfoobarfoobar", "y", 4},
142 {"oooooooooooooooooooooo", "r", -1},
145 var lastIndexTests
= []BinOpTest
{
152 {"oofofoofooo", "f", 7},
153 {"oofofoofooo", "foo", 7},
154 {"barfoobarfoo", "foo", 9},
157 {"abcABCabc", "A", 3},
158 {"abcABCabc", "a", 6},
161 var indexAnyTests
= []BinOpTest
{
171 {"a☺b☻c☹d", "cx", len("a☺b☻")},
172 {"a☺b☻c☹d", "uvw☻xyz", len("a☺b")},
173 {"aRegExp*", ".(|)*+?^$[]", 7},
174 {dots
+ dots
+ dots
, " ", -1},
175 {"012abcba210", "\xffb", 4},
176 {"012\x80bcb\x80210", "\xffb", 3},
179 var lastIndexAnyTests
= []BinOpTest
{
189 {"a☺b☻c☹d", "cx", len("a☺b☻")},
190 {"a☺b☻c☹d", "uvw☻xyz", len("a☺b")},
191 {"a.RegExp*", ".(|)*+?^$[]", 8},
192 {dots
+ dots
+ dots
, " ", -1},
193 {"012abcba210", "\xffb", 6},
194 {"012\x80bcb\x80210", "\xffb", 7},
197 // Execute f on each test case. funcName should be the name of f; it's used
198 // in failure reports.
199 func runIndexTests(t
*testing
.T
, f
func(s
, sep
[]byte) int, funcName
string, testCases
[]BinOpTest
) {
200 for _
, test
:= range testCases
{
204 if actual
!= test
.i
{
205 t
.Errorf("%s(%q,%q) = %v; want %v", funcName
, a
, b
, actual
, test
.i
)
210 func runIndexAnyTests(t
*testing
.T
, f
func(s
[]byte, chars
string) int, funcName
string, testCases
[]BinOpTest
) {
211 for _
, test
:= range testCases
{
213 actual
:= f(a
, test
.b
)
214 if actual
!= test
.i
{
215 t
.Errorf("%s(%q,%q) = %v; want %v", funcName
, a
, test
.b
, actual
, test
.i
)
220 func TestIndex(t
*testing
.T
) { runIndexTests(t
, Index
, "Index", indexTests
) }
221 func TestLastIndex(t
*testing
.T
) { runIndexTests(t
, LastIndex
, "LastIndex", lastIndexTests
) }
222 func TestIndexAny(t
*testing
.T
) { runIndexAnyTests(t
, IndexAny
, "IndexAny", indexAnyTests
) }
223 func TestLastIndexAny(t
*testing
.T
) {
224 runIndexAnyTests(t
, LastIndexAny
, "LastIndexAny", lastIndexAnyTests
)
227 func TestIndexByte(t
*testing
.T
) {
228 for _
, tt
:= range indexTests
{
234 pos
:= IndexByte(a
, b
)
236 t
.Errorf(`IndexByte(%q, '%c') = %v`, tt
.a
, b
, pos
)
238 posp
:= IndexBytePortable(a
, b
)
240 t
.Errorf(`indexBytePortable(%q, '%c') = %v`, tt
.a
, b
, posp
)
245 func TestLastIndexByte(t
*testing
.T
) {
246 testCases
:= []BinOpTest
{
249 {"abcdefabcdef", "a", len("abcdef")}, // something in the middle
250 {"abcdefabcdef", "f", len("abcdefabcde")}, // last byte
251 {"zabcdefabcdef", "z", 0}, // first byte
252 {"a☺b☻c☹d", "b", len("a☺")}, // non-ascii
254 for _
, test
:= range testCases
{
255 actual
:= LastIndexByte([]byte(test
.a
), test
.b
[0])
256 if actual
!= test
.i
{
257 t
.Errorf("LastIndexByte(%q,%c) = %v; want %v", test
.a
, test
.b
[0], actual
, test
.i
)
262 // test a larger buffer with different sizes and alignments
263 func TestIndexByteBig(t
*testing
.T
) {
269 for i
:= 0; i
< n
; i
++ {
270 // different start alignments
272 for j
:= 0; j
< len(b1
); j
++ {
274 pos
:= IndexByte(b1
, 'x')
276 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
279 pos
= IndexByte(b1
, 'x')
281 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
284 // different end alignments
286 for j
:= 0; j
< len(b1
); j
++ {
288 pos
:= IndexByte(b1
, 'x')
290 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
293 pos
= IndexByte(b1
, 'x')
295 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
298 // different start and end alignments
299 b1
= b
[i
/2 : n
-(i
+1)/2]
300 for j
:= 0; j
< len(b1
); j
++ {
302 pos
:= IndexByte(b1
, 'x')
304 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
307 pos
= IndexByte(b1
, 'x')
309 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
315 // test a small index across all page offsets
316 func TestIndexByteSmall(t
*testing
.T
) {
317 b
:= make([]byte, 5015) // bigger than a page
318 // Make sure we find the correct byte even when straddling a page.
319 for i
:= 0; i
<= len(b
)-15; i
++ {
320 for j
:= 0; j
< 15; j
++ {
321 b
[i
+j
] = byte(100 + j
)
323 for j
:= 0; j
< 15; j
++ {
324 p
:= IndexByte(b
[i
:i
+15], byte(100+j
))
326 t
.Errorf("IndexByte(%q, %d) = %d", b
[i
:i
+15], 100+j
, p
)
329 for j
:= 0; j
< 15; j
++ {
333 // Make sure matches outside the slice never trigger.
334 for i
:= 0; i
<= len(b
)-15; i
++ {
335 for j
:= 0; j
< 15; j
++ {
338 for j
:= 0; j
< 15; j
++ {
339 p
:= IndexByte(b
[i
:i
+15], byte(0))
341 t
.Errorf("IndexByte(%q, %d) = %d", b
[i
:i
+15], 0, p
)
344 for j
:= 0; j
< 15; j
++ {
350 func TestIndexRune(t
*testing
.T
) {
361 {"foo☺☻☹bar", '☹', 9},
363 {"some_text=some_value", '=', 9},
367 // RuneError should match any invalid UTF-8 byte sequence.
370 {"☻x�", '�', len("☻x")},
371 {"☻x\xe2\x98", '�', len("☻x")},
372 {"☻x\xe2\x98�", '�', len("☻x")},
373 {"☻x\xe2\x98x", '�', len("☻x")},
375 // Invalid rune values should never match.
376 {"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", -1, -1},
377 {"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", 0xD800, -1}, // Surrogate pair
378 {"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", utf8
.MaxRune
+ 1, -1},
380 for _
, tt
:= range tests
{
381 if got
:= IndexRune([]byte(tt
.in
), tt
.rune
); got
!= tt
.want
{
382 t
.Errorf("IndexRune(%q, %d) = %v; want %v", tt
.in
, tt
.rune
, got
, tt
.want
)
386 haystack
:= []byte("test世界")
387 allocs
:= testing
.AllocsPerRun(1000, func() {
388 if i
:= IndexRune(haystack
, 's'); i
!= 2 {
389 t
.Fatalf("'s' at %d; want 2", i
)
391 if i
:= IndexRune(haystack
, '世'); i
!= 4 {
392 t
.Fatalf("'世' at %d; want 4", i
)
396 if runtime
.Compiler
== "gccgo" {
397 t
.Log("does not work on gccgo without better escape analysis")
399 t
.Errorf("expected no allocations, got %f", allocs
)
406 func valName(x
int) string {
407 if s
:= x
>> 20; s
<<20 == x
{
408 return fmt
.Sprintf("%dM", s
)
410 if s
:= x
>> 10; s
<<10 == x
{
411 return fmt
.Sprintf("%dK", s
)
416 func benchBytes(b
*testing
.B
, sizes
[]int, f
func(b
*testing
.B
, n
int)) {
417 for _
, n
:= range sizes
{
418 if isRaceBuilder
&& n
> 4<<10 {
421 b
.Run(valName(n
), func(b
*testing
.B
) {
423 bmbuf
= make([]byte, n
)
431 var indexSizes
= []int{10, 32, 4 << 10, 4 << 20, 64 << 20}
433 var isRaceBuilder
= strings
.HasSuffix(testenv
.Builder(), "-race")
435 func BenchmarkIndexByte(b
*testing
.B
) {
436 benchBytes(b
, indexSizes
, bmIndexByte(IndexByte
))
439 func BenchmarkIndexBytePortable(b
*testing
.B
) {
440 benchBytes(b
, indexSizes
, bmIndexByte(IndexBytePortable
))
443 func bmIndexByte(index
func([]byte, byte) int) func(b
*testing
.B
, n
int) {
444 return func(b
*testing
.B
, n
int) {
447 for i
:= 0; i
< b
.N
; i
++ {
450 b
.Fatal("bad index", j
)
457 func BenchmarkIndexRune(b
*testing
.B
) {
458 benchBytes(b
, indexSizes
, bmIndexRune(IndexRune
))
461 func BenchmarkIndexRuneASCII(b
*testing
.B
) {
462 benchBytes(b
, indexSizes
, bmIndexRuneASCII(IndexRune
))
465 func bmIndexRuneASCII(index
func([]byte, rune
) int) func(b
*testing
.B
, n
int) {
466 return func(b
*testing
.B
, n
int) {
469 for i
:= 0; i
< b
.N
; i
++ {
472 b
.Fatal("bad index", j
)
479 func bmIndexRune(index
func([]byte, rune
) int) func(b
*testing
.B
, n
int) {
480 return func(b
*testing
.B
, n
int) {
482 utf8
.EncodeRune(buf
[n
-3:], '世')
483 for i
:= 0; i
< b
.N
; i
++ {
486 b
.Fatal("bad index", j
)
495 func BenchmarkEqual(b
*testing
.B
) {
496 b
.Run("0", func(b
*testing
.B
) {
500 for i
:= 0; i
< b
.N
; i
++ {
501 eq
:= Equal(buf1
, buf2
)
508 sizes
:= []int{1, 6, 9, 15, 16, 20, 32, 4 << 10, 4 << 20, 64 << 20}
509 benchBytes(b
, sizes
, bmEqual(Equal
))
512 func BenchmarkEqualPort(b
*testing
.B
) {
513 sizes
:= []int{1, 6, 32, 4 << 10, 4 << 20, 64 << 20}
514 benchBytes(b
, sizes
, bmEqual(EqualPortable
))
517 func bmEqual(equal
func([]byte, []byte) bool) func(b
*testing
.B
, n
int) {
518 return func(b
*testing
.B
, n
int) {
519 if len(bmbuf
) < 2*n
{
520 bmbuf
= make([]byte, 2*n
)
523 buf2
:= bmbuf
[n
: 2*n
]
526 for i
:= 0; i
< b
.N
; i
++ {
527 eq
:= equal(buf1
, buf2
)
537 func BenchmarkIndex(b
*testing
.B
) {
538 benchBytes(b
, indexSizes
, func(b
*testing
.B
, n
int) {
541 for i
:= 0; i
< b
.N
; i
++ {
542 j
:= Index(buf
, buf
[n
-7:])
544 b
.Fatal("bad index", j
)
551 func BenchmarkIndexEasy(b
*testing
.B
) {
552 benchBytes(b
, indexSizes
, func(b
*testing
.B
, n
int) {
556 for i
:= 0; i
< b
.N
; i
++ {
557 j
:= Index(buf
, buf
[n
-7:])
559 b
.Fatal("bad index", j
)
567 func BenchmarkCount(b
*testing
.B
) {
568 benchBytes(b
, indexSizes
, func(b
*testing
.B
, n
int) {
571 for i
:= 0; i
< b
.N
; i
++ {
572 j
:= Count(buf
, buf
[n
-7:])
574 b
.Fatal("bad count", j
)
581 func BenchmarkCountEasy(b
*testing
.B
) {
582 benchBytes(b
, indexSizes
, func(b
*testing
.B
, n
int) {
586 for i
:= 0; i
< b
.N
; i
++ {
587 j
:= Count(buf
, buf
[n
-7:])
589 b
.Fatal("bad count", j
)
597 type ExplodeTest
struct {
603 var explodetests
= []ExplodeTest
{
604 {"", -1, []string{}},
605 {abcd
, -1, []string{"a", "b", "c", "d"}},
606 {faces
, -1, []string{"☺", "☻", "☹"}},
607 {abcd
, 2, []string{"a", "bcd"}},
610 func TestExplode(t
*testing
.T
) {
611 for _
, tt
:= range explodetests
{
612 a
:= SplitN([]byte(tt
.s
), nil, tt
.n
)
613 result
:= sliceOfString(a
)
614 if !eq(result
, tt
.a
) {
615 t
.Errorf(`Explode("%s", %d) = %v; want %v`, tt
.s
, tt
.n
, result
, tt
.a
)
618 s
:= Join(a
, []byte{})
619 if string(s
) != tt
.s
{
620 t
.Errorf(`Join(Explode("%s", %d), "") = "%s"`, tt
.s
, tt
.n
, s
)
625 type SplitTest
struct {
632 var splittests
= []SplitTest
{
634 {abcd
, "a", -1, []string{"", "bcd"}},
635 {abcd
, "z", -1, []string{"abcd"}},
636 {abcd
, "", -1, []string{"a", "b", "c", "d"}},
637 {commas
, ",", -1, []string{"1", "2", "3", "4"}},
638 {dots
, "...", -1, []string{"1", ".2", ".3", ".4"}},
639 {faces
, "☹", -1, []string{"☺☻", ""}},
640 {faces
, "~", -1, []string{faces
}},
641 {faces
, "", -1, []string{"☺", "☻", "☹"}},
642 {"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
643 {"1 2", " ", 3, []string{"1", "2"}},
644 {"123", "", 2, []string{"1", "23"}},
645 {"123", "", 17, []string{"1", "2", "3"}},
648 func TestSplit(t
*testing
.T
) {
649 for _
, tt
:= range splittests
{
650 a
:= SplitN([]byte(tt
.s
), []byte(tt
.sep
), tt
.n
)
651 result
:= sliceOfString(a
)
652 if !eq(result
, tt
.a
) {
653 t
.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt
.s
, tt
.sep
, tt
.n
, result
, tt
.a
)
659 s
:= Join(a
, []byte(tt
.sep
))
660 if string(s
) != tt
.s
{
661 t
.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt
.s
, tt
.sep
, tt
.n
, tt
.sep
, s
)
664 b
:= Split([]byte(tt
.s
), []byte(tt
.sep
))
665 if !reflect
.DeepEqual(a
, b
) {
666 t
.Errorf("Split disagrees withSplitN(%q, %q, %d) = %v; want %v", tt
.s
, tt
.sep
, tt
.n
, b
, a
)
671 if cap(in
) == cap(out
) && &in
[:1][0] == &out
[:1][0] {
672 t
.Errorf("Join(%#v, %q) didn't copy", a
, tt
.sep
)
678 var splitaftertests
= []SplitTest
{
679 {abcd
, "a", -1, []string{"a", "bcd"}},
680 {abcd
, "z", -1, []string{"abcd"}},
681 {abcd
, "", -1, []string{"a", "b", "c", "d"}},
682 {commas
, ",", -1, []string{"1,", "2,", "3,", "4"}},
683 {dots
, "...", -1, []string{"1...", ".2...", ".3...", ".4"}},
684 {faces
, "☹", -1, []string{"☺☻☹", ""}},
685 {faces
, "~", -1, []string{faces
}},
686 {faces
, "", -1, []string{"☺", "☻", "☹"}},
687 {"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}},
688 {"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}},
689 {"1 2", " ", 3, []string{"1 ", "2"}},
690 {"123", "", 2, []string{"1", "23"}},
691 {"123", "", 17, []string{"1", "2", "3"}},
694 func TestSplitAfter(t
*testing
.T
) {
695 for _
, tt
:= range splitaftertests
{
696 a
:= SplitAfterN([]byte(tt
.s
), []byte(tt
.sep
), tt
.n
)
697 result
:= sliceOfString(a
)
698 if !eq(result
, tt
.a
) {
699 t
.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt
.s
, tt
.sep
, tt
.n
, result
, tt
.a
)
703 if string(s
) != tt
.s
{
704 t
.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt
.s
, tt
.sep
, tt
.n
, tt
.sep
, s
)
707 b
:= SplitAfter([]byte(tt
.s
), []byte(tt
.sep
))
708 if !reflect
.DeepEqual(a
, b
) {
709 t
.Errorf("SplitAfter disagrees withSplitAfterN(%q, %q, %d) = %v; want %v", tt
.s
, tt
.sep
, tt
.n
, b
, a
)
715 type FieldsTest
struct {
720 var fieldstests
= []FieldsTest
{
723 {" \t ", []string{}},
724 {" abc ", []string{"abc"}},
725 {"1 2 3 4", []string{"1", "2", "3", "4"}},
726 {"1 2 3 4", []string{"1", "2", "3", "4"}},
727 {"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
728 {"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
729 {"\u2000\u2001\u2002", []string{}},
730 {"\n™\t™\n", []string{"™", "™"}},
731 {faces
, []string{faces
}},
734 func TestFields(t
*testing
.T
) {
735 for _
, tt
:= range fieldstests
{
736 a
:= Fields([]byte(tt
.s
))
737 result
:= sliceOfString(a
)
738 if !eq(result
, tt
.a
) {
739 t
.Errorf("Fields(%q) = %v; want %v", tt
.s
, a
, tt
.a
)
745 func TestFieldsFunc(t
*testing
.T
) {
746 for _
, tt
:= range fieldstests
{
747 a
:= FieldsFunc([]byte(tt
.s
), unicode
.IsSpace
)
748 result
:= sliceOfString(a
)
749 if !eq(result
, tt
.a
) {
750 t
.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt
.s
, a
, tt
.a
)
754 pred
:= func(c rune
) bool { return c
== 'X' }
755 var fieldsFuncTests
= []FieldsTest
{
758 {"XXhiXXX", []string{"hi"}},
759 {"aXXbXXXcX", []string{"a", "b", "c"}},
761 for _
, tt
:= range fieldsFuncTests
{
762 a
:= FieldsFunc([]byte(tt
.s
), pred
)
763 result
:= sliceOfString(a
)
764 if !eq(result
, tt
.a
) {
765 t
.Errorf("FieldsFunc(%q) = %v, want %v", tt
.s
, a
, tt
.a
)
770 // Test case for any function which accepts and returns a byte slice.
771 // For ease of creation, we write the byte slices as strings.
772 type StringTest
struct {
776 var upperTests
= []StringTest
{
779 {"AbC123", "ABC123"},
780 {"azAZ09_", "AZAZ09_"},
781 {"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
784 var lowerTests
= []StringTest
{
787 {"AbC123", "abc123"},
788 {"azAZ09_", "azaz09_"},
789 {"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
792 const space
= "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
794 var trimSpaceTests
= []StringTest
{
797 {space
+ "abc" + space
, "abc"},
799 {" \t\r\n \t\t\r\r\n\n ", ""},
800 {" \t\r\n x\t\t\r\r\n\n ", "x"},
801 {" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
802 {"1 \t\r\n2", "1 \t\r\n2"},
805 {"x \xc0\xc0 ", "x \xc0\xc0"},
806 {"x \xc0", "x \xc0"},
807 {"x \xc0 ", "x \xc0"},
808 {"x \xc0\xc0 ", "x \xc0\xc0"},
809 {"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"},
813 // Execute f on each test case. funcName should be the name of f; it's used
814 // in failure reports.
815 func runStringTests(t
*testing
.T
, f
func([]byte) []byte, funcName
string, testCases
[]StringTest
) {
816 for _
, tc
:= range testCases
{
817 actual
:= string(f([]byte(tc
.in
)))
818 if actual
!= tc
.out
{
819 t
.Errorf("%s(%q) = %q; want %q", funcName
, tc
.in
, actual
, tc
.out
)
824 func tenRunes(r rune
) string {
825 runes
:= make([]rune
, 10)
826 for i
:= range runes
{
832 // User-defined self-inverse mapping function
833 func rot13(r rune
) rune
{
835 if r
>= 'a' && r
<= 'z' {
836 return ((r
- 'a' + step
) % 26) + 'a'
838 if r
>= 'A' && r
<= 'Z' {
839 return ((r
- 'A' + step
) % 26) + 'A'
844 func TestMap(t
*testing
.T
) {
845 // Run a couple of awful growth/shrinkage tests
848 // 1. Grow. This triggers two reallocations in Map.
849 maxRune
:= func(r rune
) rune
{ return unicode
.MaxRune
}
850 m
:= Map(maxRune
, []byte(a
))
851 expect
:= tenRunes(unicode
.MaxRune
)
852 if string(m
) != expect
{
853 t
.Errorf("growing: expected %q got %q", expect
, m
)
857 minRune
:= func(r rune
) rune
{ return 'a' }
858 m
= Map(minRune
, []byte(tenRunes(unicode
.MaxRune
)))
860 if string(m
) != expect
{
861 t
.Errorf("shrinking: expected %q got %q", expect
, m
)
865 m
= Map(rot13
, []byte("a to zed"))
867 if string(m
) != expect
{
868 t
.Errorf("rot13: expected %q got %q", expect
, m
)
872 m
= Map(rot13
, Map(rot13
, []byte("a to zed")))
874 if string(m
) != expect
{
875 t
.Errorf("rot13: expected %q got %q", expect
, m
)
879 dropNotLatin
:= func(r rune
) rune
{
880 if unicode
.Is(unicode
.Latin
, r
) {
885 m
= Map(dropNotLatin
, []byte("Hello, 세계"))
887 if string(m
) != expect
{
888 t
.Errorf("drop: expected %q got %q", expect
, m
)
892 invalidRune
:= func(r rune
) rune
{
893 return utf8
.MaxRune
+ 1
895 m
= Map(invalidRune
, []byte("x"))
897 if string(m
) != expect
{
898 t
.Errorf("invalidRune: expected %q got %q", expect
, m
)
902 func TestToUpper(t
*testing
.T
) { runStringTests(t
, ToUpper
, "ToUpper", upperTests
) }
904 func TestToLower(t
*testing
.T
) { runStringTests(t
, ToLower
, "ToLower", lowerTests
) }
906 func TestTrimSpace(t
*testing
.T
) { runStringTests(t
, TrimSpace
, "TrimSpace", trimSpaceTests
) }
908 type RepeatTest
struct {
913 var RepeatTests
= []RepeatTest
{
919 {"-", "----------", 10},
920 {"abc ", "abc abc abc ", 3},
923 func TestRepeat(t
*testing
.T
) {
924 for _
, tt
:= range RepeatTests
{
926 tout
:= []byte(tt
.out
)
927 a
:= Repeat(tin
, tt
.count
)
929 t
.Errorf("Repeat(%q, %d) = %q; want %q", tin
, tt
.count
, a
, tout
)
935 func repeat(b
[]byte, count
int) (err error
) {
937 if r
:= recover(); r
!= nil {
938 switch v
:= r
.(type) {
942 err
= fmt
.Errorf("%s", v
)
952 // See Issue golang.org/issue/16237
953 func TestRepeatCatchesOverflow(t
*testing
.T
) {
954 tests
:= [...]struct {
959 0: {"--", -2147483647, "negative"},
960 1: {"", int(^uint(0) >> 1), ""},
962 3: {"gopher", 0, ""},
963 4: {"-", -1, "negative"},
964 5: {"--", -102, "negative"},
965 6: {string(make([]byte, 255)), int((^uint(0))/255 + 1), "overflow"},
968 for i
, tt
:= range tests
{
969 err
:= repeat([]byte(tt
.s
), tt
.count
)
972 t
.Errorf("#%d panicked %v", i
, err
)
977 if err
== nil ||
!strings
.Contains(err
.Error(), tt
.errStr
) {
978 t
.Errorf("#%d expected %q got %q", i
, tt
.errStr
, err
)
983 func runesEqual(a
, b
[]rune
) bool {
984 if len(a
) != len(b
) {
987 for i
, r
:= range a
{
995 type RunesTest
struct {
1001 var RunesTests
= []RunesTest
{
1002 {"", []rune
{}, false},
1003 {" ", []rune
{32}, false},
1004 {"ABC", []rune
{65, 66, 67}, false},
1005 {"abc", []rune
{97, 98, 99}, false},
1006 {"\u65e5\u672c\u8a9e", []rune
{26085, 26412, 35486}, false},
1007 {"ab\x80c", []rune
{97, 98, 0xFFFD, 99}, true},
1008 {"ab\xc0c", []rune
{97, 98, 0xFFFD, 99}, true},
1011 func TestRunes(t
*testing
.T
) {
1012 for _
, tt
:= range RunesTests
{
1013 tin
:= []byte(tt
.in
)
1015 if !runesEqual(a
, tt
.out
) {
1016 t
.Errorf("Runes(%q) = %v; want %v", tin
, a
, tt
.out
)
1020 // can only test reassembly if we didn't lose information
1023 t
.Errorf("string(Runes(%q)) = %x; want %x", tin
, s
, tin
)
1029 type TrimTest
struct {
1034 var trimTests
= []TrimTest
{
1035 {"Trim", "abba", "a", "bb"},
1036 {"Trim", "abba", "ab", ""},
1037 {"TrimLeft", "abba", "ab", ""},
1038 {"TrimRight", "abba", "ab", ""},
1039 {"TrimLeft", "abba", "a", "bba"},
1040 {"TrimRight", "abba", "a", "abb"},
1041 {"Trim", "<tag>", "<>", "tag"},
1042 {"Trim", "* listitem", " *", "listitem"},
1043 {"Trim", `"quote"`, `"`, "quote"},
1044 {"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
1045 {"Trim", "\x80test\xff", "\xff", "test"},
1046 {"Trim", " Ġ ", " ", "Ġ"},
1047 {"Trim", " Ġİ0", "0 ", "Ġİ"},
1048 //empty string tests
1049 {"Trim", "abba", "", "abba"},
1050 {"Trim", "", "123", ""},
1051 {"Trim", "", "", ""},
1052 {"TrimLeft", "abba", "", "abba"},
1053 {"TrimLeft", "", "123", ""},
1054 {"TrimLeft", "", "", ""},
1055 {"TrimRight", "abba", "", "abba"},
1056 {"TrimRight", "", "123", ""},
1057 {"TrimRight", "", "", ""},
1058 {"TrimRight", "☺\xc0", "☺", "☺\xc0"},
1059 {"TrimPrefix", "aabb", "a", "abb"},
1060 {"TrimPrefix", "aabb", "b", "aabb"},
1061 {"TrimSuffix", "aabb", "a", "aabb"},
1062 {"TrimSuffix", "aabb", "b", "aab"},
1065 func TestTrim(t
*testing
.T
) {
1066 for _
, tc
:= range trimTests
{
1068 var f
func([]byte, string) []byte
1069 var fb
func([]byte, []byte) []byte
1082 t
.Errorf("Undefined trim function %s", name
)
1086 actual
= string(f([]byte(tc
.in
), tc
.arg
))
1088 actual
= string(fb([]byte(tc
.in
), []byte(tc
.arg
)))
1090 if actual
!= tc
.out
{
1091 t
.Errorf("%s(%q, %q) = %q; want %q", name
, tc
.in
, tc
.arg
, actual
, tc
.out
)
1096 type predicate
struct {
1101 var isSpace
= predicate
{unicode
.IsSpace
, "IsSpace"}
1102 var isDigit
= predicate
{unicode
.IsDigit
, "IsDigit"}
1103 var isUpper
= predicate
{unicode
.IsUpper
, "IsUpper"}
1104 var isValidRune
= predicate
{
1106 return r
!= utf8
.RuneError
1111 type TrimFuncTest
struct {
1116 func not(p predicate
) predicate
{
1125 var trimFuncTests
= []TrimFuncTest
{
1126 {isSpace
, space
+ " hello " + space
, "hello"},
1127 {isDigit
, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
1128 {isUpper
, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
1129 {not(isSpace
), "hello" + space
+ "hello", space
},
1130 {not(isDigit
), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"},
1131 {isValidRune
, "ab\xc0a\xc0cd", "\xc0a\xc0"},
1132 {not(isValidRune
), "\xc0a\xc0", "a"},
1135 func TestTrimFunc(t
*testing
.T
) {
1136 for _
, tc
:= range trimFuncTests
{
1137 actual
:= string(TrimFunc([]byte(tc
.in
), tc
.f
.f
))
1138 if actual
!= tc
.out
{
1139 t
.Errorf("TrimFunc(%q, %q) = %q; want %q", tc
.in
, tc
.f
.name
, actual
, tc
.out
)
1144 type IndexFuncTest
struct {
1150 var indexFuncTests
= []IndexFuncTest
{
1151 {"", isValidRune
, -1, -1},
1152 {"abc", isDigit
, -1, -1},
1153 {"0123", isDigit
, 0, 3},
1154 {"a1b", isDigit
, 1, 1},
1155 {space
, isSpace
, 0, len(space
) - 3}, // last rune in space is 3 bytes
1156 {"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit
, 0, 18},
1157 {"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper
, 0, 34},
1158 {"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit
), 8, 12},
1160 // tests of invalid UTF-8
1161 {"\x801", isDigit
, 1, 1},
1162 {"\x80abc", isDigit
, -1, -1},
1163 {"\xc0a\xc0", isValidRune
, 1, 1},
1164 {"\xc0a\xc0", not(isValidRune
), 0, 2},
1165 {"\xc0☺\xc0", not(isValidRune
), 0, 4},
1166 {"\xc0☺\xc0\xc0", not(isValidRune
), 0, 5},
1167 {"ab\xc0a\xc0cd", not(isValidRune
), 2, 4},
1168 {"a\xe0\x80cd", not(isValidRune
), 1, 2},
1171 func TestIndexFunc(t
*testing
.T
) {
1172 for _
, tc
:= range indexFuncTests
{
1173 first
:= IndexFunc([]byte(tc
.in
), tc
.f
.f
)
1174 if first
!= tc
.first
{
1175 t
.Errorf("IndexFunc(%q, %s) = %d; want %d", tc
.in
, tc
.f
.name
, first
, tc
.first
)
1177 last
:= LastIndexFunc([]byte(tc
.in
), tc
.f
.f
)
1178 if last
!= tc
.last
{
1179 t
.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc
.in
, tc
.f
.name
, last
, tc
.last
)
1184 type ReplaceTest
struct {
1191 var ReplaceTests
= []ReplaceTest
{
1192 {"hello", "l", "L", 0, "hello"},
1193 {"hello", "l", "L", -1, "heLLo"},
1194 {"hello", "x", "X", -1, "hello"},
1195 {"", "x", "X", -1, ""},
1196 {"radar", "r", "<r>", -1, "<r>ada<r>"},
1197 {"", "", "<>", -1, "<>"},
1198 {"banana", "a", "<>", -1, "b<>n<>n<>"},
1199 {"banana", "a", "<>", 1, "b<>nana"},
1200 {"banana", "a", "<>", 1000, "b<>n<>n<>"},
1201 {"banana", "an", "<>", -1, "b<><>a"},
1202 {"banana", "ana", "<>", -1, "b<>na"},
1203 {"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"},
1204 {"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
1205 {"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
1206 {"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
1207 {"banana", "", "<>", 1, "<>banana"},
1208 {"banana", "a", "a", -1, "banana"},
1209 {"banana", "a", "a", 1, "banana"},
1210 {"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"},
1213 func TestReplace(t
*testing
.T
) {
1214 for _
, tt
:= range ReplaceTests
{
1215 in
:= append([]byte(tt
.in
), "<spare>"...)
1216 in
= in
[:len(tt
.in
)]
1217 out
:= Replace(in
, []byte(tt
.old
), []byte(tt
.new), tt
.n
)
1218 if s
:= string(out
); s
!= tt
.out
{
1219 t
.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt
.in
, tt
.old
, tt
.new, tt
.n
, s
, tt
.out
)
1221 if cap(in
) == cap(out
) && &in
[:1][0] == &out
[:1][0] {
1222 t
.Errorf("Replace(%q, %q, %q, %d) didn't copy", tt
.in
, tt
.old
, tt
.new, tt
.n
)
1227 type TitleTest
struct {
1231 var TitleTests
= []TitleTest
{
1234 {" aaa aaa aaa ", " Aaa Aaa Aaa "},
1235 {" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
1236 {"123a456", "123a456"},
1237 {"double-blind", "Double-Blind"},
1239 {"with_underscore", "With_underscore"},
1240 {"unicode \xe2\x80\xa8 line separator", "Unicode \xe2\x80\xa8 Line Separator"},
1243 func TestTitle(t
*testing
.T
) {
1244 for _
, tt
:= range TitleTests
{
1245 if s
:= string(Title([]byte(tt
.in
))); s
!= tt
.out
{
1246 t
.Errorf("Title(%q) = %q, want %q", tt
.in
, s
, tt
.out
)
1251 var ToTitleTests
= []TitleTest
{
1254 {" aaa aaa aaa ", " AAA AAA AAA "},
1255 {" Aaa Aaa Aaa ", " AAA AAA AAA "},
1256 {"123a456", "123A456"},
1257 {"double-blind", "DOUBLE-BLIND"},
1261 func TestToTitle(t
*testing
.T
) {
1262 for _
, tt
:= range ToTitleTests
{
1263 if s
:= string(ToTitle([]byte(tt
.in
))); s
!= tt
.out
{
1264 t
.Errorf("ToTitle(%q) = %q, want %q", tt
.in
, s
, tt
.out
)
1269 var EqualFoldTests
= []struct {
1273 {"abc", "abc", true},
1274 {"ABcd", "ABcd", true},
1275 {"123abc", "123ABC", true},
1276 {"αβδ", "ΑΒΔ", true},
1277 {"abc", "xyz", false},
1278 {"abc", "XYZ", false},
1279 {"abcdefghijk", "abcdefghijX", false},
1280 {"abcdefghijk", "abcdefghij\u212A", true},
1281 {"abcdefghijK", "abcdefghij\u212A", true},
1282 {"abcdefghijkz", "abcdefghij\u212Ay", false},
1283 {"abcdefghijKz", "abcdefghij\u212Ay", false},
1286 func TestEqualFold(t
*testing
.T
) {
1287 for _
, tt
:= range EqualFoldTests
{
1288 if out
:= EqualFold([]byte(tt
.s
), []byte(tt
.t
)); out
!= tt
.out
{
1289 t
.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt
.s
, tt
.t
, out
, tt
.out
)
1291 if out
:= EqualFold([]byte(tt
.t
), []byte(tt
.s
)); out
!= tt
.out
{
1292 t
.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt
.t
, tt
.s
, out
, tt
.out
)
1297 func TestBufferGrowNegative(t
*testing
.T
) {
1299 if err
:= recover(); err
== nil {
1300 t
.Fatal("Grow(-1) should have panicked")
1307 func TestBufferTruncateNegative(t
*testing
.T
) {
1309 if err
:= recover(); err
== nil {
1310 t
.Fatal("Truncate(-1) should have panicked")
1317 func TestBufferTruncateOutOfRange(t
*testing
.T
) {
1319 if err
:= recover(); err
== nil {
1320 t
.Fatal("Truncate(20) should have panicked")
1324 b
.Write(make([]byte, 10))
1328 var containsTests
= []struct {
1332 {[]byte("hello"), []byte("hel"), true},
1333 {[]byte("日本語"), []byte("日本"), true},
1334 {[]byte("hello"), []byte("Hello, world"), false},
1335 {[]byte("東京"), []byte("京東"), false},
1338 func TestContains(t
*testing
.T
) {
1339 for _
, tt
:= range containsTests
{
1340 if got
:= Contains(tt
.b
, tt
.subslice
); got
!= tt
.want
{
1341 t
.Errorf("Contains(%q, %q) = %v, want %v", tt
.b
, tt
.subslice
, got
, tt
.want
)
1346 var ContainsAnyTests
= []struct {
1351 {[]byte(""), "", false},
1352 {[]byte(""), "a", false},
1353 {[]byte(""), "abc", false},
1354 {[]byte("a"), "", false},
1355 {[]byte("a"), "a", true},
1356 {[]byte("aaa"), "a", true},
1357 {[]byte("abc"), "xyz", false},
1358 {[]byte("abc"), "xcz", true},
1359 {[]byte("a☺b☻c☹d"), "uvw☻xyz", true},
1360 {[]byte("aRegExp*"), ".(|)*+?^$[]", true},
1361 {[]byte(dots
+ dots
+ dots
), " ", false},
1364 func TestContainsAny(t
*testing
.T
) {
1365 for _
, ct
:= range ContainsAnyTests
{
1366 if ContainsAny(ct
.b
, ct
.substr
) != ct
.expected
{
1367 t
.Errorf("ContainsAny(%s, %s) = %v, want %v",
1368 ct
.b
, ct
.substr
, !ct
.expected
, ct
.expected
)
1373 var ContainsRuneTests
= []struct {
1378 {[]byte(""), 'a', false},
1379 {[]byte("a"), 'a', true},
1380 {[]byte("aaa"), 'a', true},
1381 {[]byte("abc"), 'y', false},
1382 {[]byte("abc"), 'c', true},
1383 {[]byte("a☺b☻c☹d"), 'x', false},
1384 {[]byte("a☺b☻c☹d"), '☻', true},
1385 {[]byte("aRegExp*"), '*', true},
1388 func TestContainsRune(t
*testing
.T
) {
1389 for _
, ct
:= range ContainsRuneTests
{
1390 if ContainsRune(ct
.b
, ct
.r
) != ct
.expected
{
1391 t
.Errorf("ContainsRune(%q, %q) = %v, want %v",
1392 ct
.b
, ct
.r
, !ct
.expected
, ct
.expected
)
1397 var makeFieldsInput
= func() []byte {
1398 x
:= make([]byte, 1<<20)
1399 // Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
1401 switch rand
.Intn(10) {
1405 if i
> 0 && x
[i
-1] == 'x' {
1417 var fieldsInput
= makeFieldsInput()
1419 func BenchmarkFields(b
*testing
.B
) {
1420 b
.SetBytes(int64(len(fieldsInput
)))
1421 for i
:= 0; i
< b
.N
; i
++ {
1426 func BenchmarkFieldsFunc(b
*testing
.B
) {
1427 b
.SetBytes(int64(len(fieldsInput
)))
1428 for i
:= 0; i
< b
.N
; i
++ {
1429 FieldsFunc(fieldsInput
, unicode
.IsSpace
)
1433 func BenchmarkTrimSpace(b
*testing
.B
) {
1434 s
:= []byte(" Some text. \n")
1435 for i
:= 0; i
< b
.N
; i
++ {
1440 func BenchmarkRepeat(b
*testing
.B
) {
1441 for i
:= 0; i
< b
.N
; i
++ {
1442 Repeat([]byte("-"), 80)
1446 func BenchmarkBytesCompare(b
*testing
.B
) {
1447 for n
:= 1; n
<= 2048; n
<<= 1 {
1448 b
.Run(fmt
.Sprint(n
), func(b
*testing
.B
) {
1449 var x
= make([]byte, n
)
1450 var y
= make([]byte, n
)
1452 for i
:= 0; i
< n
; i
++ {
1456 for i
:= 0; i
< n
; i
++ {
1461 for i
:= 0; i
< b
.N
; i
++ {
1468 func BenchmarkIndexAnyASCII(b
*testing
.B
) {
1469 x
:= Repeat([]byte{'#'}, 4096) // Never matches set
1470 cs
:= "0123456789abcdef"
1471 for k
:= 1; k
<= 4096; k
<<= 4 {
1472 for j
:= 1; j
<= 16; j
<<= 1 {
1473 b
.Run(fmt
.Sprintf("%d:%d", k
, j
), func(b
*testing
.B
) {
1474 for i
:= 0; i
< b
.N
; i
++ {
1475 IndexAny(x
[:k
], cs
[:j
])
1482 func BenchmarkTrimASCII(b
*testing
.B
) {
1483 cs
:= "0123456789abcdef"
1484 for k
:= 1; k
<= 4096; k
<<= 4 {
1485 for j
:= 1; j
<= 16; j
<<= 1 {
1486 b
.Run(fmt
.Sprintf("%d:%d", k
, j
), func(b
*testing
.B
) {
1487 x
:= Repeat([]byte(cs
[:j
]), k
) // Always matches set
1488 for i
:= 0; i
< b
.N
; i
++ {