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