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.
18 func eq(a, b []string) bool {
22 for i := 0; i < len(a); i++ {
32 var commas = "1,2,3,4"
33 var dots = "1....2....3....4"
35 type IndexTest struct {
41 var indexTests = []IndexTest{
47 {"oofofoofooo", "f", 2},
48 {"oofofoofooo", "foo", 4},
49 {"barfoobarfoo", "foo", 3},
52 {"abcABCabc", "A", 3},
53 // cases with one byte strings - test special case in Index()
63 var lastIndexTests = []IndexTest{
70 {"oofofoofooo", "f", 7},
71 {"oofofoofooo", "foo", 7},
72 {"barfoobarfoo", "foo", 9},
75 {"abcABCabc", "A", 3},
76 {"abcABCabc", "a", 6},
79 var indexAnyTests = []IndexTest{
88 {"a☺b☻c☹d", "uvw☻xyz", 2 + len("☺")},
89 {"aRegExp*", ".(|)*+?^$[]", 7},
90 {dots + dots + dots, " ", -1},
92 var lastIndexAnyTests = []IndexTest{
101 {"a☺b☻c☹d", "uvw☻xyz", 2 + len("☺")},
102 {"a.RegExp*", ".(|)*+?^$[]", 8},
103 {dots + dots + dots, " ", -1},
106 // Execute f on each test case. funcName should be the name of f; it's used
107 // in failure reports.
108 func runIndexTests(t *testing.T, f func(s, sep string) int, funcName string, testCases []IndexTest) {
109 for _, test := range testCases {
110 actual := f(test.s, test.sep)
111 if actual != test.out {
112 t.Errorf("%s(%q,%q) = %v; want %v", funcName, test.s, test.sep, actual, test.out)
117 func TestIndex(t *testing.T) { runIndexTests(t, Index, "Index", indexTests) }
118 func TestLastIndex(t *testing.T) { runIndexTests(t, LastIndex, "LastIndex", lastIndexTests) }
119 func TestIndexAny(t *testing.T) { runIndexTests(t, IndexAny, "IndexAny", indexAnyTests) }
120 func TestLastIndexAny(t *testing.T) { runIndexTests(t, LastIndexAny, "LastIndexAny", lastIndexAnyTests) }
122 var indexRuneTests = []struct {
128 {"some_text=some_value", '=', 9},
133 func TestIndexRune(t *testing.T) {
134 for _, test := range indexRuneTests {
135 if actual := IndexRune(test.s, test.rune); actual != test.out {
136 t.Errorf("IndexRune(%q,%d)= %v; want %v", test.s, test.rune, actual, test.out)
141 const benchmarkString = "some_text=some☺value"
143 func BenchmarkIndexRune(b *testing.B) {
144 if got := IndexRune(benchmarkString, '☺'); got != 14 {
145 b.Fatalf("wrong index: expected 14, got=%d", got)
147 for i := 0; i < b.N; i++ {
148 IndexRune(benchmarkString, '☺')
152 func BenchmarkIndexRuneFastPath(b *testing.B) {
153 if got := IndexRune(benchmarkString, 'v'); got != 17 {
154 b.Fatalf("wrong index: expected 17, got=%d", got)
156 for i := 0; i < b.N; i++ {
157 IndexRune(benchmarkString, 'v')
161 func BenchmarkIndex(b *testing.B) {
162 if got := Index(benchmarkString, "v"); got != 17 {
163 b.Fatalf("wrong index: expected 17, got=%d", got)
165 for i := 0; i < b.N; i++ {
166 Index(benchmarkString, "v")
170 var explodetests = []struct {
175 {"", -1, []string{}},
176 {abcd, 4, []string{"a", "b", "c", "d"}},
177 {faces, 3, []string{"☺", "☻", "☹"}},
178 {abcd, 2, []string{"a", "bcd"}},
181 func TestExplode(t *testing.T) {
182 for _, tt := range explodetests {
183 a := SplitN(tt.s, "", tt.n)
185 t.Errorf("explode(%q, %d) = %v; want %v", tt.s, tt.n, a, tt.a)
190 t.Errorf(`Join(explode(%q, %d), "") = %q`, tt.s, tt.n, s)
195 type SplitTest struct {
202 var splittests = []SplitTest{
204 {abcd, "a", -1, []string{"", "bcd"}},
205 {abcd, "z", -1, []string{"abcd"}},
206 {abcd, "", -1, []string{"a", "b", "c", "d"}},
207 {commas, ",", -1, []string{"1", "2", "3", "4"}},
208 {dots, "...", -1, []string{"1", ".2", ".3", ".4"}},
209 {faces, "☹", -1, []string{"☺☻", ""}},
210 {faces, "~", -1, []string{faces}},
211 {faces, "", -1, []string{"☺", "☻", "☹"}},
212 {"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
213 {"1 2", " ", 3, []string{"1", "2"}},
214 {"123", "", 2, []string{"1", "23"}},
215 {"123", "", 17, []string{"1", "2", "3"}},
218 func TestSplit(t *testing.T) {
219 for _, tt := range splittests {
220 a := SplitN(tt.s, tt.sep, tt.n)
222 t.Errorf("Split(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, a, tt.a)
230 t.Errorf("Join(Split(%q, %q, %d), %q) = %q", tt.s, tt.sep, tt.n, tt.sep, s)
233 b := Split(tt.s, tt.sep)
234 if !reflect.DeepEqual(a, b) {
235 t.Errorf("Split disagrees with SplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
241 var splitaftertests = []SplitTest{
242 {abcd, "a", -1, []string{"a", "bcd"}},
243 {abcd, "z", -1, []string{"abcd"}},
244 {abcd, "", -1, []string{"a", "b", "c", "d"}},
245 {commas, ",", -1, []string{"1,", "2,", "3,", "4"}},
246 {dots, "...", -1, []string{"1...", ".2...", ".3...", ".4"}},
247 {faces, "☹", -1, []string{"☺☻☹", ""}},
248 {faces, "~", -1, []string{faces}},
249 {faces, "", -1, []string{"☺", "☻", "☹"}},
250 {"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}},
251 {"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}},
252 {"1 2", " ", 3, []string{"1 ", "2"}},
253 {"123", "", 2, []string{"1", "23"}},
254 {"123", "", 17, []string{"1", "2", "3"}},
257 func TestSplitAfter(t *testing.T) {
258 for _, tt := range splitaftertests {
259 a := SplitAfterN(tt.s, tt.sep, tt.n)
261 t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, a, tt.a)
266 t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
269 b := SplitAfter(tt.s, tt.sep)
270 if !reflect.DeepEqual(a, b) {
271 t.Errorf("SplitAfter disagrees with SplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
277 type FieldsTest struct {
282 var fieldstests = []FieldsTest{
285 {" \t ", []string{}},
286 {" abc ", []string{"abc"}},
287 {"1 2 3 4", []string{"1", "2", "3", "4"}},
288 {"1 2 3 4", []string{"1", "2", "3", "4"}},
289 {"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
290 {"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
291 {"\u2000\u2001\u2002", []string{}},
292 {"\n™\t™\n", []string{"™", "™"}},
293 {faces, []string{faces}},
296 func TestFields(t *testing.T) {
297 for _, tt := range fieldstests {
300 t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a)
306 var FieldsFuncTests = []FieldsTest{
309 {"XXhiXXX", []string{"hi"}},
310 {"aXXbXXXcX", []string{"a", "b", "c"}},
313 func TestFieldsFunc(t *testing.T) {
314 pred := func(c rune) bool { return c == 'X' }
315 for _, tt := range FieldsFuncTests {
316 a := FieldsFunc(tt.s, pred)
318 t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
323 // Test case for any function which accepts and returns a single string.
324 type StringTest struct {
328 // Execute f on each test case. funcName should be the name of f; it's used
329 // in failure reports.
330 func runStringTests(t *testing.T, f func(string) string, funcName string, testCases []StringTest) {
331 for _, tc := range testCases {
333 if actual != tc.out {
334 t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out)
339 var upperTests = []StringTest{
342 {"AbC123", "ABC123"},
343 {"azAZ09_", "AZAZ09_"},
344 {"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
347 var lowerTests = []StringTest{
350 {"AbC123", "abc123"},
351 {"azAZ09_", "azaz09_"},
352 {"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
355 const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
357 var trimSpaceTests = []StringTest{
360 {space + "abc" + space, "abc"},
362 {" \t\r\n \t\t\r\r\n\n ", ""},
363 {" \t\r\n x\t\t\r\r\n\n ", "x"},
364 {" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
365 {"1 \t\r\n2", "1 \t\r\n2"},
368 {"x \xc0\xc0 ", "x \xc0\xc0"},
369 {"x \xc0", "x \xc0"},
370 {"x \xc0 ", "x \xc0"},
371 {"x \xc0\xc0 ", "x \xc0\xc0"},
372 {"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"},
376 func tenRunes(ch rune) string {
377 r := make([]rune, 10)
384 // User-defined self-inverse mapping function
385 func rot13(r rune) rune {
387 if r >= 'a' && r <= 'z' {
388 return ((r - 'a' + step) % 26) + 'a'
390 if r >= 'A' && r <= 'Z' {
391 return ((r - 'A' + step) % 26) + 'A'
396 func TestMap(t *testing.T) {
397 // Run a couple of awful growth/shrinkage tests
399 // 1. Grow. This triggers two reallocations in Map.
400 maxRune := func(rune) rune { return unicode.MaxRune }
402 expect := tenRunes(unicode.MaxRune)
404 t.Errorf("growing: expected %q got %q", expect, m)
408 minRune := func(rune) rune { return 'a' }
409 m = Map(minRune, tenRunes(unicode.MaxRune))
412 t.Errorf("shrinking: expected %q got %q", expect, m)
416 m = Map(rot13, "a to zed")
419 t.Errorf("rot13: expected %q got %q", expect, m)
423 m = Map(rot13, Map(rot13, "a to zed"))
426 t.Errorf("rot13: expected %q got %q", expect, m)
430 dropNotLatin := func(r rune) rune {
431 if unicode.Is(unicode.Latin, r) {
436 m = Map(dropNotLatin, "Hello, 세계")
439 t.Errorf("drop: expected %q got %q", expect, m)
443 identity := func(r rune) rune {
446 orig := "Input string that we expect not to be copied."
447 m = Map(identity, orig)
448 if (*reflect.StringHeader)(unsafe.Pointer(&orig)).Data !=
449 (*reflect.StringHeader)(unsafe.Pointer(&m)).Data {
450 t.Error("unexpected copy during identity map")
454 func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }
456 func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTests) }
458 func BenchmarkMapNoChanges(b *testing.B) {
459 identity := func(r rune) rune {
462 for i := 0; i < b.N; i++ {
463 Map(identity, "Some string that won't be modified.")
467 func TestSpecialCase(t *testing.T) {
468 lower := "abcçdefgğhıijklmnoöprsştuüvyz"
469 upper := "ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ"
470 u := ToUpperSpecial(unicode.TurkishCase, upper)
472 t.Errorf("Upper(upper) is %s not %s", u, upper)
474 u = ToUpperSpecial(unicode.TurkishCase, lower)
476 t.Errorf("Upper(lower) is %s not %s", u, upper)
478 l := ToLowerSpecial(unicode.TurkishCase, lower)
480 t.Errorf("Lower(lower) is %s not %s", l, lower)
482 l = ToLowerSpecial(unicode.TurkishCase, upper)
484 t.Errorf("Lower(upper) is %s not %s", l, lower)
488 func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) }
490 var trimTests = []struct {
492 in, cutset, out string
494 {"Trim", "abba", "a", "bb"},
495 {"Trim", "abba", "ab", ""},
496 {"TrimLeft", "abba", "ab", ""},
497 {"TrimRight", "abba", "ab", ""},
498 {"TrimLeft", "abba", "a", "bba"},
499 {"TrimRight", "abba", "a", "abb"},
500 {"Trim", "<tag>", "<>", "tag"},
501 {"Trim", "* listitem", " *", "listitem"},
502 {"Trim", `"quote"`, `"`, "quote"},
503 {"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
505 {"Trim", "abba", "", "abba"},
506 {"Trim", "", "123", ""},
507 {"Trim", "", "", ""},
508 {"TrimLeft", "abba", "", "abba"},
509 {"TrimLeft", "", "123", ""},
510 {"TrimLeft", "", "", ""},
511 {"TrimRight", "abba", "", "abba"},
512 {"TrimRight", "", "123", ""},
513 {"TrimRight", "", "", ""},
514 {"TrimRight", "☺\xc0", "☺", "☺\xc0"},
517 func TestTrim(t *testing.T) {
518 for _, tc := range trimTests {
520 var f func(string, string) string
529 t.Errorf("Undefined trim function %s", name)
531 actual := f(tc.in, tc.cutset)
532 if actual != tc.out {
533 t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.cutset, actual, tc.out)
538 type predicate struct {
543 var isSpace = predicate{unicode.IsSpace, "IsSpace"}
544 var isDigit = predicate{unicode.IsDigit, "IsDigit"}
545 var isUpper = predicate{unicode.IsUpper, "IsUpper"}
546 var isValidRune = predicate{
548 return r != utf8.RuneError
553 func not(p predicate) predicate {
562 var trimFuncTests = []struct {
566 {isSpace, space + " hello " + space, "hello"},
567 {isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
568 {isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
569 {not(isSpace), "hello" + space + "hello", space},
570 {not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"},
571 {isValidRune, "ab\xc0a\xc0cd", "\xc0a\xc0"},
572 {not(isValidRune), "\xc0a\xc0", "a"},
575 func TestTrimFunc(t *testing.T) {
576 for _, tc := range trimFuncTests {
577 actual := TrimFunc(tc.in, tc.f.f)
578 if actual != tc.out {
579 t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.f.name, actual, tc.out)
584 var indexFuncTests = []struct {
589 {"", isValidRune, -1, -1},
590 {"abc", isDigit, -1, -1},
591 {"0123", isDigit, 0, 3},
592 {"a1b", isDigit, 1, 1},
593 {space, isSpace, 0, len(space) - 3}, // last rune in space is 3 bytes
594 {"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit, 0, 18},
595 {"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34},
596 {"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12},
598 // tests of invalid UTF-8
599 {"\x801", isDigit, 1, 1},
600 {"\x80abc", isDigit, -1, -1},
601 {"\xc0a\xc0", isValidRune, 1, 1},
602 {"\xc0a\xc0", not(isValidRune), 0, 2},
603 {"\xc0☺\xc0", not(isValidRune), 0, 4},
604 {"\xc0☺\xc0\xc0", not(isValidRune), 0, 5},
605 {"ab\xc0a\xc0cd", not(isValidRune), 2, 4},
606 {"a\xe0\x80cd", not(isValidRune), 1, 2},
607 {"\x80\x80\x80\x80", not(isValidRune), 0, 3},
610 func TestIndexFunc(t *testing.T) {
611 for _, tc := range indexFuncTests {
612 first := IndexFunc(tc.in, tc.f.f)
613 if first != tc.first {
614 t.Errorf("IndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, first, tc.first)
616 last := LastIndexFunc(tc.in, tc.f.f)
618 t.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, last, tc.last)
623 func equal(m string, s1, s2 string, t *testing.T) bool {
629 for i, c1 := range e1 {
633 r1, _ := utf8.DecodeRuneInString(c1)
634 r2, _ := utf8.DecodeRuneInString(e2[i])
636 t.Errorf("%s diff at %d: U+%04X U+%04X", m, i, r1, r2)
642 func TestCaseConsistency(t *testing.T) {
643 // Make a string of all the runes.
644 numRunes := int(unicode.MaxRune + 1)
648 a := make([]rune, numRunes)
653 // convert the cases.
657 // Consistency checks
658 if n := utf8.RuneCountInString(upper); n != numRunes {
659 t.Error("rune count wrong in upper:", n)
661 if n := utf8.RuneCountInString(lower); n != numRunes {
662 t.Error("rune count wrong in lower:", n)
664 if !equal("ToUpper(upper)", ToUpper(upper), upper, t) {
665 t.Error("ToUpper(upper) consistency fail")
667 if !equal("ToLower(lower)", ToLower(lower), lower, t) {
668 t.Error("ToLower(lower) consistency fail")
671 These fail because of non-one-to-oneness of the data, such as multiple
672 upper case 'I' mapping to 'i'. We comment them out but keep them for
674 For instance: CAPITAL LETTER I WITH DOT ABOVE:
675 unicode.ToUpper(unicode.ToLower('\u0130')) != '\u0130'
677 if !equal("ToUpper(lower)", ToUpper(lower), upper, t) {
678 t.Error("ToUpper(lower) consistency fail");
680 if !equal("ToLower(upper)", ToLower(upper), lower, t) {
681 t.Error("ToLower(upper) consistency fail");
686 var RepeatTests = []struct {
695 {"-", "----------", 10},
696 {"abc ", "abc abc abc ", 3},
699 func TestRepeat(t *testing.T) {
700 for _, tt := range RepeatTests {
701 a := Repeat(tt.in, tt.count)
702 if !equal("Repeat(s)", a, tt.out, t) {
703 t.Errorf("Repeat(%v, %d) = %v; want %v", tt.in, tt.count, a, tt.out)
709 func runesEqual(a, b []rune) bool {
710 if len(a) != len(b) {
713 for i, r := range a {
721 var RunesTests = []struct {
726 {"", []rune{}, false},
727 {" ", []rune{32}, false},
728 {"ABC", []rune{65, 66, 67}, false},
729 {"abc", []rune{97, 98, 99}, false},
730 {"\u65e5\u672c\u8a9e", []rune{26085, 26412, 35486}, false},
731 {"ab\x80c", []rune{97, 98, 0xFFFD, 99}, true},
732 {"ab\xc0c", []rune{97, 98, 0xFFFD, 99}, true},
735 func TestRunes(t *testing.T) {
736 for _, tt := range RunesTests {
738 if !runesEqual(a, tt.out) {
739 t.Errorf("[]rune(%q) = %v; want %v", tt.in, a, tt.out)
743 // can only test reassembly if we didn't lose information
746 t.Errorf("string([]rune(%q)) = %x; want %x", tt.in, s, tt.in)
752 func TestReadByte(t *testing.T) {
753 testStrings := []string{"", abcd, faces, commas}
754 for _, s := range testStrings {
755 reader := NewReader(s)
756 if e := reader.UnreadByte(); e == nil {
757 t.Errorf("Unreading %q at beginning: expected error", s)
761 b, e := reader.ReadByte()
766 t.Errorf("Reading %q: %s", s, e)
770 // unread and read again
771 e = reader.UnreadByte()
773 t.Errorf("Unreading %q: %s", s, e)
776 b1, e := reader.ReadByte()
778 t.Errorf("Reading %q after unreading: %s", s, e)
782 t.Errorf("Reading %q after unreading: want byte %q, got %q", s, b, b1)
786 if res.String() != s {
787 t.Errorf("Reader(%q).ReadByte() produced %q", s, res.String())
792 func TestReadRune(t *testing.T) {
793 testStrings := []string{"", abcd, faces, commas}
794 for _, s := range testStrings {
795 reader := NewReader(s)
796 if e := reader.UnreadRune(); e == nil {
797 t.Errorf("Unreading %q at beginning: expected error", s)
801 r, z, e := reader.ReadRune()
806 t.Errorf("Reading %q: %s", s, e)
810 // unread and read again
811 e = reader.UnreadRune()
813 t.Errorf("Unreading %q: %s", s, e)
816 r1, z1, e := reader.ReadRune()
818 t.Errorf("Reading %q after unreading: %s", s, e)
822 t.Errorf("Reading %q after unreading: want rune %q, got %q", s, r, r1)
826 t.Errorf("Reading %q after unreading: want size %d, got %d", s, z, z1)
831 t.Errorf("Reader(%q).ReadRune() produced %q", s, res)
836 var ReplaceTests = []struct {
842 {"hello", "l", "L", 0, "hello"},
843 {"hello", "l", "L", -1, "heLLo"},
844 {"hello", "x", "X", -1, "hello"},
845 {"", "x", "X", -1, ""},
846 {"radar", "r", "<r>", -1, "<r>ada<r>"},
847 {"", "", "<>", -1, "<>"},
848 {"banana", "a", "<>", -1, "b<>n<>n<>"},
849 {"banana", "a", "<>", 1, "b<>nana"},
850 {"banana", "a", "<>", 1000, "b<>n<>n<>"},
851 {"banana", "an", "<>", -1, "b<><>a"},
852 {"banana", "ana", "<>", -1, "b<>na"},
853 {"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"},
854 {"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
855 {"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
856 {"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
857 {"banana", "", "<>", 1, "<>banana"},
858 {"banana", "a", "a", -1, "banana"},
859 {"banana", "a", "a", 1, "banana"},
860 {"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"},
863 func TestReplace(t *testing.T) {
864 for _, tt := range ReplaceTests {
865 if s := Replace(tt.in, tt.old, tt.new, tt.n); s != tt.out {
866 t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
871 var TitleTests = []struct {
876 {" aaa aaa aaa ", " Aaa Aaa Aaa "},
877 {" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
878 {"123a456", "123a456"},
879 {"double-blind", "Double-Blind"},
883 func TestTitle(t *testing.T) {
884 for _, tt := range TitleTests {
885 if s := Title(tt.in); s != tt.out {
886 t.Errorf("Title(%q) = %q, want %q", tt.in, s, tt.out)
891 var ContainsTests = []struct {
896 {"abc", "bcd", false},
901 func TestContains(t *testing.T) {
902 for _, ct := range ContainsTests {
903 if Contains(ct.str, ct.substr) != ct.expected {
904 t.Errorf("Contains(%s, %s) = %v, want %v",
905 ct.str, ct.substr, !ct.expected, ct.expected)
910 var ContainsAnyTests = []struct {
920 {"abc", "xyz", false},
921 {"abc", "xcz", true},
922 {"a☺b☻c☹d", "uvw☻xyz", true},
923 {"aRegExp*", ".(|)*+?^$[]", true},
924 {dots + dots + dots, " ", false},
927 func TestContainsAny(t *testing.T) {
928 for _, ct := range ContainsAnyTests {
929 if ContainsAny(ct.str, ct.substr) != ct.expected {
930 t.Errorf("ContainsAny(%s, %s) = %v, want %v",
931 ct.str, ct.substr, !ct.expected, ct.expected)
936 var ContainsRuneTests = []struct {
946 {"a☺b☻c☹d", 'x', false},
947 {"a☺b☻c☹d", '☻', true},
948 {"aRegExp*", '*', true},
951 func TestContainsRune(t *testing.T) {
952 for _, ct := range ContainsRuneTests {
953 if ContainsRune(ct.str, ct.r) != ct.expected {
954 t.Errorf("ContainsRune(%s, %s) = %v, want %v",
955 ct.str, ct.r, !ct.expected, ct.expected)
960 var EqualFoldTests = []struct {
964 {"abc", "abc", true},
965 {"ABcd", "ABcd", true},
966 {"123abc", "123ABC", true},
967 {"αβδ", "ΑΒΔ", true},
968 {"abc", "xyz", false},
969 {"abc", "XYZ", false},
970 {"abcdefghijk", "abcdefghijX", false},
971 {"abcdefghijk", "abcdefghij\u212A", true},
972 {"abcdefghijK", "abcdefghij\u212A", true},
973 {"abcdefghijkz", "abcdefghij\u212Ay", false},
974 {"abcdefghijKz", "abcdefghij\u212Ay", false},
977 func TestEqualFold(t *testing.T) {
978 for _, tt := range EqualFoldTests {
979 if out := EqualFold(tt.s, tt.t); out != tt.out {
980 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.s, tt.t, out, tt.out)
982 if out := EqualFold(tt.t, tt.s); out != tt.out {
983 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.t, tt.s, out, tt.out)