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.
15 func eq(a, b []string) bool {
19 for i := 0; i < len(a); i++ {
27 func arrayOfString(a [][]byte) []string {
28 result := make([]string, len(a))
29 for j := 0; j < len(a); j++ {
30 result[j] = string(a[j])
35 // For ease of reading, the test cases use strings that are converted to byte
36 // arrays before invoking the functions.
40 var commas = "1,2,3,4"
41 var dots = "1....2....3....4"
43 type BinOpTest struct {
49 var comparetests = []BinOpTest{
62 func TestCompare(t *testing.T) {
63 for _, tt := range comparetests {
68 t.Errorf(`Compare(%q, %q) = %v`, tt.a, tt.b, cmp)
71 if eql != (tt.i == 0) {
72 t.Errorf(`Equal(%q, %q) = %v`, tt.a, tt.b, eql)
74 eql = EqualPortable(a, b)
75 if eql != (tt.i == 0) {
76 t.Errorf(`EqualPortable(%q, %q) = %v`, tt.a, tt.b, eql)
81 var indexTests = []BinOpTest{
87 {"oofofoofooo", "f", 2},
88 {"oofofoofooo", "foo", 4},
89 {"barfoobarfoo", "foo", 3},
92 {"abcABCabc", "A", 3},
93 // cases with one byte strings - test IndexByte and special case in Index()
101 {"barfoobarfooyyyzzzyyyzzzyyyzzzyyyxxxzzzyyy", "x", 33},
102 {"foofyfoobarfoobar", "y", 4},
103 {"oooooooooooooooooooooo", "r", -1},
106 var lastIndexTests = []BinOpTest{
113 {"oofofoofooo", "f", 7},
114 {"oofofoofooo", "foo", 7},
115 {"barfoobarfoo", "foo", 9},
118 {"abcABCabc", "A", 3},
119 {"abcABCabc", "a", 6},
122 var indexAnyTests = []BinOpTest{
132 {"aRegExp*", ".(|)*+?^$[]", 7},
133 {dots + dots + dots, " ", -1},
136 var lastIndexAnyTests = []BinOpTest{
145 {"a☺b☻c☹d", "uvw☻xyz", 2 + len("☺")},
146 {"a.RegExp*", ".(|)*+?^$[]", 8},
147 {dots + dots + dots, " ", -1},
150 var indexRuneTests = []BinOpTest{
156 {"foo☺☻☹bar", "☹", 9},
159 // Execute f on each test case. funcName should be the name of f; it's used
160 // in failure reports.
161 func runIndexTests(t *testing.T, f func(s, sep []byte) int, funcName string, testCases []BinOpTest) {
162 for _, test := range testCases {
166 if actual != test.i {
167 t.Errorf("%s(%q,%q) = %v; want %v", funcName, a, b, actual, test.i)
172 func runIndexAnyTests(t *testing.T, f func(s []byte, chars string) int, funcName string, testCases []BinOpTest) {
173 for _, test := range testCases {
175 actual := f(a, test.b)
176 if actual != test.i {
177 t.Errorf("%s(%q,%q) = %v; want %v", funcName, a, test.b, actual, test.i)
182 func TestIndex(t *testing.T) { runIndexTests(t, Index, "Index", indexTests) }
183 func TestLastIndex(t *testing.T) { runIndexTests(t, LastIndex, "LastIndex", lastIndexTests) }
184 func TestIndexAny(t *testing.T) { runIndexAnyTests(t, IndexAny, "IndexAny", indexAnyTests) }
185 func TestLastIndexAny(t *testing.T) {
186 runIndexAnyTests(t, LastIndexAny, "LastIndexAny", lastIndexAnyTests)
189 func TestIndexByte(t *testing.T) {
190 for _, tt := range indexTests {
196 pos := IndexByte(a, b)
198 t.Errorf(`IndexByte(%q, '%c') = %v`, tt.a, b, pos)
200 posp := IndexBytePortable(a, b)
202 t.Errorf(`indexBytePortable(%q, '%c') = %v`, tt.a, b, posp)
207 // test a larger buffer with different sizes and alignments
208 func TestIndexByteBig(t *testing.T) {
214 for i := 0; i < n; i++ {
215 // different start alignments
217 for j := 0; j < len(b1); j++ {
219 pos := IndexByte(b1, 'x')
221 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
224 pos = IndexByte(b1, 'x')
226 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
229 // different end alignments
231 for j := 0; j < len(b1); j++ {
233 pos := IndexByte(b1, 'x')
235 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
238 pos = IndexByte(b1, 'x')
240 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
243 // different start and end alignments
244 b1 = b[i/2 : n-(i+1)/2]
245 for j := 0; j < len(b1); j++ {
247 pos := IndexByte(b1, 'x')
249 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
252 pos = IndexByte(b1, 'x')
254 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
260 func TestIndexRune(t *testing.T) {
261 for _, tt := range indexRuneTests {
263 r, _ := utf8.DecodeRuneInString(tt.b)
264 pos := IndexRune(a, r)
266 t.Errorf(`IndexRune(%q, '%c') = %v`, tt.a, r, pos)
273 func BenchmarkIndexByte32(b *testing.B) { bmIndexByte(b, IndexByte, 32) }
274 func BenchmarkIndexByte4K(b *testing.B) { bmIndexByte(b, IndexByte, 4<<10) }
275 func BenchmarkIndexByte4M(b *testing.B) { bmIndexByte(b, IndexByte, 4<<20) }
276 func BenchmarkIndexByte64M(b *testing.B) { bmIndexByte(b, IndexByte, 64<<20) }
277 func BenchmarkIndexBytePortable32(b *testing.B) { bmIndexByte(b, IndexBytePortable, 32) }
278 func BenchmarkIndexBytePortable4K(b *testing.B) { bmIndexByte(b, IndexBytePortable, 4<<10) }
279 func BenchmarkIndexBytePortable4M(b *testing.B) { bmIndexByte(b, IndexBytePortable, 4<<20) }
280 func BenchmarkIndexBytePortable64M(b *testing.B) { bmIndexByte(b, IndexBytePortable, 64<<20) }
282 func bmIndexByte(b *testing.B, index func([]byte, byte) int, n int) {
284 bmbuf = make([]byte, n)
289 for i := 0; i < b.N; i++ {
292 b.Fatal("bad index", j)
298 func BenchmarkEqual32(b *testing.B) { bmEqual(b, Equal, 32) }
299 func BenchmarkEqual4K(b *testing.B) { bmEqual(b, Equal, 4<<10) }
300 func BenchmarkEqual4M(b *testing.B) { bmEqual(b, Equal, 4<<20) }
301 func BenchmarkEqual64M(b *testing.B) { bmEqual(b, Equal, 64<<20) }
302 func BenchmarkEqualPort32(b *testing.B) { bmEqual(b, EqualPortable, 32) }
303 func BenchmarkEqualPort4K(b *testing.B) { bmEqual(b, EqualPortable, 4<<10) }
304 func BenchmarkEqualPortable4M(b *testing.B) { bmEqual(b, EqualPortable, 4<<20) }
305 func BenchmarkEqualPortable64M(b *testing.B) { bmEqual(b, EqualPortable, 64<<20) }
307 func bmEqual(b *testing.B, equal func([]byte, []byte) bool, n int) {
308 if len(bmbuf) < 2*n {
309 bmbuf = make([]byte, 2*n)
313 buf2 := bmbuf[n : 2*n]
316 for i := 0; i < b.N; i++ {
317 eq := equal(buf1, buf2)
326 func BenchmarkIndex32(b *testing.B) { bmIndex(b, Index, 32) }
327 func BenchmarkIndex4K(b *testing.B) { bmIndex(b, Index, 4<<10) }
328 func BenchmarkIndex4M(b *testing.B) { bmIndex(b, Index, 4<<20) }
329 func BenchmarkIndex64M(b *testing.B) { bmIndex(b, Index, 64<<20) }
331 func bmIndex(b *testing.B, index func([]byte, []byte) int, n int) {
333 bmbuf = make([]byte, n)
338 for i := 0; i < b.N; i++ {
339 j := index(buf, buf[n-7:])
341 b.Fatal("bad index", j)
347 func BenchmarkIndexEasy32(b *testing.B) { bmIndexEasy(b, Index, 32) }
348 func BenchmarkIndexEasy4K(b *testing.B) { bmIndexEasy(b, Index, 4<<10) }
349 func BenchmarkIndexEasy4M(b *testing.B) { bmIndexEasy(b, Index, 4<<20) }
350 func BenchmarkIndexEasy64M(b *testing.B) { bmIndexEasy(b, Index, 64<<20) }
352 func bmIndexEasy(b *testing.B, index func([]byte, []byte) int, n int) {
354 bmbuf = make([]byte, n)
360 for i := 0; i < b.N; i++ {
361 j := index(buf, buf[n-7:])
363 b.Fatal("bad index", j)
370 func BenchmarkCount32(b *testing.B) { bmCount(b, Count, 32) }
371 func BenchmarkCount4K(b *testing.B) { bmCount(b, Count, 4<<10) }
372 func BenchmarkCount4M(b *testing.B) { bmCount(b, Count, 4<<20) }
373 func BenchmarkCount64M(b *testing.B) { bmCount(b, Count, 64<<20) }
375 func bmCount(b *testing.B, count func([]byte, []byte) int, n int) {
377 bmbuf = make([]byte, n)
382 for i := 0; i < b.N; i++ {
383 j := count(buf, buf[n-7:])
385 b.Fatal("bad count", j)
391 func BenchmarkCountEasy32(b *testing.B) { bmCountEasy(b, Count, 32) }
392 func BenchmarkCountEasy4K(b *testing.B) { bmCountEasy(b, Count, 4<<10) }
393 func BenchmarkCountEasy4M(b *testing.B) { bmCountEasy(b, Count, 4<<20) }
394 func BenchmarkCountEasy64M(b *testing.B) { bmCountEasy(b, Count, 64<<20) }
396 func bmCountEasy(b *testing.B, count func([]byte, []byte) int, n int) {
398 bmbuf = make([]byte, n)
404 for i := 0; i < b.N; i++ {
405 j := count(buf, buf[n-7:])
407 b.Fatal("bad count", j)
414 type ExplodeTest struct {
420 var explodetests = []ExplodeTest{
421 {"", -1, []string{}},
422 {abcd, -1, []string{"a", "b", "c", "d"}},
423 {faces, -1, []string{"☺", "☻", "☹"}},
424 {abcd, 2, []string{"a", "bcd"}},
427 func TestExplode(t *testing.T) {
428 for _, tt := range explodetests {
429 a := SplitN([]byte(tt.s), nil, tt.n)
430 result := arrayOfString(a)
431 if !eq(result, tt.a) {
432 t.Errorf(`Explode("%s", %d) = %v; want %v`, tt.s, tt.n, result, tt.a)
435 s := Join(a, []byte{})
436 if string(s) != tt.s {
437 t.Errorf(`Join(Explode("%s", %d), "") = "%s"`, tt.s, tt.n, s)
442 type SplitTest struct {
449 var splittests = []SplitTest{
451 {abcd, "a", -1, []string{"", "bcd"}},
452 {abcd, "z", -1, []string{"abcd"}},
453 {abcd, "", -1, []string{"a", "b", "c", "d"}},
454 {commas, ",", -1, []string{"1", "2", "3", "4"}},
455 {dots, "...", -1, []string{"1", ".2", ".3", ".4"}},
456 {faces, "☹", -1, []string{"☺☻", ""}},
457 {faces, "~", -1, []string{faces}},
458 {faces, "", -1, []string{"☺", "☻", "☹"}},
459 {"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
460 {"1 2", " ", 3, []string{"1", "2"}},
461 {"123", "", 2, []string{"1", "23"}},
462 {"123", "", 17, []string{"1", "2", "3"}},
465 func TestSplit(t *testing.T) {
466 for _, tt := range splittests {
467 a := SplitN([]byte(tt.s), []byte(tt.sep), tt.n)
468 result := arrayOfString(a)
469 if !eq(result, tt.a) {
470 t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, result, tt.a)
476 s := Join(a, []byte(tt.sep))
477 if string(s) != tt.s {
478 t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
481 b := Split([]byte(tt.s), []byte(tt.sep))
482 if !reflect.DeepEqual(a, b) {
483 t.Errorf("Split disagrees withSplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
489 var splitaftertests = []SplitTest{
490 {abcd, "a", -1, []string{"a", "bcd"}},
491 {abcd, "z", -1, []string{"abcd"}},
492 {abcd, "", -1, []string{"a", "b", "c", "d"}},
493 {commas, ",", -1, []string{"1,", "2,", "3,", "4"}},
494 {dots, "...", -1, []string{"1...", ".2...", ".3...", ".4"}},
495 {faces, "☹", -1, []string{"☺☻☹", ""}},
496 {faces, "~", -1, []string{faces}},
497 {faces, "", -1, []string{"☺", "☻", "☹"}},
498 {"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}},
499 {"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}},
500 {"1 2", " ", 3, []string{"1 ", "2"}},
501 {"123", "", 2, []string{"1", "23"}},
502 {"123", "", 17, []string{"1", "2", "3"}},
505 func TestSplitAfter(t *testing.T) {
506 for _, tt := range splitaftertests {
507 a := SplitAfterN([]byte(tt.s), []byte(tt.sep), tt.n)
508 result := arrayOfString(a)
509 if !eq(result, tt.a) {
510 t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, result, tt.a)
514 if string(s) != tt.s {
515 t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
518 b := SplitAfter([]byte(tt.s), []byte(tt.sep))
519 if !reflect.DeepEqual(a, b) {
520 t.Errorf("SplitAfter disagrees withSplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
526 type FieldsTest struct {
531 var fieldstests = []FieldsTest{
534 {" \t ", []string{}},
535 {" abc ", []string{"abc"}},
536 {"1 2 3 4", []string{"1", "2", "3", "4"}},
537 {"1 2 3 4", []string{"1", "2", "3", "4"}},
538 {"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
539 {"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
540 {"\u2000\u2001\u2002", []string{}},
541 {"\n™\t™\n", []string{"™", "™"}},
542 {faces, []string{faces}},
545 func TestFields(t *testing.T) {
546 for _, tt := range fieldstests {
547 a := Fields([]byte(tt.s))
548 result := arrayOfString(a)
549 if !eq(result, tt.a) {
550 t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a)
556 func TestFieldsFunc(t *testing.T) {
557 pred := func(c rune) bool { return c == 'X' }
558 var fieldsFuncTests = []FieldsTest{
561 {"XXhiXXX", []string{"hi"}},
562 {"aXXbXXXcX", []string{"a", "b", "c"}},
564 for _, tt := range fieldsFuncTests {
565 a := FieldsFunc([]byte(tt.s), pred)
566 result := arrayOfString(a)
567 if !eq(result, tt.a) {
568 t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
573 // Test case for any function which accepts and returns a byte array.
574 // For ease of creation, we write the byte arrays as strings.
575 type StringTest struct {
579 var upperTests = []StringTest{
582 {"AbC123", "ABC123"},
583 {"azAZ09_", "AZAZ09_"},
584 {"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
587 var lowerTests = []StringTest{
590 {"AbC123", "abc123"},
591 {"azAZ09_", "azaz09_"},
592 {"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
595 const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
597 var trimSpaceTests = []StringTest{
600 {space + "abc" + space, "abc"},
602 {" \t\r\n \t\t\r\r\n\n ", ""},
603 {" \t\r\n x\t\t\r\r\n\n ", "x"},
604 {" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
605 {"1 \t\r\n2", "1 \t\r\n2"},
608 {"x \xc0\xc0 ", "x \xc0\xc0"},
609 {"x \xc0", "x \xc0"},
610 {"x \xc0 ", "x \xc0"},
611 {"x \xc0\xc0 ", "x \xc0\xc0"},
612 {"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"},
616 // Execute f on each test case. funcName should be the name of f; it's used
617 // in failure reports.
618 func runStringTests(t *testing.T, f func([]byte) []byte, funcName string, testCases []StringTest) {
619 for _, tc := range testCases {
620 actual := string(f([]byte(tc.in)))
621 if actual != tc.out {
622 t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out)
627 func tenRunes(r rune) string {
628 runes := make([]rune, 10)
629 for i := range runes {
635 // User-defined self-inverse mapping function
636 func rot13(r rune) rune {
638 if r >= 'a' && r <= 'z' {
639 return ((r - 'a' + step) % 26) + 'a'
641 if r >= 'A' && r <= 'Z' {
642 return ((r - 'A' + step) % 26) + 'A'
647 func TestMap(t *testing.T) {
648 // Run a couple of awful growth/shrinkage tests
651 // 1. Grow. This triggers two reallocations in Map.
652 maxRune := func(r rune) rune { return unicode.MaxRune }
653 m := Map(maxRune, []byte(a))
654 expect := tenRunes(unicode.MaxRune)
655 if string(m) != expect {
656 t.Errorf("growing: expected %q got %q", expect, m)
660 minRune := func(r rune) rune { return 'a' }
661 m = Map(minRune, []byte(tenRunes(unicode.MaxRune)))
663 if string(m) != expect {
664 t.Errorf("shrinking: expected %q got %q", expect, m)
668 m = Map(rot13, []byte("a to zed"))
670 if string(m) != expect {
671 t.Errorf("rot13: expected %q got %q", expect, m)
675 m = Map(rot13, Map(rot13, []byte("a to zed")))
677 if string(m) != expect {
678 t.Errorf("rot13: expected %q got %q", expect, m)
682 dropNotLatin := func(r rune) rune {
683 if unicode.Is(unicode.Latin, r) {
688 m = Map(dropNotLatin, []byte("Hello, 세계"))
690 if string(m) != expect {
691 t.Errorf("drop: expected %q got %q", expect, m)
695 func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }
697 func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTests) }
699 func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) }
701 type RepeatTest struct {
706 var RepeatTests = []RepeatTest{
712 {"-", "----------", 10},
713 {"abc ", "abc abc abc ", 3},
716 func TestRepeat(t *testing.T) {
717 for _, tt := range RepeatTests {
719 tout := []byte(tt.out)
720 a := Repeat(tin, tt.count)
722 t.Errorf("Repeat(%q, %d) = %q; want %q", tin, tt.count, a, tout)
728 func runesEqual(a, b []rune) bool {
729 if len(a) != len(b) {
732 for i, r := range a {
740 type RunesTest struct {
746 var RunesTests = []RunesTest{
747 {"", []rune{}, false},
748 {" ", []rune{32}, false},
749 {"ABC", []rune{65, 66, 67}, false},
750 {"abc", []rune{97, 98, 99}, false},
751 {"\u65e5\u672c\u8a9e", []rune{26085, 26412, 35486}, false},
752 {"ab\x80c", []rune{97, 98, 0xFFFD, 99}, true},
753 {"ab\xc0c", []rune{97, 98, 0xFFFD, 99}, true},
756 func TestRunes(t *testing.T) {
757 for _, tt := range RunesTests {
760 if !runesEqual(a, tt.out) {
761 t.Errorf("Runes(%q) = %v; want %v", tin, a, tt.out)
765 // can only test reassembly if we didn't lose information
768 t.Errorf("string(Runes(%q)) = %x; want %x", tin, s, tin)
774 type TrimTest struct {
776 in, cutset, out string
779 var trimTests = []TrimTest{
780 {"Trim", "abba", "a", "bb"},
781 {"Trim", "abba", "ab", ""},
782 {"TrimLeft", "abba", "ab", ""},
783 {"TrimRight", "abba", "ab", ""},
784 {"TrimLeft", "abba", "a", "bba"},
785 {"TrimRight", "abba", "a", "abb"},
786 {"Trim", "<tag>", "<>", "tag"},
787 {"Trim", "* listitem", " *", "listitem"},
788 {"Trim", `"quote"`, `"`, "quote"},
789 {"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
791 {"Trim", "abba", "", "abba"},
792 {"Trim", "", "123", ""},
793 {"Trim", "", "", ""},
794 {"TrimLeft", "abba", "", "abba"},
795 {"TrimLeft", "", "123", ""},
796 {"TrimLeft", "", "", ""},
797 {"TrimRight", "abba", "", "abba"},
798 {"TrimRight", "", "123", ""},
799 {"TrimRight", "", "", ""},
800 {"TrimRight", "☺\xc0", "☺", "☺\xc0"},
803 func TestTrim(t *testing.T) {
804 for _, tc := range trimTests {
806 var f func([]byte, string) []byte
815 t.Errorf("Undefined trim function %s", name)
817 actual := string(f([]byte(tc.in), tc.cutset))
818 if actual != tc.out {
819 t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.cutset, actual, tc.out)
824 type predicate struct {
829 var isSpace = predicate{unicode.IsSpace, "IsSpace"}
830 var isDigit = predicate{unicode.IsDigit, "IsDigit"}
831 var isUpper = predicate{unicode.IsUpper, "IsUpper"}
832 var isValidRune = predicate{
834 return r != utf8.RuneError
839 type TrimFuncTest struct {
844 func not(p predicate) predicate {
853 var trimFuncTests = []TrimFuncTest{
854 {isSpace, space + " hello " + space, "hello"},
855 {isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
856 {isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
857 {not(isSpace), "hello" + space + "hello", space},
858 {not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"},
859 {isValidRune, "ab\xc0a\xc0cd", "\xc0a\xc0"},
860 {not(isValidRune), "\xc0a\xc0", "a"},
863 func TestTrimFunc(t *testing.T) {
864 for _, tc := range trimFuncTests {
865 actual := string(TrimFunc([]byte(tc.in), tc.f.f))
866 if actual != tc.out {
867 t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.f.name, actual, tc.out)
872 type IndexFuncTest struct {
878 var indexFuncTests = []IndexFuncTest{
879 {"", isValidRune, -1, -1},
880 {"abc", isDigit, -1, -1},
881 {"0123", isDigit, 0, 3},
882 {"a1b", isDigit, 1, 1},
883 {space, isSpace, 0, len(space) - 3}, // last rune in space is 3 bytes
884 {"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit, 0, 18},
885 {"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34},
886 {"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12},
888 // tests of invalid UTF-8
889 {"\x801", isDigit, 1, 1},
890 {"\x80abc", isDigit, -1, -1},
891 {"\xc0a\xc0", isValidRune, 1, 1},
892 {"\xc0a\xc0", not(isValidRune), 0, 2},
893 {"\xc0☺\xc0", not(isValidRune), 0, 4},
894 {"\xc0☺\xc0\xc0", not(isValidRune), 0, 5},
895 {"ab\xc0a\xc0cd", not(isValidRune), 2, 4},
896 {"a\xe0\x80cd", not(isValidRune), 1, 2},
899 func TestIndexFunc(t *testing.T) {
900 for _, tc := range indexFuncTests {
901 first := IndexFunc([]byte(tc.in), tc.f.f)
902 if first != tc.first {
903 t.Errorf("IndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, first, tc.first)
905 last := LastIndexFunc([]byte(tc.in), tc.f.f)
907 t.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, last, tc.last)
912 type ReplaceTest struct {
919 var ReplaceTests = []ReplaceTest{
920 {"hello", "l", "L", 0, "hello"},
921 {"hello", "l", "L", -1, "heLLo"},
922 {"hello", "x", "X", -1, "hello"},
923 {"", "x", "X", -1, ""},
924 {"radar", "r", "<r>", -1, "<r>ada<r>"},
925 {"", "", "<>", -1, "<>"},
926 {"banana", "a", "<>", -1, "b<>n<>n<>"},
927 {"banana", "a", "<>", 1, "b<>nana"},
928 {"banana", "a", "<>", 1000, "b<>n<>n<>"},
929 {"banana", "an", "<>", -1, "b<><>a"},
930 {"banana", "ana", "<>", -1, "b<>na"},
931 {"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"},
932 {"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
933 {"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
934 {"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
935 {"banana", "", "<>", 1, "<>banana"},
936 {"banana", "a", "a", -1, "banana"},
937 {"banana", "a", "a", 1, "banana"},
938 {"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"},
941 func TestReplace(t *testing.T) {
942 for _, tt := range ReplaceTests {
943 in := append([]byte(tt.in), "<spare>"...)
945 out := Replace(in, []byte(tt.old), []byte(tt.new), tt.n)
946 if s := string(out); s != tt.out {
947 t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
949 if cap(in) == cap(out) && &in[:1][0] == &out[:1][0] {
950 t.Errorf("Replace(%q, %q, %q, %d) didn't copy", tt.in, tt.old, tt.new, tt.n)
955 type TitleTest struct {
959 var TitleTests = []TitleTest{
962 {" aaa aaa aaa ", " Aaa Aaa Aaa "},
963 {" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
964 {"123a456", "123a456"},
965 {"double-blind", "Double-Blind"},
969 func TestTitle(t *testing.T) {
970 for _, tt := range TitleTests {
971 if s := string(Title([]byte(tt.in))); s != tt.out {
972 t.Errorf("Title(%q) = %q, want %q", tt.in, s, tt.out)
977 var EqualFoldTests = []struct {
981 {"abc", "abc", true},
982 {"ABcd", "ABcd", true},
983 {"123abc", "123ABC", true},
984 {"αβδ", "ΑΒΔ", true},
985 {"abc", "xyz", false},
986 {"abc", "XYZ", false},
987 {"abcdefghijk", "abcdefghijX", false},
988 {"abcdefghijk", "abcdefghij\u212A", true},
989 {"abcdefghijK", "abcdefghij\u212A", true},
990 {"abcdefghijkz", "abcdefghij\u212Ay", false},
991 {"abcdefghijKz", "abcdefghij\u212Ay", false},
994 func TestEqualFold(t *testing.T) {
995 for _, tt := range EqualFoldTests {
996 if out := EqualFold([]byte(tt.s), []byte(tt.t)); out != tt.out {
997 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.s, tt.t, out, tt.out)
999 if out := EqualFold([]byte(tt.t), []byte(tt.s)); out != tt.out {
1000 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.t, tt.s, out, tt.out)