github.com/AESNooper/go/src@v0.0.0-20220218095104-b56a4ab1bbbb/bytes/bytes_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 bytes_test 6 7 import ( 8 . "bytes" 9 "fmt" 10 "internal/testenv" 11 "math/rand" 12 "reflect" 13 "strings" 14 "testing" 15 "unicode" 16 "unicode/utf8" 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 func sliceOfString(s [][]byte) []string { 32 result := make([]string, len(s)) 33 for i, v := range s { 34 result[i] = string(v) 35 } 36 return result 37 } 38 39 // For ease of reading, the test cases use strings that are converted to byte 40 // slices before invoking the functions. 41 42 var abcd = "abcd" 43 var faces = "☺☻☹" 44 var commas = "1,2,3,4" 45 var dots = "1....2....3....4" 46 47 type BinOpTest struct { 48 a string 49 b string 50 i int 51 } 52 53 func TestEqual(t *testing.T) { 54 // Run the tests and check for allocation at the same time. 55 allocs := testing.AllocsPerRun(10, func() { 56 for _, tt := range compareTests { 57 eql := Equal(tt.a, tt.b) 58 if eql != (tt.i == 0) { 59 t.Errorf(`Equal(%q, %q) = %v`, tt.a, tt.b, eql) 60 } 61 } 62 }) 63 if allocs > 0 { 64 t.Errorf("Equal allocated %v times", allocs) 65 } 66 } 67 68 func TestEqualExhaustive(t *testing.T) { 69 var size = 128 70 if testing.Short() { 71 size = 32 72 } 73 a := make([]byte, size) 74 b := make([]byte, size) 75 b_init := make([]byte, size) 76 // randomish but deterministic data 77 for i := 0; i < size; i++ { 78 a[i] = byte(17 * i) 79 b_init[i] = byte(23*i + 100) 80 } 81 82 for len := 0; len <= size; len++ { 83 for x := 0; x <= size-len; x++ { 84 for y := 0; y <= size-len; y++ { 85 copy(b, b_init) 86 copy(b[y:y+len], a[x:x+len]) 87 if !Equal(a[x:x+len], b[y:y+len]) || !Equal(b[y:y+len], a[x:x+len]) { 88 t.Errorf("Equal(%d, %d, %d) = false", len, x, y) 89 } 90 } 91 } 92 } 93 } 94 95 // make sure Equal returns false for minimally different strings. The data 96 // is all zeros except for a single one in one location. 97 func TestNotEqual(t *testing.T) { 98 var size = 128 99 if testing.Short() { 100 size = 32 101 } 102 a := make([]byte, size) 103 b := make([]byte, size) 104 105 for len := 0; len <= size; len++ { 106 for x := 0; x <= size-len; x++ { 107 for y := 0; y <= size-len; y++ { 108 for diffpos := x; diffpos < x+len; diffpos++ { 109 a[diffpos] = 1 110 if Equal(a[x:x+len], b[y:y+len]) || Equal(b[y:y+len], a[x:x+len]) { 111 t.Errorf("NotEqual(%d, %d, %d, %d) = true", len, x, y, diffpos) 112 } 113 a[diffpos] = 0 114 } 115 } 116 } 117 } 118 } 119 120 var indexTests = []BinOpTest{ 121 {"", "", 0}, 122 {"", "a", -1}, 123 {"", "foo", -1}, 124 {"fo", "foo", -1}, 125 {"foo", "baz", -1}, 126 {"foo", "foo", 0}, 127 {"oofofoofooo", "f", 2}, 128 {"oofofoofooo", "foo", 4}, 129 {"barfoobarfoo", "foo", 3}, 130 {"foo", "", 0}, 131 {"foo", "o", 1}, 132 {"abcABCabc", "A", 3}, 133 // cases with one byte strings - test IndexByte and special case in Index() 134 {"", "a", -1}, 135 {"x", "a", -1}, 136 {"x", "x", 0}, 137 {"abc", "a", 0}, 138 {"abc", "b", 1}, 139 {"abc", "c", 2}, 140 {"abc", "x", -1}, 141 {"barfoobarfooyyyzzzyyyzzzyyyzzzyyyxxxzzzyyy", "x", 33}, 142 {"foofyfoobarfoobar", "y", 4}, 143 {"oooooooooooooooooooooo", "r", -1}, 144 {"oxoxoxoxoxoxoxoxoxoxoxoy", "oy", 22}, 145 {"oxoxoxoxoxoxoxoxoxoxoxox", "oy", -1}, 146 // test fallback to Rabin-Karp. 147 {"000000000000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000001", 5}, 148 } 149 150 var lastIndexTests = []BinOpTest{ 151 {"", "", 0}, 152 {"", "a", -1}, 153 {"", "foo", -1}, 154 {"fo", "foo", -1}, 155 {"foo", "foo", 0}, 156 {"foo", "f", 0}, 157 {"oofofoofooo", "f", 7}, 158 {"oofofoofooo", "foo", 7}, 159 {"barfoobarfoo", "foo", 9}, 160 {"foo", "", 3}, 161 {"foo", "o", 2}, 162 {"abcABCabc", "A", 3}, 163 {"abcABCabc", "a", 6}, 164 } 165 166 var indexAnyTests = []BinOpTest{ 167 {"", "", -1}, 168 {"", "a", -1}, 169 {"", "abc", -1}, 170 {"a", "", -1}, 171 {"a", "a", 0}, 172 {"\x80", "\xffb", 0}, 173 {"aaa", "a", 0}, 174 {"abc", "xyz", -1}, 175 {"abc", "xcz", 2}, 176 {"ab☺c", "x☺yz", 2}, 177 {"a☺b☻c☹d", "cx", len("a☺b☻")}, 178 {"a☺b☻c☹d", "uvw☻xyz", len("a☺b")}, 179 {"aRegExp*", ".(|)*+?^$[]", 7}, 180 {dots + dots + dots, " ", -1}, 181 {"012abcba210", "\xffb", 4}, 182 {"012\x80bcb\x80210", "\xffb", 3}, 183 {"0123456\xcf\x80abc", "\xcfb\x80", 10}, 184 } 185 186 var lastIndexAnyTests = []BinOpTest{ 187 {"", "", -1}, 188 {"", "a", -1}, 189 {"", "abc", -1}, 190 {"a", "", -1}, 191 {"a", "a", 0}, 192 {"\x80", "\xffb", 0}, 193 {"aaa", "a", 2}, 194 {"abc", "xyz", -1}, 195 {"abc", "ab", 1}, 196 {"ab☺c", "x☺yz", 2}, 197 {"a☺b☻c☹d", "cx", len("a☺b☻")}, 198 {"a☺b☻c☹d", "uvw☻xyz", len("a☺b")}, 199 {"a.RegExp*", ".(|)*+?^$[]", 8}, 200 {dots + dots + dots, " ", -1}, 201 {"012abcba210", "\xffb", 6}, 202 {"012\x80bcb\x80210", "\xffb", 7}, 203 {"0123456\xcf\x80abc", "\xcfb\x80", 10}, 204 } 205 206 // Execute f on each test case. funcName should be the name of f; it's used 207 // in failure reports. 208 func runIndexTests(t *testing.T, f func(s, sep []byte) int, funcName string, testCases []BinOpTest) { 209 for _, test := range testCases { 210 a := []byte(test.a) 211 b := []byte(test.b) 212 actual := f(a, b) 213 if actual != test.i { 214 t.Errorf("%s(%q,%q) = %v; want %v", funcName, a, b, actual, test.i) 215 } 216 } 217 var allocTests = []struct { 218 a []byte 219 b []byte 220 i int 221 }{ 222 // case for function Index. 223 {[]byte("000000000000000000000000000000000000000000000000000000000000000000000001"), []byte("0000000000000000000000000000000000000000000000000000000000000000001"), 5}, 224 // case for function LastIndex. 225 {[]byte("000000000000000000000000000000000000000000000000000000000000000010000"), []byte("00000000000000000000000000000000000000000000000000000000000001"), 3}, 226 } 227 allocs := testing.AllocsPerRun(100, func() { 228 if i := Index(allocTests[1].a, allocTests[1].b); i != allocTests[1].i { 229 t.Errorf("Index([]byte(%q), []byte(%q)) = %v; want %v", allocTests[1].a, allocTests[1].b, i, allocTests[1].i) 230 } 231 if i := LastIndex(allocTests[0].a, allocTests[0].b); i != allocTests[0].i { 232 t.Errorf("LastIndex([]byte(%q), []byte(%q)) = %v; want %v", allocTests[0].a, allocTests[0].b, i, allocTests[0].i) 233 } 234 }) 235 if allocs != 0 { 236 t.Errorf("expected no allocations, got %f", allocs) 237 } 238 } 239 240 func runIndexAnyTests(t *testing.T, f func(s []byte, chars string) int, funcName string, testCases []BinOpTest) { 241 for _, test := range testCases { 242 a := []byte(test.a) 243 actual := f(a, test.b) 244 if actual != test.i { 245 t.Errorf("%s(%q,%q) = %v; want %v", funcName, a, test.b, actual, test.i) 246 } 247 } 248 } 249 250 func TestIndex(t *testing.T) { runIndexTests(t, Index, "Index", indexTests) } 251 func TestLastIndex(t *testing.T) { runIndexTests(t, LastIndex, "LastIndex", lastIndexTests) } 252 func TestIndexAny(t *testing.T) { runIndexAnyTests(t, IndexAny, "IndexAny", indexAnyTests) } 253 func TestLastIndexAny(t *testing.T) { 254 runIndexAnyTests(t, LastIndexAny, "LastIndexAny", lastIndexAnyTests) 255 } 256 257 func TestIndexByte(t *testing.T) { 258 for _, tt := range indexTests { 259 if len(tt.b) != 1 { 260 continue 261 } 262 a := []byte(tt.a) 263 b := tt.b[0] 264 pos := IndexByte(a, b) 265 if pos != tt.i { 266 t.Errorf(`IndexByte(%q, '%c') = %v`, tt.a, b, pos) 267 } 268 posp := IndexBytePortable(a, b) 269 if posp != tt.i { 270 t.Errorf(`indexBytePortable(%q, '%c') = %v`, tt.a, b, posp) 271 } 272 } 273 } 274 275 func TestLastIndexByte(t *testing.T) { 276 testCases := []BinOpTest{ 277 {"", "q", -1}, 278 {"abcdef", "q", -1}, 279 {"abcdefabcdef", "a", len("abcdef")}, // something in the middle 280 {"abcdefabcdef", "f", len("abcdefabcde")}, // last byte 281 {"zabcdefabcdef", "z", 0}, // first byte 282 {"a☺b☻c☹d", "b", len("a☺")}, // non-ascii 283 } 284 for _, test := range testCases { 285 actual := LastIndexByte([]byte(test.a), test.b[0]) 286 if actual != test.i { 287 t.Errorf("LastIndexByte(%q,%c) = %v; want %v", test.a, test.b[0], actual, test.i) 288 } 289 } 290 } 291 292 // test a larger buffer with different sizes and alignments 293 func TestIndexByteBig(t *testing.T) { 294 var n = 1024 295 if testing.Short() { 296 n = 128 297 } 298 b := make([]byte, n) 299 for i := 0; i < n; i++ { 300 // different start alignments 301 b1 := b[i:] 302 for j := 0; j < len(b1); j++ { 303 b1[j] = 'x' 304 pos := IndexByte(b1, 'x') 305 if pos != j { 306 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos) 307 } 308 b1[j] = 0 309 pos = IndexByte(b1, 'x') 310 if pos != -1 { 311 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos) 312 } 313 } 314 // different end alignments 315 b1 = b[:i] 316 for j := 0; j < len(b1); j++ { 317 b1[j] = 'x' 318 pos := IndexByte(b1, 'x') 319 if pos != j { 320 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos) 321 } 322 b1[j] = 0 323 pos = IndexByte(b1, 'x') 324 if pos != -1 { 325 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos) 326 } 327 } 328 // different start and end alignments 329 b1 = b[i/2 : n-(i+1)/2] 330 for j := 0; j < len(b1); j++ { 331 b1[j] = 'x' 332 pos := IndexByte(b1, 'x') 333 if pos != j { 334 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos) 335 } 336 b1[j] = 0 337 pos = IndexByte(b1, 'x') 338 if pos != -1 { 339 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos) 340 } 341 } 342 } 343 } 344 345 // test a small index across all page offsets 346 func TestIndexByteSmall(t *testing.T) { 347 b := make([]byte, 5015) // bigger than a page 348 // Make sure we find the correct byte even when straddling a page. 349 for i := 0; i <= len(b)-15; i++ { 350 for j := 0; j < 15; j++ { 351 b[i+j] = byte(100 + j) 352 } 353 for j := 0; j < 15; j++ { 354 p := IndexByte(b[i:i+15], byte(100+j)) 355 if p != j { 356 t.Errorf("IndexByte(%q, %d) = %d", b[i:i+15], 100+j, p) 357 } 358 } 359 for j := 0; j < 15; j++ { 360 b[i+j] = 0 361 } 362 } 363 // Make sure matches outside the slice never trigger. 364 for i := 0; i <= len(b)-15; i++ { 365 for j := 0; j < 15; j++ { 366 b[i+j] = 1 367 } 368 for j := 0; j < 15; j++ { 369 p := IndexByte(b[i:i+15], byte(0)) 370 if p != -1 { 371 t.Errorf("IndexByte(%q, %d) = %d", b[i:i+15], 0, p) 372 } 373 } 374 for j := 0; j < 15; j++ { 375 b[i+j] = 0 376 } 377 } 378 } 379 380 func TestIndexRune(t *testing.T) { 381 tests := []struct { 382 in string 383 rune rune 384 want int 385 }{ 386 {"", 'a', -1}, 387 {"", '☺', -1}, 388 {"foo", '☹', -1}, 389 {"foo", 'o', 1}, 390 {"foo☺bar", '☺', 3}, 391 {"foo☺☻☹bar", '☹', 9}, 392 {"a A x", 'A', 2}, 393 {"some_text=some_value", '=', 9}, 394 {"☺a", 'a', 3}, 395 {"a☻☺b", '☺', 4}, 396 397 // RuneError should match any invalid UTF-8 byte sequence. 398 {"�", '�', 0}, 399 {"\xff", '�', 0}, 400 {"☻x�", '�', len("☻x")}, 401 {"☻x\xe2\x98", '�', len("☻x")}, 402 {"☻x\xe2\x98�", '�', len("☻x")}, 403 {"☻x\xe2\x98x", '�', len("☻x")}, 404 405 // Invalid rune values should never match. 406 {"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", -1, -1}, 407 {"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", 0xD800, -1}, // Surrogate pair 408 {"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", utf8.MaxRune + 1, -1}, 409 } 410 for _, tt := range tests { 411 if got := IndexRune([]byte(tt.in), tt.rune); got != tt.want { 412 t.Errorf("IndexRune(%q, %d) = %v; want %v", tt.in, tt.rune, got, tt.want) 413 } 414 } 415 416 haystack := []byte("test世界") 417 allocs := testing.AllocsPerRun(1000, func() { 418 if i := IndexRune(haystack, 's'); i != 2 { 419 t.Fatalf("'s' at %d; want 2", i) 420 } 421 if i := IndexRune(haystack, '世'); i != 4 { 422 t.Fatalf("'世' at %d; want 4", i) 423 } 424 }) 425 if allocs != 0 { 426 t.Errorf("expected no allocations, got %f", allocs) 427 } 428 } 429 430 // test count of a single byte across page offsets 431 func TestCountByte(t *testing.T) { 432 b := make([]byte, 5015) // bigger than a page 433 windows := []int{1, 2, 3, 4, 15, 16, 17, 31, 32, 33, 63, 64, 65, 128} 434 testCountWindow := func(i, window int) { 435 for j := 0; j < window; j++ { 436 b[i+j] = byte(100) 437 p := Count(b[i:i+window], []byte{100}) 438 if p != j+1 { 439 t.Errorf("TestCountByte.Count(%q, 100) = %d", b[i:i+window], p) 440 } 441 } 442 } 443 444 maxWnd := windows[len(windows)-1] 445 446 for i := 0; i <= 2*maxWnd; i++ { 447 for _, window := range windows { 448 if window > len(b[i:]) { 449 window = len(b[i:]) 450 } 451 testCountWindow(i, window) 452 for j := 0; j < window; j++ { 453 b[i+j] = byte(0) 454 } 455 } 456 } 457 for i := 4096 - (maxWnd + 1); i < len(b); i++ { 458 for _, window := range windows { 459 if window > len(b[i:]) { 460 window = len(b[i:]) 461 } 462 testCountWindow(i, window) 463 for j := 0; j < window; j++ { 464 b[i+j] = byte(0) 465 } 466 } 467 } 468 } 469 470 // Make sure we don't count bytes outside our window 471 func TestCountByteNoMatch(t *testing.T) { 472 b := make([]byte, 5015) 473 windows := []int{1, 2, 3, 4, 15, 16, 17, 31, 32, 33, 63, 64, 65, 128} 474 for i := 0; i <= len(b); i++ { 475 for _, window := range windows { 476 if window > len(b[i:]) { 477 window = len(b[i:]) 478 } 479 // Fill the window with non-match 480 for j := 0; j < window; j++ { 481 b[i+j] = byte(100) 482 } 483 // Try to find something that doesn't exist 484 p := Count(b[i:i+window], []byte{0}) 485 if p != 0 { 486 t.Errorf("TestCountByteNoMatch(%q, 0) = %d", b[i:i+window], p) 487 } 488 for j := 0; j < window; j++ { 489 b[i+j] = byte(0) 490 } 491 } 492 } 493 } 494 495 var bmbuf []byte 496 497 func valName(x int) string { 498 if s := x >> 20; s<<20 == x { 499 return fmt.Sprintf("%dM", s) 500 } 501 if s := x >> 10; s<<10 == x { 502 return fmt.Sprintf("%dK", s) 503 } 504 return fmt.Sprint(x) 505 } 506 507 func benchBytes(b *testing.B, sizes []int, f func(b *testing.B, n int)) { 508 for _, n := range sizes { 509 if isRaceBuilder && n > 4<<10 { 510 continue 511 } 512 b.Run(valName(n), func(b *testing.B) { 513 if len(bmbuf) < n { 514 bmbuf = make([]byte, n) 515 } 516 b.SetBytes(int64(n)) 517 f(b, n) 518 }) 519 } 520 } 521 522 var indexSizes = []int{10, 32, 4 << 10, 4 << 20, 64 << 20} 523 524 var isRaceBuilder = strings.HasSuffix(testenv.Builder(), "-race") 525 526 func BenchmarkIndexByte(b *testing.B) { 527 benchBytes(b, indexSizes, bmIndexByte(IndexByte)) 528 } 529 530 func BenchmarkIndexBytePortable(b *testing.B) { 531 benchBytes(b, indexSizes, bmIndexByte(IndexBytePortable)) 532 } 533 534 func bmIndexByte(index func([]byte, byte) int) func(b *testing.B, n int) { 535 return func(b *testing.B, n int) { 536 buf := bmbuf[0:n] 537 buf[n-1] = 'x' 538 for i := 0; i < b.N; i++ { 539 j := index(buf, 'x') 540 if j != n-1 { 541 b.Fatal("bad index", j) 542 } 543 } 544 buf[n-1] = '\x00' 545 } 546 } 547 548 func BenchmarkIndexRune(b *testing.B) { 549 benchBytes(b, indexSizes, bmIndexRune(IndexRune)) 550 } 551 552 func BenchmarkIndexRuneASCII(b *testing.B) { 553 benchBytes(b, indexSizes, bmIndexRuneASCII(IndexRune)) 554 } 555 556 func bmIndexRuneASCII(index func([]byte, rune) int) func(b *testing.B, n int) { 557 return func(b *testing.B, n int) { 558 buf := bmbuf[0:n] 559 buf[n-1] = 'x' 560 for i := 0; i < b.N; i++ { 561 j := index(buf, 'x') 562 if j != n-1 { 563 b.Fatal("bad index", j) 564 } 565 } 566 buf[n-1] = '\x00' 567 } 568 } 569 570 func bmIndexRune(index func([]byte, rune) int) func(b *testing.B, n int) { 571 return func(b *testing.B, n int) { 572 buf := bmbuf[0:n] 573 utf8.EncodeRune(buf[n-3:], '世') 574 for i := 0; i < b.N; i++ { 575 j := index(buf, '世') 576 if j != n-3 { 577 b.Fatal("bad index", j) 578 } 579 } 580 buf[n-3] = '\x00' 581 buf[n-2] = '\x00' 582 buf[n-1] = '\x00' 583 } 584 } 585 586 func BenchmarkEqual(b *testing.B) { 587 b.Run("0", func(b *testing.B) { 588 var buf [4]byte 589 buf1 := buf[0:0] 590 buf2 := buf[1:1] 591 for i := 0; i < b.N; i++ { 592 eq := Equal(buf1, buf2) 593 if !eq { 594 b.Fatal("bad equal") 595 } 596 } 597 }) 598 599 sizes := []int{1, 6, 9, 15, 16, 20, 32, 4 << 10, 4 << 20, 64 << 20} 600 benchBytes(b, sizes, bmEqual(Equal)) 601 } 602 603 func bmEqual(equal func([]byte, []byte) bool) func(b *testing.B, n int) { 604 return func(b *testing.B, n int) { 605 if len(bmbuf) < 2*n { 606 bmbuf = make([]byte, 2*n) 607 } 608 buf1 := bmbuf[0:n] 609 buf2 := bmbuf[n : 2*n] 610 buf1[n-1] = 'x' 611 buf2[n-1] = 'x' 612 for i := 0; i < b.N; i++ { 613 eq := equal(buf1, buf2) 614 if !eq { 615 b.Fatal("bad equal") 616 } 617 } 618 buf1[n-1] = '\x00' 619 buf2[n-1] = '\x00' 620 } 621 } 622 623 func BenchmarkIndex(b *testing.B) { 624 benchBytes(b, indexSizes, func(b *testing.B, n int) { 625 buf := bmbuf[0:n] 626 buf[n-1] = 'x' 627 for i := 0; i < b.N; i++ { 628 j := Index(buf, buf[n-7:]) 629 if j != n-7 { 630 b.Fatal("bad index", j) 631 } 632 } 633 buf[n-1] = '\x00' 634 }) 635 } 636 637 func BenchmarkIndexEasy(b *testing.B) { 638 benchBytes(b, indexSizes, func(b *testing.B, n int) { 639 buf := bmbuf[0:n] 640 buf[n-1] = 'x' 641 buf[n-7] = 'x' 642 for i := 0; i < b.N; i++ { 643 j := Index(buf, buf[n-7:]) 644 if j != n-7 { 645 b.Fatal("bad index", j) 646 } 647 } 648 buf[n-1] = '\x00' 649 buf[n-7] = '\x00' 650 }) 651 } 652 653 func BenchmarkCount(b *testing.B) { 654 benchBytes(b, indexSizes, func(b *testing.B, n int) { 655 buf := bmbuf[0:n] 656 buf[n-1] = 'x' 657 for i := 0; i < b.N; i++ { 658 j := Count(buf, buf[n-7:]) 659 if j != 1 { 660 b.Fatal("bad count", j) 661 } 662 } 663 buf[n-1] = '\x00' 664 }) 665 } 666 667 func BenchmarkCountEasy(b *testing.B) { 668 benchBytes(b, indexSizes, func(b *testing.B, n int) { 669 buf := bmbuf[0:n] 670 buf[n-1] = 'x' 671 buf[n-7] = 'x' 672 for i := 0; i < b.N; i++ { 673 j := Count(buf, buf[n-7:]) 674 if j != 1 { 675 b.Fatal("bad count", j) 676 } 677 } 678 buf[n-1] = '\x00' 679 buf[n-7] = '\x00' 680 }) 681 } 682 683 func BenchmarkCountSingle(b *testing.B) { 684 benchBytes(b, indexSizes, func(b *testing.B, n int) { 685 buf := bmbuf[0:n] 686 step := 8 687 for i := 0; i < len(buf); i += step { 688 buf[i] = 1 689 } 690 expect := (len(buf) + (step - 1)) / step 691 for i := 0; i < b.N; i++ { 692 j := Count(buf, []byte{1}) 693 if j != expect { 694 b.Fatal("bad count", j, expect) 695 } 696 } 697 for i := 0; i < len(buf); i++ { 698 buf[i] = 0 699 } 700 }) 701 } 702 703 type SplitTest struct { 704 s string 705 sep string 706 n int 707 a []string 708 } 709 710 var splittests = []SplitTest{ 711 {"", "", -1, []string{}}, 712 {abcd, "a", 0, nil}, 713 {abcd, "", 2, []string{"a", "bcd"}}, 714 {abcd, "a", -1, []string{"", "bcd"}}, 715 {abcd, "z", -1, []string{"abcd"}}, 716 {abcd, "", -1, []string{"a", "b", "c", "d"}}, 717 {commas, ",", -1, []string{"1", "2", "3", "4"}}, 718 {dots, "...", -1, []string{"1", ".2", ".3", ".4"}}, 719 {faces, "☹", -1, []string{"☺☻", ""}}, 720 {faces, "~", -1, []string{faces}}, 721 {faces, "", -1, []string{"☺", "☻", "☹"}}, 722 {"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}}, 723 {"1 2", " ", 3, []string{"1", "2"}}, 724 {"123", "", 2, []string{"1", "23"}}, 725 {"123", "", 17, []string{"1", "2", "3"}}, 726 } 727 728 func TestSplit(t *testing.T) { 729 for _, tt := range splittests { 730 a := SplitN([]byte(tt.s), []byte(tt.sep), tt.n) 731 732 // Appending to the results should not change future results. 733 var x []byte 734 for _, v := range a { 735 x = append(v, 'z') 736 } 737 738 result := sliceOfString(a) 739 if !eq(result, tt.a) { 740 t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, result, tt.a) 741 continue 742 } 743 if tt.n == 0 || len(a) == 0 { 744 continue 745 } 746 747 if want := tt.a[len(tt.a)-1] + "z"; string(x) != want { 748 t.Errorf("last appended result was %s; want %s", x, want) 749 } 750 751 s := Join(a, []byte(tt.sep)) 752 if string(s) != tt.s { 753 t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s) 754 } 755 if tt.n < 0 { 756 b := Split([]byte(tt.s), []byte(tt.sep)) 757 if !reflect.DeepEqual(a, b) { 758 t.Errorf("Split disagrees withSplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a) 759 } 760 } 761 if len(a) > 0 { 762 in, out := a[0], s 763 if cap(in) == cap(out) && &in[:1][0] == &out[:1][0] { 764 t.Errorf("Join(%#v, %q) didn't copy", a, tt.sep) 765 } 766 } 767 } 768 } 769 770 var splitaftertests = []SplitTest{ 771 {abcd, "a", -1, []string{"a", "bcd"}}, 772 {abcd, "z", -1, []string{"abcd"}}, 773 {abcd, "", -1, []string{"a", "b", "c", "d"}}, 774 {commas, ",", -1, []string{"1,", "2,", "3,", "4"}}, 775 {dots, "...", -1, []string{"1...", ".2...", ".3...", ".4"}}, 776 {faces, "☹", -1, []string{"☺☻☹", ""}}, 777 {faces, "~", -1, []string{faces}}, 778 {faces, "", -1, []string{"☺", "☻", "☹"}}, 779 {"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}}, 780 {"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}}, 781 {"1 2", " ", 3, []string{"1 ", "2"}}, 782 {"123", "", 2, []string{"1", "23"}}, 783 {"123", "", 17, []string{"1", "2", "3"}}, 784 } 785 786 func TestSplitAfter(t *testing.T) { 787 for _, tt := range splitaftertests { 788 a := SplitAfterN([]byte(tt.s), []byte(tt.sep), tt.n) 789 790 // Appending to the results should not change future results. 791 var x []byte 792 for _, v := range a { 793 x = append(v, 'z') 794 } 795 796 result := sliceOfString(a) 797 if !eq(result, tt.a) { 798 t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, result, tt.a) 799 continue 800 } 801 802 if want := tt.a[len(tt.a)-1] + "z"; string(x) != want { 803 t.Errorf("last appended result was %s; want %s", x, want) 804 } 805 806 s := Join(a, nil) 807 if string(s) != tt.s { 808 t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s) 809 } 810 if tt.n < 0 { 811 b := SplitAfter([]byte(tt.s), []byte(tt.sep)) 812 if !reflect.DeepEqual(a, b) { 813 t.Errorf("SplitAfter disagrees withSplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a) 814 } 815 } 816 } 817 } 818 819 type FieldsTest struct { 820 s string 821 a []string 822 } 823 824 var fieldstests = []FieldsTest{ 825 {"", []string{}}, 826 {" ", []string{}}, 827 {" \t ", []string{}}, 828 {" abc ", []string{"abc"}}, 829 {"1 2 3 4", []string{"1", "2", "3", "4"}}, 830 {"1 2 3 4", []string{"1", "2", "3", "4"}}, 831 {"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}}, 832 {"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}}, 833 {"\u2000\u2001\u2002", []string{}}, 834 {"\n™\t™\n", []string{"™", "™"}}, 835 {faces, []string{faces}}, 836 } 837 838 func TestFields(t *testing.T) { 839 for _, tt := range fieldstests { 840 b := []byte(tt.s) 841 a := Fields(b) 842 843 // Appending to the results should not change future results. 844 var x []byte 845 for _, v := range a { 846 x = append(v, 'z') 847 } 848 849 result := sliceOfString(a) 850 if !eq(result, tt.a) { 851 t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a) 852 continue 853 } 854 855 if string(b) != tt.s { 856 t.Errorf("slice changed to %s; want %s", string(b), tt.s) 857 } 858 if len(tt.a) > 0 { 859 if want := tt.a[len(tt.a)-1] + "z"; string(x) != want { 860 t.Errorf("last appended result was %s; want %s", x, want) 861 } 862 } 863 } 864 } 865 866 func TestFieldsFunc(t *testing.T) { 867 for _, tt := range fieldstests { 868 a := FieldsFunc([]byte(tt.s), unicode.IsSpace) 869 result := sliceOfString(a) 870 if !eq(result, tt.a) { 871 t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a) 872 continue 873 } 874 } 875 pred := func(c rune) bool { return c == 'X' } 876 var fieldsFuncTests = []FieldsTest{ 877 {"", []string{}}, 878 {"XX", []string{}}, 879 {"XXhiXXX", []string{"hi"}}, 880 {"aXXbXXXcX", []string{"a", "b", "c"}}, 881 } 882 for _, tt := range fieldsFuncTests { 883 b := []byte(tt.s) 884 a := FieldsFunc(b, pred) 885 886 // Appending to the results should not change future results. 887 var x []byte 888 for _, v := range a { 889 x = append(v, 'z') 890 } 891 892 result := sliceOfString(a) 893 if !eq(result, tt.a) { 894 t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a) 895 } 896 897 if string(b) != tt.s { 898 t.Errorf("slice changed to %s; want %s", b, tt.s) 899 } 900 if len(tt.a) > 0 { 901 if want := tt.a[len(tt.a)-1] + "z"; string(x) != want { 902 t.Errorf("last appended result was %s; want %s", x, want) 903 } 904 } 905 } 906 } 907 908 // Test case for any function which accepts and returns a byte slice. 909 // For ease of creation, we write the input byte slice as a string. 910 type StringTest struct { 911 in string 912 out []byte 913 } 914 915 var upperTests = []StringTest{ 916 {"", []byte("")}, 917 {"ONLYUPPER", []byte("ONLYUPPER")}, 918 {"abc", []byte("ABC")}, 919 {"AbC123", []byte("ABC123")}, 920 {"azAZ09_", []byte("AZAZ09_")}, 921 {"longStrinGwitHmixofsmaLLandcAps", []byte("LONGSTRINGWITHMIXOFSMALLANDCAPS")}, 922 {"long\u0250string\u0250with\u0250nonascii\u2C6Fchars", []byte("LONG\u2C6FSTRING\u2C6FWITH\u2C6FNONASCII\u2C6FCHARS")}, 923 {"\u0250\u0250\u0250\u0250\u0250", []byte("\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F")}, // grows one byte per char 924 {"a\u0080\U0010FFFF", []byte("A\u0080\U0010FFFF")}, // test utf8.RuneSelf and utf8.MaxRune 925 } 926 927 var lowerTests = []StringTest{ 928 {"", []byte("")}, 929 {"abc", []byte("abc")}, 930 {"AbC123", []byte("abc123")}, 931 {"azAZ09_", []byte("azaz09_")}, 932 {"longStrinGwitHmixofsmaLLandcAps", []byte("longstringwithmixofsmallandcaps")}, 933 {"LONG\u2C6FSTRING\u2C6FWITH\u2C6FNONASCII\u2C6FCHARS", []byte("long\u0250string\u0250with\u0250nonascii\u0250chars")}, 934 {"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", []byte("\u0251\u0251\u0251\u0251\u0251")}, // shrinks one byte per char 935 {"A\u0080\U0010FFFF", []byte("a\u0080\U0010FFFF")}, // test utf8.RuneSelf and utf8.MaxRune 936 } 937 938 const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000" 939 940 var trimSpaceTests = []StringTest{ 941 {"", nil}, 942 {" a", []byte("a")}, 943 {"b ", []byte("b")}, 944 {"abc", []byte("abc")}, 945 {space + "abc" + space, []byte("abc")}, 946 {" ", nil}, 947 {"\u3000 ", nil}, 948 {" \u3000", nil}, 949 {" \t\r\n \t\t\r\r\n\n ", nil}, 950 {" \t\r\n x\t\t\r\r\n\n ", []byte("x")}, 951 {" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", []byte("x\t\t\r\r\ny")}, 952 {"1 \t\r\n2", []byte("1 \t\r\n2")}, 953 {" x\x80", []byte("x\x80")}, 954 {" x\xc0", []byte("x\xc0")}, 955 {"x \xc0\xc0 ", []byte("x \xc0\xc0")}, 956 {"x \xc0", []byte("x \xc0")}, 957 {"x \xc0 ", []byte("x \xc0")}, 958 {"x \xc0\xc0 ", []byte("x \xc0\xc0")}, 959 {"x ☺\xc0\xc0 ", []byte("x ☺\xc0\xc0")}, 960 {"x ☺ ", []byte("x ☺")}, 961 } 962 963 // Execute f on each test case. funcName should be the name of f; it's used 964 // in failure reports. 965 func runStringTests(t *testing.T, f func([]byte) []byte, funcName string, testCases []StringTest) { 966 for _, tc := range testCases { 967 actual := f([]byte(tc.in)) 968 if actual == nil && tc.out != nil { 969 t.Errorf("%s(%q) = nil; want %q", funcName, tc.in, tc.out) 970 } 971 if actual != nil && tc.out == nil { 972 t.Errorf("%s(%q) = %q; want nil", funcName, tc.in, actual) 973 } 974 if !Equal(actual, tc.out) { 975 t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out) 976 } 977 } 978 } 979 980 func tenRunes(r rune) string { 981 runes := make([]rune, 10) 982 for i := range runes { 983 runes[i] = r 984 } 985 return string(runes) 986 } 987 988 // User-defined self-inverse mapping function 989 func rot13(r rune) rune { 990 const step = 13 991 if r >= 'a' && r <= 'z' { 992 return ((r - 'a' + step) % 26) + 'a' 993 } 994 if r >= 'A' && r <= 'Z' { 995 return ((r - 'A' + step) % 26) + 'A' 996 } 997 return r 998 } 999 1000 func TestMap(t *testing.T) { 1001 // Run a couple of awful growth/shrinkage tests 1002 a := tenRunes('a') 1003 1004 // 1. Grow. This triggers two reallocations in Map. 1005 maxRune := func(r rune) rune { return unicode.MaxRune } 1006 m := Map(maxRune, []byte(a)) 1007 expect := tenRunes(unicode.MaxRune) 1008 if string(m) != expect { 1009 t.Errorf("growing: expected %q got %q", expect, m) 1010 } 1011 1012 // 2. Shrink 1013 minRune := func(r rune) rune { return 'a' } 1014 m = Map(minRune, []byte(tenRunes(unicode.MaxRune))) 1015 expect = a 1016 if string(m) != expect { 1017 t.Errorf("shrinking: expected %q got %q", expect, m) 1018 } 1019 1020 // 3. Rot13 1021 m = Map(rot13, []byte("a to zed")) 1022 expect = "n gb mrq" 1023 if string(m) != expect { 1024 t.Errorf("rot13: expected %q got %q", expect, m) 1025 } 1026 1027 // 4. Rot13^2 1028 m = Map(rot13, Map(rot13, []byte("a to zed"))) 1029 expect = "a to zed" 1030 if string(m) != expect { 1031 t.Errorf("rot13: expected %q got %q", expect, m) 1032 } 1033 1034 // 5. Drop 1035 dropNotLatin := func(r rune) rune { 1036 if unicode.Is(unicode.Latin, r) { 1037 return r 1038 } 1039 return -1 1040 } 1041 m = Map(dropNotLatin, []byte("Hello, 세계")) 1042 expect = "Hello" 1043 if string(m) != expect { 1044 t.Errorf("drop: expected %q got %q", expect, m) 1045 } 1046 1047 // 6. Invalid rune 1048 invalidRune := func(r rune) rune { 1049 return utf8.MaxRune + 1 1050 } 1051 m = Map(invalidRune, []byte("x")) 1052 expect = "\uFFFD" 1053 if string(m) != expect { 1054 t.Errorf("invalidRune: expected %q got %q", expect, m) 1055 } 1056 } 1057 1058 func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) } 1059 1060 func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTests) } 1061 1062 func BenchmarkToUpper(b *testing.B) { 1063 for _, tc := range upperTests { 1064 tin := []byte(tc.in) 1065 b.Run(tc.in, func(b *testing.B) { 1066 for i := 0; i < b.N; i++ { 1067 actual := ToUpper(tin) 1068 if !Equal(actual, tc.out) { 1069 b.Errorf("ToUpper(%q) = %q; want %q", tc.in, actual, tc.out) 1070 } 1071 } 1072 }) 1073 } 1074 } 1075 1076 func BenchmarkToLower(b *testing.B) { 1077 for _, tc := range lowerTests { 1078 tin := []byte(tc.in) 1079 b.Run(tc.in, func(b *testing.B) { 1080 for i := 0; i < b.N; i++ { 1081 actual := ToLower(tin) 1082 if !Equal(actual, tc.out) { 1083 b.Errorf("ToLower(%q) = %q; want %q", tc.in, actual, tc.out) 1084 } 1085 } 1086 }) 1087 } 1088 } 1089 1090 var toValidUTF8Tests = []struct { 1091 in string 1092 repl string 1093 out string 1094 }{ 1095 {"", "\uFFFD", ""}, 1096 {"abc", "\uFFFD", "abc"}, 1097 {"\uFDDD", "\uFFFD", "\uFDDD"}, 1098 {"a\xffb", "\uFFFD", "a\uFFFDb"}, 1099 {"a\xffb\uFFFD", "X", "aXb\uFFFD"}, 1100 {"a☺\xffb☺\xC0\xAFc☺\xff", "", "a☺b☺c☺"}, 1101 {"a☺\xffb☺\xC0\xAFc☺\xff", "日本語", "a☺日本語b☺日本語c☺日本語"}, 1102 {"\xC0\xAF", "\uFFFD", "\uFFFD"}, 1103 {"\xE0\x80\xAF", "\uFFFD", "\uFFFD"}, 1104 {"\xed\xa0\x80", "abc", "abc"}, 1105 {"\xed\xbf\xbf", "\uFFFD", "\uFFFD"}, 1106 {"\xF0\x80\x80\xaf", "☺", "☺"}, 1107 {"\xF8\x80\x80\x80\xAF", "\uFFFD", "\uFFFD"}, 1108 {"\xFC\x80\x80\x80\x80\xAF", "\uFFFD", "\uFFFD"}, 1109 } 1110 1111 func TestToValidUTF8(t *testing.T) { 1112 for _, tc := range toValidUTF8Tests { 1113 got := ToValidUTF8([]byte(tc.in), []byte(tc.repl)) 1114 if !Equal(got, []byte(tc.out)) { 1115 t.Errorf("ToValidUTF8(%q, %q) = %q; want %q", tc.in, tc.repl, got, tc.out) 1116 } 1117 } 1118 } 1119 1120 func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) } 1121 1122 type RepeatTest struct { 1123 in, out string 1124 count int 1125 } 1126 1127 var RepeatTests = []RepeatTest{ 1128 {"", "", 0}, 1129 {"", "", 1}, 1130 {"", "", 2}, 1131 {"-", "", 0}, 1132 {"-", "-", 1}, 1133 {"-", "----------", 10}, 1134 {"abc ", "abc abc abc ", 3}, 1135 } 1136 1137 func TestRepeat(t *testing.T) { 1138 for _, tt := range RepeatTests { 1139 tin := []byte(tt.in) 1140 tout := []byte(tt.out) 1141 a := Repeat(tin, tt.count) 1142 if !Equal(a, tout) { 1143 t.Errorf("Repeat(%q, %d) = %q; want %q", tin, tt.count, a, tout) 1144 continue 1145 } 1146 } 1147 } 1148 1149 func repeat(b []byte, count int) (err error) { 1150 defer func() { 1151 if r := recover(); r != nil { 1152 switch v := r.(type) { 1153 case error: 1154 err = v 1155 default: 1156 err = fmt.Errorf("%s", v) 1157 } 1158 } 1159 }() 1160 1161 Repeat(b, count) 1162 1163 return 1164 } 1165 1166 // See Issue golang.org/issue/16237 1167 func TestRepeatCatchesOverflow(t *testing.T) { 1168 tests := [...]struct { 1169 s string 1170 count int 1171 errStr string 1172 }{ 1173 0: {"--", -2147483647, "negative"}, 1174 1: {"", int(^uint(0) >> 1), ""}, 1175 2: {"-", 10, ""}, 1176 3: {"gopher", 0, ""}, 1177 4: {"-", -1, "negative"}, 1178 5: {"--", -102, "negative"}, 1179 6: {string(make([]byte, 255)), int((^uint(0))/255 + 1), "overflow"}, 1180 } 1181 1182 for i, tt := range tests { 1183 err := repeat([]byte(tt.s), tt.count) 1184 if tt.errStr == "" { 1185 if err != nil { 1186 t.Errorf("#%d panicked %v", i, err) 1187 } 1188 continue 1189 } 1190 1191 if err == nil || !strings.Contains(err.Error(), tt.errStr) { 1192 t.Errorf("#%d expected %q got %q", i, tt.errStr, err) 1193 } 1194 } 1195 } 1196 1197 func runesEqual(a, b []rune) bool { 1198 if len(a) != len(b) { 1199 return false 1200 } 1201 for i, r := range a { 1202 if r != b[i] { 1203 return false 1204 } 1205 } 1206 return true 1207 } 1208 1209 type RunesTest struct { 1210 in string 1211 out []rune 1212 lossy bool 1213 } 1214 1215 var RunesTests = []RunesTest{ 1216 {"", []rune{}, false}, 1217 {" ", []rune{32}, false}, 1218 {"ABC", []rune{65, 66, 67}, false}, 1219 {"abc", []rune{97, 98, 99}, false}, 1220 {"\u65e5\u672c\u8a9e", []rune{26085, 26412, 35486}, false}, 1221 {"ab\x80c", []rune{97, 98, 0xFFFD, 99}, true}, 1222 {"ab\xc0c", []rune{97, 98, 0xFFFD, 99}, true}, 1223 } 1224 1225 func TestRunes(t *testing.T) { 1226 for _, tt := range RunesTests { 1227 tin := []byte(tt.in) 1228 a := Runes(tin) 1229 if !runesEqual(a, tt.out) { 1230 t.Errorf("Runes(%q) = %v; want %v", tin, a, tt.out) 1231 continue 1232 } 1233 if !tt.lossy { 1234 // can only test reassembly if we didn't lose information 1235 s := string(a) 1236 if s != tt.in { 1237 t.Errorf("string(Runes(%q)) = %x; want %x", tin, s, tin) 1238 } 1239 } 1240 } 1241 } 1242 1243 type TrimTest struct { 1244 f string 1245 in, arg, out string 1246 } 1247 1248 var trimTests = []TrimTest{ 1249 {"Trim", "abba", "a", "bb"}, 1250 {"Trim", "abba", "ab", ""}, 1251 {"TrimLeft", "abba", "ab", ""}, 1252 {"TrimRight", "abba", "ab", ""}, 1253 {"TrimLeft", "abba", "a", "bba"}, 1254 {"TrimLeft", "abba", "b", "abba"}, 1255 {"TrimRight", "abba", "a", "abb"}, 1256 {"TrimRight", "abba", "b", "abba"}, 1257 {"Trim", "<tag>", "<>", "tag"}, 1258 {"Trim", "* listitem", " *", "listitem"}, 1259 {"Trim", `"quote"`, `"`, "quote"}, 1260 {"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"}, 1261 {"Trim", "\x80test\xff", "\xff", "test"}, 1262 {"Trim", " Ġ ", " ", "Ġ"}, 1263 {"Trim", " Ġİ0", "0 ", "Ġİ"}, 1264 //empty string tests 1265 {"Trim", "abba", "", "abba"}, 1266 {"Trim", "", "123", ""}, 1267 {"Trim", "", "", ""}, 1268 {"TrimLeft", "abba", "", "abba"}, 1269 {"TrimLeft", "", "123", ""}, 1270 {"TrimLeft", "", "", ""}, 1271 {"TrimRight", "abba", "", "abba"}, 1272 {"TrimRight", "", "123", ""}, 1273 {"TrimRight", "", "", ""}, 1274 {"TrimRight", "☺\xc0", "☺", "☺\xc0"}, 1275 {"TrimPrefix", "aabb", "a", "abb"}, 1276 {"TrimPrefix", "aabb", "b", "aabb"}, 1277 {"TrimSuffix", "aabb", "a", "aabb"}, 1278 {"TrimSuffix", "aabb", "b", "aab"}, 1279 } 1280 1281 func TestTrim(t *testing.T) { 1282 for _, tc := range trimTests { 1283 name := tc.f 1284 var f func([]byte, string) []byte 1285 var fb func([]byte, []byte) []byte 1286 switch name { 1287 case "Trim": 1288 f = Trim 1289 case "TrimLeft": 1290 f = TrimLeft 1291 case "TrimRight": 1292 f = TrimRight 1293 case "TrimPrefix": 1294 fb = TrimPrefix 1295 case "TrimSuffix": 1296 fb = TrimSuffix 1297 default: 1298 t.Errorf("Undefined trim function %s", name) 1299 } 1300 var actual string 1301 if f != nil { 1302 actual = string(f([]byte(tc.in), tc.arg)) 1303 } else { 1304 actual = string(fb([]byte(tc.in), []byte(tc.arg))) 1305 } 1306 if actual != tc.out { 1307 t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out) 1308 } 1309 } 1310 } 1311 1312 type predicate struct { 1313 f func(r rune) bool 1314 name string 1315 } 1316 1317 var isSpace = predicate{unicode.IsSpace, "IsSpace"} 1318 var isDigit = predicate{unicode.IsDigit, "IsDigit"} 1319 var isUpper = predicate{unicode.IsUpper, "IsUpper"} 1320 var isValidRune = predicate{ 1321 func(r rune) bool { 1322 return r != utf8.RuneError 1323 }, 1324 "IsValidRune", 1325 } 1326 1327 type TrimFuncTest struct { 1328 f predicate 1329 in string 1330 trimOut []byte 1331 leftOut []byte 1332 rightOut []byte 1333 } 1334 1335 func not(p predicate) predicate { 1336 return predicate{ 1337 func(r rune) bool { 1338 return !p.f(r) 1339 }, 1340 "not " + p.name, 1341 } 1342 } 1343 1344 var trimFuncTests = []TrimFuncTest{ 1345 {isSpace, space + " hello " + space, 1346 []byte("hello"), 1347 []byte("hello " + space), 1348 []byte(space + " hello")}, 1349 {isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51", 1350 []byte("hello"), 1351 []byte("hello34\u0e50\u0e51"), 1352 []byte("\u0e50\u0e5212hello")}, 1353 {isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", 1354 []byte("hello"), 1355 []byte("helloEF\u2C6F\u2C6FGH\u2C6F\u2C6F"), 1356 []byte("\u2C6F\u2C6F\u2C6F\u2C6FABCDhello")}, 1357 {not(isSpace), "hello" + space + "hello", 1358 []byte(space), 1359 []byte(space + "hello"), 1360 []byte("hello" + space)}, 1361 {not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo", 1362 []byte("\u0e50\u0e521234\u0e50\u0e51"), 1363 []byte("\u0e50\u0e521234\u0e50\u0e51helo"), 1364 []byte("hello\u0e50\u0e521234\u0e50\u0e51")}, 1365 {isValidRune, "ab\xc0a\xc0cd", 1366 []byte("\xc0a\xc0"), 1367 []byte("\xc0a\xc0cd"), 1368 []byte("ab\xc0a\xc0")}, 1369 {not(isValidRune), "\xc0a\xc0", 1370 []byte("a"), 1371 []byte("a\xc0"), 1372 []byte("\xc0a")}, 1373 // The nils returned by TrimLeftFunc are odd behavior, but we need 1374 // to preserve backwards compatibility. 1375 {isSpace, "", 1376 nil, 1377 nil, 1378 []byte("")}, 1379 {isSpace, " ", 1380 nil, 1381 nil, 1382 []byte("")}, 1383 } 1384 1385 func TestTrimFunc(t *testing.T) { 1386 for _, tc := range trimFuncTests { 1387 trimmers := []struct { 1388 name string 1389 trim func(s []byte, f func(r rune) bool) []byte 1390 out []byte 1391 }{ 1392 {"TrimFunc", TrimFunc, tc.trimOut}, 1393 {"TrimLeftFunc", TrimLeftFunc, tc.leftOut}, 1394 {"TrimRightFunc", TrimRightFunc, tc.rightOut}, 1395 } 1396 for _, trimmer := range trimmers { 1397 actual := trimmer.trim([]byte(tc.in), tc.f.f) 1398 if actual == nil && trimmer.out != nil { 1399 t.Errorf("%s(%q, %q) = nil; want %q", trimmer.name, tc.in, tc.f.name, trimmer.out) 1400 } 1401 if actual != nil && trimmer.out == nil { 1402 t.Errorf("%s(%q, %q) = %q; want nil", trimmer.name, tc.in, tc.f.name, actual) 1403 } 1404 if !Equal(actual, trimmer.out) { 1405 t.Errorf("%s(%q, %q) = %q; want %q", trimmer.name, tc.in, tc.f.name, actual, trimmer.out) 1406 } 1407 } 1408 } 1409 } 1410 1411 type IndexFuncTest struct { 1412 in string 1413 f predicate 1414 first, last int 1415 } 1416 1417 var indexFuncTests = []IndexFuncTest{ 1418 {"", isValidRune, -1, -1}, 1419 {"abc", isDigit, -1, -1}, 1420 {"0123", isDigit, 0, 3}, 1421 {"a1b", isDigit, 1, 1}, 1422 {space, isSpace, 0, len(space) - 3}, // last rune in space is 3 bytes 1423 {"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit, 0, 18}, 1424 {"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34}, 1425 {"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12}, 1426 1427 // tests of invalid UTF-8 1428 {"\x801", isDigit, 1, 1}, 1429 {"\x80abc", isDigit, -1, -1}, 1430 {"\xc0a\xc0", isValidRune, 1, 1}, 1431 {"\xc0a\xc0", not(isValidRune), 0, 2}, 1432 {"\xc0☺\xc0", not(isValidRune), 0, 4}, 1433 {"\xc0☺\xc0\xc0", not(isValidRune), 0, 5}, 1434 {"ab\xc0a\xc0cd", not(isValidRune), 2, 4}, 1435 {"a\xe0\x80cd", not(isValidRune), 1, 2}, 1436 } 1437 1438 func TestIndexFunc(t *testing.T) { 1439 for _, tc := range indexFuncTests { 1440 first := IndexFunc([]byte(tc.in), tc.f.f) 1441 if first != tc.first { 1442 t.Errorf("IndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, first, tc.first) 1443 } 1444 last := LastIndexFunc([]byte(tc.in), tc.f.f) 1445 if last != tc.last { 1446 t.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, last, tc.last) 1447 } 1448 } 1449 } 1450 1451 type ReplaceTest struct { 1452 in string 1453 old, new string 1454 n int 1455 out string 1456 } 1457 1458 var ReplaceTests = []ReplaceTest{ 1459 {"hello", "l", "L", 0, "hello"}, 1460 {"hello", "l", "L", -1, "heLLo"}, 1461 {"hello", "x", "X", -1, "hello"}, 1462 {"", "x", "X", -1, ""}, 1463 {"radar", "r", "<r>", -1, "<r>ada<r>"}, 1464 {"", "", "<>", -1, "<>"}, 1465 {"banana", "a", "<>", -1, "b<>n<>n<>"}, 1466 {"banana", "a", "<>", 1, "b<>nana"}, 1467 {"banana", "a", "<>", 1000, "b<>n<>n<>"}, 1468 {"banana", "an", "<>", -1, "b<><>a"}, 1469 {"banana", "ana", "<>", -1, "b<>na"}, 1470 {"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"}, 1471 {"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"}, 1472 {"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"}, 1473 {"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"}, 1474 {"banana", "", "<>", 1, "<>banana"}, 1475 {"banana", "a", "a", -1, "banana"}, 1476 {"banana", "a", "a", 1, "banana"}, 1477 {"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"}, 1478 } 1479 1480 func TestReplace(t *testing.T) { 1481 for _, tt := range ReplaceTests { 1482 in := append([]byte(tt.in), "<spare>"...) 1483 in = in[:len(tt.in)] 1484 out := Replace(in, []byte(tt.old), []byte(tt.new), tt.n) 1485 if s := string(out); s != tt.out { 1486 t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out) 1487 } 1488 if cap(in) == cap(out) && &in[:1][0] == &out[:1][0] { 1489 t.Errorf("Replace(%q, %q, %q, %d) didn't copy", tt.in, tt.old, tt.new, tt.n) 1490 } 1491 if tt.n == -1 { 1492 out := ReplaceAll(in, []byte(tt.old), []byte(tt.new)) 1493 if s := string(out); s != tt.out { 1494 t.Errorf("ReplaceAll(%q, %q, %q) = %q, want %q", tt.in, tt.old, tt.new, s, tt.out) 1495 } 1496 } 1497 } 1498 } 1499 1500 type TitleTest struct { 1501 in, out string 1502 } 1503 1504 var TitleTests = []TitleTest{ 1505 {"", ""}, 1506 {"a", "A"}, 1507 {" aaa aaa aaa ", " Aaa Aaa Aaa "}, 1508 {" Aaa Aaa Aaa ", " Aaa Aaa Aaa "}, 1509 {"123a456", "123a456"}, 1510 {"double-blind", "Double-Blind"}, 1511 {"ÿøû", "Ÿøû"}, 1512 {"with_underscore", "With_underscore"}, 1513 {"unicode \xe2\x80\xa8 line separator", "Unicode \xe2\x80\xa8 Line Separator"}, 1514 } 1515 1516 func TestTitle(t *testing.T) { 1517 for _, tt := range TitleTests { 1518 if s := string(Title([]byte(tt.in))); s != tt.out { 1519 t.Errorf("Title(%q) = %q, want %q", tt.in, s, tt.out) 1520 } 1521 } 1522 } 1523 1524 var ToTitleTests = []TitleTest{ 1525 {"", ""}, 1526 {"a", "A"}, 1527 {" aaa aaa aaa ", " AAA AAA AAA "}, 1528 {" Aaa Aaa Aaa ", " AAA AAA AAA "}, 1529 {"123a456", "123A456"}, 1530 {"double-blind", "DOUBLE-BLIND"}, 1531 {"ÿøû", "ŸØÛ"}, 1532 } 1533 1534 func TestToTitle(t *testing.T) { 1535 for _, tt := range ToTitleTests { 1536 if s := string(ToTitle([]byte(tt.in))); s != tt.out { 1537 t.Errorf("ToTitle(%q) = %q, want %q", tt.in, s, tt.out) 1538 } 1539 } 1540 } 1541 1542 var EqualFoldTests = []struct { 1543 s, t string 1544 out bool 1545 }{ 1546 {"abc", "abc", true}, 1547 {"ABcd", "ABcd", true}, 1548 {"123abc", "123ABC", true}, 1549 {"αβδ", "ΑΒΔ", true}, 1550 {"abc", "xyz", false}, 1551 {"abc", "XYZ", false}, 1552 {"abcdefghijk", "abcdefghijX", false}, 1553 {"abcdefghijk", "abcdefghij\u212A", true}, 1554 {"abcdefghijK", "abcdefghij\u212A", true}, 1555 {"abcdefghijkz", "abcdefghij\u212Ay", false}, 1556 {"abcdefghijKz", "abcdefghij\u212Ay", false}, 1557 } 1558 1559 func TestEqualFold(t *testing.T) { 1560 for _, tt := range EqualFoldTests { 1561 if out := EqualFold([]byte(tt.s), []byte(tt.t)); out != tt.out { 1562 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.s, tt.t, out, tt.out) 1563 } 1564 if out := EqualFold([]byte(tt.t), []byte(tt.s)); out != tt.out { 1565 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.t, tt.s, out, tt.out) 1566 } 1567 } 1568 } 1569 1570 var cutTests = []struct { 1571 s, sep string 1572 before, after string 1573 found bool 1574 }{ 1575 {"abc", "b", "a", "c", true}, 1576 {"abc", "a", "", "bc", true}, 1577 {"abc", "c", "ab", "", true}, 1578 {"abc", "abc", "", "", true}, 1579 {"abc", "", "", "abc", true}, 1580 {"abc", "d", "abc", "", false}, 1581 {"", "d", "", "", false}, 1582 {"", "", "", "", true}, 1583 } 1584 1585 func TestCut(t *testing.T) { 1586 for _, tt := range cutTests { 1587 if before, after, found := Cut([]byte(tt.s), []byte(tt.sep)); string(before) != tt.before || string(after) != tt.after || found != tt.found { 1588 t.Errorf("Cut(%q, %q) = %q, %q, %v, want %q, %q, %v", tt.s, tt.sep, before, after, found, tt.before, tt.after, tt.found) 1589 } 1590 } 1591 } 1592 1593 func TestBufferGrowNegative(t *testing.T) { 1594 defer func() { 1595 if err := recover(); err == nil { 1596 t.Fatal("Grow(-1) should have panicked") 1597 } 1598 }() 1599 var b Buffer 1600 b.Grow(-1) 1601 } 1602 1603 func TestBufferTruncateNegative(t *testing.T) { 1604 defer func() { 1605 if err := recover(); err == nil { 1606 t.Fatal("Truncate(-1) should have panicked") 1607 } 1608 }() 1609 var b Buffer 1610 b.Truncate(-1) 1611 } 1612 1613 func TestBufferTruncateOutOfRange(t *testing.T) { 1614 defer func() { 1615 if err := recover(); err == nil { 1616 t.Fatal("Truncate(20) should have panicked") 1617 } 1618 }() 1619 var b Buffer 1620 b.Write(make([]byte, 10)) 1621 b.Truncate(20) 1622 } 1623 1624 var containsTests = []struct { 1625 b, subslice []byte 1626 want bool 1627 }{ 1628 {[]byte("hello"), []byte("hel"), true}, 1629 {[]byte("日本語"), []byte("日本"), true}, 1630 {[]byte("hello"), []byte("Hello, world"), false}, 1631 {[]byte("東京"), []byte("京東"), false}, 1632 } 1633 1634 func TestContains(t *testing.T) { 1635 for _, tt := range containsTests { 1636 if got := Contains(tt.b, tt.subslice); got != tt.want { 1637 t.Errorf("Contains(%q, %q) = %v, want %v", tt.b, tt.subslice, got, tt.want) 1638 } 1639 } 1640 } 1641 1642 var ContainsAnyTests = []struct { 1643 b []byte 1644 substr string 1645 expected bool 1646 }{ 1647 {[]byte(""), "", false}, 1648 {[]byte(""), "a", false}, 1649 {[]byte(""), "abc", false}, 1650 {[]byte("a"), "", false}, 1651 {[]byte("a"), "a", true}, 1652 {[]byte("aaa"), "a", true}, 1653 {[]byte("abc"), "xyz", false}, 1654 {[]byte("abc"), "xcz", true}, 1655 {[]byte("a☺b☻c☹d"), "uvw☻xyz", true}, 1656 {[]byte("aRegExp*"), ".(|)*+?^$[]", true}, 1657 {[]byte(dots + dots + dots), " ", false}, 1658 } 1659 1660 func TestContainsAny(t *testing.T) { 1661 for _, ct := range ContainsAnyTests { 1662 if ContainsAny(ct.b, ct.substr) != ct.expected { 1663 t.Errorf("ContainsAny(%s, %s) = %v, want %v", 1664 ct.b, ct.substr, !ct.expected, ct.expected) 1665 } 1666 } 1667 } 1668 1669 var ContainsRuneTests = []struct { 1670 b []byte 1671 r rune 1672 expected bool 1673 }{ 1674 {[]byte(""), 'a', false}, 1675 {[]byte("a"), 'a', true}, 1676 {[]byte("aaa"), 'a', true}, 1677 {[]byte("abc"), 'y', false}, 1678 {[]byte("abc"), 'c', true}, 1679 {[]byte("a☺b☻c☹d"), 'x', false}, 1680 {[]byte("a☺b☻c☹d"), '☻', true}, 1681 {[]byte("aRegExp*"), '*', true}, 1682 } 1683 1684 func TestContainsRune(t *testing.T) { 1685 for _, ct := range ContainsRuneTests { 1686 if ContainsRune(ct.b, ct.r) != ct.expected { 1687 t.Errorf("ContainsRune(%q, %q) = %v, want %v", 1688 ct.b, ct.r, !ct.expected, ct.expected) 1689 } 1690 } 1691 } 1692 1693 var makeFieldsInput = func() []byte { 1694 x := make([]byte, 1<<20) 1695 // Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space. 1696 for i := range x { 1697 switch rand.Intn(10) { 1698 case 0: 1699 x[i] = ' ' 1700 case 1: 1701 if i > 0 && x[i-1] == 'x' { 1702 copy(x[i-1:], "χ") 1703 break 1704 } 1705 fallthrough 1706 default: 1707 x[i] = 'x' 1708 } 1709 } 1710 return x 1711 } 1712 1713 var makeFieldsInputASCII = func() []byte { 1714 x := make([]byte, 1<<20) 1715 // Input is ~10% space, rest ASCII non-space. 1716 for i := range x { 1717 if rand.Intn(10) == 0 { 1718 x[i] = ' ' 1719 } else { 1720 x[i] = 'x' 1721 } 1722 } 1723 return x 1724 } 1725 1726 var bytesdata = []struct { 1727 name string 1728 data []byte 1729 }{ 1730 {"ASCII", makeFieldsInputASCII()}, 1731 {"Mixed", makeFieldsInput()}, 1732 } 1733 1734 func BenchmarkFields(b *testing.B) { 1735 for _, sd := range bytesdata { 1736 b.Run(sd.name, func(b *testing.B) { 1737 for j := 1 << 4; j <= 1<<20; j <<= 4 { 1738 b.Run(fmt.Sprintf("%d", j), func(b *testing.B) { 1739 b.ReportAllocs() 1740 b.SetBytes(int64(j)) 1741 data := sd.data[:j] 1742 for i := 0; i < b.N; i++ { 1743 Fields(data) 1744 } 1745 }) 1746 } 1747 }) 1748 } 1749 } 1750 1751 func BenchmarkFieldsFunc(b *testing.B) { 1752 for _, sd := range bytesdata { 1753 b.Run(sd.name, func(b *testing.B) { 1754 for j := 1 << 4; j <= 1<<20; j <<= 4 { 1755 b.Run(fmt.Sprintf("%d", j), func(b *testing.B) { 1756 b.ReportAllocs() 1757 b.SetBytes(int64(j)) 1758 data := sd.data[:j] 1759 for i := 0; i < b.N; i++ { 1760 FieldsFunc(data, unicode.IsSpace) 1761 } 1762 }) 1763 } 1764 }) 1765 } 1766 } 1767 1768 func BenchmarkTrimSpace(b *testing.B) { 1769 tests := []struct { 1770 name string 1771 input []byte 1772 }{ 1773 {"NoTrim", []byte("typical")}, 1774 {"ASCII", []byte(" foo bar ")}, 1775 {"SomeNonASCII", []byte(" \u2000\t\r\n x\t\t\r\r\ny\n \u3000 ")}, 1776 {"JustNonASCII", []byte("\u2000\u2000\u2000☺☺☺☺\u3000\u3000\u3000")}, 1777 } 1778 for _, test := range tests { 1779 b.Run(test.name, func(b *testing.B) { 1780 for i := 0; i < b.N; i++ { 1781 TrimSpace(test.input) 1782 } 1783 }) 1784 } 1785 } 1786 1787 func BenchmarkToValidUTF8(b *testing.B) { 1788 tests := []struct { 1789 name string 1790 input []byte 1791 }{ 1792 {"Valid", []byte("typical")}, 1793 {"InvalidASCII", []byte("foo\xffbar")}, 1794 {"InvalidNonASCII", []byte("日本語\xff日本語")}, 1795 } 1796 replacement := []byte("\uFFFD") 1797 b.ResetTimer() 1798 for _, test := range tests { 1799 b.Run(test.name, func(b *testing.B) { 1800 for i := 0; i < b.N; i++ { 1801 ToValidUTF8(test.input, replacement) 1802 } 1803 }) 1804 } 1805 } 1806 1807 func makeBenchInputHard() []byte { 1808 tokens := [...]string{ 1809 "<a>", "<p>", "<b>", "<strong>", 1810 "</a>", "</p>", "</b>", "</strong>", 1811 "hello", "world", 1812 } 1813 x := make([]byte, 0, 1<<20) 1814 for { 1815 i := rand.Intn(len(tokens)) 1816 if len(x)+len(tokens[i]) >= 1<<20 { 1817 break 1818 } 1819 x = append(x, tokens[i]...) 1820 } 1821 return x 1822 } 1823 1824 var benchInputHard = makeBenchInputHard() 1825 1826 func benchmarkIndexHard(b *testing.B, sep []byte) { 1827 for i := 0; i < b.N; i++ { 1828 Index(benchInputHard, sep) 1829 } 1830 } 1831 1832 func benchmarkLastIndexHard(b *testing.B, sep []byte) { 1833 for i := 0; i < b.N; i++ { 1834 LastIndex(benchInputHard, sep) 1835 } 1836 } 1837 1838 func benchmarkCountHard(b *testing.B, sep []byte) { 1839 for i := 0; i < b.N; i++ { 1840 Count(benchInputHard, sep) 1841 } 1842 } 1843 1844 func BenchmarkIndexHard1(b *testing.B) { benchmarkIndexHard(b, []byte("<>")) } 1845 func BenchmarkIndexHard2(b *testing.B) { benchmarkIndexHard(b, []byte("</pre>")) } 1846 func BenchmarkIndexHard3(b *testing.B) { benchmarkIndexHard(b, []byte("<b>hello world</b>")) } 1847 func BenchmarkIndexHard4(b *testing.B) { 1848 benchmarkIndexHard(b, []byte("<pre><b>hello</b><strong>world</strong></pre>")) 1849 } 1850 1851 func BenchmarkLastIndexHard1(b *testing.B) { benchmarkLastIndexHard(b, []byte("<>")) } 1852 func BenchmarkLastIndexHard2(b *testing.B) { benchmarkLastIndexHard(b, []byte("</pre>")) } 1853 func BenchmarkLastIndexHard3(b *testing.B) { benchmarkLastIndexHard(b, []byte("<b>hello world</b>")) } 1854 1855 func BenchmarkCountHard1(b *testing.B) { benchmarkCountHard(b, []byte("<>")) } 1856 func BenchmarkCountHard2(b *testing.B) { benchmarkCountHard(b, []byte("</pre>")) } 1857 func BenchmarkCountHard3(b *testing.B) { benchmarkCountHard(b, []byte("<b>hello world</b>")) } 1858 1859 func BenchmarkSplitEmptySeparator(b *testing.B) { 1860 for i := 0; i < b.N; i++ { 1861 Split(benchInputHard, nil) 1862 } 1863 } 1864 1865 func BenchmarkSplitSingleByteSeparator(b *testing.B) { 1866 sep := []byte("/") 1867 for i := 0; i < b.N; i++ { 1868 Split(benchInputHard, sep) 1869 } 1870 } 1871 1872 func BenchmarkSplitMultiByteSeparator(b *testing.B) { 1873 sep := []byte("hello") 1874 for i := 0; i < b.N; i++ { 1875 Split(benchInputHard, sep) 1876 } 1877 } 1878 1879 func BenchmarkSplitNSingleByteSeparator(b *testing.B) { 1880 sep := []byte("/") 1881 for i := 0; i < b.N; i++ { 1882 SplitN(benchInputHard, sep, 10) 1883 } 1884 } 1885 1886 func BenchmarkSplitNMultiByteSeparator(b *testing.B) { 1887 sep := []byte("hello") 1888 for i := 0; i < b.N; i++ { 1889 SplitN(benchInputHard, sep, 10) 1890 } 1891 } 1892 1893 func BenchmarkRepeat(b *testing.B) { 1894 for i := 0; i < b.N; i++ { 1895 Repeat([]byte("-"), 80) 1896 } 1897 } 1898 1899 func BenchmarkBytesCompare(b *testing.B) { 1900 for n := 1; n <= 2048; n <<= 1 { 1901 b.Run(fmt.Sprint(n), func(b *testing.B) { 1902 var x = make([]byte, n) 1903 var y = make([]byte, n) 1904 1905 for i := 0; i < n; i++ { 1906 x[i] = 'a' 1907 } 1908 1909 for i := 0; i < n; i++ { 1910 y[i] = 'a' 1911 } 1912 1913 b.ResetTimer() 1914 for i := 0; i < b.N; i++ { 1915 Compare(x, y) 1916 } 1917 }) 1918 } 1919 } 1920 1921 func BenchmarkIndexAnyASCII(b *testing.B) { 1922 x := Repeat([]byte{'#'}, 2048) // Never matches set 1923 cs := "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz" 1924 for k := 1; k <= 2048; k <<= 4 { 1925 for j := 1; j <= 64; j <<= 1 { 1926 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) { 1927 for i := 0; i < b.N; i++ { 1928 IndexAny(x[:k], cs[:j]) 1929 } 1930 }) 1931 } 1932 } 1933 } 1934 1935 func BenchmarkIndexAnyUTF8(b *testing.B) { 1936 x := Repeat([]byte{'#'}, 2048) // Never matches set 1937 cs := "你好世界, hello world. 你好世界, hello world. 你好世界, hello world." 1938 for k := 1; k <= 2048; k <<= 4 { 1939 for j := 1; j <= 64; j <<= 1 { 1940 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) { 1941 for i := 0; i < b.N; i++ { 1942 IndexAny(x[:k], cs[:j]) 1943 } 1944 }) 1945 } 1946 } 1947 } 1948 1949 func BenchmarkLastIndexAnyASCII(b *testing.B) { 1950 x := Repeat([]byte{'#'}, 2048) // Never matches set 1951 cs := "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz" 1952 for k := 1; k <= 2048; k <<= 4 { 1953 for j := 1; j <= 64; j <<= 1 { 1954 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) { 1955 for i := 0; i < b.N; i++ { 1956 LastIndexAny(x[:k], cs[:j]) 1957 } 1958 }) 1959 } 1960 } 1961 } 1962 1963 func BenchmarkLastIndexAnyUTF8(b *testing.B) { 1964 x := Repeat([]byte{'#'}, 2048) // Never matches set 1965 cs := "你好世界, hello world. 你好世界, hello world. 你好世界, hello world." 1966 for k := 1; k <= 2048; k <<= 4 { 1967 for j := 1; j <= 64; j <<= 1 { 1968 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) { 1969 for i := 0; i < b.N; i++ { 1970 LastIndexAny(x[:k], cs[:j]) 1971 } 1972 }) 1973 } 1974 } 1975 } 1976 1977 func BenchmarkTrimASCII(b *testing.B) { 1978 cs := "0123456789abcdef" 1979 for k := 1; k <= 4096; k <<= 4 { 1980 for j := 1; j <= 16; j <<= 1 { 1981 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) { 1982 x := Repeat([]byte(cs[:j]), k) // Always matches set 1983 for i := 0; i < b.N; i++ { 1984 Trim(x[:k], cs[:j]) 1985 } 1986 }) 1987 } 1988 } 1989 } 1990 1991 func BenchmarkTrimByte(b *testing.B) { 1992 x := []byte(" the quick brown fox ") 1993 for i := 0; i < b.N; i++ { 1994 Trim(x, " ") 1995 } 1996 } 1997 1998 func BenchmarkIndexPeriodic(b *testing.B) { 1999 key := []byte{1, 1} 2000 for _, skip := range [...]int{2, 4, 8, 16, 32, 64} { 2001 b.Run(fmt.Sprintf("IndexPeriodic%d", skip), func(b *testing.B) { 2002 buf := make([]byte, 1<<16) 2003 for i := 0; i < len(buf); i += skip { 2004 buf[i] = 1 2005 } 2006 for i := 0; i < b.N; i++ { 2007 Index(buf, key) 2008 } 2009 }) 2010 } 2011 }