Home | History | Annotate | Download | only in strings
      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.
      4 
      5 package strings_test
      6 
      7 import (
      8 	"bytes"
      9 	"fmt"
     10 	"io"
     11 	"math/rand"
     12 	"reflect"
     13 	. "strings"
     14 	"testing"
     15 	"unicode"
     16 	"unicode/utf8"
     17 	"unsafe"
     18 )
     19 
     20 func eq(a, b []string) bool {
     21 	if len(a) != len(b) {
     22 		return false
     23 	}
     24 	for i := 0; i < len(a); i++ {
     25 		if a[i] != b[i] {
     26 			return false
     27 		}
     28 	}
     29 	return true
     30 }
     31 
     32 var abcd = "abcd"
     33 var faces = ""
     34 var commas = "1,2,3,4"
     35 var dots = "1....2....3....4"
     36 
     37 type IndexTest struct {
     38 	s   string
     39 	sep string
     40 	out int
     41 }
     42 
     43 var indexTests = []IndexTest{
     44 	{"", "", 0},
     45 	{"", "a", -1},
     46 	{"", "foo", -1},
     47 	{"fo", "foo", -1},
     48 	{"foo", "foo", 0},
     49 	{"oofofoofooo", "f", 2},
     50 	{"oofofoofooo", "foo", 4},
     51 	{"barfoobarfoo", "foo", 3},
     52 	{"foo", "", 0},
     53 	{"foo", "o", 1},
     54 	{"abcABCabc", "A", 3},
     55 	// cases with one byte strings - test special case in Index()
     56 	{"", "a", -1},
     57 	{"x", "a", -1},
     58 	{"x", "x", 0},
     59 	{"abc", "a", 0},
     60 	{"abc", "b", 1},
     61 	{"abc", "c", 2},
     62 	{"abc", "x", -1},
     63 	// test special cases in Index() for short strings
     64 	{"", "ab", -1},
     65 	{"bc", "ab", -1},
     66 	{"ab", "ab", 0},
     67 	{"xab", "ab", 1},
     68 	{"xab"[:2], "ab", -1},
     69 	{"", "abc", -1},
     70 	{"xbc", "abc", -1},
     71 	{"abc", "abc", 0},
     72 	{"xabc", "abc", 1},
     73 	{"xabc"[:3], "abc", -1},
     74 	{"xabxc", "abc", -1},
     75 	{"", "abcd", -1},
     76 	{"xbcd", "abcd", -1},
     77 	{"abcd", "abcd", 0},
     78 	{"xabcd", "abcd", 1},
     79 	{"xyabcd"[:5], "abcd", -1},
     80 	{"xbcqq", "abcqq", -1},
     81 	{"abcqq", "abcqq", 0},
     82 	{"xabcqq", "abcqq", 1},
     83 	{"xyabcqq"[:6], "abcqq", -1},
     84 	{"xabxcqq", "abcqq", -1},
     85 	{"xabcqxq", "abcqq", -1},
     86 	{"", "01234567", -1},
     87 	{"32145678", "01234567", -1},
     88 	{"01234567", "01234567", 0},
     89 	{"x01234567", "01234567", 1},
     90 	{"x0123456x01234567", "01234567", 9},
     91 	{"xx01234567"[:9], "01234567", -1},
     92 	{"", "0123456789", -1},
     93 	{"3214567844", "0123456789", -1},
     94 	{"0123456789", "0123456789", 0},
     95 	{"x0123456789", "0123456789", 1},
     96 	{"x012345678x0123456789", "0123456789", 11},
     97 	{"xyz0123456789"[:12], "0123456789", -1},
     98 	{"x01234567x89", "0123456789", -1},
     99 	{"", "0123456789012345", -1},
    100 	{"3214567889012345", "0123456789012345", -1},
    101 	{"0123456789012345", "0123456789012345", 0},
    102 	{"x0123456789012345", "0123456789012345", 1},
    103 	{"x012345678901234x0123456789012345", "0123456789012345", 17},
    104 	{"", "01234567890123456789", -1},
    105 	{"32145678890123456789", "01234567890123456789", -1},
    106 	{"01234567890123456789", "01234567890123456789", 0},
    107 	{"x01234567890123456789", "01234567890123456789", 1},
    108 	{"x0123456789012345678x01234567890123456789", "01234567890123456789", 21},
    109 	{"xyz01234567890123456789"[:22], "01234567890123456789", -1},
    110 	{"", "0123456789012345678901234567890", -1},
    111 	{"321456788901234567890123456789012345678911", "0123456789012345678901234567890", -1},
    112 	{"0123456789012345678901234567890", "0123456789012345678901234567890", 0},
    113 	{"x0123456789012345678901234567890", "0123456789012345678901234567890", 1},
    114 	{"x012345678901234567890123456789x0123456789012345678901234567890", "0123456789012345678901234567890", 32},
    115 	{"xyz0123456789012345678901234567890"[:33], "0123456789012345678901234567890", -1},
    116 	{"", "01234567890123456789012345678901", -1},
    117 	{"32145678890123456789012345678901234567890211", "01234567890123456789012345678901", -1},
    118 	{"01234567890123456789012345678901", "01234567890123456789012345678901", 0},
    119 	{"x01234567890123456789012345678901", "01234567890123456789012345678901", 1},
    120 	{"x0123456789012345678901234567890x01234567890123456789012345678901", "01234567890123456789012345678901", 33},
    121 	{"xyz01234567890123456789012345678901"[:34], "01234567890123456789012345678901", -1},
    122 	{"xxxxxx012345678901234567890123456789012345678901234567890123456789012", "012345678901234567890123456789012345678901234567890123456789012", 6},
    123 	{"", "0123456789012345678901234567890123456789", -1},
    124 	{"xx012345678901234567890123456789012345678901234567890123456789012", "0123456789012345678901234567890123456789", 2},
    125 	{"xx012345678901234567890123456789012345678901234567890123456789012"[:41], "0123456789012345678901234567890123456789", -1},
    126 	{"xx012345678901234567890123456789012345678901234567890123456789012", "0123456789012345678901234567890123456xxx", -1},
    127 	{"xx0123456789012345678901234567890123456789012345678901234567890120123456789012345678901234567890123456xxx", "0123456789012345678901234567890123456xxx", 65},
    128 }
    129 
    130 var lastIndexTests = []IndexTest{
    131 	{"", "", 0},
    132 	{"", "a", -1},
    133 	{"", "foo", -1},
    134 	{"fo", "foo", -1},
    135 	{"foo", "foo", 0},
    136 	{"foo", "f", 0},
    137 	{"oofofoofooo", "f", 7},
    138 	{"oofofoofooo", "foo", 7},
    139 	{"barfoobarfoo", "foo", 9},
    140 	{"foo", "", 3},
    141 	{"foo", "o", 2},
    142 	{"abcABCabc", "A", 3},
    143 	{"abcABCabc", "a", 6},
    144 }
    145 
    146 var indexAnyTests = []IndexTest{
    147 	{"", "", -1},
    148 	{"", "a", -1},
    149 	{"", "abc", -1},
    150 	{"a", "", -1},
    151 	{"a", "a", 0},
    152 	{"aaa", "a", 0},
    153 	{"abc", "xyz", -1},
    154 	{"abc", "xcz", 2},
    155 	{"abc", "xyz", 2},
    156 	{"abcd", "cx", len("ab")},
    157 	{"abcd", "uvwxyz", len("ab")},
    158 	{"aRegExp*", ".(|)*+?^$[]", 7},
    159 	{dots + dots + dots, " ", -1},
    160 	{"012abcba210", "\xffb", 4},
    161 	{"012\x80bcb\x80210", "\xffb", 3},
    162 }
    163 
    164 var lastIndexAnyTests = []IndexTest{
    165 	{"", "", -1},
    166 	{"", "a", -1},
    167 	{"", "abc", -1},
    168 	{"a", "", -1},
    169 	{"a", "a", 0},
    170 	{"aaa", "a", 2},
    171 	{"abc", "xyz", -1},
    172 	{"abc", "ab", 1},
    173 	{"abc", "xyz", 2},
    174 	{"abcd", "cx", len("ab")},
    175 	{"abcd", "uvwxyz", len("ab")},
    176 	{"a.RegExp*", ".(|)*+?^$[]", 8},
    177 	{dots + dots + dots, " ", -1},
    178 	{"012abcba210", "\xffb", 6},
    179 	{"012\x80bcb\x80210", "\xffb", 7},
    180 }
    181 
    182 // Execute f on each test case.  funcName should be the name of f; it's used
    183 // in failure reports.
    184 func runIndexTests(t *testing.T, f func(s, sep string) int, funcName string, testCases []IndexTest) {
    185 	for _, test := range testCases {
    186 		actual := f(test.s, test.sep)
    187 		if actual != test.out {
    188 			t.Errorf("%s(%q,%q) = %v; want %v", funcName, test.s, test.sep, actual, test.out)
    189 		}
    190 	}
    191 }
    192 
    193 func TestIndex(t *testing.T)        { runIndexTests(t, Index, "Index", indexTests) }
    194 func TestLastIndex(t *testing.T)    { runIndexTests(t, LastIndex, "LastIndex", lastIndexTests) }
    195 func TestIndexAny(t *testing.T)     { runIndexTests(t, IndexAny, "IndexAny", indexAnyTests) }
    196 func TestLastIndexAny(t *testing.T) { runIndexTests(t, LastIndexAny, "LastIndexAny", lastIndexAnyTests) }
    197 
    198 func TestLastIndexByte(t *testing.T) {
    199 	testCases := []IndexTest{
    200 		{"", "q", -1},
    201 		{"abcdef", "q", -1},
    202 		{"abcdefabcdef", "a", len("abcdef")},      // something in the middle
    203 		{"abcdefabcdef", "f", len("abcdefabcde")}, // last byte
    204 		{"zabcdefabcdef", "z", 0},                 // first byte
    205 		{"abcd", "b", len("a")},               // non-ascii
    206 	}
    207 	for _, test := range testCases {
    208 		actual := LastIndexByte(test.s, test.sep[0])
    209 		if actual != test.out {
    210 			t.Errorf("LastIndexByte(%q,%c) = %v; want %v", test.s, test.sep[0], actual, test.out)
    211 		}
    212 	}
    213 }
    214 
    215 func simpleIndex(s, sep string) int {
    216 	n := len(sep)
    217 	for i := n; i <= len(s); i++ {
    218 		if s[i-n:i] == sep {
    219 			return i - n
    220 		}
    221 	}
    222 	return -1
    223 }
    224 
    225 func TestIndexRandom(t *testing.T) {
    226 	const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
    227 	for times := 0; times < 10; times++ {
    228 		for strLen := 5 + rand.Intn(5); strLen < 140; strLen += 10 { // Arbitrary
    229 			s1 := make([]byte, strLen)
    230 			for i := range s1 {
    231 				s1[i] = chars[rand.Intn(len(chars))]
    232 			}
    233 			s := string(s1)
    234 			for i := 0; i < 50; i++ {
    235 				begin := rand.Intn(len(s) + 1)
    236 				end := begin + rand.Intn(len(s)+1-begin)
    237 				sep := s[begin:end]
    238 				if i%4 == 0 {
    239 					pos := rand.Intn(len(sep) + 1)
    240 					sep = sep[:pos] + "A" + sep[pos:]
    241 				}
    242 				want := simpleIndex(s, sep)
    243 				res := Index(s, sep)
    244 				if res != want {
    245 					t.Errorf("Index(%s,%s) = %d; want %d", s, sep, res, want)
    246 				}
    247 			}
    248 		}
    249 	}
    250 }
    251 
    252 func TestIndexRune(t *testing.T) {
    253 	tests := []struct {
    254 		in   string
    255 		rune rune
    256 		want int
    257 	}{
    258 		{"", 'a', -1},
    259 		{"", '', -1},
    260 		{"foo", '', -1},
    261 		{"foo", 'o', 1},
    262 		{"foobar", '', 3},
    263 		{"foobar", '', 9},
    264 		{"a A x", 'A', 2},
    265 		{"some_text=some_value", '=', 9},
    266 		{"a", 'a', 3},
    267 		{"ab", '', 4},
    268 
    269 		// RuneError should match any invalid UTF-8 byte sequence.
    270 		{"", '', 0},
    271 		{"\xff", '', 0},
    272 		{"x", '', len("x")},
    273 		{"x\xe2\x98", '', len("x")},
    274 		{"x\xe2\x98", '', len("x")},
    275 		{"x\xe2\x98x", '', len("x")},
    276 
    277 		// Invalid rune values should never match.
    278 		{"abcd\xe2\x98\xff\xed\xa0\x80", -1, -1},
    279 		{"abcd\xe2\x98\xff\xed\xa0\x80", 0xD800, -1}, // Surrogate pair
    280 		{"abcd\xe2\x98\xff\xed\xa0\x80", utf8.MaxRune + 1, -1},
    281 	}
    282 	for _, tt := range tests {
    283 		if got := IndexRune(tt.in, tt.rune); got != tt.want {
    284 			t.Errorf("IndexRune(%q, %d) = %v; want %v", tt.in, tt.rune, got, tt.want)
    285 		}
    286 	}
    287 
    288 	haystack := "test"
    289 	allocs := testing.AllocsPerRun(1000, func() {
    290 		if i := IndexRune(haystack, 's'); i != 2 {
    291 			t.Fatalf("'s' at %d; want 2", i)
    292 		}
    293 		if i := IndexRune(haystack, ''); i != 4 {
    294 			t.Fatalf("'' at %d; want 4", i)
    295 		}
    296 	})
    297 	if allocs != 0 && testing.CoverMode() == "" {
    298 		t.Errorf("expected no allocations, got %f", allocs)
    299 	}
    300 }
    301 
    302 const benchmarkString = "some_text=somevalue"
    303 
    304 func BenchmarkIndexRune(b *testing.B) {
    305 	if got := IndexRune(benchmarkString, ''); got != 14 {
    306 		b.Fatalf("wrong index: expected 14, got=%d", got)
    307 	}
    308 	for i := 0; i < b.N; i++ {
    309 		IndexRune(benchmarkString, '')
    310 	}
    311 }
    312 
    313 var benchmarkLongString = Repeat(" ", 100) + benchmarkString
    314 
    315 func BenchmarkIndexRuneLongString(b *testing.B) {
    316 	if got := IndexRune(benchmarkLongString, ''); got != 114 {
    317 		b.Fatalf("wrong index: expected 114, got=%d", got)
    318 	}
    319 	for i := 0; i < b.N; i++ {
    320 		IndexRune(benchmarkLongString, '')
    321 	}
    322 }
    323 
    324 func BenchmarkIndexRuneFastPath(b *testing.B) {
    325 	if got := IndexRune(benchmarkString, 'v'); got != 17 {
    326 		b.Fatalf("wrong index: expected 17, got=%d", got)
    327 	}
    328 	for i := 0; i < b.N; i++ {
    329 		IndexRune(benchmarkString, 'v')
    330 	}
    331 }
    332 
    333 func BenchmarkIndex(b *testing.B) {
    334 	if got := Index(benchmarkString, "v"); got != 17 {
    335 		b.Fatalf("wrong index: expected 17, got=%d", got)
    336 	}
    337 	for i := 0; i < b.N; i++ {
    338 		Index(benchmarkString, "v")
    339 	}
    340 }
    341 
    342 func BenchmarkLastIndex(b *testing.B) {
    343 	if got := Index(benchmarkString, "v"); got != 17 {
    344 		b.Fatalf("wrong index: expected 17, got=%d", got)
    345 	}
    346 	for i := 0; i < b.N; i++ {
    347 		LastIndex(benchmarkString, "v")
    348 	}
    349 }
    350 
    351 func BenchmarkIndexByte(b *testing.B) {
    352 	if got := IndexByte(benchmarkString, 'v'); got != 17 {
    353 		b.Fatalf("wrong index: expected 17, got=%d", got)
    354 	}
    355 	for i := 0; i < b.N; i++ {
    356 		IndexByte(benchmarkString, 'v')
    357 	}
    358 }
    359 
    360 type SplitTest struct {
    361 	s   string
    362 	sep string
    363 	n   int
    364 	a   []string
    365 }
    366 
    367 var splittests = []SplitTest{
    368 	{"", "", -1, []string{}},
    369 	{abcd, "", 2, []string{"a", "bcd"}},
    370 	{abcd, "", 4, []string{"a", "b", "c", "d"}},
    371 	{abcd, "", -1, []string{"a", "b", "c", "d"}},
    372 	{faces, "", -1, []string{"", "", ""}},
    373 	{faces, "", 3, []string{"", "", ""}},
    374 	{faces, "", 17, []string{"", "", ""}},
    375 	{"", "", -1, []string{"", "", ""}},
    376 	{abcd, "a", 0, nil},
    377 	{abcd, "a", -1, []string{"", "bcd"}},
    378 	{abcd, "z", -1, []string{"abcd"}},
    379 	{commas, ",", -1, []string{"1", "2", "3", "4"}},
    380 	{dots, "...", -1, []string{"1", ".2", ".3", ".4"}},
    381 	{faces, "", -1, []string{"", ""}},
    382 	{faces, "~", -1, []string{faces}},
    383 	{"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
    384 	{"1 2", " ", 3, []string{"1", "2"}},
    385 }
    386 
    387 func TestSplit(t *testing.T) {
    388 	for _, tt := range splittests {
    389 		a := SplitN(tt.s, tt.sep, tt.n)
    390 		if !eq(a, tt.a) {
    391 			t.Errorf("Split(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, a, tt.a)
    392 			continue
    393 		}
    394 		if tt.n == 0 {
    395 			continue
    396 		}
    397 		s := Join(a, tt.sep)
    398 		if s != tt.s {
    399 			t.Errorf("Join(Split(%q, %q, %d), %q) = %q", tt.s, tt.sep, tt.n, tt.sep, s)
    400 		}
    401 		if tt.n < 0 {
    402 			b := Split(tt.s, tt.sep)
    403 			if !reflect.DeepEqual(a, b) {
    404 				t.Errorf("Split disagrees with SplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
    405 			}
    406 		}
    407 	}
    408 }
    409 
    410 var splitaftertests = []SplitTest{
    411 	{abcd, "a", -1, []string{"a", "bcd"}},
    412 	{abcd, "z", -1, []string{"abcd"}},
    413 	{abcd, "", -1, []string{"a", "b", "c", "d"}},
    414 	{commas, ",", -1, []string{"1,", "2,", "3,", "4"}},
    415 	{dots, "...", -1, []string{"1...", ".2...", ".3...", ".4"}},
    416 	{faces, "", -1, []string{"", ""}},
    417 	{faces, "~", -1, []string{faces}},
    418 	{faces, "", -1, []string{"", "", ""}},
    419 	{"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}},
    420 	{"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}},
    421 	{"1 2", " ", 3, []string{"1 ", "2"}},
    422 	{"123", "", 2, []string{"1", "23"}},
    423 	{"123", "", 17, []string{"1", "2", "3"}},
    424 }
    425 
    426 func TestSplitAfter(t *testing.T) {
    427 	for _, tt := range splitaftertests {
    428 		a := SplitAfterN(tt.s, tt.sep, tt.n)
    429 		if !eq(a, tt.a) {
    430 			t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, a, tt.a)
    431 			continue
    432 		}
    433 		s := Join(a, "")
    434 		if s != tt.s {
    435 			t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
    436 		}
    437 		if tt.n < 0 {
    438 			b := SplitAfter(tt.s, tt.sep)
    439 			if !reflect.DeepEqual(a, b) {
    440 				t.Errorf("SplitAfter disagrees with SplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
    441 			}
    442 		}
    443 	}
    444 }
    445 
    446 type FieldsTest struct {
    447 	s string
    448 	a []string
    449 }
    450 
    451 var fieldstests = []FieldsTest{
    452 	{"", []string{}},
    453 	{" ", []string{}},
    454 	{" \t ", []string{}},
    455 	{"  abc  ", []string{"abc"}},
    456 	{"1 2 3 4", []string{"1", "2", "3", "4"}},
    457 	{"1  2  3  4", []string{"1", "2", "3", "4"}},
    458 	{"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
    459 	{"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
    460 	{"\u2000\u2001\u2002", []string{}},
    461 	{"\n\t\n", []string{"", ""}},
    462 	{faces, []string{faces}},
    463 }
    464 
    465 func TestFields(t *testing.T) {
    466 	for _, tt := range fieldstests {
    467 		a := Fields(tt.s)
    468 		if !eq(a, tt.a) {
    469 			t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a)
    470 			continue
    471 		}
    472 	}
    473 }
    474 
    475 var FieldsFuncTests = []FieldsTest{
    476 	{"", []string{}},
    477 	{"XX", []string{}},
    478 	{"XXhiXXX", []string{"hi"}},
    479 	{"aXXbXXXcX", []string{"a", "b", "c"}},
    480 }
    481 
    482 func TestFieldsFunc(t *testing.T) {
    483 	for _, tt := range fieldstests {
    484 		a := FieldsFunc(tt.s, unicode.IsSpace)
    485 		if !eq(a, tt.a) {
    486 			t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a)
    487 			continue
    488 		}
    489 	}
    490 	pred := func(c rune) bool { return c == 'X' }
    491 	for _, tt := range FieldsFuncTests {
    492 		a := FieldsFunc(tt.s, pred)
    493 		if !eq(a, tt.a) {
    494 			t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
    495 		}
    496 	}
    497 }
    498 
    499 // Test case for any function which accepts and returns a single string.
    500 type StringTest struct {
    501 	in, out string
    502 }
    503 
    504 // Execute f on each test case.  funcName should be the name of f; it's used
    505 // in failure reports.
    506 func runStringTests(t *testing.T, f func(string) string, funcName string, testCases []StringTest) {
    507 	for _, tc := range testCases {
    508 		actual := f(tc.in)
    509 		if actual != tc.out {
    510 			t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out)
    511 		}
    512 	}
    513 }
    514 
    515 var upperTests = []StringTest{
    516 	{"", ""},
    517 	{"abc", "ABC"},
    518 	{"AbC123", "ABC123"},
    519 	{"azAZ09_", "AZAZ09_"},
    520 	{"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
    521 }
    522 
    523 var lowerTests = []StringTest{
    524 	{"", ""},
    525 	{"abc", "abc"},
    526 	{"AbC123", "abc123"},
    527 	{"azAZ09_", "azaz09_"},
    528 	{"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
    529 }
    530 
    531 const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
    532 
    533 var trimSpaceTests = []StringTest{
    534 	{"", ""},
    535 	{"abc", "abc"},
    536 	{space + "abc" + space, "abc"},
    537 	{" ", ""},
    538 	{" \t\r\n \t\t\r\r\n\n ", ""},
    539 	{" \t\r\n x\t\t\r\r\n\n ", "x"},
    540 	{" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
    541 	{"1 \t\r\n2", "1 \t\r\n2"},
    542 	{" x\x80", "x\x80"},
    543 	{" x\xc0", "x\xc0"},
    544 	{"x \xc0\xc0 ", "x \xc0\xc0"},
    545 	{"x \xc0", "x \xc0"},
    546 	{"x \xc0 ", "x \xc0"},
    547 	{"x \xc0\xc0 ", "x \xc0\xc0"},
    548 	{"x \xc0\xc0 ", "x \xc0\xc0"},
    549 	{"x  ", "x "},
    550 }
    551 
    552 func tenRunes(ch rune) string {
    553 	r := make([]rune, 10)
    554 	for i := range r {
    555 		r[i] = ch
    556 	}
    557 	return string(r)
    558 }
    559 
    560 // User-defined self-inverse mapping function
    561 func rot13(r rune) rune {
    562 	step := rune(13)
    563 	if r >= 'a' && r <= 'z' {
    564 		return ((r - 'a' + step) % 26) + 'a'
    565 	}
    566 	if r >= 'A' && r <= 'Z' {
    567 		return ((r - 'A' + step) % 26) + 'A'
    568 	}
    569 	return r
    570 }
    571 
    572 func TestMap(t *testing.T) {
    573 	// Run a couple of awful growth/shrinkage tests
    574 	a := tenRunes('a')
    575 	// 1.  Grow. This triggers two reallocations in Map.
    576 	maxRune := func(rune) rune { return unicode.MaxRune }
    577 	m := Map(maxRune, a)
    578 	expect := tenRunes(unicode.MaxRune)
    579 	if m != expect {
    580 		t.Errorf("growing: expected %q got %q", expect, m)
    581 	}
    582 
    583 	// 2. Shrink
    584 	minRune := func(rune) rune { return 'a' }
    585 	m = Map(minRune, tenRunes(unicode.MaxRune))
    586 	expect = a
    587 	if m != expect {
    588 		t.Errorf("shrinking: expected %q got %q", expect, m)
    589 	}
    590 
    591 	// 3. Rot13
    592 	m = Map(rot13, "a to zed")
    593 	expect = "n gb mrq"
    594 	if m != expect {
    595 		t.Errorf("rot13: expected %q got %q", expect, m)
    596 	}
    597 
    598 	// 4. Rot13^2
    599 	m = Map(rot13, Map(rot13, "a to zed"))
    600 	expect = "a to zed"
    601 	if m != expect {
    602 		t.Errorf("rot13: expected %q got %q", expect, m)
    603 	}
    604 
    605 	// 5. Drop
    606 	dropNotLatin := func(r rune) rune {
    607 		if unicode.Is(unicode.Latin, r) {
    608 			return r
    609 		}
    610 		return -1
    611 	}
    612 	m = Map(dropNotLatin, "Hello, ")
    613 	expect = "Hello"
    614 	if m != expect {
    615 		t.Errorf("drop: expected %q got %q", expect, m)
    616 	}
    617 
    618 	// 6. Identity
    619 	identity := func(r rune) rune {
    620 		return r
    621 	}
    622 	orig := "Input string that we expect not to be copied."
    623 	m = Map(identity, orig)
    624 	if (*reflect.StringHeader)(unsafe.Pointer(&orig)).Data !=
    625 		(*reflect.StringHeader)(unsafe.Pointer(&m)).Data {
    626 		t.Error("unexpected copy during identity map")
    627 	}
    628 }
    629 
    630 func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }
    631 
    632 func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTests) }
    633 
    634 func BenchmarkMapNoChanges(b *testing.B) {
    635 	identity := func(r rune) rune {
    636 		return r
    637 	}
    638 	for i := 0; i < b.N; i++ {
    639 		Map(identity, "Some string that won't be modified.")
    640 	}
    641 }
    642 
    643 func TestSpecialCase(t *testing.T) {
    644 	lower := "abcdefghijklmnoprstuvyz"
    645 	upper := "ABCDEFGHIJKLMNOPRSTUVYZ"
    646 	u := ToUpperSpecial(unicode.TurkishCase, upper)
    647 	if u != upper {
    648 		t.Errorf("Upper(upper) is %s not %s", u, upper)
    649 	}
    650 	u = ToUpperSpecial(unicode.TurkishCase, lower)
    651 	if u != upper {
    652 		t.Errorf("Upper(lower) is %s not %s", u, upper)
    653 	}
    654 	l := ToLowerSpecial(unicode.TurkishCase, lower)
    655 	if l != lower {
    656 		t.Errorf("Lower(lower) is %s not %s", l, lower)
    657 	}
    658 	l = ToLowerSpecial(unicode.TurkishCase, upper)
    659 	if l != lower {
    660 		t.Errorf("Lower(upper) is %s not %s", l, lower)
    661 	}
    662 }
    663 
    664 func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) }
    665 
    666 var trimTests = []struct {
    667 	f            string
    668 	in, arg, out string
    669 }{
    670 	{"Trim", "abba", "a", "bb"},
    671 	{"Trim", "abba", "ab", ""},
    672 	{"TrimLeft", "abba", "ab", ""},
    673 	{"TrimRight", "abba", "ab", ""},
    674 	{"TrimLeft", "abba", "a", "bba"},
    675 	{"TrimRight", "abba", "a", "abb"},
    676 	{"Trim", "<tag>", "<>", "tag"},
    677 	{"Trim", "* listitem", " *", "listitem"},
    678 	{"Trim", `"quote"`, `"`, "quote"},
    679 	{"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
    680 	{"Trim", "\x80test\xff", "\xff", "test"},
    681 	{"Trim", "  ", " ", ""},
    682 	{"Trim", " 0", "0 ", ""},
    683 	//empty string tests
    684 	{"Trim", "abba", "", "abba"},
    685 	{"Trim", "", "123", ""},
    686 	{"Trim", "", "", ""},
    687 	{"TrimLeft", "abba", "", "abba"},
    688 	{"TrimLeft", "", "123", ""},
    689 	{"TrimLeft", "", "", ""},
    690 	{"TrimRight", "abba", "", "abba"},
    691 	{"TrimRight", "", "123", ""},
    692 	{"TrimRight", "", "", ""},
    693 	{"TrimRight", "\xc0", "", "\xc0"},
    694 	{"TrimPrefix", "aabb", "a", "abb"},
    695 	{"TrimPrefix", "aabb", "b", "aabb"},
    696 	{"TrimSuffix", "aabb", "a", "aabb"},
    697 	{"TrimSuffix", "aabb", "b", "aab"},
    698 }
    699 
    700 func TestTrim(t *testing.T) {
    701 	for _, tc := range trimTests {
    702 		name := tc.f
    703 		var f func(string, string) string
    704 		switch name {
    705 		case "Trim":
    706 			f = Trim
    707 		case "TrimLeft":
    708 			f = TrimLeft
    709 		case "TrimRight":
    710 			f = TrimRight
    711 		case "TrimPrefix":
    712 			f = TrimPrefix
    713 		case "TrimSuffix":
    714 			f = TrimSuffix
    715 		default:
    716 			t.Errorf("Undefined trim function %s", name)
    717 		}
    718 		actual := f(tc.in, tc.arg)
    719 		if actual != tc.out {
    720 			t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
    721 		}
    722 	}
    723 }
    724 
    725 func BenchmarkTrim(b *testing.B) {
    726 	b.ReportAllocs()
    727 
    728 	for i := 0; i < b.N; i++ {
    729 		for _, tc := range trimTests {
    730 			name := tc.f
    731 			var f func(string, string) string
    732 			switch name {
    733 			case "Trim":
    734 				f = Trim
    735 			case "TrimLeft":
    736 				f = TrimLeft
    737 			case "TrimRight":
    738 				f = TrimRight
    739 			case "TrimPrefix":
    740 				f = TrimPrefix
    741 			case "TrimSuffix":
    742 				f = TrimSuffix
    743 			default:
    744 				b.Errorf("Undefined trim function %s", name)
    745 			}
    746 			actual := f(tc.in, tc.arg)
    747 			if actual != tc.out {
    748 				b.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
    749 			}
    750 		}
    751 	}
    752 }
    753 
    754 type predicate struct {
    755 	f    func(rune) bool
    756 	name string
    757 }
    758 
    759 var isSpace = predicate{unicode.IsSpace, "IsSpace"}
    760 var isDigit = predicate{unicode.IsDigit, "IsDigit"}
    761 var isUpper = predicate{unicode.IsUpper, "IsUpper"}
    762 var isValidRune = predicate{
    763 	func(r rune) bool {
    764 		return r != utf8.RuneError
    765 	},
    766 	"IsValidRune",
    767 }
    768 
    769 func not(p predicate) predicate {
    770 	return predicate{
    771 		func(r rune) bool {
    772 			return !p.f(r)
    773 		},
    774 		"not " + p.name,
    775 	}
    776 }
    777 
    778 var trimFuncTests = []struct {
    779 	f       predicate
    780 	in, out string
    781 }{
    782 	{isSpace, space + " hello " + space, "hello"},
    783 	{isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
    784 	{isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
    785 	{not(isSpace), "hello" + space + "hello", space},
    786 	{not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"},
    787 	{isValidRune, "ab\xc0a\xc0cd", "\xc0a\xc0"},
    788 	{not(isValidRune), "\xc0a\xc0", "a"},
    789 }
    790 
    791 func TestTrimFunc(t *testing.T) {
    792 	for _, tc := range trimFuncTests {
    793 		actual := TrimFunc(tc.in, tc.f.f)
    794 		if actual != tc.out {
    795 			t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.f.name, actual, tc.out)
    796 		}
    797 	}
    798 }
    799 
    800 var indexFuncTests = []struct {
    801 	in          string
    802 	f           predicate
    803 	first, last int
    804 }{
    805 	{"", isValidRune, -1, -1},
    806 	{"abc", isDigit, -1, -1},
    807 	{"0123", isDigit, 0, 3},
    808 	{"a1b", isDigit, 1, 1},
    809 	{space, isSpace, 0, len(space) - 3}, // last rune in space is 3 bytes
    810 	{"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit, 0, 18},
    811 	{"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34},
    812 	{"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12},
    813 
    814 	// tests of invalid UTF-8
    815 	{"\x801", isDigit, 1, 1},
    816 	{"\x80abc", isDigit, -1, -1},
    817 	{"\xc0a\xc0", isValidRune, 1, 1},
    818 	{"\xc0a\xc0", not(isValidRune), 0, 2},
    819 	{"\xc0\xc0", not(isValidRune), 0, 4},
    820 	{"\xc0\xc0\xc0", not(isValidRune), 0, 5},
    821 	{"ab\xc0a\xc0cd", not(isValidRune), 2, 4},
    822 	{"a\xe0\x80cd", not(isValidRune), 1, 2},
    823 	{"\x80\x80\x80\x80", not(isValidRune), 0, 3},
    824 }
    825 
    826 func TestIndexFunc(t *testing.T) {
    827 	for _, tc := range indexFuncTests {
    828 		first := IndexFunc(tc.in, tc.f.f)
    829 		if first != tc.first {
    830 			t.Errorf("IndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, first, tc.first)
    831 		}
    832 		last := LastIndexFunc(tc.in, tc.f.f)
    833 		if last != tc.last {
    834 			t.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, last, tc.last)
    835 		}
    836 	}
    837 }
    838 
    839 func equal(m string, s1, s2 string, t *testing.T) bool {
    840 	if s1 == s2 {
    841 		return true
    842 	}
    843 	e1 := Split(s1, "")
    844 	e2 := Split(s2, "")
    845 	for i, c1 := range e1 {
    846 		if i >= len(e2) {
    847 			break
    848 		}
    849 		r1, _ := utf8.DecodeRuneInString(c1)
    850 		r2, _ := utf8.DecodeRuneInString(e2[i])
    851 		if r1 != r2 {
    852 			t.Errorf("%s diff at %d: U+%04X U+%04X", m, i, r1, r2)
    853 		}
    854 	}
    855 	return false
    856 }
    857 
    858 func TestCaseConsistency(t *testing.T) {
    859 	// Make a string of all the runes.
    860 	numRunes := int(unicode.MaxRune + 1)
    861 	if testing.Short() {
    862 		numRunes = 1000
    863 	}
    864 	a := make([]rune, numRunes)
    865 	for i := range a {
    866 		a[i] = rune(i)
    867 	}
    868 	s := string(a)
    869 	// convert the cases.
    870 	upper := ToUpper(s)
    871 	lower := ToLower(s)
    872 
    873 	// Consistency checks
    874 	if n := utf8.RuneCountInString(upper); n != numRunes {
    875 		t.Error("rune count wrong in upper:", n)
    876 	}
    877 	if n := utf8.RuneCountInString(lower); n != numRunes {
    878 		t.Error("rune count wrong in lower:", n)
    879 	}
    880 	if !equal("ToUpper(upper)", ToUpper(upper), upper, t) {
    881 		t.Error("ToUpper(upper) consistency fail")
    882 	}
    883 	if !equal("ToLower(lower)", ToLower(lower), lower, t) {
    884 		t.Error("ToLower(lower) consistency fail")
    885 	}
    886 	/*
    887 		  These fail because of non-one-to-oneness of the data, such as multiple
    888 		  upper case 'I' mapping to 'i'.  We comment them out but keep them for
    889 		  interest.
    890 		  For instance: CAPITAL LETTER I WITH DOT ABOVE:
    891 			unicode.ToUpper(unicode.ToLower('\u0130')) != '\u0130'
    892 
    893 		if !equal("ToUpper(lower)", ToUpper(lower), upper, t) {
    894 			t.Error("ToUpper(lower) consistency fail");
    895 		}
    896 		if !equal("ToLower(upper)", ToLower(upper), lower, t) {
    897 			t.Error("ToLower(upper) consistency fail");
    898 		}
    899 	*/
    900 }
    901 
    902 var RepeatTests = []struct {
    903 	in, out string
    904 	count   int
    905 }{
    906 	{"", "", 0},
    907 	{"", "", 1},
    908 	{"", "", 2},
    909 	{"-", "", 0},
    910 	{"-", "-", 1},
    911 	{"-", "----------", 10},
    912 	{"abc ", "abc abc abc ", 3},
    913 }
    914 
    915 func TestRepeat(t *testing.T) {
    916 	for _, tt := range RepeatTests {
    917 		a := Repeat(tt.in, tt.count)
    918 		if !equal("Repeat(s)", a, tt.out, t) {
    919 			t.Errorf("Repeat(%v, %d) = %v; want %v", tt.in, tt.count, a, tt.out)
    920 			continue
    921 		}
    922 	}
    923 }
    924 
    925 func repeat(s string, count int) (err error) {
    926 	defer func() {
    927 		if r := recover(); r != nil {
    928 			switch v := r.(type) {
    929 			case error:
    930 				err = v
    931 			default:
    932 				err = fmt.Errorf("%s", v)
    933 			}
    934 		}
    935 	}()
    936 
    937 	Repeat(s, count)
    938 
    939 	return
    940 }
    941 
    942 // See Issue golang.org/issue/16237
    943 func TestRepeatCatchesOverflow(t *testing.T) {
    944 	tests := [...]struct {
    945 		s      string
    946 		count  int
    947 		errStr string
    948 	}{
    949 		0: {"--", -2147483647, "negative"},
    950 		1: {"", int(^uint(0) >> 1), ""},
    951 		2: {"-", 10, ""},
    952 		3: {"gopher", 0, ""},
    953 		4: {"-", -1, "negative"},
    954 		5: {"--", -102, "negative"},
    955 		6: {string(make([]byte, 255)), int((^uint(0))/255 + 1), "overflow"},
    956 	}
    957 
    958 	for i, tt := range tests {
    959 		err := repeat(tt.s, tt.count)
    960 		if tt.errStr == "" {
    961 			if err != nil {
    962 				t.Errorf("#%d panicked %v", i, err)
    963 			}
    964 			continue
    965 		}
    966 
    967 		if err == nil || !Contains(err.Error(), tt.errStr) {
    968 			t.Errorf("#%d expected %q got %q", i, tt.errStr, err)
    969 		}
    970 	}
    971 }
    972 
    973 func runesEqual(a, b []rune) bool {
    974 	if len(a) != len(b) {
    975 		return false
    976 	}
    977 	for i, r := range a {
    978 		if r != b[i] {
    979 			return false
    980 		}
    981 	}
    982 	return true
    983 }
    984 
    985 var RunesTests = []struct {
    986 	in    string
    987 	out   []rune
    988 	lossy bool
    989 }{
    990 	{"", []rune{}, false},
    991 	{" ", []rune{32}, false},
    992 	{"ABC", []rune{65, 66, 67}, false},
    993 	{"abc", []rune{97, 98, 99}, false},
    994 	{"\u65e5\u672c\u8a9e", []rune{26085, 26412, 35486}, false},
    995 	{"ab\x80c", []rune{97, 98, 0xFFFD, 99}, true},
    996 	{"ab\xc0c", []rune{97, 98, 0xFFFD, 99}, true},
    997 }
    998 
    999 func TestRunes(t *testing.T) {
   1000 	for _, tt := range RunesTests {
   1001 		a := []rune(tt.in)
   1002 		if !runesEqual(a, tt.out) {
   1003 			t.Errorf("[]rune(%q) = %v; want %v", tt.in, a, tt.out)
   1004 			continue
   1005 		}
   1006 		if !tt.lossy {
   1007 			// can only test reassembly if we didn't lose information
   1008 			s := string(a)
   1009 			if s != tt.in {
   1010 				t.Errorf("string([]rune(%q)) = %x; want %x", tt.in, s, tt.in)
   1011 			}
   1012 		}
   1013 	}
   1014 }
   1015 
   1016 func TestReadByte(t *testing.T) {
   1017 	testStrings := []string{"", abcd, faces, commas}
   1018 	for _, s := range testStrings {
   1019 		reader := NewReader(s)
   1020 		if e := reader.UnreadByte(); e == nil {
   1021 			t.Errorf("Unreading %q at beginning: expected error", s)
   1022 		}
   1023 		var res bytes.Buffer
   1024 		for {
   1025 			b, e := reader.ReadByte()
   1026 			if e == io.EOF {
   1027 				break
   1028 			}
   1029 			if e != nil {
   1030 				t.Errorf("Reading %q: %s", s, e)
   1031 				break
   1032 			}
   1033 			res.WriteByte(b)
   1034 			// unread and read again
   1035 			e = reader.UnreadByte()
   1036 			if e != nil {
   1037 				t.Errorf("Unreading %q: %s", s, e)
   1038 				break
   1039 			}
   1040 			b1, e := reader.ReadByte()
   1041 			if e != nil {
   1042 				t.Errorf("Reading %q after unreading: %s", s, e)
   1043 				break
   1044 			}
   1045 			if b1 != b {
   1046 				t.Errorf("Reading %q after unreading: want byte %q, got %q", s, b, b1)
   1047 				break
   1048 			}
   1049 		}
   1050 		if res.String() != s {
   1051 			t.Errorf("Reader(%q).ReadByte() produced %q", s, res.String())
   1052 		}
   1053 	}
   1054 }
   1055 
   1056 func TestReadRune(t *testing.T) {
   1057 	testStrings := []string{"", abcd, faces, commas}
   1058 	for _, s := range testStrings {
   1059 		reader := NewReader(s)
   1060 		if e := reader.UnreadRune(); e == nil {
   1061 			t.Errorf("Unreading %q at beginning: expected error", s)
   1062 		}
   1063 		res := ""
   1064 		for {
   1065 			r, z, e := reader.ReadRune()
   1066 			if e == io.EOF {
   1067 				break
   1068 			}
   1069 			if e != nil {
   1070 				t.Errorf("Reading %q: %s", s, e)
   1071 				break
   1072 			}
   1073 			res += string(r)
   1074 			// unread and read again
   1075 			e = reader.UnreadRune()
   1076 			if e != nil {
   1077 				t.Errorf("Unreading %q: %s", s, e)
   1078 				break
   1079 			}
   1080 			r1, z1, e := reader.ReadRune()
   1081 			if e != nil {
   1082 				t.Errorf("Reading %q after unreading: %s", s, e)
   1083 				break
   1084 			}
   1085 			if r1 != r {
   1086 				t.Errorf("Reading %q after unreading: want rune %q, got %q", s, r, r1)
   1087 				break
   1088 			}
   1089 			if z1 != z {
   1090 				t.Errorf("Reading %q after unreading: want size %d, got %d", s, z, z1)
   1091 				break
   1092 			}
   1093 		}
   1094 		if res != s {
   1095 			t.Errorf("Reader(%q).ReadRune() produced %q", s, res)
   1096 		}
   1097 	}
   1098 }
   1099 
   1100 var UnreadRuneErrorTests = []struct {
   1101 	name string
   1102 	f    func(*Reader)
   1103 }{
   1104 	{"Read", func(r *Reader) { r.Read([]byte{0}) }},
   1105 	{"ReadByte", func(r *Reader) { r.ReadByte() }},
   1106 	{"UnreadRune", func(r *Reader) { r.UnreadRune() }},
   1107 	{"Seek", func(r *Reader) { r.Seek(0, io.SeekCurrent) }},
   1108 	{"WriteTo", func(r *Reader) { r.WriteTo(&bytes.Buffer{}) }},
   1109 }
   1110 
   1111 func TestUnreadRuneError(t *testing.T) {
   1112 	for _, tt := range UnreadRuneErrorTests {
   1113 		reader := NewReader("0123456789")
   1114 		if _, _, err := reader.ReadRune(); err != nil {
   1115 			// should not happen
   1116 			t.Fatal(err)
   1117 		}
   1118 		tt.f(reader)
   1119 		err := reader.UnreadRune()
   1120 		if err == nil {
   1121 			t.Errorf("Unreading after %s: expected error", tt.name)
   1122 		}
   1123 	}
   1124 }
   1125 
   1126 var ReplaceTests = []struct {
   1127 	in       string
   1128 	old, new string
   1129 	n        int
   1130 	out      string
   1131 }{
   1132 	{"hello", "l", "L", 0, "hello"},
   1133 	{"hello", "l", "L", -1, "heLLo"},
   1134 	{"hello", "x", "X", -1, "hello"},
   1135 	{"", "x", "X", -1, ""},
   1136 	{"radar", "r", "<r>", -1, "<r>ada<r>"},
   1137 	{"", "", "<>", -1, "<>"},
   1138 	{"banana", "a", "<>", -1, "b<>n<>n<>"},
   1139 	{"banana", "a", "<>", 1, "b<>nana"},
   1140 	{"banana", "a", "<>", 1000, "b<>n<>n<>"},
   1141 	{"banana", "an", "<>", -1, "b<><>a"},
   1142 	{"banana", "ana", "<>", -1, "b<>na"},
   1143 	{"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"},
   1144 	{"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
   1145 	{"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
   1146 	{"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
   1147 	{"banana", "", "<>", 1, "<>banana"},
   1148 	{"banana", "a", "a", -1, "banana"},
   1149 	{"banana", "a", "a", 1, "banana"},
   1150 	{"", "", "<>", -1, "<><><><>"},
   1151 }
   1152 
   1153 func TestReplace(t *testing.T) {
   1154 	for _, tt := range ReplaceTests {
   1155 		if s := Replace(tt.in, tt.old, tt.new, tt.n); s != tt.out {
   1156 			t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
   1157 		}
   1158 	}
   1159 }
   1160 
   1161 var TitleTests = []struct {
   1162 	in, out string
   1163 }{
   1164 	{"", ""},
   1165 	{"a", "A"},
   1166 	{" aaa aaa aaa ", " Aaa Aaa Aaa "},
   1167 	{" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
   1168 	{"123a456", "123a456"},
   1169 	{"double-blind", "Double-Blind"},
   1170 	{"", ""},
   1171 	{"with_underscore", "With_underscore"},
   1172 	{"unicode \xe2\x80\xa8 line separator", "Unicode \xe2\x80\xa8 Line Separator"},
   1173 }
   1174 
   1175 func TestTitle(t *testing.T) {
   1176 	for _, tt := range TitleTests {
   1177 		if s := Title(tt.in); s != tt.out {
   1178 			t.Errorf("Title(%q) = %q, want %q", tt.in, s, tt.out)
   1179 		}
   1180 	}
   1181 }
   1182 
   1183 var ContainsTests = []struct {
   1184 	str, substr string
   1185 	expected    bool
   1186 }{
   1187 	{"abc", "bc", true},
   1188 	{"abc", "bcd", false},
   1189 	{"abc", "", true},
   1190 	{"", "a", false},
   1191 
   1192 	// cases to cover code in runtime/asm_amd64.s:indexShortStr
   1193 	// 2-byte needle
   1194 	{"xxxxxx", "01", false},
   1195 	{"01xxxx", "01", true},
   1196 	{"xx01xx", "01", true},
   1197 	{"xxxx01", "01", true},
   1198 	{"01xxxxx"[1:], "01", false},
   1199 	{"xxxxx01"[:6], "01", false},
   1200 	// 3-byte needle
   1201 	{"xxxxxxx", "012", false},
   1202 	{"012xxxx", "012", true},
   1203 	{"xx012xx", "012", true},
   1204 	{"xxxx012", "012", true},
   1205 	{"012xxxxx"[1:], "012", false},
   1206 	{"xxxxx012"[:7], "012", false},
   1207 	// 4-byte needle
   1208 	{"xxxxxxxx", "0123", false},
   1209 	{"0123xxxx", "0123", true},
   1210 	{"xx0123xx", "0123", true},
   1211 	{"xxxx0123", "0123", true},
   1212 	{"0123xxxxx"[1:], "0123", false},
   1213 	{"xxxxx0123"[:8], "0123", false},
   1214 	// 5-7-byte needle
   1215 	{"xxxxxxxxx", "01234", false},
   1216 	{"01234xxxx", "01234", true},
   1217 	{"xx01234xx", "01234", true},
   1218 	{"xxxx01234", "01234", true},
   1219 	{"01234xxxxx"[1:], "01234", false},
   1220 	{"xxxxx01234"[:9], "01234", false},
   1221 	// 8-byte needle
   1222 	{"xxxxxxxxxxxx", "01234567", false},
   1223 	{"01234567xxxx", "01234567", true},
   1224 	{"xx01234567xx", "01234567", true},
   1225 	{"xxxx01234567", "01234567", true},
   1226 	{"01234567xxxxx"[1:], "01234567", false},
   1227 	{"xxxxx01234567"[:12], "01234567", false},
   1228 	// 9-15-byte needle
   1229 	{"xxxxxxxxxxxxx", "012345678", false},
   1230 	{"012345678xxxx", "012345678", true},
   1231 	{"xx012345678xx", "012345678", true},
   1232 	{"xxxx012345678", "012345678", true},
   1233 	{"012345678xxxxx"[1:], "012345678", false},
   1234 	{"xxxxx012345678"[:13], "012345678", false},
   1235 	// 16-byte needle
   1236 	{"xxxxxxxxxxxxxxxxxxxx", "0123456789ABCDEF", false},
   1237 	{"0123456789ABCDEFxxxx", "0123456789ABCDEF", true},
   1238 	{"xx0123456789ABCDEFxx", "0123456789ABCDEF", true},
   1239 	{"xxxx0123456789ABCDEF", "0123456789ABCDEF", true},
   1240 	{"0123456789ABCDEFxxxxx"[1:], "0123456789ABCDEF", false},
   1241 	{"xxxxx0123456789ABCDEF"[:20], "0123456789ABCDEF", false},
   1242 	// 17-31-byte needle
   1243 	{"xxxxxxxxxxxxxxxxxxxxx", "0123456789ABCDEFG", false},
   1244 	{"0123456789ABCDEFGxxxx", "0123456789ABCDEFG", true},
   1245 	{"xx0123456789ABCDEFGxx", "0123456789ABCDEFG", true},
   1246 	{"xxxx0123456789ABCDEFG", "0123456789ABCDEFG", true},
   1247 	{"0123456789ABCDEFGxxxxx"[1:], "0123456789ABCDEFG", false},
   1248 	{"xxxxx0123456789ABCDEFG"[:21], "0123456789ABCDEFG", false},
   1249 
   1250 	// partial match cases
   1251 	{"xx01x", "012", false},                             // 3
   1252 	{"xx0123x", "01234", false},                         // 5-7
   1253 	{"xx01234567x", "012345678", false},                 // 9-15
   1254 	{"xx0123456789ABCDEFx", "0123456789ABCDEFG", false}, // 17-31, issue 15679
   1255 }
   1256 
   1257 func TestContains(t *testing.T) {
   1258 	for _, ct := range ContainsTests {
   1259 		if Contains(ct.str, ct.substr) != ct.expected {
   1260 			t.Errorf("Contains(%s, %s) = %v, want %v",
   1261 				ct.str, ct.substr, !ct.expected, ct.expected)
   1262 		}
   1263 	}
   1264 }
   1265 
   1266 var ContainsAnyTests = []struct {
   1267 	str, substr string
   1268 	expected    bool
   1269 }{
   1270 	{"", "", false},
   1271 	{"", "a", false},
   1272 	{"", "abc", false},
   1273 	{"a", "", false},
   1274 	{"a", "a", true},
   1275 	{"aaa", "a", true},
   1276 	{"abc", "xyz", false},
   1277 	{"abc", "xcz", true},
   1278 	{"abcd", "uvwxyz", true},
   1279 	{"aRegExp*", ".(|)*+?^$[]", true},
   1280 	{dots + dots + dots, " ", false},
   1281 }
   1282 
   1283 func TestContainsAny(t *testing.T) {
   1284 	for _, ct := range ContainsAnyTests {
   1285 		if ContainsAny(ct.str, ct.substr) != ct.expected {
   1286 			t.Errorf("ContainsAny(%s, %s) = %v, want %v",
   1287 				ct.str, ct.substr, !ct.expected, ct.expected)
   1288 		}
   1289 	}
   1290 }
   1291 
   1292 var ContainsRuneTests = []struct {
   1293 	str      string
   1294 	r        rune
   1295 	expected bool
   1296 }{
   1297 	{"", 'a', false},
   1298 	{"a", 'a', true},
   1299 	{"aaa", 'a', true},
   1300 	{"abc", 'y', false},
   1301 	{"abc", 'c', true},
   1302 	{"abcd", 'x', false},
   1303 	{"abcd", '', true},
   1304 	{"aRegExp*", '*', true},
   1305 }
   1306 
   1307 func TestContainsRune(t *testing.T) {
   1308 	for _, ct := range ContainsRuneTests {
   1309 		if ContainsRune(ct.str, ct.r) != ct.expected {
   1310 			t.Errorf("ContainsRune(%q, %q) = %v, want %v",
   1311 				ct.str, ct.r, !ct.expected, ct.expected)
   1312 		}
   1313 	}
   1314 }
   1315 
   1316 var EqualFoldTests = []struct {
   1317 	s, t string
   1318 	out  bool
   1319 }{
   1320 	{"abc", "abc", true},
   1321 	{"ABcd", "ABcd", true},
   1322 	{"123abc", "123ABC", true},
   1323 	{"", "", true},
   1324 	{"abc", "xyz", false},
   1325 	{"abc", "XYZ", false},
   1326 	{"abcdefghijk", "abcdefghijX", false},
   1327 	{"abcdefghijk", "abcdefghij\u212A", true},
   1328 	{"abcdefghijK", "abcdefghij\u212A", true},
   1329 	{"abcdefghijkz", "abcdefghij\u212Ay", false},
   1330 	{"abcdefghijKz", "abcdefghij\u212Ay", false},
   1331 }
   1332 
   1333 func TestEqualFold(t *testing.T) {
   1334 	for _, tt := range EqualFoldTests {
   1335 		if out := EqualFold(tt.s, tt.t); out != tt.out {
   1336 			t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.s, tt.t, out, tt.out)
   1337 		}
   1338 		if out := EqualFold(tt.t, tt.s); out != tt.out {
   1339 			t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.t, tt.s, out, tt.out)
   1340 		}
   1341 	}
   1342 }
   1343 
   1344 var CountTests = []struct {
   1345 	s, sep string
   1346 	num    int
   1347 }{
   1348 	{"", "", 1},
   1349 	{"", "notempty", 0},
   1350 	{"notempty", "", 9},
   1351 	{"smaller", "not smaller", 0},
   1352 	{"12345678987654321", "6", 2},
   1353 	{"611161116", "6", 3},
   1354 	{"notequal", "NotEqual", 0},
   1355 	{"equal", "equal", 1},
   1356 	{"abc1231231123q", "123", 3},
   1357 	{"11111", "11", 2},
   1358 }
   1359 
   1360 func TestCount(t *testing.T) {
   1361 	for _, tt := range CountTests {
   1362 		if num := Count(tt.s, tt.sep); num != tt.num {
   1363 			t.Errorf("Count(\"%s\", \"%s\") = %d, want %d", tt.s, tt.sep, num, tt.num)
   1364 		}
   1365 	}
   1366 }
   1367 
   1368 func makeBenchInputHard() string {
   1369 	tokens := [...]string{
   1370 		"<a>", "<p>", "<b>", "<strong>",
   1371 		"</a>", "</p>", "</b>", "</strong>",
   1372 		"hello", "world",
   1373 	}
   1374 	x := make([]byte, 0, 1<<20)
   1375 	for {
   1376 		i := rand.Intn(len(tokens))
   1377 		if len(x)+len(tokens[i]) >= 1<<20 {
   1378 			break
   1379 		}
   1380 		x = append(x, tokens[i]...)
   1381 	}
   1382 	return string(x)
   1383 }
   1384 
   1385 var benchInputHard = makeBenchInputHard()
   1386 
   1387 func benchmarkIndexHard(b *testing.B, sep string) {
   1388 	for i := 0; i < b.N; i++ {
   1389 		Index(benchInputHard, sep)
   1390 	}
   1391 }
   1392 
   1393 func benchmarkLastIndexHard(b *testing.B, sep string) {
   1394 	for i := 0; i < b.N; i++ {
   1395 		LastIndex(benchInputHard, sep)
   1396 	}
   1397 }
   1398 
   1399 func benchmarkCountHard(b *testing.B, sep string) {
   1400 	for i := 0; i < b.N; i++ {
   1401 		Count(benchInputHard, sep)
   1402 	}
   1403 }
   1404 
   1405 func BenchmarkIndexHard1(b *testing.B) { benchmarkIndexHard(b, "<>") }
   1406 func BenchmarkIndexHard2(b *testing.B) { benchmarkIndexHard(b, "</pre>") }
   1407 func BenchmarkIndexHard3(b *testing.B) { benchmarkIndexHard(b, "<b>hello world</b>") }
   1408 func BenchmarkIndexHard4(b *testing.B) {
   1409 	benchmarkIndexHard(b, "<pre><b>hello</b><strong>world</strong></pre>")
   1410 }
   1411 
   1412 func BenchmarkLastIndexHard1(b *testing.B) { benchmarkLastIndexHard(b, "<>") }
   1413 func BenchmarkLastIndexHard2(b *testing.B) { benchmarkLastIndexHard(b, "</pre>") }
   1414 func BenchmarkLastIndexHard3(b *testing.B) { benchmarkLastIndexHard(b, "<b>hello world</b>") }
   1415 
   1416 func BenchmarkCountHard1(b *testing.B) { benchmarkCountHard(b, "<>") }
   1417 func BenchmarkCountHard2(b *testing.B) { benchmarkCountHard(b, "</pre>") }
   1418 func BenchmarkCountHard3(b *testing.B) { benchmarkCountHard(b, "<b>hello world</b>") }
   1419 
   1420 var benchInputTorture = Repeat("ABC", 1<<10) + "123" + Repeat("ABC", 1<<10)
   1421 var benchNeedleTorture = Repeat("ABC", 1<<10+1)
   1422 
   1423 func BenchmarkIndexTorture(b *testing.B) {
   1424 	for i := 0; i < b.N; i++ {
   1425 		Index(benchInputTorture, benchNeedleTorture)
   1426 	}
   1427 }
   1428 
   1429 func BenchmarkCountTorture(b *testing.B) {
   1430 	for i := 0; i < b.N; i++ {
   1431 		Count(benchInputTorture, benchNeedleTorture)
   1432 	}
   1433 }
   1434 
   1435 func BenchmarkCountTortureOverlapping(b *testing.B) {
   1436 	A := Repeat("ABC", 1<<20)
   1437 	B := Repeat("ABC", 1<<10)
   1438 	for i := 0; i < b.N; i++ {
   1439 		Count(A, B)
   1440 	}
   1441 }
   1442 
   1443 var makeFieldsInput = func() string {
   1444 	x := make([]byte, 1<<20)
   1445 	// Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
   1446 	for i := range x {
   1447 		switch rand.Intn(10) {
   1448 		case 0:
   1449 			x[i] = ' '
   1450 		case 1:
   1451 			if i > 0 && x[i-1] == 'x' {
   1452 				copy(x[i-1:], "")
   1453 				break
   1454 			}
   1455 			fallthrough
   1456 		default:
   1457 			x[i] = 'x'
   1458 		}
   1459 	}
   1460 	return string(x)
   1461 }
   1462 
   1463 var fieldsInput = makeFieldsInput()
   1464 
   1465 func BenchmarkFields(b *testing.B) {
   1466 	b.SetBytes(int64(len(fieldsInput)))
   1467 	for i := 0; i < b.N; i++ {
   1468 		Fields(fieldsInput)
   1469 	}
   1470 }
   1471 
   1472 func BenchmarkFieldsFunc(b *testing.B) {
   1473 	b.SetBytes(int64(len(fieldsInput)))
   1474 	for i := 0; i < b.N; i++ {
   1475 		FieldsFunc(fieldsInput, unicode.IsSpace)
   1476 	}
   1477 }
   1478 
   1479 func BenchmarkSplit1(b *testing.B) {
   1480 	for i := 0; i < b.N; i++ {
   1481 		Split(benchInputHard, "")
   1482 	}
   1483 }
   1484 
   1485 func BenchmarkSplit2(b *testing.B) {
   1486 	for i := 0; i < b.N; i++ {
   1487 		Split(benchInputHard, "/")
   1488 	}
   1489 }
   1490 
   1491 func BenchmarkSplit3(b *testing.B) {
   1492 	for i := 0; i < b.N; i++ {
   1493 		Split(benchInputHard, "hello")
   1494 	}
   1495 }
   1496 
   1497 func BenchmarkRepeat(b *testing.B) {
   1498 	for i := 0; i < b.N; i++ {
   1499 		Repeat("-", 80)
   1500 	}
   1501 }
   1502 
   1503 func BenchmarkIndexAnyASCII(b *testing.B) {
   1504 	x := Repeat("#", 4096) // Never matches set
   1505 	cs := "0123456789abcdef"
   1506 	for k := 1; k <= 4096; k <<= 4 {
   1507 		for j := 1; j <= 16; j <<= 1 {
   1508 			b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
   1509 				for i := 0; i < b.N; i++ {
   1510 					IndexAny(x[:k], cs[:j])
   1511 				}
   1512 			})
   1513 		}
   1514 	}
   1515 }
   1516 
   1517 func BenchmarkTrimASCII(b *testing.B) {
   1518 	cs := "0123456789abcdef"
   1519 	for k := 1; k <= 4096; k <<= 4 {
   1520 		for j := 1; j <= 16; j <<= 1 {
   1521 			b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
   1522 				x := Repeat(cs[:j], k) // Always matches set
   1523 				for i := 0; i < b.N; i++ {
   1524 					Trim(x[:k], cs[:j])
   1525 				}
   1526 			})
   1527 		}
   1528 	}
   1529 }
   1530