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.
17 func eq(a
, b
[]string) bool {
21 for i
:= 0; i
< len(a
); i
++ {
29 func sliceOfString(s
[][]byte) []string {
30 result
:= make([]string, len(s
))
37 // For ease of reading, the test cases use strings that are converted to byte
38 // slices before invoking the functions.
42 var commas
= "1,2,3,4"
43 var dots
= "1....2....3....4"
45 type BinOpTest
struct {
51 func TestEqual(t
*testing
.T
) {
52 for _
, tt
:= range compareTests
{
53 eql
:= Equal(tt
.a
, tt
.b
)
54 if eql
!= (tt
.i
== 0) {
55 t
.Errorf(`Equal(%q, %q) = %v`, tt
.a
, tt
.b
, eql
)
57 eql
= EqualPortable(tt
.a
, tt
.b
)
58 if eql
!= (tt
.i
== 0) {
59 t
.Errorf(`EqualPortable(%q, %q) = %v`, tt
.a
, tt
.b
, eql
)
64 func TestEqualExhaustive(t
*testing
.T
) {
69 a
:= make([]byte, size
)
70 b
:= make([]byte, size
)
71 b_init
:= make([]byte, size
)
72 // randomish but deterministic data
73 for i
:= 0; i
< size
; i
++ {
75 b_init
[i
] = byte(23*i
+ 100)
78 for len := 0; len <= size
; len++ {
79 for x
:= 0; x
<= size
-len; x
++ {
80 for y
:= 0; y
<= size
-len; y
++ {
82 copy(b
[y
:y
+len], a
[x
:x
+len])
83 if !Equal(a
[x
:x
+len], b
[y
:y
+len]) ||
!Equal(b
[y
:y
+len], a
[x
:x
+len]) {
84 t
.Errorf("Equal(%d, %d, %d) = false", len, x
, y
)
91 // make sure Equal returns false for minimally different strings. The data
92 // is all zeros except for a single one in one location.
93 func TestNotEqual(t
*testing
.T
) {
98 a
:= make([]byte, size
)
99 b
:= make([]byte, size
)
101 for len := 0; len <= size
; len++ {
102 for x
:= 0; x
<= size
-len; x
++ {
103 for y
:= 0; y
<= size
-len; y
++ {
104 for diffpos
:= x
; diffpos
< x
+len; diffpos
++ {
106 if Equal(a
[x
:x
+len], b
[y
:y
+len]) ||
Equal(b
[y
:y
+len], a
[x
:x
+len]) {
107 t
.Errorf("NotEqual(%d, %d, %d, %d) = true", len, x
, y
, diffpos
)
116 var indexTests
= []BinOpTest
{
123 {"oofofoofooo", "f", 2},
124 {"oofofoofooo", "foo", 4},
125 {"barfoobarfoo", "foo", 3},
128 {"abcABCabc", "A", 3},
129 // cases with one byte strings - test IndexByte and special case in Index()
137 {"barfoobarfooyyyzzzyyyzzzyyyzzzyyyxxxzzzyyy", "x", 33},
138 {"foofyfoobarfoobar", "y", 4},
139 {"oooooooooooooooooooooo", "r", -1},
142 var lastIndexTests
= []BinOpTest
{
149 {"oofofoofooo", "f", 7},
150 {"oofofoofooo", "foo", 7},
151 {"barfoobarfoo", "foo", 9},
154 {"abcABCabc", "A", 3},
155 {"abcABCabc", "a", 6},
158 var indexAnyTests
= []BinOpTest
{
168 {"aRegExp*", ".(|)*+?^$[]", 7},
169 {dots
+ dots
+ dots
, " ", -1},
172 var lastIndexAnyTests
= []BinOpTest
{
181 {"a☺b☻c☹d", "uvw☻xyz", 2 + len("☺")},
182 {"a.RegExp*", ".(|)*+?^$[]", 8},
183 {dots
+ dots
+ dots
, " ", -1},
186 var indexRuneTests
= []BinOpTest
{
192 {"foo☺☻☹bar", "☹", 9},
195 // Execute f on each test case. funcName should be the name of f; it's used
196 // in failure reports.
197 func runIndexTests(t
*testing
.T
, f
func(s
, sep
[]byte) int, funcName
string, testCases
[]BinOpTest
) {
198 for _
, test
:= range testCases
{
202 if actual
!= test
.i
{
203 t
.Errorf("%s(%q,%q) = %v; want %v", funcName
, a
, b
, actual
, test
.i
)
208 func runIndexAnyTests(t
*testing
.T
, f
func(s
[]byte, chars
string) int, funcName
string, testCases
[]BinOpTest
) {
209 for _
, test
:= range testCases
{
211 actual
:= f(a
, test
.b
)
212 if actual
!= test
.i
{
213 t
.Errorf("%s(%q,%q) = %v; want %v", funcName
, a
, test
.b
, actual
, test
.i
)
218 func TestIndex(t
*testing
.T
) { runIndexTests(t
, Index
, "Index", indexTests
) }
219 func TestLastIndex(t
*testing
.T
) { runIndexTests(t
, LastIndex
, "LastIndex", lastIndexTests
) }
220 func TestIndexAny(t
*testing
.T
) { runIndexAnyTests(t
, IndexAny
, "IndexAny", indexAnyTests
) }
221 func TestLastIndexAny(t
*testing
.T
) {
222 runIndexAnyTests(t
, LastIndexAny
, "LastIndexAny", lastIndexAnyTests
)
225 func TestIndexByte(t
*testing
.T
) {
226 for _
, tt
:= range indexTests
{
232 pos
:= IndexByte(a
, b
)
234 t
.Errorf(`IndexByte(%q, '%c') = %v`, tt
.a
, b
, pos
)
236 posp
:= IndexBytePortable(a
, b
)
238 t
.Errorf(`indexBytePortable(%q, '%c') = %v`, tt
.a
, b
, posp
)
243 func TestLastIndexByte(t
*testing
.T
) {
244 testCases
:= []BinOpTest
{
247 {"abcdefabcdef", "a", len("abcdef")}, // something in the middle
248 {"abcdefabcdef", "f", len("abcdefabcde")}, // last byte
249 {"zabcdefabcdef", "z", 0}, // first byte
250 {"a☺b☻c☹d", "b", len("a☺")}, // non-ascii
252 for _
, test
:= range testCases
{
253 actual
:= LastIndexByte([]byte(test
.a
), test
.b
[0])
254 if actual
!= test
.i
{
255 t
.Errorf("LastIndexByte(%q,%c) = %v; want %v", test
.a
, test
.b
[0], actual
, test
.i
)
260 // test a larger buffer with different sizes and alignments
261 func TestIndexByteBig(t
*testing
.T
) {
267 for i
:= 0; i
< n
; i
++ {
268 // different start alignments
270 for j
:= 0; j
< len(b1
); j
++ {
272 pos
:= IndexByte(b1
, 'x')
274 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
277 pos
= IndexByte(b1
, 'x')
279 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
282 // different end alignments
284 for j
:= 0; j
< len(b1
); j
++ {
286 pos
:= IndexByte(b1
, 'x')
288 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
291 pos
= IndexByte(b1
, 'x')
293 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
296 // different start and end alignments
297 b1
= b
[i
/2 : n
-(i
+1)/2]
298 for j
:= 0; j
< len(b1
); j
++ {
300 pos
:= IndexByte(b1
, 'x')
302 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
305 pos
= IndexByte(b1
, 'x')
307 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
313 // test a small index across all page offsets
314 func TestIndexByteSmall(t
*testing
.T
) {
315 b
:= make([]byte, 5015) // bigger than a page
316 // Make sure we find the correct byte even when straddling a page.
317 for i
:= 0; i
<= len(b
)-15; i
++ {
318 for j
:= 0; j
< 15; j
++ {
319 b
[i
+j
] = byte(100 + j
)
321 for j
:= 0; j
< 15; j
++ {
322 p
:= IndexByte(b
[i
:i
+15], byte(100+j
))
324 t
.Errorf("IndexByte(%q, %d) = %d", b
[i
:i
+15], 100+j
, p
)
327 for j
:= 0; j
< 15; j
++ {
331 // Make sure matches outside the slice never trigger.
332 for i
:= 0; i
<= len(b
)-15; i
++ {
333 for j
:= 0; j
< 15; j
++ {
336 for j
:= 0; j
< 15; j
++ {
337 p
:= IndexByte(b
[i
:i
+15], byte(0))
339 t
.Errorf("IndexByte(%q, %d) = %d", b
[i
:i
+15], 0, p
)
342 for j
:= 0; j
< 15; j
++ {
348 func TestIndexRune(t
*testing
.T
) {
349 for _
, tt
:= range indexRuneTests
{
351 r
, _
:= utf8
.DecodeRuneInString(tt
.b
)
352 pos
:= IndexRune(a
, r
)
354 t
.Errorf(`IndexRune(%q, '%c') = %v`, tt
.a
, r
, pos
)
361 func valName(x
int) string {
362 if s
:= x
>> 20; s
<<20 == x
{
363 return fmt
.Sprintf("%dM", s
)
365 if s
:= x
>> 10; s
<<10 == x
{
366 return fmt
.Sprintf("%dK", s
)
371 func benchBytes(b
*testing
.B
, sizes
[]int, f
func(b
*testing
.B
, n
int)) {
372 for _
, n
:= range sizes
{
373 b
.Run(valName(n
), func(b
*testing
.B
) {
375 bmbuf
= make([]byte, n
)
383 var indexSizes
= []int{10, 32, 4 << 10, 4 << 20, 64 << 20}
385 func BenchmarkIndexByte(b
*testing
.B
) {
386 benchBytes(b
, indexSizes
, bmIndexByte(IndexByte
))
389 func BenchmarkIndexBytePortable(b
*testing
.B
) {
390 benchBytes(b
, indexSizes
, bmIndexByte(IndexBytePortable
))
393 func bmIndexByte(index
func([]byte, byte) int) func(b
*testing
.B
, n
int) {
394 return func(b
*testing
.B
, n
int) {
397 for i
:= 0; i
< b
.N
; i
++ {
400 b
.Fatal("bad index", j
)
407 func BenchmarkEqual(b
*testing
.B
) {
408 b
.Run("0", func(b
*testing
.B
) {
412 for i
:= 0; i
< b
.N
; i
++ {
413 eq
:= Equal(buf1
, buf2
)
420 sizes
:= []int{1, 6, 9, 15, 16, 20, 32, 4 << 10, 4 << 20, 64 << 20}
421 benchBytes(b
, sizes
, bmEqual(Equal
))
424 func BenchmarkEqualPort(b
*testing
.B
) {
425 sizes
:= []int{1, 6, 32, 4 << 10, 4 << 20, 64 << 20}
426 benchBytes(b
, sizes
, bmEqual(EqualPortable
))
429 func bmEqual(equal
func([]byte, []byte) bool) func(b
*testing
.B
, n
int) {
430 return func(b
*testing
.B
, n
int) {
431 if len(bmbuf
) < 2*n
{
432 bmbuf
= make([]byte, 2*n
)
435 buf2
:= bmbuf
[n
: 2*n
]
438 for i
:= 0; i
< b
.N
; i
++ {
439 eq
:= equal(buf1
, buf2
)
449 func BenchmarkIndex(b
*testing
.B
) {
450 benchBytes(b
, indexSizes
, func(b
*testing
.B
, n
int) {
453 for i
:= 0; i
< b
.N
; i
++ {
454 j
:= Index(buf
, buf
[n
-7:])
456 b
.Fatal("bad index", j
)
463 func BenchmarkIndexEasy(b
*testing
.B
) {
464 benchBytes(b
, indexSizes
, func(b
*testing
.B
, n
int) {
468 for i
:= 0; i
< b
.N
; i
++ {
469 j
:= Index(buf
, buf
[n
-7:])
471 b
.Fatal("bad index", j
)
479 func BenchmarkCount(b
*testing
.B
) {
480 benchBytes(b
, indexSizes
, func(b
*testing
.B
, n
int) {
483 for i
:= 0; i
< b
.N
; i
++ {
484 j
:= Count(buf
, buf
[n
-7:])
486 b
.Fatal("bad count", j
)
493 func BenchmarkCountEasy(b
*testing
.B
) {
494 benchBytes(b
, indexSizes
, func(b
*testing
.B
, n
int) {
498 for i
:= 0; i
< b
.N
; i
++ {
499 j
:= Count(buf
, buf
[n
-7:])
501 b
.Fatal("bad count", j
)
509 type ExplodeTest
struct {
515 var explodetests
= []ExplodeTest
{
516 {"", -1, []string{}},
517 {abcd
, -1, []string{"a", "b", "c", "d"}},
518 {faces
, -1, []string{"☺", "☻", "☹"}},
519 {abcd
, 2, []string{"a", "bcd"}},
522 func TestExplode(t
*testing
.T
) {
523 for _
, tt
:= range explodetests
{
524 a
:= SplitN([]byte(tt
.s
), nil, tt
.n
)
525 result
:= sliceOfString(a
)
526 if !eq(result
, tt
.a
) {
527 t
.Errorf(`Explode("%s", %d) = %v; want %v`, tt
.s
, tt
.n
, result
, tt
.a
)
530 s
:= Join(a
, []byte{})
531 if string(s
) != tt
.s
{
532 t
.Errorf(`Join(Explode("%s", %d), "") = "%s"`, tt
.s
, tt
.n
, s
)
537 type SplitTest
struct {
544 var splittests
= []SplitTest
{
546 {abcd
, "a", -1, []string{"", "bcd"}},
547 {abcd
, "z", -1, []string{"abcd"}},
548 {abcd
, "", -1, []string{"a", "b", "c", "d"}},
549 {commas
, ",", -1, []string{"1", "2", "3", "4"}},
550 {dots
, "...", -1, []string{"1", ".2", ".3", ".4"}},
551 {faces
, "☹", -1, []string{"☺☻", ""}},
552 {faces
, "~", -1, []string{faces
}},
553 {faces
, "", -1, []string{"☺", "☻", "☹"}},
554 {"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
555 {"1 2", " ", 3, []string{"1", "2"}},
556 {"123", "", 2, []string{"1", "23"}},
557 {"123", "", 17, []string{"1", "2", "3"}},
560 func TestSplit(t
*testing
.T
) {
561 for _
, tt
:= range splittests
{
562 a
:= SplitN([]byte(tt
.s
), []byte(tt
.sep
), tt
.n
)
563 result
:= sliceOfString(a
)
564 if !eq(result
, tt
.a
) {
565 t
.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt
.s
, tt
.sep
, tt
.n
, result
, tt
.a
)
571 s
:= Join(a
, []byte(tt
.sep
))
572 if string(s
) != tt
.s
{
573 t
.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt
.s
, tt
.sep
, tt
.n
, tt
.sep
, s
)
576 b
:= Split([]byte(tt
.s
), []byte(tt
.sep
))
577 if !reflect
.DeepEqual(a
, b
) {
578 t
.Errorf("Split disagrees withSplitN(%q, %q, %d) = %v; want %v", tt
.s
, tt
.sep
, tt
.n
, b
, a
)
583 if cap(in
) == cap(out
) && &in
[:1][0] == &out
[:1][0] {
584 t
.Errorf("Join(%#v, %q) didn't copy", a
, tt
.sep
)
590 var splitaftertests
= []SplitTest
{
591 {abcd
, "a", -1, []string{"a", "bcd"}},
592 {abcd
, "z", -1, []string{"abcd"}},
593 {abcd
, "", -1, []string{"a", "b", "c", "d"}},
594 {commas
, ",", -1, []string{"1,", "2,", "3,", "4"}},
595 {dots
, "...", -1, []string{"1...", ".2...", ".3...", ".4"}},
596 {faces
, "☹", -1, []string{"☺☻☹", ""}},
597 {faces
, "~", -1, []string{faces
}},
598 {faces
, "", -1, []string{"☺", "☻", "☹"}},
599 {"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}},
600 {"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}},
601 {"1 2", " ", 3, []string{"1 ", "2"}},
602 {"123", "", 2, []string{"1", "23"}},
603 {"123", "", 17, []string{"1", "2", "3"}},
606 func TestSplitAfter(t
*testing
.T
) {
607 for _
, tt
:= range splitaftertests
{
608 a
:= SplitAfterN([]byte(tt
.s
), []byte(tt
.sep
), tt
.n
)
609 result
:= sliceOfString(a
)
610 if !eq(result
, tt
.a
) {
611 t
.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt
.s
, tt
.sep
, tt
.n
, result
, tt
.a
)
615 if string(s
) != tt
.s
{
616 t
.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt
.s
, tt
.sep
, tt
.n
, tt
.sep
, s
)
619 b
:= SplitAfter([]byte(tt
.s
), []byte(tt
.sep
))
620 if !reflect
.DeepEqual(a
, b
) {
621 t
.Errorf("SplitAfter disagrees withSplitAfterN(%q, %q, %d) = %v; want %v", tt
.s
, tt
.sep
, tt
.n
, b
, a
)
627 type FieldsTest
struct {
632 var fieldstests
= []FieldsTest
{
635 {" \t ", []string{}},
636 {" abc ", []string{"abc"}},
637 {"1 2 3 4", []string{"1", "2", "3", "4"}},
638 {"1 2 3 4", []string{"1", "2", "3", "4"}},
639 {"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
640 {"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
641 {"\u2000\u2001\u2002", []string{}},
642 {"\n™\t™\n", []string{"™", "™"}},
643 {faces
, []string{faces
}},
646 func TestFields(t
*testing
.T
) {
647 for _
, tt
:= range fieldstests
{
648 a
:= Fields([]byte(tt
.s
))
649 result
:= sliceOfString(a
)
650 if !eq(result
, tt
.a
) {
651 t
.Errorf("Fields(%q) = %v; want %v", tt
.s
, a
, tt
.a
)
657 func TestFieldsFunc(t
*testing
.T
) {
658 for _
, tt
:= range fieldstests
{
659 a
:= FieldsFunc([]byte(tt
.s
), unicode
.IsSpace
)
660 result
:= sliceOfString(a
)
661 if !eq(result
, tt
.a
) {
662 t
.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt
.s
, a
, tt
.a
)
666 pred
:= func(c rune
) bool { return c
== 'X' }
667 var fieldsFuncTests
= []FieldsTest
{
670 {"XXhiXXX", []string{"hi"}},
671 {"aXXbXXXcX", []string{"a", "b", "c"}},
673 for _
, tt
:= range fieldsFuncTests
{
674 a
:= FieldsFunc([]byte(tt
.s
), pred
)
675 result
:= sliceOfString(a
)
676 if !eq(result
, tt
.a
) {
677 t
.Errorf("FieldsFunc(%q) = %v, want %v", tt
.s
, a
, tt
.a
)
682 // Test case for any function which accepts and returns a byte slice.
683 // For ease of creation, we write the byte slices as strings.
684 type StringTest
struct {
688 var upperTests
= []StringTest
{
691 {"AbC123", "ABC123"},
692 {"azAZ09_", "AZAZ09_"},
693 {"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
696 var lowerTests
= []StringTest
{
699 {"AbC123", "abc123"},
700 {"azAZ09_", "azaz09_"},
701 {"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
704 const space
= "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
706 var trimSpaceTests
= []StringTest
{
709 {space
+ "abc" + space
, "abc"},
711 {" \t\r\n \t\t\r\r\n\n ", ""},
712 {" \t\r\n x\t\t\r\r\n\n ", "x"},
713 {" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
714 {"1 \t\r\n2", "1 \t\r\n2"},
717 {"x \xc0\xc0 ", "x \xc0\xc0"},
718 {"x \xc0", "x \xc0"},
719 {"x \xc0 ", "x \xc0"},
720 {"x \xc0\xc0 ", "x \xc0\xc0"},
721 {"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"},
725 // Execute f on each test case. funcName should be the name of f; it's used
726 // in failure reports.
727 func runStringTests(t
*testing
.T
, f
func([]byte) []byte, funcName
string, testCases
[]StringTest
) {
728 for _
, tc
:= range testCases
{
729 actual
:= string(f([]byte(tc
.in
)))
730 if actual
!= tc
.out
{
731 t
.Errorf("%s(%q) = %q; want %q", funcName
, tc
.in
, actual
, tc
.out
)
736 func tenRunes(r rune
) string {
737 runes
:= make([]rune
, 10)
738 for i
:= range runes
{
744 // User-defined self-inverse mapping function
745 func rot13(r rune
) rune
{
747 if r
>= 'a' && r
<= 'z' {
748 return ((r
- 'a' + step
) % 26) + 'a'
750 if r
>= 'A' && r
<= 'Z' {
751 return ((r
- 'A' + step
) % 26) + 'A'
756 func TestMap(t
*testing
.T
) {
757 // Run a couple of awful growth/shrinkage tests
760 // 1. Grow. This triggers two reallocations in Map.
761 maxRune
:= func(r rune
) rune
{ return unicode
.MaxRune
}
762 m
:= Map(maxRune
, []byte(a
))
763 expect
:= tenRunes(unicode
.MaxRune
)
764 if string(m
) != expect
{
765 t
.Errorf("growing: expected %q got %q", expect
, m
)
769 minRune
:= func(r rune
) rune
{ return 'a' }
770 m
= Map(minRune
, []byte(tenRunes(unicode
.MaxRune
)))
772 if string(m
) != expect
{
773 t
.Errorf("shrinking: expected %q got %q", expect
, m
)
777 m
= Map(rot13
, []byte("a to zed"))
779 if string(m
) != expect
{
780 t
.Errorf("rot13: expected %q got %q", expect
, m
)
784 m
= Map(rot13
, Map(rot13
, []byte("a to zed")))
786 if string(m
) != expect
{
787 t
.Errorf("rot13: expected %q got %q", expect
, m
)
791 dropNotLatin
:= func(r rune
) rune
{
792 if unicode
.Is(unicode
.Latin
, r
) {
797 m
= Map(dropNotLatin
, []byte("Hello, 세계"))
799 if string(m
) != expect
{
800 t
.Errorf("drop: expected %q got %q", expect
, m
)
804 invalidRune
:= func(r rune
) rune
{
805 return utf8
.MaxRune
+ 1
807 m
= Map(invalidRune
, []byte("x"))
809 if string(m
) != expect
{
810 t
.Errorf("invalidRune: expected %q got %q", expect
, m
)
814 func TestToUpper(t
*testing
.T
) { runStringTests(t
, ToUpper
, "ToUpper", upperTests
) }
816 func TestToLower(t
*testing
.T
) { runStringTests(t
, ToLower
, "ToLower", lowerTests
) }
818 func TestTrimSpace(t
*testing
.T
) { runStringTests(t
, TrimSpace
, "TrimSpace", trimSpaceTests
) }
820 type RepeatTest
struct {
825 var RepeatTests
= []RepeatTest
{
831 {"-", "----------", 10},
832 {"abc ", "abc abc abc ", 3},
835 func TestRepeat(t
*testing
.T
) {
836 for _
, tt
:= range RepeatTests
{
838 tout
:= []byte(tt
.out
)
839 a
:= Repeat(tin
, tt
.count
)
841 t
.Errorf("Repeat(%q, %d) = %q; want %q", tin
, tt
.count
, a
, tout
)
847 func runesEqual(a
, b
[]rune
) bool {
848 if len(a
) != len(b
) {
851 for i
, r
:= range a
{
859 type RunesTest
struct {
865 var RunesTests
= []RunesTest
{
866 {"", []rune
{}, false},
867 {" ", []rune
{32}, false},
868 {"ABC", []rune
{65, 66, 67}, false},
869 {"abc", []rune
{97, 98, 99}, false},
870 {"\u65e5\u672c\u8a9e", []rune
{26085, 26412, 35486}, false},
871 {"ab\x80c", []rune
{97, 98, 0xFFFD, 99}, true},
872 {"ab\xc0c", []rune
{97, 98, 0xFFFD, 99}, true},
875 func TestRunes(t
*testing
.T
) {
876 for _
, tt
:= range RunesTests
{
879 if !runesEqual(a
, tt
.out
) {
880 t
.Errorf("Runes(%q) = %v; want %v", tin
, a
, tt
.out
)
884 // can only test reassembly if we didn't lose information
887 t
.Errorf("string(Runes(%q)) = %x; want %x", tin
, s
, tin
)
893 type TrimTest
struct {
898 var trimTests
= []TrimTest
{
899 {"Trim", "abba", "a", "bb"},
900 {"Trim", "abba", "ab", ""},
901 {"TrimLeft", "abba", "ab", ""},
902 {"TrimRight", "abba", "ab", ""},
903 {"TrimLeft", "abba", "a", "bba"},
904 {"TrimRight", "abba", "a", "abb"},
905 {"Trim", "<tag>", "<>", "tag"},
906 {"Trim", "* listitem", " *", "listitem"},
907 {"Trim", `"quote"`, `"`, "quote"},
908 {"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
910 {"Trim", "abba", "", "abba"},
911 {"Trim", "", "123", ""},
912 {"Trim", "", "", ""},
913 {"TrimLeft", "abba", "", "abba"},
914 {"TrimLeft", "", "123", ""},
915 {"TrimLeft", "", "", ""},
916 {"TrimRight", "abba", "", "abba"},
917 {"TrimRight", "", "123", ""},
918 {"TrimRight", "", "", ""},
919 {"TrimRight", "☺\xc0", "☺", "☺\xc0"},
920 {"TrimPrefix", "aabb", "a", "abb"},
921 {"TrimPrefix", "aabb", "b", "aabb"},
922 {"TrimSuffix", "aabb", "a", "aabb"},
923 {"TrimSuffix", "aabb", "b", "aab"},
926 func TestTrim(t
*testing
.T
) {
927 for _
, tc
:= range trimTests
{
929 var f
func([]byte, string) []byte
930 var fb
func([]byte, []byte) []byte
943 t
.Errorf("Undefined trim function %s", name
)
947 actual
= string(f([]byte(tc
.in
), tc
.arg
))
949 actual
= string(fb([]byte(tc
.in
), []byte(tc
.arg
)))
951 if actual
!= tc
.out
{
952 t
.Errorf("%s(%q, %q) = %q; want %q", name
, tc
.in
, tc
.arg
, actual
, tc
.out
)
957 type predicate
struct {
962 var isSpace
= predicate
{unicode
.IsSpace
, "IsSpace"}
963 var isDigit
= predicate
{unicode
.IsDigit
, "IsDigit"}
964 var isUpper
= predicate
{unicode
.IsUpper
, "IsUpper"}
965 var isValidRune
= predicate
{
967 return r
!= utf8
.RuneError
972 type TrimFuncTest
struct {
977 func not(p predicate
) predicate
{
986 var trimFuncTests
= []TrimFuncTest
{
987 {isSpace
, space
+ " hello " + space
, "hello"},
988 {isDigit
, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
989 {isUpper
, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
990 {not(isSpace
), "hello" + space
+ "hello", space
},
991 {not(isDigit
), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"},
992 {isValidRune
, "ab\xc0a\xc0cd", "\xc0a\xc0"},
993 {not(isValidRune
), "\xc0a\xc0", "a"},
996 func TestTrimFunc(t
*testing
.T
) {
997 for _
, tc
:= range trimFuncTests
{
998 actual
:= string(TrimFunc([]byte(tc
.in
), tc
.f
.f
))
999 if actual
!= tc
.out
{
1000 t
.Errorf("TrimFunc(%q, %q) = %q; want %q", tc
.in
, tc
.f
.name
, actual
, tc
.out
)
1005 type IndexFuncTest
struct {
1011 var indexFuncTests
= []IndexFuncTest
{
1012 {"", isValidRune
, -1, -1},
1013 {"abc", isDigit
, -1, -1},
1014 {"0123", isDigit
, 0, 3},
1015 {"a1b", isDigit
, 1, 1},
1016 {space
, isSpace
, 0, len(space
) - 3}, // last rune in space is 3 bytes
1017 {"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit
, 0, 18},
1018 {"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper
, 0, 34},
1019 {"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit
), 8, 12},
1021 // tests of invalid UTF-8
1022 {"\x801", isDigit
, 1, 1},
1023 {"\x80abc", isDigit
, -1, -1},
1024 {"\xc0a\xc0", isValidRune
, 1, 1},
1025 {"\xc0a\xc0", not(isValidRune
), 0, 2},
1026 {"\xc0☺\xc0", not(isValidRune
), 0, 4},
1027 {"\xc0☺\xc0\xc0", not(isValidRune
), 0, 5},
1028 {"ab\xc0a\xc0cd", not(isValidRune
), 2, 4},
1029 {"a\xe0\x80cd", not(isValidRune
), 1, 2},
1032 func TestIndexFunc(t
*testing
.T
) {
1033 for _
, tc
:= range indexFuncTests
{
1034 first
:= IndexFunc([]byte(tc
.in
), tc
.f
.f
)
1035 if first
!= tc
.first
{
1036 t
.Errorf("IndexFunc(%q, %s) = %d; want %d", tc
.in
, tc
.f
.name
, first
, tc
.first
)
1038 last
:= LastIndexFunc([]byte(tc
.in
), tc
.f
.f
)
1039 if last
!= tc
.last
{
1040 t
.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc
.in
, tc
.f
.name
, last
, tc
.last
)
1045 type ReplaceTest
struct {
1052 var ReplaceTests
= []ReplaceTest
{
1053 {"hello", "l", "L", 0, "hello"},
1054 {"hello", "l", "L", -1, "heLLo"},
1055 {"hello", "x", "X", -1, "hello"},
1056 {"", "x", "X", -1, ""},
1057 {"radar", "r", "<r>", -1, "<r>ada<r>"},
1058 {"", "", "<>", -1, "<>"},
1059 {"banana", "a", "<>", -1, "b<>n<>n<>"},
1060 {"banana", "a", "<>", 1, "b<>nana"},
1061 {"banana", "a", "<>", 1000, "b<>n<>n<>"},
1062 {"banana", "an", "<>", -1, "b<><>a"},
1063 {"banana", "ana", "<>", -1, "b<>na"},
1064 {"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"},
1065 {"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
1066 {"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
1067 {"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
1068 {"banana", "", "<>", 1, "<>banana"},
1069 {"banana", "a", "a", -1, "banana"},
1070 {"banana", "a", "a", 1, "banana"},
1071 {"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"},
1074 func TestReplace(t
*testing
.T
) {
1075 for _
, tt
:= range ReplaceTests
{
1076 in
:= append([]byte(tt
.in
), "<spare>"...)
1077 in
= in
[:len(tt
.in
)]
1078 out
:= Replace(in
, []byte(tt
.old
), []byte(tt
.new), tt
.n
)
1079 if s
:= string(out
); s
!= tt
.out
{
1080 t
.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt
.in
, tt
.old
, tt
.new, tt
.n
, s
, tt
.out
)
1082 if cap(in
) == cap(out
) && &in
[:1][0] == &out
[:1][0] {
1083 t
.Errorf("Replace(%q, %q, %q, %d) didn't copy", tt
.in
, tt
.old
, tt
.new, tt
.n
)
1088 type TitleTest
struct {
1092 var TitleTests
= []TitleTest
{
1095 {" aaa aaa aaa ", " Aaa Aaa Aaa "},
1096 {" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
1097 {"123a456", "123a456"},
1098 {"double-blind", "Double-Blind"},
1100 {"with_underscore", "With_underscore"},
1101 {"unicode \xe2\x80\xa8 line separator", "Unicode \xe2\x80\xa8 Line Separator"},
1104 func TestTitle(t
*testing
.T
) {
1105 for _
, tt
:= range TitleTests
{
1106 if s
:= string(Title([]byte(tt
.in
))); s
!= tt
.out
{
1107 t
.Errorf("Title(%q) = %q, want %q", tt
.in
, s
, tt
.out
)
1112 var ToTitleTests
= []TitleTest
{
1115 {" aaa aaa aaa ", " AAA AAA AAA "},
1116 {" Aaa Aaa Aaa ", " AAA AAA AAA "},
1117 {"123a456", "123A456"},
1118 {"double-blind", "DOUBLE-BLIND"},
1122 func TestToTitle(t
*testing
.T
) {
1123 for _
, tt
:= range ToTitleTests
{
1124 if s
:= string(ToTitle([]byte(tt
.in
))); s
!= tt
.out
{
1125 t
.Errorf("ToTitle(%q) = %q, want %q", tt
.in
, s
, tt
.out
)
1130 var EqualFoldTests
= []struct {
1134 {"abc", "abc", true},
1135 {"ABcd", "ABcd", true},
1136 {"123abc", "123ABC", true},
1137 {"αβδ", "ΑΒΔ", true},
1138 {"abc", "xyz", false},
1139 {"abc", "XYZ", false},
1140 {"abcdefghijk", "abcdefghijX", false},
1141 {"abcdefghijk", "abcdefghij\u212A", true},
1142 {"abcdefghijK", "abcdefghij\u212A", true},
1143 {"abcdefghijkz", "abcdefghij\u212Ay", false},
1144 {"abcdefghijKz", "abcdefghij\u212Ay", false},
1147 func TestEqualFold(t
*testing
.T
) {
1148 for _
, tt
:= range EqualFoldTests
{
1149 if out
:= EqualFold([]byte(tt
.s
), []byte(tt
.t
)); out
!= tt
.out
{
1150 t
.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt
.s
, tt
.t
, out
, tt
.out
)
1152 if out
:= EqualFold([]byte(tt
.t
), []byte(tt
.s
)); out
!= tt
.out
{
1153 t
.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt
.t
, tt
.s
, out
, tt
.out
)
1158 func TestBufferGrowNegative(t
*testing
.T
) {
1160 if err
:= recover(); err
== nil {
1161 t
.Fatal("Grow(-1) should have panicked")
1168 func TestBufferTruncateNegative(t
*testing
.T
) {
1170 if err
:= recover(); err
== nil {
1171 t
.Fatal("Truncate(-1) should have panicked")
1178 func TestBufferTruncateOutOfRange(t
*testing
.T
) {
1180 if err
:= recover(); err
== nil {
1181 t
.Fatal("Truncate(20) should have panicked")
1185 b
.Write(make([]byte, 10))
1189 var containsTests
= []struct {
1193 {[]byte("hello"), []byte("hel"), true},
1194 {[]byte("日本語"), []byte("日本"), true},
1195 {[]byte("hello"), []byte("Hello, world"), false},
1196 {[]byte("東京"), []byte("京東"), false},
1199 func TestContains(t
*testing
.T
) {
1200 for _
, tt
:= range containsTests
{
1201 if got
:= Contains(tt
.b
, tt
.subslice
); got
!= tt
.want
{
1202 t
.Errorf("Contains(%q, %q) = %v, want %v", tt
.b
, tt
.subslice
, got
, tt
.want
)
1207 var ContainsAnyTests
= []struct {
1212 {[]byte(""), "", false},
1213 {[]byte(""), "a", false},
1214 {[]byte(""), "abc", false},
1215 {[]byte("a"), "", false},
1216 {[]byte("a"), "a", true},
1217 {[]byte("aaa"), "a", true},
1218 {[]byte("abc"), "xyz", false},
1219 {[]byte("abc"), "xcz", true},
1220 {[]byte("a☺b☻c☹d"), "uvw☻xyz", true},
1221 {[]byte("aRegExp*"), ".(|)*+?^$[]", true},
1222 {[]byte(dots
+ dots
+ dots
), " ", false},
1225 func TestContainsAny(t
*testing
.T
) {
1226 for _
, ct
:= range ContainsAnyTests
{
1227 if ContainsAny(ct
.b
, ct
.substr
) != ct
.expected
{
1228 t
.Errorf("ContainsAny(%s, %s) = %v, want %v",
1229 ct
.b
, ct
.substr
, !ct
.expected
, ct
.expected
)
1234 var ContainsRuneTests
= []struct {
1239 {[]byte(""), 'a', false},
1240 {[]byte("a"), 'a', true},
1241 {[]byte("aaa"), 'a', true},
1242 {[]byte("abc"), 'y', false},
1243 {[]byte("abc"), 'c', true},
1244 {[]byte("a☺b☻c☹d"), 'x', false},
1245 {[]byte("a☺b☻c☹d"), '☻', true},
1246 {[]byte("aRegExp*"), '*', true},
1249 func TestContainsRune(t
*testing
.T
) {
1250 for _
, ct
:= range ContainsRuneTests
{
1251 if ContainsRune(ct
.b
, ct
.r
) != ct
.expected
{
1252 t
.Errorf("ContainsRune(%q, %q) = %v, want %v",
1253 ct
.b
, ct
.r
, !ct
.expected
, ct
.expected
)
1258 var makeFieldsInput
= func() []byte {
1259 x
:= make([]byte, 1<<20)
1260 // Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
1262 switch rand
.Intn(10) {
1266 if i
> 0 && x
[i
-1] == 'x' {
1278 var fieldsInput
= makeFieldsInput()
1280 func BenchmarkFields(b
*testing
.B
) {
1281 b
.SetBytes(int64(len(fieldsInput
)))
1282 for i
:= 0; i
< b
.N
; i
++ {
1287 func BenchmarkFieldsFunc(b
*testing
.B
) {
1288 b
.SetBytes(int64(len(fieldsInput
)))
1289 for i
:= 0; i
< b
.N
; i
++ {
1290 FieldsFunc(fieldsInput
, unicode
.IsSpace
)
1294 func BenchmarkTrimSpace(b
*testing
.B
) {
1295 s
:= []byte(" Some text. \n")
1296 for i
:= 0; i
< b
.N
; i
++ {
1301 func BenchmarkRepeat(b
*testing
.B
) {
1302 for i
:= 0; i
< b
.N
; i
++ {
1303 Repeat([]byte("-"), 80)
1307 func BenchmarkBytesCompare(b
*testing
.B
) {
1308 for n
:= 1; n
<= 2048; n
<<= 1 {
1309 b
.Run(fmt
.Sprint(n
), func(b
*testing
.B
) {
1310 var x
= make([]byte, n
)
1311 var y
= make([]byte, n
)
1313 for i
:= 0; i
< n
; i
++ {
1317 for i
:= 0; i
< n
; i
++ {
1322 for i
:= 0; i
< b
.N
; i
++ {