github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/src/strings/strings_test.go (about) 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 {"ab☺c", "x☺yz", 2}, 156 {"a☺b☻c☹d", "cx", len("a☺b☻")}, 157 {"a☺b☻c☹d", "uvw☻xyz", len("a☺b")}, 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 {"ab☺c", "x☺yz", 2}, 174 {"a☺b☻c☹d", "cx", len("a☺b☻")}, 175 {"a☺b☻c☹d", "uvw☻xyz", len("a☺b")}, 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 {"a☺b☻c☹d", "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 {"foo☺bar", '☺', 3}, 263 {"foo☺☻☹bar", '☹', 9}, 264 {"a A x", 'A', 2}, 265 {"some_text=some_value", '=', 9}, 266 {"☺a", 'a', 3}, 267 {"a☻☺b", '☺', 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 {"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", -1, -1}, 279 {"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", 0xD800, -1}, // Surrogate pair 280 {"a☺b☻c☹d\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=some☺value" 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 {"\u2000", []string{}}, 456 {" abc ", []string{"abc"}}, 457 {"1 2 3 4", []string{"1", "2", "3", "4"}}, 458 {"1 2 3 4", []string{"1", "2", "3", "4"}}, 459 {"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}}, 460 {"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}}, 461 {"\u2000\u2001\u2002", []string{}}, 462 {"\n™\t™\n", []string{"™", "™"}}, 463 {"\n\u20001™2\u2000 \u2001 ™", []string{"1™2", "™"}}, 464 {"\n1\uFFFD \uFFFD2\u20003\uFFFD4", []string{"1\uFFFD", "\uFFFD2", "3\uFFFD4"}}, 465 {"1\xFF\u2000\xFF2\xFF \xFF", []string{"1\xFF", "\xFF2\xFF", "\xFF"}}, 466 {faces, []string{faces}}, 467 } 468 469 func TestFields(t *testing.T) { 470 for _, tt := range fieldstests { 471 a := Fields(tt.s) 472 if !eq(a, tt.a) { 473 t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a) 474 continue 475 } 476 } 477 } 478 479 var FieldsFuncTests = []FieldsTest{ 480 {"", []string{}}, 481 {"XX", []string{}}, 482 {"XXhiXXX", []string{"hi"}}, 483 {"aXXbXXXcX", []string{"a", "b", "c"}}, 484 } 485 486 func TestFieldsFunc(t *testing.T) { 487 for _, tt := range fieldstests { 488 a := FieldsFunc(tt.s, unicode.IsSpace) 489 if !eq(a, tt.a) { 490 t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a) 491 continue 492 } 493 } 494 pred := func(c rune) bool { return c == 'X' } 495 for _, tt := range FieldsFuncTests { 496 a := FieldsFunc(tt.s, pred) 497 if !eq(a, tt.a) { 498 t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a) 499 } 500 } 501 } 502 503 // Test case for any function which accepts and returns a single string. 504 type StringTest struct { 505 in, out string 506 } 507 508 // Execute f on each test case. funcName should be the name of f; it's used 509 // in failure reports. 510 func runStringTests(t *testing.T, f func(string) string, funcName string, testCases []StringTest) { 511 for _, tc := range testCases { 512 actual := f(tc.in) 513 if actual != tc.out { 514 t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out) 515 } 516 } 517 } 518 519 var upperTests = []StringTest{ 520 {"", ""}, 521 {"abc", "ABC"}, 522 {"AbC123", "ABC123"}, 523 {"azAZ09_", "AZAZ09_"}, 524 {"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char 525 {"a\u0080\U0010FFFF", "A\u0080\U0010FFFF"}, // test utf8.RuneSelf and utf8.MaxRune 526 } 527 528 var lowerTests = []StringTest{ 529 {"", ""}, 530 {"abc", "abc"}, 531 {"AbC123", "abc123"}, 532 {"azAZ09_", "azaz09_"}, 533 {"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char 534 {"A\u0080\U0010FFFF", "a\u0080\U0010FFFF"}, // test utf8.RuneSelf and utf8.MaxRune 535 } 536 537 const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000" 538 539 var trimSpaceTests = []StringTest{ 540 {"", ""}, 541 {"abc", "abc"}, 542 {space + "abc" + space, "abc"}, 543 {" ", ""}, 544 {" \t\r\n \t\t\r\r\n\n ", ""}, 545 {" \t\r\n x\t\t\r\r\n\n ", "x"}, 546 {" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"}, 547 {"1 \t\r\n2", "1 \t\r\n2"}, 548 {" x\x80", "x\x80"}, 549 {" x\xc0", "x\xc0"}, 550 {"x \xc0\xc0 ", "x \xc0\xc0"}, 551 {"x \xc0", "x \xc0"}, 552 {"x \xc0 ", "x \xc0"}, 553 {"x \xc0\xc0 ", "x \xc0\xc0"}, 554 {"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"}, 555 {"x ☺ ", "x ☺"}, 556 } 557 558 func tenRunes(ch rune) string { 559 r := make([]rune, 10) 560 for i := range r { 561 r[i] = ch 562 } 563 return string(r) 564 } 565 566 // User-defined self-inverse mapping function 567 func rot13(r rune) rune { 568 step := rune(13) 569 if r >= 'a' && r <= 'z' { 570 return ((r - 'a' + step) % 26) + 'a' 571 } 572 if r >= 'A' && r <= 'Z' { 573 return ((r - 'A' + step) % 26) + 'A' 574 } 575 return r 576 } 577 578 func TestMap(t *testing.T) { 579 // Run a couple of awful growth/shrinkage tests 580 a := tenRunes('a') 581 // 1. Grow. This triggers two reallocations in Map. 582 maxRune := func(rune) rune { return unicode.MaxRune } 583 m := Map(maxRune, a) 584 expect := tenRunes(unicode.MaxRune) 585 if m != expect { 586 t.Errorf("growing: expected %q got %q", expect, m) 587 } 588 589 // 2. Shrink 590 minRune := func(rune) rune { return 'a' } 591 m = Map(minRune, tenRunes(unicode.MaxRune)) 592 expect = a 593 if m != expect { 594 t.Errorf("shrinking: expected %q got %q", expect, m) 595 } 596 597 // 3. Rot13 598 m = Map(rot13, "a to zed") 599 expect = "n gb mrq" 600 if m != expect { 601 t.Errorf("rot13: expected %q got %q", expect, m) 602 } 603 604 // 4. Rot13^2 605 m = Map(rot13, Map(rot13, "a to zed")) 606 expect = "a to zed" 607 if m != expect { 608 t.Errorf("rot13: expected %q got %q", expect, m) 609 } 610 611 // 5. Drop 612 dropNotLatin := func(r rune) rune { 613 if unicode.Is(unicode.Latin, r) { 614 return r 615 } 616 return -1 617 } 618 m = Map(dropNotLatin, "Hello, 세계") 619 expect = "Hello" 620 if m != expect { 621 t.Errorf("drop: expected %q got %q", expect, m) 622 } 623 624 // 6. Identity 625 identity := func(r rune) rune { 626 return r 627 } 628 orig := "Input string that we expect not to be copied." 629 m = Map(identity, orig) 630 if (*reflect.StringHeader)(unsafe.Pointer(&orig)).Data != 631 (*reflect.StringHeader)(unsafe.Pointer(&m)).Data { 632 t.Error("unexpected copy during identity map") 633 } 634 635 // 7. Handle invalid UTF-8 sequence 636 replaceNotLatin := func(r rune) rune { 637 if unicode.Is(unicode.Latin, r) { 638 return r 639 } 640 return '?' 641 } 642 m = Map(replaceNotLatin, "Hello\255World") 643 expect = "Hello?World" 644 if m != expect { 645 t.Errorf("replace invalid sequence: expected %q got %q", expect, m) 646 } 647 648 // 8. Check utf8.RuneSelf and utf8.MaxRune encoding 649 encode := func(r rune) rune { 650 switch r { 651 case utf8.RuneSelf: 652 return unicode.MaxRune 653 case unicode.MaxRune: 654 return utf8.RuneSelf 655 } 656 return r 657 } 658 s := string(utf8.RuneSelf) + string(utf8.MaxRune) 659 r := string(utf8.MaxRune) + string(utf8.RuneSelf) // reverse of s 660 m = Map(encode, s) 661 if m != r { 662 t.Errorf("encoding not handled correctly: expected %q got %q", r, m) 663 } 664 m = Map(encode, r) 665 if m != s { 666 t.Errorf("encoding not handled correctly: expected %q got %q", s, m) 667 } 668 } 669 670 func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) } 671 672 func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTests) } 673 674 func BenchmarkMapNoChanges(b *testing.B) { 675 identity := func(r rune) rune { 676 return r 677 } 678 for i := 0; i < b.N; i++ { 679 Map(identity, "Some string that won't be modified.") 680 } 681 } 682 683 func TestSpecialCase(t *testing.T) { 684 lower := "abcçdefgğhıijklmnoöprsştuüvyz" 685 upper := "ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ" 686 u := ToUpperSpecial(unicode.TurkishCase, upper) 687 if u != upper { 688 t.Errorf("Upper(upper) is %s not %s", u, upper) 689 } 690 u = ToUpperSpecial(unicode.TurkishCase, lower) 691 if u != upper { 692 t.Errorf("Upper(lower) is %s not %s", u, upper) 693 } 694 l := ToLowerSpecial(unicode.TurkishCase, lower) 695 if l != lower { 696 t.Errorf("Lower(lower) is %s not %s", l, lower) 697 } 698 l = ToLowerSpecial(unicode.TurkishCase, upper) 699 if l != lower { 700 t.Errorf("Lower(upper) is %s not %s", l, lower) 701 } 702 } 703 704 func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) } 705 706 var trimTests = []struct { 707 f string 708 in, arg, out string 709 }{ 710 {"Trim", "abba", "a", "bb"}, 711 {"Trim", "abba", "ab", ""}, 712 {"TrimLeft", "abba", "ab", ""}, 713 {"TrimRight", "abba", "ab", ""}, 714 {"TrimLeft", "abba", "a", "bba"}, 715 {"TrimRight", "abba", "a", "abb"}, 716 {"Trim", "<tag>", "<>", "tag"}, 717 {"Trim", "* listitem", " *", "listitem"}, 718 {"Trim", `"quote"`, `"`, "quote"}, 719 {"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"}, 720 {"Trim", "\x80test\xff", "\xff", "test"}, 721 {"Trim", " Ġ ", " ", "Ġ"}, 722 {"Trim", " Ġİ0", "0 ", "Ġİ"}, 723 //empty string tests 724 {"Trim", "abba", "", "abba"}, 725 {"Trim", "", "123", ""}, 726 {"Trim", "", "", ""}, 727 {"TrimLeft", "abba", "", "abba"}, 728 {"TrimLeft", "", "123", ""}, 729 {"TrimLeft", "", "", ""}, 730 {"TrimRight", "abba", "", "abba"}, 731 {"TrimRight", "", "123", ""}, 732 {"TrimRight", "", "", ""}, 733 {"TrimRight", "☺\xc0", "☺", "☺\xc0"}, 734 {"TrimPrefix", "aabb", "a", "abb"}, 735 {"TrimPrefix", "aabb", "b", "aabb"}, 736 {"TrimSuffix", "aabb", "a", "aabb"}, 737 {"TrimSuffix", "aabb", "b", "aab"}, 738 } 739 740 func TestTrim(t *testing.T) { 741 for _, tc := range trimTests { 742 name := tc.f 743 var f func(string, string) string 744 switch name { 745 case "Trim": 746 f = Trim 747 case "TrimLeft": 748 f = TrimLeft 749 case "TrimRight": 750 f = TrimRight 751 case "TrimPrefix": 752 f = TrimPrefix 753 case "TrimSuffix": 754 f = TrimSuffix 755 default: 756 t.Errorf("Undefined trim function %s", name) 757 } 758 actual := f(tc.in, tc.arg) 759 if actual != tc.out { 760 t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out) 761 } 762 } 763 } 764 765 func BenchmarkTrim(b *testing.B) { 766 b.ReportAllocs() 767 768 for i := 0; i < b.N; i++ { 769 for _, tc := range trimTests { 770 name := tc.f 771 var f func(string, string) string 772 switch name { 773 case "Trim": 774 f = Trim 775 case "TrimLeft": 776 f = TrimLeft 777 case "TrimRight": 778 f = TrimRight 779 case "TrimPrefix": 780 f = TrimPrefix 781 case "TrimSuffix": 782 f = TrimSuffix 783 default: 784 b.Errorf("Undefined trim function %s", name) 785 } 786 actual := f(tc.in, tc.arg) 787 if actual != tc.out { 788 b.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out) 789 } 790 } 791 } 792 } 793 794 type predicate struct { 795 f func(rune) bool 796 name string 797 } 798 799 var isSpace = predicate{unicode.IsSpace, "IsSpace"} 800 var isDigit = predicate{unicode.IsDigit, "IsDigit"} 801 var isUpper = predicate{unicode.IsUpper, "IsUpper"} 802 var isValidRune = predicate{ 803 func(r rune) bool { 804 return r != utf8.RuneError 805 }, 806 "IsValidRune", 807 } 808 809 func not(p predicate) predicate { 810 return predicate{ 811 func(r rune) bool { 812 return !p.f(r) 813 }, 814 "not " + p.name, 815 } 816 } 817 818 var trimFuncTests = []struct { 819 f predicate 820 in, out string 821 }{ 822 {isSpace, space + " hello " + space, "hello"}, 823 {isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"}, 824 {isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"}, 825 {not(isSpace), "hello" + space + "hello", space}, 826 {not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"}, 827 {isValidRune, "ab\xc0a\xc0cd", "\xc0a\xc0"}, 828 {not(isValidRune), "\xc0a\xc0", "a"}, 829 } 830 831 func TestTrimFunc(t *testing.T) { 832 for _, tc := range trimFuncTests { 833 actual := TrimFunc(tc.in, tc.f.f) 834 if actual != tc.out { 835 t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.f.name, actual, tc.out) 836 } 837 } 838 } 839 840 var indexFuncTests = []struct { 841 in string 842 f predicate 843 first, last int 844 }{ 845 {"", isValidRune, -1, -1}, 846 {"abc", isDigit, -1, -1}, 847 {"0123", isDigit, 0, 3}, 848 {"a1b", isDigit, 1, 1}, 849 {space, isSpace, 0, len(space) - 3}, // last rune in space is 3 bytes 850 {"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit, 0, 18}, 851 {"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34}, 852 {"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12}, 853 854 // tests of invalid UTF-8 855 {"\x801", isDigit, 1, 1}, 856 {"\x80abc", isDigit, -1, -1}, 857 {"\xc0a\xc0", isValidRune, 1, 1}, 858 {"\xc0a\xc0", not(isValidRune), 0, 2}, 859 {"\xc0☺\xc0", not(isValidRune), 0, 4}, 860 {"\xc0☺\xc0\xc0", not(isValidRune), 0, 5}, 861 {"ab\xc0a\xc0cd", not(isValidRune), 2, 4}, 862 {"a\xe0\x80cd", not(isValidRune), 1, 2}, 863 {"\x80\x80\x80\x80", not(isValidRune), 0, 3}, 864 } 865 866 func TestIndexFunc(t *testing.T) { 867 for _, tc := range indexFuncTests { 868 first := IndexFunc(tc.in, tc.f.f) 869 if first != tc.first { 870 t.Errorf("IndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, first, tc.first) 871 } 872 last := LastIndexFunc(tc.in, tc.f.f) 873 if last != tc.last { 874 t.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, last, tc.last) 875 } 876 } 877 } 878 879 func equal(m string, s1, s2 string, t *testing.T) bool { 880 if s1 == s2 { 881 return true 882 } 883 e1 := Split(s1, "") 884 e2 := Split(s2, "") 885 for i, c1 := range e1 { 886 if i >= len(e2) { 887 break 888 } 889 r1, _ := utf8.DecodeRuneInString(c1) 890 r2, _ := utf8.DecodeRuneInString(e2[i]) 891 if r1 != r2 { 892 t.Errorf("%s diff at %d: U+%04X U+%04X", m, i, r1, r2) 893 } 894 } 895 return false 896 } 897 898 func TestCaseConsistency(t *testing.T) { 899 // Make a string of all the runes. 900 numRunes := int(unicode.MaxRune + 1) 901 if testing.Short() { 902 numRunes = 1000 903 } 904 a := make([]rune, numRunes) 905 for i := range a { 906 a[i] = rune(i) 907 } 908 s := string(a) 909 // convert the cases. 910 upper := ToUpper(s) 911 lower := ToLower(s) 912 913 // Consistency checks 914 if n := utf8.RuneCountInString(upper); n != numRunes { 915 t.Error("rune count wrong in upper:", n) 916 } 917 if n := utf8.RuneCountInString(lower); n != numRunes { 918 t.Error("rune count wrong in lower:", n) 919 } 920 if !equal("ToUpper(upper)", ToUpper(upper), upper, t) { 921 t.Error("ToUpper(upper) consistency fail") 922 } 923 if !equal("ToLower(lower)", ToLower(lower), lower, t) { 924 t.Error("ToLower(lower) consistency fail") 925 } 926 /* 927 These fail because of non-one-to-oneness of the data, such as multiple 928 upper case 'I' mapping to 'i'. We comment them out but keep them for 929 interest. 930 For instance: CAPITAL LETTER I WITH DOT ABOVE: 931 unicode.ToUpper(unicode.ToLower('\u0130')) != '\u0130' 932 933 if !equal("ToUpper(lower)", ToUpper(lower), upper, t) { 934 t.Error("ToUpper(lower) consistency fail"); 935 } 936 if !equal("ToLower(upper)", ToLower(upper), lower, t) { 937 t.Error("ToLower(upper) consistency fail"); 938 } 939 */ 940 } 941 942 var RepeatTests = []struct { 943 in, out string 944 count int 945 }{ 946 {"", "", 0}, 947 {"", "", 1}, 948 {"", "", 2}, 949 {"-", "", 0}, 950 {"-", "-", 1}, 951 {"-", "----------", 10}, 952 {"abc ", "abc abc abc ", 3}, 953 } 954 955 func TestRepeat(t *testing.T) { 956 for _, tt := range RepeatTests { 957 a := Repeat(tt.in, tt.count) 958 if !equal("Repeat(s)", a, tt.out, t) { 959 t.Errorf("Repeat(%v, %d) = %v; want %v", tt.in, tt.count, a, tt.out) 960 continue 961 } 962 } 963 } 964 965 func repeat(s string, count int) (err error) { 966 defer func() { 967 if r := recover(); r != nil { 968 switch v := r.(type) { 969 case error: 970 err = v 971 default: 972 err = fmt.Errorf("%s", v) 973 } 974 } 975 }() 976 977 Repeat(s, count) 978 979 return 980 } 981 982 // See Issue golang.org/issue/16237 983 func TestRepeatCatchesOverflow(t *testing.T) { 984 tests := [...]struct { 985 s string 986 count int 987 errStr string 988 }{ 989 0: {"--", -2147483647, "negative"}, 990 1: {"", int(^uint(0) >> 1), ""}, 991 2: {"-", 10, ""}, 992 3: {"gopher", 0, ""}, 993 4: {"-", -1, "negative"}, 994 5: {"--", -102, "negative"}, 995 6: {string(make([]byte, 255)), int((^uint(0))/255 + 1), "overflow"}, 996 } 997 998 for i, tt := range tests { 999 err := repeat(tt.s, tt.count) 1000 if tt.errStr == "" { 1001 if err != nil { 1002 t.Errorf("#%d panicked %v", i, err) 1003 } 1004 continue 1005 } 1006 1007 if err == nil || !Contains(err.Error(), tt.errStr) { 1008 t.Errorf("#%d expected %q got %q", i, tt.errStr, err) 1009 } 1010 } 1011 } 1012 1013 func runesEqual(a, b []rune) bool { 1014 if len(a) != len(b) { 1015 return false 1016 } 1017 for i, r := range a { 1018 if r != b[i] { 1019 return false 1020 } 1021 } 1022 return true 1023 } 1024 1025 var RunesTests = []struct { 1026 in string 1027 out []rune 1028 lossy bool 1029 }{ 1030 {"", []rune{}, false}, 1031 {" ", []rune{32}, false}, 1032 {"ABC", []rune{65, 66, 67}, false}, 1033 {"abc", []rune{97, 98, 99}, false}, 1034 {"\u65e5\u672c\u8a9e", []rune{26085, 26412, 35486}, false}, 1035 {"ab\x80c", []rune{97, 98, 0xFFFD, 99}, true}, 1036 {"ab\xc0c", []rune{97, 98, 0xFFFD, 99}, true}, 1037 } 1038 1039 func TestRunes(t *testing.T) { 1040 for _, tt := range RunesTests { 1041 a := []rune(tt.in) 1042 if !runesEqual(a, tt.out) { 1043 t.Errorf("[]rune(%q) = %v; want %v", tt.in, a, tt.out) 1044 continue 1045 } 1046 if !tt.lossy { 1047 // can only test reassembly if we didn't lose information 1048 s := string(a) 1049 if s != tt.in { 1050 t.Errorf("string([]rune(%q)) = %x; want %x", tt.in, s, tt.in) 1051 } 1052 } 1053 } 1054 } 1055 1056 func TestReadByte(t *testing.T) { 1057 testStrings := []string{"", abcd, faces, commas} 1058 for _, s := range testStrings { 1059 reader := NewReader(s) 1060 if e := reader.UnreadByte(); e == nil { 1061 t.Errorf("Unreading %q at beginning: expected error", s) 1062 } 1063 var res bytes.Buffer 1064 for { 1065 b, e := reader.ReadByte() 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.WriteByte(b) 1074 // unread and read again 1075 e = reader.UnreadByte() 1076 if e != nil { 1077 t.Errorf("Unreading %q: %s", s, e) 1078 break 1079 } 1080 b1, e := reader.ReadByte() 1081 if e != nil { 1082 t.Errorf("Reading %q after unreading: %s", s, e) 1083 break 1084 } 1085 if b1 != b { 1086 t.Errorf("Reading %q after unreading: want byte %q, got %q", s, b, b1) 1087 break 1088 } 1089 } 1090 if res.String() != s { 1091 t.Errorf("Reader(%q).ReadByte() produced %q", s, res.String()) 1092 } 1093 } 1094 } 1095 1096 func TestReadRune(t *testing.T) { 1097 testStrings := []string{"", abcd, faces, commas} 1098 for _, s := range testStrings { 1099 reader := NewReader(s) 1100 if e := reader.UnreadRune(); e == nil { 1101 t.Errorf("Unreading %q at beginning: expected error", s) 1102 } 1103 res := "" 1104 for { 1105 r, z, e := reader.ReadRune() 1106 if e == io.EOF { 1107 break 1108 } 1109 if e != nil { 1110 t.Errorf("Reading %q: %s", s, e) 1111 break 1112 } 1113 res += string(r) 1114 // unread and read again 1115 e = reader.UnreadRune() 1116 if e != nil { 1117 t.Errorf("Unreading %q: %s", s, e) 1118 break 1119 } 1120 r1, z1, e := reader.ReadRune() 1121 if e != nil { 1122 t.Errorf("Reading %q after unreading: %s", s, e) 1123 break 1124 } 1125 if r1 != r { 1126 t.Errorf("Reading %q after unreading: want rune %q, got %q", s, r, r1) 1127 break 1128 } 1129 if z1 != z { 1130 t.Errorf("Reading %q after unreading: want size %d, got %d", s, z, z1) 1131 break 1132 } 1133 } 1134 if res != s { 1135 t.Errorf("Reader(%q).ReadRune() produced %q", s, res) 1136 } 1137 } 1138 } 1139 1140 var UnreadRuneErrorTests = []struct { 1141 name string 1142 f func(*Reader) 1143 }{ 1144 {"Read", func(r *Reader) { r.Read([]byte{0}) }}, 1145 {"ReadByte", func(r *Reader) { r.ReadByte() }}, 1146 {"UnreadRune", func(r *Reader) { r.UnreadRune() }}, 1147 {"Seek", func(r *Reader) { r.Seek(0, io.SeekCurrent) }}, 1148 {"WriteTo", func(r *Reader) { r.WriteTo(&bytes.Buffer{}) }}, 1149 } 1150 1151 func TestUnreadRuneError(t *testing.T) { 1152 for _, tt := range UnreadRuneErrorTests { 1153 reader := NewReader("0123456789") 1154 if _, _, err := reader.ReadRune(); err != nil { 1155 // should not happen 1156 t.Fatal(err) 1157 } 1158 tt.f(reader) 1159 err := reader.UnreadRune() 1160 if err == nil { 1161 t.Errorf("Unreading after %s: expected error", tt.name) 1162 } 1163 } 1164 } 1165 1166 var ReplaceTests = []struct { 1167 in string 1168 old, new string 1169 n int 1170 out string 1171 }{ 1172 {"hello", "l", "L", 0, "hello"}, 1173 {"hello", "l", "L", -1, "heLLo"}, 1174 {"hello", "x", "X", -1, "hello"}, 1175 {"", "x", "X", -1, ""}, 1176 {"radar", "r", "<r>", -1, "<r>ada<r>"}, 1177 {"", "", "<>", -1, "<>"}, 1178 {"banana", "a", "<>", -1, "b<>n<>n<>"}, 1179 {"banana", "a", "<>", 1, "b<>nana"}, 1180 {"banana", "a", "<>", 1000, "b<>n<>n<>"}, 1181 {"banana", "an", "<>", -1, "b<><>a"}, 1182 {"banana", "ana", "<>", -1, "b<>na"}, 1183 {"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"}, 1184 {"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"}, 1185 {"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"}, 1186 {"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"}, 1187 {"banana", "", "<>", 1, "<>banana"}, 1188 {"banana", "a", "a", -1, "banana"}, 1189 {"banana", "a", "a", 1, "banana"}, 1190 {"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"}, 1191 } 1192 1193 func TestReplace(t *testing.T) { 1194 for _, tt := range ReplaceTests { 1195 if s := Replace(tt.in, tt.old, tt.new, tt.n); s != tt.out { 1196 t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out) 1197 } 1198 } 1199 } 1200 1201 var TitleTests = []struct { 1202 in, out string 1203 }{ 1204 {"", ""}, 1205 {"a", "A"}, 1206 {" aaa aaa aaa ", " Aaa Aaa Aaa "}, 1207 {" Aaa Aaa Aaa ", " Aaa Aaa Aaa "}, 1208 {"123a456", "123a456"}, 1209 {"double-blind", "Double-Blind"}, 1210 {"ÿøû", "Ÿøû"}, 1211 {"with_underscore", "With_underscore"}, 1212 {"unicode \xe2\x80\xa8 line separator", "Unicode \xe2\x80\xa8 Line Separator"}, 1213 } 1214 1215 func TestTitle(t *testing.T) { 1216 for _, tt := range TitleTests { 1217 if s := Title(tt.in); s != tt.out { 1218 t.Errorf("Title(%q) = %q, want %q", tt.in, s, tt.out) 1219 } 1220 } 1221 } 1222 1223 var ContainsTests = []struct { 1224 str, substr string 1225 expected bool 1226 }{ 1227 {"abc", "bc", true}, 1228 {"abc", "bcd", false}, 1229 {"abc", "", true}, 1230 {"", "a", false}, 1231 1232 // cases to cover code in runtime/asm_amd64.s:indexShortStr 1233 // 2-byte needle 1234 {"xxxxxx", "01", false}, 1235 {"01xxxx", "01", true}, 1236 {"xx01xx", "01", true}, 1237 {"xxxx01", "01", true}, 1238 {"01xxxxx"[1:], "01", false}, 1239 {"xxxxx01"[:6], "01", false}, 1240 // 3-byte needle 1241 {"xxxxxxx", "012", false}, 1242 {"012xxxx", "012", true}, 1243 {"xx012xx", "012", true}, 1244 {"xxxx012", "012", true}, 1245 {"012xxxxx"[1:], "012", false}, 1246 {"xxxxx012"[:7], "012", false}, 1247 // 4-byte needle 1248 {"xxxxxxxx", "0123", false}, 1249 {"0123xxxx", "0123", true}, 1250 {"xx0123xx", "0123", true}, 1251 {"xxxx0123", "0123", true}, 1252 {"0123xxxxx"[1:], "0123", false}, 1253 {"xxxxx0123"[:8], "0123", false}, 1254 // 5-7-byte needle 1255 {"xxxxxxxxx", "01234", false}, 1256 {"01234xxxx", "01234", true}, 1257 {"xx01234xx", "01234", true}, 1258 {"xxxx01234", "01234", true}, 1259 {"01234xxxxx"[1:], "01234", false}, 1260 {"xxxxx01234"[:9], "01234", false}, 1261 // 8-byte needle 1262 {"xxxxxxxxxxxx", "01234567", false}, 1263 {"01234567xxxx", "01234567", true}, 1264 {"xx01234567xx", "01234567", true}, 1265 {"xxxx01234567", "01234567", true}, 1266 {"01234567xxxxx"[1:], "01234567", false}, 1267 {"xxxxx01234567"[:12], "01234567", false}, 1268 // 9-15-byte needle 1269 {"xxxxxxxxxxxxx", "012345678", false}, 1270 {"012345678xxxx", "012345678", true}, 1271 {"xx012345678xx", "012345678", true}, 1272 {"xxxx012345678", "012345678", true}, 1273 {"012345678xxxxx"[1:], "012345678", false}, 1274 {"xxxxx012345678"[:13], "012345678", false}, 1275 // 16-byte needle 1276 {"xxxxxxxxxxxxxxxxxxxx", "0123456789ABCDEF", false}, 1277 {"0123456789ABCDEFxxxx", "0123456789ABCDEF", true}, 1278 {"xx0123456789ABCDEFxx", "0123456789ABCDEF", true}, 1279 {"xxxx0123456789ABCDEF", "0123456789ABCDEF", true}, 1280 {"0123456789ABCDEFxxxxx"[1:], "0123456789ABCDEF", false}, 1281 {"xxxxx0123456789ABCDEF"[:20], "0123456789ABCDEF", false}, 1282 // 17-31-byte needle 1283 {"xxxxxxxxxxxxxxxxxxxxx", "0123456789ABCDEFG", false}, 1284 {"0123456789ABCDEFGxxxx", "0123456789ABCDEFG", true}, 1285 {"xx0123456789ABCDEFGxx", "0123456789ABCDEFG", true}, 1286 {"xxxx0123456789ABCDEFG", "0123456789ABCDEFG", true}, 1287 {"0123456789ABCDEFGxxxxx"[1:], "0123456789ABCDEFG", false}, 1288 {"xxxxx0123456789ABCDEFG"[:21], "0123456789ABCDEFG", false}, 1289 1290 // partial match cases 1291 {"xx01x", "012", false}, // 3 1292 {"xx0123x", "01234", false}, // 5-7 1293 {"xx01234567x", "012345678", false}, // 9-15 1294 {"xx0123456789ABCDEFx", "0123456789ABCDEFG", false}, // 17-31, issue 15679 1295 } 1296 1297 func TestContains(t *testing.T) { 1298 for _, ct := range ContainsTests { 1299 if Contains(ct.str, ct.substr) != ct.expected { 1300 t.Errorf("Contains(%s, %s) = %v, want %v", 1301 ct.str, ct.substr, !ct.expected, ct.expected) 1302 } 1303 } 1304 } 1305 1306 var ContainsAnyTests = []struct { 1307 str, substr string 1308 expected bool 1309 }{ 1310 {"", "", false}, 1311 {"", "a", false}, 1312 {"", "abc", false}, 1313 {"a", "", false}, 1314 {"a", "a", true}, 1315 {"aaa", "a", true}, 1316 {"abc", "xyz", false}, 1317 {"abc", "xcz", true}, 1318 {"a☺b☻c☹d", "uvw☻xyz", true}, 1319 {"aRegExp*", ".(|)*+?^$[]", true}, 1320 {dots + dots + dots, " ", false}, 1321 } 1322 1323 func TestContainsAny(t *testing.T) { 1324 for _, ct := range ContainsAnyTests { 1325 if ContainsAny(ct.str, ct.substr) != ct.expected { 1326 t.Errorf("ContainsAny(%s, %s) = %v, want %v", 1327 ct.str, ct.substr, !ct.expected, ct.expected) 1328 } 1329 } 1330 } 1331 1332 var ContainsRuneTests = []struct { 1333 str string 1334 r rune 1335 expected bool 1336 }{ 1337 {"", 'a', false}, 1338 {"a", 'a', true}, 1339 {"aaa", 'a', true}, 1340 {"abc", 'y', false}, 1341 {"abc", 'c', true}, 1342 {"a☺b☻c☹d", 'x', false}, 1343 {"a☺b☻c☹d", '☻', true}, 1344 {"aRegExp*", '*', true}, 1345 } 1346 1347 func TestContainsRune(t *testing.T) { 1348 for _, ct := range ContainsRuneTests { 1349 if ContainsRune(ct.str, ct.r) != ct.expected { 1350 t.Errorf("ContainsRune(%q, %q) = %v, want %v", 1351 ct.str, ct.r, !ct.expected, ct.expected) 1352 } 1353 } 1354 } 1355 1356 var EqualFoldTests = []struct { 1357 s, t string 1358 out bool 1359 }{ 1360 {"abc", "abc", true}, 1361 {"ABcd", "ABcd", true}, 1362 {"123abc", "123ABC", true}, 1363 {"αβδ", "ΑΒΔ", true}, 1364 {"abc", "xyz", false}, 1365 {"abc", "XYZ", false}, 1366 {"abcdefghijk", "abcdefghijX", false}, 1367 {"abcdefghijk", "abcdefghij\u212A", true}, 1368 {"abcdefghijK", "abcdefghij\u212A", true}, 1369 {"abcdefghijkz", "abcdefghij\u212Ay", false}, 1370 {"abcdefghijKz", "abcdefghij\u212Ay", false}, 1371 } 1372 1373 func TestEqualFold(t *testing.T) { 1374 for _, tt := range EqualFoldTests { 1375 if out := EqualFold(tt.s, tt.t); out != tt.out { 1376 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.s, tt.t, out, tt.out) 1377 } 1378 if out := EqualFold(tt.t, tt.s); out != tt.out { 1379 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.t, tt.s, out, tt.out) 1380 } 1381 } 1382 } 1383 1384 var CountTests = []struct { 1385 s, sep string 1386 num int 1387 }{ 1388 {"", "", 1}, 1389 {"", "notempty", 0}, 1390 {"notempty", "", 9}, 1391 {"smaller", "not smaller", 0}, 1392 {"12345678987654321", "6", 2}, 1393 {"611161116", "6", 3}, 1394 {"notequal", "NotEqual", 0}, 1395 {"equal", "equal", 1}, 1396 {"abc1231231123q", "123", 3}, 1397 {"11111", "11", 2}, 1398 } 1399 1400 func TestCount(t *testing.T) { 1401 for _, tt := range CountTests { 1402 if num := Count(tt.s, tt.sep); num != tt.num { 1403 t.Errorf("Count(\"%s\", \"%s\") = %d, want %d", tt.s, tt.sep, num, tt.num) 1404 } 1405 } 1406 } 1407 1408 func makeBenchInputHard() string { 1409 tokens := [...]string{ 1410 "<a>", "<p>", "<b>", "<strong>", 1411 "</a>", "</p>", "</b>", "</strong>", 1412 "hello", "world", 1413 } 1414 x := make([]byte, 0, 1<<20) 1415 for { 1416 i := rand.Intn(len(tokens)) 1417 if len(x)+len(tokens[i]) >= 1<<20 { 1418 break 1419 } 1420 x = append(x, tokens[i]...) 1421 } 1422 return string(x) 1423 } 1424 1425 var benchInputHard = makeBenchInputHard() 1426 1427 func benchmarkIndexHard(b *testing.B, sep string) { 1428 for i := 0; i < b.N; i++ { 1429 Index(benchInputHard, sep) 1430 } 1431 } 1432 1433 func benchmarkLastIndexHard(b *testing.B, sep string) { 1434 for i := 0; i < b.N; i++ { 1435 LastIndex(benchInputHard, sep) 1436 } 1437 } 1438 1439 func benchmarkCountHard(b *testing.B, sep string) { 1440 for i := 0; i < b.N; i++ { 1441 Count(benchInputHard, sep) 1442 } 1443 } 1444 1445 func BenchmarkIndexHard1(b *testing.B) { benchmarkIndexHard(b, "<>") } 1446 func BenchmarkIndexHard2(b *testing.B) { benchmarkIndexHard(b, "</pre>") } 1447 func BenchmarkIndexHard3(b *testing.B) { benchmarkIndexHard(b, "<b>hello world</b>") } 1448 func BenchmarkIndexHard4(b *testing.B) { 1449 benchmarkIndexHard(b, "<pre><b>hello</b><strong>world</strong></pre>") 1450 } 1451 1452 func BenchmarkLastIndexHard1(b *testing.B) { benchmarkLastIndexHard(b, "<>") } 1453 func BenchmarkLastIndexHard2(b *testing.B) { benchmarkLastIndexHard(b, "</pre>") } 1454 func BenchmarkLastIndexHard3(b *testing.B) { benchmarkLastIndexHard(b, "<b>hello world</b>") } 1455 1456 func BenchmarkCountHard1(b *testing.B) { benchmarkCountHard(b, "<>") } 1457 func BenchmarkCountHard2(b *testing.B) { benchmarkCountHard(b, "</pre>") } 1458 func BenchmarkCountHard3(b *testing.B) { benchmarkCountHard(b, "<b>hello world</b>") } 1459 1460 var benchInputTorture = Repeat("ABC", 1<<10) + "123" + Repeat("ABC", 1<<10) 1461 var benchNeedleTorture = Repeat("ABC", 1<<10+1) 1462 1463 func BenchmarkIndexTorture(b *testing.B) { 1464 for i := 0; i < b.N; i++ { 1465 Index(benchInputTorture, benchNeedleTorture) 1466 } 1467 } 1468 1469 func BenchmarkCountTorture(b *testing.B) { 1470 for i := 0; i < b.N; i++ { 1471 Count(benchInputTorture, benchNeedleTorture) 1472 } 1473 } 1474 1475 func BenchmarkCountTortureOverlapping(b *testing.B) { 1476 A := Repeat("ABC", 1<<20) 1477 B := Repeat("ABC", 1<<10) 1478 for i := 0; i < b.N; i++ { 1479 Count(A, B) 1480 } 1481 } 1482 1483 func BenchmarkCountByte(b *testing.B) { 1484 indexSizes := []int{10, 32, 4 << 10, 4 << 20, 64 << 20} 1485 benchStr := Repeat(benchmarkString, 1486 (indexSizes[len(indexSizes)-1]+len(benchmarkString)-1)/len(benchmarkString)) 1487 benchFunc := func(b *testing.B, benchStr string) { 1488 b.SetBytes(int64(len(benchStr))) 1489 for i := 0; i < b.N; i++ { 1490 Count(benchStr, "=") 1491 } 1492 } 1493 for _, size := range indexSizes { 1494 b.Run(fmt.Sprintf("%d", size), func(b *testing.B) { 1495 benchFunc(b, benchStr[:size]) 1496 }) 1497 } 1498 1499 } 1500 1501 var makeFieldsInput = func() string { 1502 x := make([]byte, 1<<20) 1503 // Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space. 1504 for i := range x { 1505 switch rand.Intn(10) { 1506 case 0: 1507 x[i] = ' ' 1508 case 1: 1509 if i > 0 && x[i-1] == 'x' { 1510 copy(x[i-1:], "χ") 1511 break 1512 } 1513 fallthrough 1514 default: 1515 x[i] = 'x' 1516 } 1517 } 1518 return string(x) 1519 } 1520 1521 var makeFieldsInputASCII = func() string { 1522 x := make([]byte, 1<<20) 1523 // Input is ~10% space, rest ASCII non-space. 1524 for i := range x { 1525 if rand.Intn(10) == 0 { 1526 x[i] = ' ' 1527 } else { 1528 x[i] = 'x' 1529 } 1530 } 1531 return string(x) 1532 } 1533 1534 var stringdata = []struct{ name, data string }{ 1535 {"ASCII", makeFieldsInputASCII()}, 1536 {"Mixed", makeFieldsInput()}, 1537 } 1538 1539 func BenchmarkFields(b *testing.B) { 1540 for _, sd := range stringdata { 1541 b.Run(sd.name, func(b *testing.B) { 1542 for j := 1 << 4; j <= 1<<20; j <<= 4 { 1543 b.Run(fmt.Sprintf("%d", j), func(b *testing.B) { 1544 b.ReportAllocs() 1545 b.SetBytes(int64(j)) 1546 data := sd.data[:j] 1547 for i := 0; i < b.N; i++ { 1548 Fields(data) 1549 } 1550 }) 1551 } 1552 }) 1553 } 1554 } 1555 1556 func BenchmarkFieldsFunc(b *testing.B) { 1557 for _, sd := range stringdata { 1558 b.Run(sd.name, func(b *testing.B) { 1559 for j := 1 << 4; j <= 1<<20; j <<= 4 { 1560 b.Run(fmt.Sprintf("%d", j), func(b *testing.B) { 1561 b.ReportAllocs() 1562 b.SetBytes(int64(j)) 1563 data := sd.data[:j] 1564 for i := 0; i < b.N; i++ { 1565 FieldsFunc(data, unicode.IsSpace) 1566 } 1567 }) 1568 } 1569 }) 1570 } 1571 } 1572 1573 func BenchmarkSplitEmptySeparator(b *testing.B) { 1574 for i := 0; i < b.N; i++ { 1575 Split(benchInputHard, "") 1576 } 1577 } 1578 1579 func BenchmarkSplitSingleByteSeparator(b *testing.B) { 1580 for i := 0; i < b.N; i++ { 1581 Split(benchInputHard, "/") 1582 } 1583 } 1584 1585 func BenchmarkSplitMultiByteSeparator(b *testing.B) { 1586 for i := 0; i < b.N; i++ { 1587 Split(benchInputHard, "hello") 1588 } 1589 } 1590 1591 func BenchmarkSplitNSingleByteSeparator(b *testing.B) { 1592 for i := 0; i < b.N; i++ { 1593 SplitN(benchInputHard, "/", 10) 1594 } 1595 } 1596 1597 func BenchmarkSplitNMultiByteSeparator(b *testing.B) { 1598 for i := 0; i < b.N; i++ { 1599 SplitN(benchInputHard, "hello", 10) 1600 } 1601 } 1602 1603 func BenchmarkRepeat(b *testing.B) { 1604 for i := 0; i < b.N; i++ { 1605 Repeat("-", 80) 1606 } 1607 } 1608 1609 func BenchmarkIndexAnyASCII(b *testing.B) { 1610 x := Repeat("#", 4096) // Never matches set 1611 cs := "0123456789abcdef" 1612 for k := 1; k <= 4096; k <<= 4 { 1613 for j := 1; j <= 16; j <<= 1 { 1614 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) { 1615 for i := 0; i < b.N; i++ { 1616 IndexAny(x[:k], cs[:j]) 1617 } 1618 }) 1619 } 1620 } 1621 } 1622 1623 func BenchmarkTrimASCII(b *testing.B) { 1624 cs := "0123456789abcdef" 1625 for k := 1; k <= 4096; k <<= 4 { 1626 for j := 1; j <= 16; j <<= 1 { 1627 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) { 1628 x := Repeat(cs[:j], k) // Always matches set 1629 for i := 0; i < b.N; i++ { 1630 Trim(x[:k], cs[:j]) 1631 } 1632 }) 1633 } 1634 } 1635 }