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.
19 func eq(a, b []string) bool {
23 for i := 0; i < len(a); i++ {
33 var commas = "1,2,3,4"
34 var dots = "1....2....3....4"
36 type IndexTest struct {
42 var indexTests = []IndexTest{
48 {"oofofoofooo", "f", 2},
49 {"oofofoofooo", "foo", 4},
50 {"barfoobarfoo", "foo", 3},
53 {"abcABCabc", "A", 3},
54 // cases with one byte strings - test special case in Index()
64 var lastIndexTests = []IndexTest{
71 {"oofofoofooo", "f", 7},
72 {"oofofoofooo", "foo", 7},
73 {"barfoobarfoo", "foo", 9},
76 {"abcABCabc", "A", 3},
77 {"abcABCabc", "a", 6},
80 var indexAnyTests = []IndexTest{
89 {"a☺b☻c☹d", "uvw☻xyz", 2 + len("☺")},
90 {"aRegExp*", ".(|)*+?^$[]", 7},
91 {dots + dots + dots, " ", -1},
93 var lastIndexAnyTests = []IndexTest{
102 {"a☺b☻c☹d", "uvw☻xyz", 2 + len("☺")},
103 {"a.RegExp*", ".(|)*+?^$[]", 8},
104 {dots + dots + dots, " ", -1},
107 // Execute f on each test case. funcName should be the name of f; it's used
108 // in failure reports.
109 func runIndexTests(t *testing.T, f func(s, sep string) int, funcName string, testCases []IndexTest) {
110 for _, test := range testCases {
111 actual := f(test.s, test.sep)
112 if actual != test.out {
113 t.Errorf("%s(%q,%q) = %v; want %v", funcName, test.s, test.sep, actual, test.out)
118 func TestIndex(t *testing.T) { runIndexTests(t, Index, "Index", indexTests) }
119 func TestLastIndex(t *testing.T) { runIndexTests(t, LastIndex, "LastIndex", lastIndexTests) }
120 func TestIndexAny(t *testing.T) { runIndexTests(t, IndexAny, "IndexAny", indexAnyTests) }
121 func TestLastIndexAny(t *testing.T) { runIndexTests(t, LastIndexAny, "LastIndexAny", lastIndexAnyTests) }
123 var indexRuneTests = []struct {
129 {"some_text=some_value", '=', 9},
134 func TestIndexRune(t *testing.T) {
135 for _, test := range indexRuneTests {
136 if actual := IndexRune(test.s, test.rune); actual != test.out {
137 t.Errorf("IndexRune(%q,%d)= %v; want %v", test.s, test.rune, actual, test.out)
142 const benchmarkString = "some_text=some☺value"
144 func BenchmarkIndexRune(b *testing.B) {
145 if got := IndexRune(benchmarkString, '☺'); got != 14 {
146 b.Fatalf("wrong index: expected 14, got=%d", got)
148 for i := 0; i < b.N; i++ {
149 IndexRune(benchmarkString, '☺')
153 func BenchmarkIndexRuneFastPath(b *testing.B) {
154 if got := IndexRune(benchmarkString, 'v'); got != 17 {
155 b.Fatalf("wrong index: expected 17, got=%d", got)
157 for i := 0; i < b.N; i++ {
158 IndexRune(benchmarkString, 'v')
162 func BenchmarkIndex(b *testing.B) {
163 if got := Index(benchmarkString, "v"); got != 17 {
164 b.Fatalf("wrong index: expected 17, got=%d", got)
166 for i := 0; i < b.N; i++ {
167 Index(benchmarkString, "v")
171 var explodetests = []struct {
176 {"", -1, []string{}},
177 {abcd, 4, []string{"a", "b", "c", "d"}},
178 {faces, 3, []string{"☺", "☻", "☹"}},
179 {abcd, 2, []string{"a", "bcd"}},
182 func TestExplode(t *testing.T) {
183 for _, tt := range explodetests {
184 a := SplitN(tt.s, "", tt.n)
186 t.Errorf("explode(%q, %d) = %v; want %v", tt.s, tt.n, a, tt.a)
191 t.Errorf(`Join(explode(%q, %d), "") = %q`, tt.s, tt.n, s)
196 type SplitTest struct {
203 var splittests = []SplitTest{
205 {abcd, "a", -1, []string{"", "bcd"}},
206 {abcd, "z", -1, []string{"abcd"}},
207 {abcd, "", -1, []string{"a", "b", "c", "d"}},
208 {commas, ",", -1, []string{"1", "2", "3", "4"}},
209 {dots, "...", -1, []string{"1", ".2", ".3", ".4"}},
210 {faces, "☹", -1, []string{"☺☻", ""}},
211 {faces, "~", -1, []string{faces}},
212 {faces, "", -1, []string{"☺", "☻", "☹"}},
213 {"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
214 {"1 2", " ", 3, []string{"1", "2"}},
215 {"123", "", 2, []string{"1", "23"}},
216 {"123", "", 17, []string{"1", "2", "3"}},
219 func TestSplit(t *testing.T) {
220 for _, tt := range splittests {
221 a := SplitN(tt.s, tt.sep, tt.n)
223 t.Errorf("Split(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, a, tt.a)
231 t.Errorf("Join(Split(%q, %q, %d), %q) = %q", tt.s, tt.sep, tt.n, tt.sep, s)
234 b := Split(tt.s, tt.sep)
235 if !reflect.DeepEqual(a, b) {
236 t.Errorf("Split disagrees with SplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
242 var splitaftertests = []SplitTest{
243 {abcd, "a", -1, []string{"a", "bcd"}},
244 {abcd, "z", -1, []string{"abcd"}},
245 {abcd, "", -1, []string{"a", "b", "c", "d"}},
246 {commas, ",", -1, []string{"1,", "2,", "3,", "4"}},
247 {dots, "...", -1, []string{"1...", ".2...", ".3...", ".4"}},
248 {faces, "☹", -1, []string{"☺☻☹", ""}},
249 {faces, "~", -1, []string{faces}},
250 {faces, "", -1, []string{"☺", "☻", "☹"}},
251 {"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}},
252 {"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}},
253 {"1 2", " ", 3, []string{"1 ", "2"}},
254 {"123", "", 2, []string{"1", "23"}},
255 {"123", "", 17, []string{"1", "2", "3"}},
258 func TestSplitAfter(t *testing.T) {
259 for _, tt := range splitaftertests {
260 a := SplitAfterN(tt.s, tt.sep, tt.n)
262 t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, a, tt.a)
267 t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
270 b := SplitAfter(tt.s, tt.sep)
271 if !reflect.DeepEqual(a, b) {
272 t.Errorf("SplitAfter disagrees with SplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
278 type FieldsTest struct {
283 var fieldstests = []FieldsTest{
286 {" \t ", []string{}},
287 {" abc ", []string{"abc"}},
288 {"1 2 3 4", []string{"1", "2", "3", "4"}},
289 {"1 2 3 4", []string{"1", "2", "3", "4"}},
290 {"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
291 {"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
292 {"\u2000\u2001\u2002", []string{}},
293 {"\n™\t™\n", []string{"™", "™"}},
294 {faces, []string{faces}},
297 func TestFields(t *testing.T) {
298 for _, tt := range fieldstests {
301 t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a)
307 var FieldsFuncTests = []FieldsTest{
310 {"XXhiXXX", []string{"hi"}},
311 {"aXXbXXXcX", []string{"a", "b", "c"}},
314 func TestFieldsFunc(t *testing.T) {
315 for _, tt := range fieldstests {
316 a := FieldsFunc(tt.s, unicode.IsSpace)
318 t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a)
322 pred := func(c rune) bool { return c == 'X' }
323 for _, tt := range FieldsFuncTests {
324 a := FieldsFunc(tt.s, pred)
326 t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
331 // Test case for any function which accepts and returns a single string.
332 type StringTest struct {
336 // Execute f on each test case. funcName should be the name of f; it's used
337 // in failure reports.
338 func runStringTests(t *testing.T, f func(string) string, funcName string, testCases []StringTest) {
339 for _, tc := range testCases {
341 if actual != tc.out {
342 t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out)
347 var upperTests = []StringTest{
350 {"AbC123", "ABC123"},
351 {"azAZ09_", "AZAZ09_"},
352 {"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
355 var lowerTests = []StringTest{
358 {"AbC123", "abc123"},
359 {"azAZ09_", "azaz09_"},
360 {"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
363 const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
365 var trimSpaceTests = []StringTest{
368 {space + "abc" + space, "abc"},
370 {" \t\r\n \t\t\r\r\n\n ", ""},
371 {" \t\r\n x\t\t\r\r\n\n ", "x"},
372 {" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
373 {"1 \t\r\n2", "1 \t\r\n2"},
376 {"x \xc0\xc0 ", "x \xc0\xc0"},
377 {"x \xc0", "x \xc0"},
378 {"x \xc0 ", "x \xc0"},
379 {"x \xc0\xc0 ", "x \xc0\xc0"},
380 {"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"},
384 func tenRunes(ch rune) string {
385 r := make([]rune, 10)
392 // User-defined self-inverse mapping function
393 func rot13(r rune) rune {
395 if r >= 'a' && r <= 'z' {
396 return ((r - 'a' + step) % 26) + 'a'
398 if r >= 'A' && r <= 'Z' {
399 return ((r - 'A' + step) % 26) + 'A'
404 func TestMap(t *testing.T) {
405 // Run a couple of awful growth/shrinkage tests
407 // 1. Grow. This triggers two reallocations in Map.
408 maxRune := func(rune) rune { return unicode.MaxRune }
410 expect := tenRunes(unicode.MaxRune)
412 t.Errorf("growing: expected %q got %q", expect, m)
416 minRune := func(rune) rune { return 'a' }
417 m = Map(minRune, tenRunes(unicode.MaxRune))
420 t.Errorf("shrinking: expected %q got %q", expect, m)
424 m = Map(rot13, "a to zed")
427 t.Errorf("rot13: expected %q got %q", expect, m)
431 m = Map(rot13, Map(rot13, "a to zed"))
434 t.Errorf("rot13: expected %q got %q", expect, m)
438 dropNotLatin := func(r rune) rune {
439 if unicode.Is(unicode.Latin, r) {
444 m = Map(dropNotLatin, "Hello, 세계")
447 t.Errorf("drop: expected %q got %q", expect, m)
451 identity := func(r rune) rune {
454 orig := "Input string that we expect not to be copied."
455 m = Map(identity, orig)
456 if (*reflect.StringHeader)(unsafe.Pointer(&orig)).Data !=
457 (*reflect.StringHeader)(unsafe.Pointer(&m)).Data {
458 t.Error("unexpected copy during identity map")
462 func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }
464 func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTests) }
466 func BenchmarkMapNoChanges(b *testing.B) {
467 identity := func(r rune) rune {
470 for i := 0; i < b.N; i++ {
471 Map(identity, "Some string that won't be modified.")
475 func TestSpecialCase(t *testing.T) {
476 lower := "abcçdefgğhıijklmnoöprsştuüvyz"
477 upper := "ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ"
478 u := ToUpperSpecial(unicode.TurkishCase, upper)
480 t.Errorf("Upper(upper) is %s not %s", u, upper)
482 u = ToUpperSpecial(unicode.TurkishCase, lower)
484 t.Errorf("Upper(lower) is %s not %s", u, upper)
486 l := ToLowerSpecial(unicode.TurkishCase, lower)
488 t.Errorf("Lower(lower) is %s not %s", l, lower)
490 l = ToLowerSpecial(unicode.TurkishCase, upper)
492 t.Errorf("Lower(upper) is %s not %s", l, lower)
496 func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) }
498 var trimTests = []struct {
500 in, cutset, out string
502 {"Trim", "abba", "a", "bb"},
503 {"Trim", "abba", "ab", ""},
504 {"TrimLeft", "abba", "ab", ""},
505 {"TrimRight", "abba", "ab", ""},
506 {"TrimLeft", "abba", "a", "bba"},
507 {"TrimRight", "abba", "a", "abb"},
508 {"Trim", "<tag>", "<>", "tag"},
509 {"Trim", "* listitem", " *", "listitem"},
510 {"Trim", `"quote"`, `"`, "quote"},
511 {"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
513 {"Trim", "abba", "", "abba"},
514 {"Trim", "", "123", ""},
515 {"Trim", "", "", ""},
516 {"TrimLeft", "abba", "", "abba"},
517 {"TrimLeft", "", "123", ""},
518 {"TrimLeft", "", "", ""},
519 {"TrimRight", "abba", "", "abba"},
520 {"TrimRight", "", "123", ""},
521 {"TrimRight", "", "", ""},
522 {"TrimRight", "☺\xc0", "☺", "☺\xc0"},
525 func TestTrim(t *testing.T) {
526 for _, tc := range trimTests {
528 var f func(string, string) string
537 t.Errorf("Undefined trim function %s", name)
539 actual := f(tc.in, tc.cutset)
540 if actual != tc.out {
541 t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.cutset, actual, tc.out)
546 type predicate struct {
551 var isSpace = predicate{unicode.IsSpace, "IsSpace"}
552 var isDigit = predicate{unicode.IsDigit, "IsDigit"}
553 var isUpper = predicate{unicode.IsUpper, "IsUpper"}
554 var isValidRune = predicate{
556 return r != utf8.RuneError
561 func not(p predicate) predicate {
570 var trimFuncTests = []struct {
574 {isSpace, space + " hello " + space, "hello"},
575 {isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
576 {isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
577 {not(isSpace), "hello" + space + "hello", space},
578 {not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"},
579 {isValidRune, "ab\xc0a\xc0cd", "\xc0a\xc0"},
580 {not(isValidRune), "\xc0a\xc0", "a"},
583 func TestTrimFunc(t *testing.T) {
584 for _, tc := range trimFuncTests {
585 actual := TrimFunc(tc.in, tc.f.f)
586 if actual != tc.out {
587 t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.f.name, actual, tc.out)
592 var indexFuncTests = []struct {
597 {"", isValidRune, -1, -1},
598 {"abc", isDigit, -1, -1},
599 {"0123", isDigit, 0, 3},
600 {"a1b", isDigit, 1, 1},
601 {space, isSpace, 0, len(space) - 3}, // last rune in space is 3 bytes
602 {"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit, 0, 18},
603 {"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34},
604 {"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12},
606 // tests of invalid UTF-8
607 {"\x801", isDigit, 1, 1},
608 {"\x80abc", isDigit, -1, -1},
609 {"\xc0a\xc0", isValidRune, 1, 1},
610 {"\xc0a\xc0", not(isValidRune), 0, 2},
611 {"\xc0☺\xc0", not(isValidRune), 0, 4},
612 {"\xc0☺\xc0\xc0", not(isValidRune), 0, 5},
613 {"ab\xc0a\xc0cd", not(isValidRune), 2, 4},
614 {"a\xe0\x80cd", not(isValidRune), 1, 2},
615 {"\x80\x80\x80\x80", not(isValidRune), 0, 3},
618 func TestIndexFunc(t *testing.T) {
619 for _, tc := range indexFuncTests {
620 first := IndexFunc(tc.in, tc.f.f)
621 if first != tc.first {
622 t.Errorf("IndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, first, tc.first)
624 last := LastIndexFunc(tc.in, tc.f.f)
626 t.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, last, tc.last)
631 func equal(m string, s1, s2 string, t *testing.T) bool {
637 for i, c1 := range e1 {
641 r1, _ := utf8.DecodeRuneInString(c1)
642 r2, _ := utf8.DecodeRuneInString(e2[i])
644 t.Errorf("%s diff at %d: U+%04X U+%04X", m, i, r1, r2)
650 func TestCaseConsistency(t *testing.T) {
651 // Make a string of all the runes.
652 numRunes := int(unicode.MaxRune + 1)
656 a := make([]rune, numRunes)
661 // convert the cases.
665 // Consistency checks
666 if n := utf8.RuneCountInString(upper); n != numRunes {
667 t.Error("rune count wrong in upper:", n)
669 if n := utf8.RuneCountInString(lower); n != numRunes {
670 t.Error("rune count wrong in lower:", n)
672 if !equal("ToUpper(upper)", ToUpper(upper), upper, t) {
673 t.Error("ToUpper(upper) consistency fail")
675 if !equal("ToLower(lower)", ToLower(lower), lower, t) {
676 t.Error("ToLower(lower) consistency fail")
679 These fail because of non-one-to-oneness of the data, such as multiple
680 upper case 'I' mapping to 'i'. We comment them out but keep them for
682 For instance: CAPITAL LETTER I WITH DOT ABOVE:
683 unicode.ToUpper(unicode.ToLower('\u0130')) != '\u0130'
685 if !equal("ToUpper(lower)", ToUpper(lower), upper, t) {
686 t.Error("ToUpper(lower) consistency fail");
688 if !equal("ToLower(upper)", ToLower(upper), lower, t) {
689 t.Error("ToLower(upper) consistency fail");
694 var RepeatTests = []struct {
703 {"-", "----------", 10},
704 {"abc ", "abc abc abc ", 3},
707 func TestRepeat(t *testing.T) {
708 for _, tt := range RepeatTests {
709 a := Repeat(tt.in, tt.count)
710 if !equal("Repeat(s)", a, tt.out, t) {
711 t.Errorf("Repeat(%v, %d) = %v; want %v", tt.in, tt.count, a, tt.out)
717 func runesEqual(a, b []rune) bool {
718 if len(a) != len(b) {
721 for i, r := range a {
729 var RunesTests = []struct {
734 {"", []rune{}, false},
735 {" ", []rune{32}, false},
736 {"ABC", []rune{65, 66, 67}, false},
737 {"abc", []rune{97, 98, 99}, false},
738 {"\u65e5\u672c\u8a9e", []rune{26085, 26412, 35486}, false},
739 {"ab\x80c", []rune{97, 98, 0xFFFD, 99}, true},
740 {"ab\xc0c", []rune{97, 98, 0xFFFD, 99}, true},
743 func TestRunes(t *testing.T) {
744 for _, tt := range RunesTests {
746 if !runesEqual(a, tt.out) {
747 t.Errorf("[]rune(%q) = %v; want %v", tt.in, a, tt.out)
751 // can only test reassembly if we didn't lose information
754 t.Errorf("string([]rune(%q)) = %x; want %x", tt.in, s, tt.in)
760 func TestReadByte(t *testing.T) {
761 testStrings := []string{"", abcd, faces, commas}
762 for _, s := range testStrings {
763 reader := NewReader(s)
764 if e := reader.UnreadByte(); e == nil {
765 t.Errorf("Unreading %q at beginning: expected error", s)
769 b, e := reader.ReadByte()
774 t.Errorf("Reading %q: %s", s, e)
778 // unread and read again
779 e = reader.UnreadByte()
781 t.Errorf("Unreading %q: %s", s, e)
784 b1, e := reader.ReadByte()
786 t.Errorf("Reading %q after unreading: %s", s, e)
790 t.Errorf("Reading %q after unreading: want byte %q, got %q", s, b, b1)
794 if res.String() != s {
795 t.Errorf("Reader(%q).ReadByte() produced %q", s, res.String())
800 func TestReadRune(t *testing.T) {
801 testStrings := []string{"", abcd, faces, commas}
802 for _, s := range testStrings {
803 reader := NewReader(s)
804 if e := reader.UnreadRune(); e == nil {
805 t.Errorf("Unreading %q at beginning: expected error", s)
809 r, z, e := reader.ReadRune()
814 t.Errorf("Reading %q: %s", s, e)
818 // unread and read again
819 e = reader.UnreadRune()
821 t.Errorf("Unreading %q: %s", s, e)
824 r1, z1, e := reader.ReadRune()
826 t.Errorf("Reading %q after unreading: %s", s, e)
830 t.Errorf("Reading %q after unreading: want rune %q, got %q", s, r, r1)
834 t.Errorf("Reading %q after unreading: want size %d, got %d", s, z, z1)
839 t.Errorf("Reader(%q).ReadRune() produced %q", s, res)
844 var ReplaceTests = []struct {
850 {"hello", "l", "L", 0, "hello"},
851 {"hello", "l", "L", -1, "heLLo"},
852 {"hello", "x", "X", -1, "hello"},
853 {"", "x", "X", -1, ""},
854 {"radar", "r", "<r>", -1, "<r>ada<r>"},
855 {"", "", "<>", -1, "<>"},
856 {"banana", "a", "<>", -1, "b<>n<>n<>"},
857 {"banana", "a", "<>", 1, "b<>nana"},
858 {"banana", "a", "<>", 1000, "b<>n<>n<>"},
859 {"banana", "an", "<>", -1, "b<><>a"},
860 {"banana", "ana", "<>", -1, "b<>na"},
861 {"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"},
862 {"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
863 {"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
864 {"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
865 {"banana", "", "<>", 1, "<>banana"},
866 {"banana", "a", "a", -1, "banana"},
867 {"banana", "a", "a", 1, "banana"},
868 {"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"},
871 func TestReplace(t *testing.T) {
872 for _, tt := range ReplaceTests {
873 if s := Replace(tt.in, tt.old, tt.new, tt.n); s != tt.out {
874 t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
879 var TitleTests = []struct {
884 {" aaa aaa aaa ", " Aaa Aaa Aaa "},
885 {" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
886 {"123a456", "123a456"},
887 {"double-blind", "Double-Blind"},
891 func TestTitle(t *testing.T) {
892 for _, tt := range TitleTests {
893 if s := Title(tt.in); s != tt.out {
894 t.Errorf("Title(%q) = %q, want %q", tt.in, s, tt.out)
899 var ContainsTests = []struct {
904 {"abc", "bcd", false},
909 func TestContains(t *testing.T) {
910 for _, ct := range ContainsTests {
911 if Contains(ct.str, ct.substr) != ct.expected {
912 t.Errorf("Contains(%s, %s) = %v, want %v",
913 ct.str, ct.substr, !ct.expected, ct.expected)
918 var ContainsAnyTests = []struct {
928 {"abc", "xyz", false},
929 {"abc", "xcz", true},
930 {"a☺b☻c☹d", "uvw☻xyz", true},
931 {"aRegExp*", ".(|)*+?^$[]", true},
932 {dots + dots + dots, " ", false},
935 func TestContainsAny(t *testing.T) {
936 for _, ct := range ContainsAnyTests {
937 if ContainsAny(ct.str, ct.substr) != ct.expected {
938 t.Errorf("ContainsAny(%s, %s) = %v, want %v",
939 ct.str, ct.substr, !ct.expected, ct.expected)
944 var ContainsRuneTests = []struct {
954 {"a☺b☻c☹d", 'x', false},
955 {"a☺b☻c☹d", '☻', true},
956 {"aRegExp*", '*', true},
959 func TestContainsRune(t *testing.T) {
960 for _, ct := range ContainsRuneTests {
961 if ContainsRune(ct.str, ct.r) != ct.expected {
962 t.Errorf("ContainsRune(%s, %s) = %v, want %v",
963 ct.str, ct.r, !ct.expected, ct.expected)
968 var EqualFoldTests = []struct {
972 {"abc", "abc", true},
973 {"ABcd", "ABcd", true},
974 {"123abc", "123ABC", true},
975 {"αβδ", "ΑΒΔ", true},
976 {"abc", "xyz", false},
977 {"abc", "XYZ", false},
978 {"abcdefghijk", "abcdefghijX", false},
979 {"abcdefghijk", "abcdefghij\u212A", true},
980 {"abcdefghijK", "abcdefghij\u212A", true},
981 {"abcdefghijkz", "abcdefghij\u212Ay", false},
982 {"abcdefghijKz", "abcdefghij\u212Ay", false},
985 func TestEqualFold(t *testing.T) {
986 for _, tt := range EqualFoldTests {
987 if out := EqualFold(tt.s, tt.t); out != tt.out {
988 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.s, tt.t, out, tt.out)
990 if out := EqualFold(tt.t, tt.s); out != tt.out {
991 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.t, tt.s, out, tt.out)
996 var makeFieldsInput = func() string {
997 x := make([]byte, 1<<20)
998 // Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
1000 switch rand.Intn(10) {
1004 if i > 0 && x[i-1] == 'x' {
1016 var fieldsInput = makeFieldsInput()
1018 func BenchmarkFields(b *testing.B) {
1019 b.SetBytes(int64(len(fieldsInput)))
1020 for i := 0; i < b.N; i++ {
1025 func BenchmarkFieldsFunc(b *testing.B) {
1026 b.SetBytes(int64(len(fieldsInput)))
1027 for i := 0; i < b.N; i++ {
1028 FieldsFunc(fieldsInput, unicode.IsSpace)