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.
16 func eq(a
, b
[]string) bool {
20 for i
:= 0; i
< len(a
); i
++ {
28 func sliceOfString(s
[][]byte) []string {
29 result
:= make([]string, len(s
))
36 // For ease of reading, the test cases use strings that are converted to byte
37 // slices before invoking the functions.
41 var commas
= "1,2,3,4"
42 var dots
= "1....2....3....4"
44 type BinOpTest
struct {
50 var equalTests
= []struct {
54 {[]byte(""), []byte(""), 0},
55 {[]byte("a"), []byte(""), 1},
56 {[]byte(""), []byte("a"), -1},
57 {[]byte("abc"), []byte("abc"), 0},
58 {[]byte("ab"), []byte("abc"), -1},
59 {[]byte("abc"), []byte("ab"), 1},
60 {[]byte("x"), []byte("ab"), 1},
61 {[]byte("ab"), []byte("x"), -1},
62 {[]byte("x"), []byte("a"), 1},
63 {[]byte("b"), []byte("x"), -1},
64 // test runtime·memeq's chunked implementation
65 {[]byte("abcdefgh"), []byte("abcdefgh"), 0},
66 {[]byte("abcdefghi"), []byte("abcdefghi"), 0},
67 {[]byte("abcdefghi"), []byte("abcdefghj"), -1},
72 {[]byte("a"), nil, 1},
73 {nil, []byte("a"), -1},
76 func TestEqual(t
*testing
.T
) {
77 for _
, tt
:= range compareTests
{
78 eql
:= Equal(tt
.a
, tt
.b
)
79 if eql
!= (tt
.i
== 0) {
80 t
.Errorf(`Equal(%q, %q) = %v`, tt
.a
, tt
.b
, eql
)
82 eql
= EqualPortable(tt
.a
, tt
.b
)
83 if eql
!= (tt
.i
== 0) {
84 t
.Errorf(`EqualPortable(%q, %q) = %v`, tt
.a
, tt
.b
, eql
)
89 func TestEqualExhaustive(t
*testing
.T
) {
94 a
:= make([]byte, size
)
95 b
:= make([]byte, size
)
96 b_init
:= make([]byte, size
)
97 // randomish but deterministic data
98 for i
:= 0; i
< size
; i
++ {
100 b_init
[i
] = byte(23*i
+ 100)
103 for len := 0; len <= size
; len++ {
104 for x
:= 0; x
<= size
-len; x
++ {
105 for y
:= 0; y
<= size
-len; y
++ {
107 copy(b
[y
:y
+len], a
[x
:x
+len])
108 if !Equal(a
[x
:x
+len], b
[y
:y
+len]) ||
!Equal(b
[y
:y
+len], a
[x
:x
+len]) {
109 t
.Errorf("Equal(%d, %d, %d) = false", len, x
, y
)
116 // make sure Equal returns false for minimally different strings. The data
117 // is all zeros except for a single one in one location.
118 func TestNotEqual(t
*testing
.T
) {
123 a
:= make([]byte, size
)
124 b
:= make([]byte, size
)
126 for len := 0; len <= size
; len++ {
127 for x
:= 0; x
<= size
-len; x
++ {
128 for y
:= 0; y
<= size
-len; y
++ {
129 for diffpos
:= x
; diffpos
< x
+len; diffpos
++ {
131 if Equal(a
[x
:x
+len], b
[y
:y
+len]) ||
Equal(b
[y
:y
+len], a
[x
:x
+len]) {
132 t
.Errorf("NotEqual(%d, %d, %d, %d) = true", len, x
, y
, diffpos
)
141 var indexTests
= []BinOpTest
{
148 {"oofofoofooo", "f", 2},
149 {"oofofoofooo", "foo", 4},
150 {"barfoobarfoo", "foo", 3},
153 {"abcABCabc", "A", 3},
154 // cases with one byte strings - test IndexByte and special case in Index()
162 {"barfoobarfooyyyzzzyyyzzzyyyzzzyyyxxxzzzyyy", "x", 33},
163 {"foofyfoobarfoobar", "y", 4},
164 {"oooooooooooooooooooooo", "r", -1},
167 var lastIndexTests
= []BinOpTest
{
174 {"oofofoofooo", "f", 7},
175 {"oofofoofooo", "foo", 7},
176 {"barfoobarfoo", "foo", 9},
179 {"abcABCabc", "A", 3},
180 {"abcABCabc", "a", 6},
183 var indexAnyTests
= []BinOpTest
{
193 {"aRegExp*", ".(|)*+?^$[]", 7},
194 {dots
+ dots
+ dots
, " ", -1},
197 var lastIndexAnyTests
= []BinOpTest
{
206 {"a☺b☻c☹d", "uvw☻xyz", 2 + len("☺")},
207 {"a.RegExp*", ".(|)*+?^$[]", 8},
208 {dots
+ dots
+ dots
, " ", -1},
211 var indexRuneTests
= []BinOpTest
{
217 {"foo☺☻☹bar", "☹", 9},
220 // Execute f on each test case. funcName should be the name of f; it's used
221 // in failure reports.
222 func runIndexTests(t
*testing
.T
, f
func(s
, sep
[]byte) int, funcName
string, testCases
[]BinOpTest
) {
223 for _
, test
:= range testCases
{
227 if actual
!= test
.i
{
228 t
.Errorf("%s(%q,%q) = %v; want %v", funcName
, a
, b
, actual
, test
.i
)
233 func runIndexAnyTests(t
*testing
.T
, f
func(s
[]byte, chars
string) int, funcName
string, testCases
[]BinOpTest
) {
234 for _
, test
:= range testCases
{
236 actual
:= f(a
, test
.b
)
237 if actual
!= test
.i
{
238 t
.Errorf("%s(%q,%q) = %v; want %v", funcName
, a
, test
.b
, actual
, test
.i
)
243 func TestIndex(t
*testing
.T
) { runIndexTests(t
, Index
, "Index", indexTests
) }
244 func TestLastIndex(t
*testing
.T
) { runIndexTests(t
, LastIndex
, "LastIndex", lastIndexTests
) }
245 func TestIndexAny(t
*testing
.T
) { runIndexAnyTests(t
, IndexAny
, "IndexAny", indexAnyTests
) }
246 func TestLastIndexAny(t
*testing
.T
) {
247 runIndexAnyTests(t
, LastIndexAny
, "LastIndexAny", lastIndexAnyTests
)
250 func TestIndexByte(t
*testing
.T
) {
251 for _
, tt
:= range indexTests
{
257 pos
:= IndexByte(a
, b
)
259 t
.Errorf(`IndexByte(%q, '%c') = %v`, tt
.a
, b
, pos
)
261 posp
:= IndexBytePortable(a
, b
)
263 t
.Errorf(`indexBytePortable(%q, '%c') = %v`, tt
.a
, b
, posp
)
268 // test a larger buffer with different sizes and alignments
269 func TestIndexByteBig(t
*testing
.T
) {
275 for i
:= 0; i
< n
; i
++ {
276 // different start alignments
278 for j
:= 0; j
< len(b1
); j
++ {
280 pos
:= IndexByte(b1
, 'x')
282 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
285 pos
= IndexByte(b1
, 'x')
287 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
290 // different end alignments
292 for j
:= 0; j
< len(b1
); j
++ {
294 pos
:= IndexByte(b1
, 'x')
296 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
299 pos
= IndexByte(b1
, 'x')
301 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
304 // different start and end alignments
305 b1
= b
[i
/2 : n
-(i
+1)/2]
306 for j
:= 0; j
< len(b1
); j
++ {
308 pos
:= IndexByte(b1
, 'x')
310 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
313 pos
= IndexByte(b1
, 'x')
315 t
.Errorf("IndexByte(%q, 'x') = %v", b1
, pos
)
321 func TestIndexRune(t
*testing
.T
) {
322 for _
, tt
:= range indexRuneTests
{
324 r
, _
:= utf8
.DecodeRuneInString(tt
.b
)
325 pos
:= IndexRune(a
, r
)
327 t
.Errorf(`IndexRune(%q, '%c') = %v`, tt
.a
, r
, pos
)
334 func BenchmarkIndexByte32(b
*testing
.B
) { bmIndexByte(b
, IndexByte
, 32) }
335 func BenchmarkIndexByte4K(b
*testing
.B
) { bmIndexByte(b
, IndexByte
, 4<<10) }
336 func BenchmarkIndexByte4M(b
*testing
.B
) { bmIndexByte(b
, IndexByte
, 4<<20) }
337 func BenchmarkIndexByte64M(b
*testing
.B
) { bmIndexByte(b
, IndexByte
, 64<<20) }
338 func BenchmarkIndexBytePortable32(b
*testing
.B
) { bmIndexByte(b
, IndexBytePortable
, 32) }
339 func BenchmarkIndexBytePortable4K(b
*testing
.B
) { bmIndexByte(b
, IndexBytePortable
, 4<<10) }
340 func BenchmarkIndexBytePortable4M(b
*testing
.B
) { bmIndexByte(b
, IndexBytePortable
, 4<<20) }
341 func BenchmarkIndexBytePortable64M(b
*testing
.B
) { bmIndexByte(b
, IndexBytePortable
, 64<<20) }
343 func bmIndexByte(b
*testing
.B
, index
func([]byte, byte) int, n
int) {
345 bmbuf
= make([]byte, n
)
350 for i
:= 0; i
< b
.N
; i
++ {
353 b
.Fatal("bad index", j
)
359 func BenchmarkEqual0(b
*testing
.B
) {
363 for i
:= 0; i
< b
.N
; i
++ {
364 eq
:= Equal(buf1
, buf2
)
371 func BenchmarkEqual1(b
*testing
.B
) { bmEqual(b
, Equal
, 1) }
372 func BenchmarkEqual6(b
*testing
.B
) { bmEqual(b
, Equal
, 6) }
373 func BenchmarkEqual9(b
*testing
.B
) { bmEqual(b
, Equal
, 9) }
374 func BenchmarkEqual15(b
*testing
.B
) { bmEqual(b
, Equal
, 15) }
375 func BenchmarkEqual16(b
*testing
.B
) { bmEqual(b
, Equal
, 16) }
376 func BenchmarkEqual20(b
*testing
.B
) { bmEqual(b
, Equal
, 20) }
377 func BenchmarkEqual32(b
*testing
.B
) { bmEqual(b
, Equal
, 32) }
378 func BenchmarkEqual4K(b
*testing
.B
) { bmEqual(b
, Equal
, 4<<10) }
379 func BenchmarkEqual4M(b
*testing
.B
) { bmEqual(b
, Equal
, 4<<20) }
380 func BenchmarkEqual64M(b
*testing
.B
) { bmEqual(b
, Equal
, 64<<20) }
381 func BenchmarkEqualPort1(b
*testing
.B
) { bmEqual(b
, EqualPortable
, 1) }
382 func BenchmarkEqualPort6(b
*testing
.B
) { bmEqual(b
, EqualPortable
, 6) }
383 func BenchmarkEqualPort32(b
*testing
.B
) { bmEqual(b
, EqualPortable
, 32) }
384 func BenchmarkEqualPort4K(b
*testing
.B
) { bmEqual(b
, EqualPortable
, 4<<10) }
385 func BenchmarkEqualPortable4M(b
*testing
.B
) { bmEqual(b
, EqualPortable
, 4<<20) }
386 func BenchmarkEqualPortable64M(b
*testing
.B
) { bmEqual(b
, EqualPortable
, 64<<20) }
388 func bmEqual(b
*testing
.B
, equal
func([]byte, []byte) bool, n
int) {
389 if len(bmbuf
) < 2*n
{
390 bmbuf
= make([]byte, 2*n
)
394 buf2
:= bmbuf
[n
: 2*n
]
397 for i
:= 0; i
< b
.N
; i
++ {
398 eq
:= equal(buf1
, buf2
)
407 func BenchmarkIndex32(b
*testing
.B
) { bmIndex(b
, Index
, 32) }
408 func BenchmarkIndex4K(b
*testing
.B
) { bmIndex(b
, Index
, 4<<10) }
409 func BenchmarkIndex4M(b
*testing
.B
) { bmIndex(b
, Index
, 4<<20) }
410 func BenchmarkIndex64M(b
*testing
.B
) { bmIndex(b
, Index
, 64<<20) }
412 func bmIndex(b
*testing
.B
, index
func([]byte, []byte) int, n
int) {
414 bmbuf
= make([]byte, n
)
419 for i
:= 0; i
< b
.N
; i
++ {
420 j
:= index(buf
, buf
[n
-7:])
422 b
.Fatal("bad index", j
)
428 func BenchmarkIndexEasy32(b
*testing
.B
) { bmIndexEasy(b
, Index
, 32) }
429 func BenchmarkIndexEasy4K(b
*testing
.B
) { bmIndexEasy(b
, Index
, 4<<10) }
430 func BenchmarkIndexEasy4M(b
*testing
.B
) { bmIndexEasy(b
, Index
, 4<<20) }
431 func BenchmarkIndexEasy64M(b
*testing
.B
) { bmIndexEasy(b
, Index
, 64<<20) }
433 func bmIndexEasy(b
*testing
.B
, index
func([]byte, []byte) int, n
int) {
435 bmbuf
= make([]byte, n
)
441 for i
:= 0; i
< b
.N
; i
++ {
442 j
:= index(buf
, buf
[n
-7:])
444 b
.Fatal("bad index", j
)
451 func BenchmarkCount32(b
*testing
.B
) { bmCount(b
, Count
, 32) }
452 func BenchmarkCount4K(b
*testing
.B
) { bmCount(b
, Count
, 4<<10) }
453 func BenchmarkCount4M(b
*testing
.B
) { bmCount(b
, Count
, 4<<20) }
454 func BenchmarkCount64M(b
*testing
.B
) { bmCount(b
, Count
, 64<<20) }
456 func bmCount(b
*testing
.B
, count
func([]byte, []byte) int, n
int) {
458 bmbuf
= make([]byte, n
)
463 for i
:= 0; i
< b
.N
; i
++ {
464 j
:= count(buf
, buf
[n
-7:])
466 b
.Fatal("bad count", j
)
472 func BenchmarkCountEasy32(b
*testing
.B
) { bmCountEasy(b
, Count
, 32) }
473 func BenchmarkCountEasy4K(b
*testing
.B
) { bmCountEasy(b
, Count
, 4<<10) }
474 func BenchmarkCountEasy4M(b
*testing
.B
) { bmCountEasy(b
, Count
, 4<<20) }
475 func BenchmarkCountEasy64M(b
*testing
.B
) { bmCountEasy(b
, Count
, 64<<20) }
477 func bmCountEasy(b
*testing
.B
, count
func([]byte, []byte) int, n
int) {
479 bmbuf
= make([]byte, n
)
485 for i
:= 0; i
< b
.N
; i
++ {
486 j
:= count(buf
, buf
[n
-7:])
488 b
.Fatal("bad count", j
)
495 type ExplodeTest
struct {
501 var explodetests
= []ExplodeTest
{
502 {"", -1, []string{}},
503 {abcd
, -1, []string{"a", "b", "c", "d"}},
504 {faces
, -1, []string{"☺", "☻", "☹"}},
505 {abcd
, 2, []string{"a", "bcd"}},
508 func TestExplode(t
*testing
.T
) {
509 for _
, tt
:= range explodetests
{
510 a
:= SplitN([]byte(tt
.s
), nil, tt
.n
)
511 result
:= sliceOfString(a
)
512 if !eq(result
, tt
.a
) {
513 t
.Errorf(`Explode("%s", %d) = %v; want %v`, tt
.s
, tt
.n
, result
, tt
.a
)
516 s
:= Join(a
, []byte{})
517 if string(s
) != tt
.s
{
518 t
.Errorf(`Join(Explode("%s", %d), "") = "%s"`, tt
.s
, tt
.n
, s
)
523 type SplitTest
struct {
530 var splittests
= []SplitTest
{
532 {abcd
, "a", -1, []string{"", "bcd"}},
533 {abcd
, "z", -1, []string{"abcd"}},
534 {abcd
, "", -1, []string{"a", "b", "c", "d"}},
535 {commas
, ",", -1, []string{"1", "2", "3", "4"}},
536 {dots
, "...", -1, []string{"1", ".2", ".3", ".4"}},
537 {faces
, "☹", -1, []string{"☺☻", ""}},
538 {faces
, "~", -1, []string{faces
}},
539 {faces
, "", -1, []string{"☺", "☻", "☹"}},
540 {"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
541 {"1 2", " ", 3, []string{"1", "2"}},
542 {"123", "", 2, []string{"1", "23"}},
543 {"123", "", 17, []string{"1", "2", "3"}},
546 func TestSplit(t
*testing
.T
) {
547 for _
, tt
:= range splittests
{
548 a
:= SplitN([]byte(tt
.s
), []byte(tt
.sep
), tt
.n
)
549 result
:= sliceOfString(a
)
550 if !eq(result
, tt
.a
) {
551 t
.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt
.s
, tt
.sep
, tt
.n
, result
, tt
.a
)
557 s
:= Join(a
, []byte(tt
.sep
))
558 if string(s
) != tt
.s
{
559 t
.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt
.s
, tt
.sep
, tt
.n
, tt
.sep
, s
)
562 b
:= Split([]byte(tt
.s
), []byte(tt
.sep
))
563 if !reflect
.DeepEqual(a
, b
) {
564 t
.Errorf("Split disagrees withSplitN(%q, %q, %d) = %v; want %v", tt
.s
, tt
.sep
, tt
.n
, b
, a
)
569 if cap(in
) == cap(out
) && &in
[:1][0] == &out
[:1][0] {
570 t
.Errorf("Join(%#v, %q) didn't copy", a
, tt
.sep
)
576 var splitaftertests
= []SplitTest
{
577 {abcd
, "a", -1, []string{"a", "bcd"}},
578 {abcd
, "z", -1, []string{"abcd"}},
579 {abcd
, "", -1, []string{"a", "b", "c", "d"}},
580 {commas
, ",", -1, []string{"1,", "2,", "3,", "4"}},
581 {dots
, "...", -1, []string{"1...", ".2...", ".3...", ".4"}},
582 {faces
, "☹", -1, []string{"☺☻☹", ""}},
583 {faces
, "~", -1, []string{faces
}},
584 {faces
, "", -1, []string{"☺", "☻", "☹"}},
585 {"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}},
586 {"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}},
587 {"1 2", " ", 3, []string{"1 ", "2"}},
588 {"123", "", 2, []string{"1", "23"}},
589 {"123", "", 17, []string{"1", "2", "3"}},
592 func TestSplitAfter(t
*testing
.T
) {
593 for _
, tt
:= range splitaftertests
{
594 a
:= SplitAfterN([]byte(tt
.s
), []byte(tt
.sep
), tt
.n
)
595 result
:= sliceOfString(a
)
596 if !eq(result
, tt
.a
) {
597 t
.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt
.s
, tt
.sep
, tt
.n
, result
, tt
.a
)
601 if string(s
) != tt
.s
{
602 t
.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt
.s
, tt
.sep
, tt
.n
, tt
.sep
, s
)
605 b
:= SplitAfter([]byte(tt
.s
), []byte(tt
.sep
))
606 if !reflect
.DeepEqual(a
, b
) {
607 t
.Errorf("SplitAfter disagrees withSplitAfterN(%q, %q, %d) = %v; want %v", tt
.s
, tt
.sep
, tt
.n
, b
, a
)
613 type FieldsTest
struct {
618 var fieldstests
= []FieldsTest
{
621 {" \t ", []string{}},
622 {" abc ", []string{"abc"}},
623 {"1 2 3 4", []string{"1", "2", "3", "4"}},
624 {"1 2 3 4", []string{"1", "2", "3", "4"}},
625 {"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
626 {"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
627 {"\u2000\u2001\u2002", []string{}},
628 {"\n™\t™\n", []string{"™", "™"}},
629 {faces
, []string{faces
}},
632 func TestFields(t
*testing
.T
) {
633 for _
, tt
:= range fieldstests
{
634 a
:= Fields([]byte(tt
.s
))
635 result
:= sliceOfString(a
)
636 if !eq(result
, tt
.a
) {
637 t
.Errorf("Fields(%q) = %v; want %v", tt
.s
, a
, tt
.a
)
643 func TestFieldsFunc(t
*testing
.T
) {
644 for _
, tt
:= range fieldstests
{
645 a
:= FieldsFunc([]byte(tt
.s
), unicode
.IsSpace
)
646 result
:= sliceOfString(a
)
647 if !eq(result
, tt
.a
) {
648 t
.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt
.s
, a
, tt
.a
)
652 pred
:= func(c rune
) bool { return c
== 'X' }
653 var fieldsFuncTests
= []FieldsTest
{
656 {"XXhiXXX", []string{"hi"}},
657 {"aXXbXXXcX", []string{"a", "b", "c"}},
659 for _
, tt
:= range fieldsFuncTests
{
660 a
:= FieldsFunc([]byte(tt
.s
), pred
)
661 result
:= sliceOfString(a
)
662 if !eq(result
, tt
.a
) {
663 t
.Errorf("FieldsFunc(%q) = %v, want %v", tt
.s
, a
, tt
.a
)
668 // Test case for any function which accepts and returns a byte slice.
669 // For ease of creation, we write the byte slices as strings.
670 type StringTest
struct {
674 var upperTests
= []StringTest
{
677 {"AbC123", "ABC123"},
678 {"azAZ09_", "AZAZ09_"},
679 {"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
682 var lowerTests
= []StringTest
{
685 {"AbC123", "abc123"},
686 {"azAZ09_", "azaz09_"},
687 {"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
690 const space
= "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
692 var trimSpaceTests
= []StringTest
{
695 {space
+ "abc" + space
, "abc"},
697 {" \t\r\n \t\t\r\r\n\n ", ""},
698 {" \t\r\n x\t\t\r\r\n\n ", "x"},
699 {" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
700 {"1 \t\r\n2", "1 \t\r\n2"},
703 {"x \xc0\xc0 ", "x \xc0\xc0"},
704 {"x \xc0", "x \xc0"},
705 {"x \xc0 ", "x \xc0"},
706 {"x \xc0\xc0 ", "x \xc0\xc0"},
707 {"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"},
711 // Execute f on each test case. funcName should be the name of f; it's used
712 // in failure reports.
713 func runStringTests(t
*testing
.T
, f
func([]byte) []byte, funcName
string, testCases
[]StringTest
) {
714 for _
, tc
:= range testCases
{
715 actual
:= string(f([]byte(tc
.in
)))
716 if actual
!= tc
.out
{
717 t
.Errorf("%s(%q) = %q; want %q", funcName
, tc
.in
, actual
, tc
.out
)
722 func tenRunes(r rune
) string {
723 runes
:= make([]rune
, 10)
724 for i
:= range runes
{
730 // User-defined self-inverse mapping function
731 func rot13(r rune
) rune
{
733 if r
>= 'a' && r
<= 'z' {
734 return ((r
- 'a' + step
) % 26) + 'a'
736 if r
>= 'A' && r
<= 'Z' {
737 return ((r
- 'A' + step
) % 26) + 'A'
742 func TestMap(t
*testing
.T
) {
743 // Run a couple of awful growth/shrinkage tests
746 // 1. Grow. This triggers two reallocations in Map.
747 maxRune
:= func(r rune
) rune
{ return unicode
.MaxRune
}
748 m
:= Map(maxRune
, []byte(a
))
749 expect
:= tenRunes(unicode
.MaxRune
)
750 if string(m
) != expect
{
751 t
.Errorf("growing: expected %q got %q", expect
, m
)
755 minRune
:= func(r rune
) rune
{ return 'a' }
756 m
= Map(minRune
, []byte(tenRunes(unicode
.MaxRune
)))
758 if string(m
) != expect
{
759 t
.Errorf("shrinking: expected %q got %q", expect
, m
)
763 m
= Map(rot13
, []byte("a to zed"))
765 if string(m
) != expect
{
766 t
.Errorf("rot13: expected %q got %q", expect
, m
)
770 m
= Map(rot13
, Map(rot13
, []byte("a to zed")))
772 if string(m
) != expect
{
773 t
.Errorf("rot13: expected %q got %q", expect
, m
)
777 dropNotLatin
:= func(r rune
) rune
{
778 if unicode
.Is(unicode
.Latin
, r
) {
783 m
= Map(dropNotLatin
, []byte("Hello, 세계"))
785 if string(m
) != expect
{
786 t
.Errorf("drop: expected %q got %q", expect
, m
)
790 invalidRune
:= func(r rune
) rune
{
791 return utf8
.MaxRune
+ 1
793 m
= Map(invalidRune
, []byte("x"))
795 if string(m
) != expect
{
796 t
.Errorf("invalidRune: expected %q got %q", expect
, m
)
800 func TestToUpper(t
*testing
.T
) { runStringTests(t
, ToUpper
, "ToUpper", upperTests
) }
802 func TestToLower(t
*testing
.T
) { runStringTests(t
, ToLower
, "ToLower", lowerTests
) }
804 func TestTrimSpace(t
*testing
.T
) { runStringTests(t
, TrimSpace
, "TrimSpace", trimSpaceTests
) }
806 type RepeatTest
struct {
811 var RepeatTests
= []RepeatTest
{
817 {"-", "----------", 10},
818 {"abc ", "abc abc abc ", 3},
821 func TestRepeat(t
*testing
.T
) {
822 for _
, tt
:= range RepeatTests
{
824 tout
:= []byte(tt
.out
)
825 a
:= Repeat(tin
, tt
.count
)
827 t
.Errorf("Repeat(%q, %d) = %q; want %q", tin
, tt
.count
, a
, tout
)
833 func runesEqual(a
, b
[]rune
) bool {
834 if len(a
) != len(b
) {
837 for i
, r
:= range a
{
845 type RunesTest
struct {
851 var RunesTests
= []RunesTest
{
852 {"", []rune
{}, false},
853 {" ", []rune
{32}, false},
854 {"ABC", []rune
{65, 66, 67}, false},
855 {"abc", []rune
{97, 98, 99}, false},
856 {"\u65e5\u672c\u8a9e", []rune
{26085, 26412, 35486}, false},
857 {"ab\x80c", []rune
{97, 98, 0xFFFD, 99}, true},
858 {"ab\xc0c", []rune
{97, 98, 0xFFFD, 99}, true},
861 func TestRunes(t
*testing
.T
) {
862 for _
, tt
:= range RunesTests
{
865 if !runesEqual(a
, tt
.out
) {
866 t
.Errorf("Runes(%q) = %v; want %v", tin
, a
, tt
.out
)
870 // can only test reassembly if we didn't lose information
873 t
.Errorf("string(Runes(%q)) = %x; want %x", tin
, s
, tin
)
879 type TrimTest
struct {
884 var trimTests
= []TrimTest
{
885 {"Trim", "abba", "a", "bb"},
886 {"Trim", "abba", "ab", ""},
887 {"TrimLeft", "abba", "ab", ""},
888 {"TrimRight", "abba", "ab", ""},
889 {"TrimLeft", "abba", "a", "bba"},
890 {"TrimRight", "abba", "a", "abb"},
891 {"Trim", "<tag>", "<>", "tag"},
892 {"Trim", "* listitem", " *", "listitem"},
893 {"Trim", `"quote"`, `"`, "quote"},
894 {"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
896 {"Trim", "abba", "", "abba"},
897 {"Trim", "", "123", ""},
898 {"Trim", "", "", ""},
899 {"TrimLeft", "abba", "", "abba"},
900 {"TrimLeft", "", "123", ""},
901 {"TrimLeft", "", "", ""},
902 {"TrimRight", "abba", "", "abba"},
903 {"TrimRight", "", "123", ""},
904 {"TrimRight", "", "", ""},
905 {"TrimRight", "☺\xc0", "☺", "☺\xc0"},
906 {"TrimPrefix", "aabb", "a", "abb"},
907 {"TrimPrefix", "aabb", "b", "aabb"},
908 {"TrimSuffix", "aabb", "a", "aabb"},
909 {"TrimSuffix", "aabb", "b", "aab"},
912 func TestTrim(t
*testing
.T
) {
913 for _
, tc
:= range trimTests
{
915 var f
func([]byte, string) []byte
916 var fb
func([]byte, []byte) []byte
929 t
.Errorf("Undefined trim function %s", name
)
933 actual
= string(f([]byte(tc
.in
), tc
.arg
))
935 actual
= string(fb([]byte(tc
.in
), []byte(tc
.arg
)))
937 if actual
!= tc
.out
{
938 t
.Errorf("%s(%q, %q) = %q; want %q", name
, tc
.in
, tc
.arg
, actual
, tc
.out
)
943 type predicate
struct {
948 var isSpace
= predicate
{unicode
.IsSpace
, "IsSpace"}
949 var isDigit
= predicate
{unicode
.IsDigit
, "IsDigit"}
950 var isUpper
= predicate
{unicode
.IsUpper
, "IsUpper"}
951 var isValidRune
= predicate
{
953 return r
!= utf8
.RuneError
958 type TrimFuncTest
struct {
963 func not(p predicate
) predicate
{
972 var trimFuncTests
= []TrimFuncTest
{
973 {isSpace
, space
+ " hello " + space
, "hello"},
974 {isDigit
, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
975 {isUpper
, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
976 {not(isSpace
), "hello" + space
+ "hello", space
},
977 {not(isDigit
), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"},
978 {isValidRune
, "ab\xc0a\xc0cd", "\xc0a\xc0"},
979 {not(isValidRune
), "\xc0a\xc0", "a"},
982 func TestTrimFunc(t
*testing
.T
) {
983 for _
, tc
:= range trimFuncTests
{
984 actual
:= string(TrimFunc([]byte(tc
.in
), tc
.f
.f
))
985 if actual
!= tc
.out
{
986 t
.Errorf("TrimFunc(%q, %q) = %q; want %q", tc
.in
, tc
.f
.name
, actual
, tc
.out
)
991 type IndexFuncTest
struct {
997 var indexFuncTests
= []IndexFuncTest
{
998 {"", isValidRune
, -1, -1},
999 {"abc", isDigit
, -1, -1},
1000 {"0123", isDigit
, 0, 3},
1001 {"a1b", isDigit
, 1, 1},
1002 {space
, isSpace
, 0, len(space
) - 3}, // last rune in space is 3 bytes
1003 {"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit
, 0, 18},
1004 {"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper
, 0, 34},
1005 {"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit
), 8, 12},
1007 // tests of invalid UTF-8
1008 {"\x801", isDigit
, 1, 1},
1009 {"\x80abc", isDigit
, -1, -1},
1010 {"\xc0a\xc0", isValidRune
, 1, 1},
1011 {"\xc0a\xc0", not(isValidRune
), 0, 2},
1012 {"\xc0☺\xc0", not(isValidRune
), 0, 4},
1013 {"\xc0☺\xc0\xc0", not(isValidRune
), 0, 5},
1014 {"ab\xc0a\xc0cd", not(isValidRune
), 2, 4},
1015 {"a\xe0\x80cd", not(isValidRune
), 1, 2},
1018 func TestIndexFunc(t
*testing
.T
) {
1019 for _
, tc
:= range indexFuncTests
{
1020 first
:= IndexFunc([]byte(tc
.in
), tc
.f
.f
)
1021 if first
!= tc
.first
{
1022 t
.Errorf("IndexFunc(%q, %s) = %d; want %d", tc
.in
, tc
.f
.name
, first
, tc
.first
)
1024 last
:= LastIndexFunc([]byte(tc
.in
), tc
.f
.f
)
1025 if last
!= tc
.last
{
1026 t
.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc
.in
, tc
.f
.name
, last
, tc
.last
)
1031 type ReplaceTest
struct {
1038 var ReplaceTests
= []ReplaceTest
{
1039 {"hello", "l", "L", 0, "hello"},
1040 {"hello", "l", "L", -1, "heLLo"},
1041 {"hello", "x", "X", -1, "hello"},
1042 {"", "x", "X", -1, ""},
1043 {"radar", "r", "<r>", -1, "<r>ada<r>"},
1044 {"", "", "<>", -1, "<>"},
1045 {"banana", "a", "<>", -1, "b<>n<>n<>"},
1046 {"banana", "a", "<>", 1, "b<>nana"},
1047 {"banana", "a", "<>", 1000, "b<>n<>n<>"},
1048 {"banana", "an", "<>", -1, "b<><>a"},
1049 {"banana", "ana", "<>", -1, "b<>na"},
1050 {"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"},
1051 {"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
1052 {"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
1053 {"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
1054 {"banana", "", "<>", 1, "<>banana"},
1055 {"banana", "a", "a", -1, "banana"},
1056 {"banana", "a", "a", 1, "banana"},
1057 {"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"},
1060 func TestReplace(t
*testing
.T
) {
1061 for _
, tt
:= range ReplaceTests
{
1062 in
:= append([]byte(tt
.in
), "<spare>"...)
1063 in
= in
[:len(tt
.in
)]
1064 out
:= Replace(in
, []byte(tt
.old
), []byte(tt
.new), tt
.n
)
1065 if s
:= string(out
); s
!= tt
.out
{
1066 t
.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt
.in
, tt
.old
, tt
.new, tt
.n
, s
, tt
.out
)
1068 if cap(in
) == cap(out
) && &in
[:1][0] == &out
[:1][0] {
1069 t
.Errorf("Replace(%q, %q, %q, %d) didn't copy", tt
.in
, tt
.old
, tt
.new, tt
.n
)
1074 type TitleTest
struct {
1078 var TitleTests
= []TitleTest
{
1081 {" aaa aaa aaa ", " Aaa Aaa Aaa "},
1082 {" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
1083 {"123a456", "123a456"},
1084 {"double-blind", "Double-Blind"},
1086 {"with_underscore", "With_underscore"},
1087 {"unicode \xe2\x80\xa8 line separator", "Unicode \xe2\x80\xa8 Line Separator"},
1090 func TestTitle(t
*testing
.T
) {
1091 for _
, tt
:= range TitleTests
{
1092 if s
:= string(Title([]byte(tt
.in
))); s
!= tt
.out
{
1093 t
.Errorf("Title(%q) = %q, want %q", tt
.in
, s
, tt
.out
)
1098 var ToTitleTests
= []TitleTest
{
1101 {" aaa aaa aaa ", " AAA AAA AAA "},
1102 {" Aaa Aaa Aaa ", " AAA AAA AAA "},
1103 {"123a456", "123A456"},
1104 {"double-blind", "DOUBLE-BLIND"},
1108 func TestToTitle(t
*testing
.T
) {
1109 for _
, tt
:= range ToTitleTests
{
1110 if s
:= string(ToTitle([]byte(tt
.in
))); s
!= tt
.out
{
1111 t
.Errorf("ToTitle(%q) = %q, want %q", tt
.in
, s
, tt
.out
)
1116 var EqualFoldTests
= []struct {
1120 {"abc", "abc", true},
1121 {"ABcd", "ABcd", true},
1122 {"123abc", "123ABC", true},
1123 {"αβδ", "ΑΒΔ", true},
1124 {"abc", "xyz", false},
1125 {"abc", "XYZ", false},
1126 {"abcdefghijk", "abcdefghijX", false},
1127 {"abcdefghijk", "abcdefghij\u212A", true},
1128 {"abcdefghijK", "abcdefghij\u212A", true},
1129 {"abcdefghijkz", "abcdefghij\u212Ay", false},
1130 {"abcdefghijKz", "abcdefghij\u212Ay", false},
1133 func TestEqualFold(t
*testing
.T
) {
1134 for _
, tt
:= range EqualFoldTests
{
1135 if out
:= EqualFold([]byte(tt
.s
), []byte(tt
.t
)); out
!= tt
.out
{
1136 t
.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt
.s
, tt
.t
, out
, tt
.out
)
1138 if out
:= EqualFold([]byte(tt
.t
), []byte(tt
.s
)); out
!= tt
.out
{
1139 t
.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt
.t
, tt
.s
, out
, tt
.out
)
1144 func TestBufferGrowNegative(t
*testing
.T
) {
1146 if err
:= recover(); err
== nil {
1147 t
.Fatal("Grow(-1) should have panicked")
1154 func TestBufferTruncateNegative(t
*testing
.T
) {
1156 if err
:= recover(); err
== nil {
1157 t
.Fatal("Truncate(-1) should have panicked")
1164 func TestBufferTruncateOutOfRange(t
*testing
.T
) {
1166 if err
:= recover(); err
== nil {
1167 t
.Fatal("Truncate(20) should have panicked")
1171 b
.Write(make([]byte, 10))
1175 var containsTests
= []struct {
1179 {[]byte("hello"), []byte("hel"), true},
1180 {[]byte("日本語"), []byte("日本"), true},
1181 {[]byte("hello"), []byte("Hello, world"), false},
1182 {[]byte("東京"), []byte("京東"), false},
1185 func TestContains(t
*testing
.T
) {
1186 for _
, tt
:= range containsTests
{
1187 if got
:= Contains(tt
.b
, tt
.subslice
); got
!= tt
.want
{
1188 t
.Errorf("Contains(%q, %q) = %v, want %v", tt
.b
, tt
.subslice
, got
, tt
.want
)
1193 var makeFieldsInput
= func() []byte {
1194 x
:= make([]byte, 1<<20)
1195 // Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
1197 switch rand
.Intn(10) {
1201 if i
> 0 && x
[i
-1] == 'x' {
1213 var fieldsInput
= makeFieldsInput()
1215 func BenchmarkFields(b
*testing
.B
) {
1216 b
.SetBytes(int64(len(fieldsInput
)))
1217 for i
:= 0; i
< b
.N
; i
++ {
1222 func BenchmarkFieldsFunc(b
*testing
.B
) {
1223 b
.SetBytes(int64(len(fieldsInput
)))
1224 for i
:= 0; i
< b
.N
; i
++ {
1225 FieldsFunc(fieldsInput
, unicode
.IsSpace
)
1229 func BenchmarkTrimSpace(b
*testing
.B
) {
1230 s
:= []byte(" Some text. \n")
1231 for i
:= 0; i
< b
.N
; i
++ {