github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/bytes/buffer_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 "io" 10 "math/rand" 11 "runtime" 12 "testing" 13 "unicode/utf8" 14 ) 15 16 const N = 10000 // make this bigger for a larger (and slower) test 17 var testString string // test data for write tests 18 var testBytes []byte // test data; same as testString but as a slice. 19 20 type negativeReader struct{} 21 22 func (r *negativeReader) Read([]byte) (int, error) { return -1, nil } 23 24 func init() { 25 testBytes = make([]byte, N) 26 for i := 0; i < N; i++ { 27 testBytes[i] = 'a' + byte(i%26) 28 } 29 testString = string(testBytes) 30 } 31 32 // Verify that contents of buf match the string s. 33 func check(t *testing.T, testname string, buf *Buffer, s string) { 34 bytes := buf.Bytes() 35 str := buf.String() 36 if buf.Len() != len(bytes) { 37 t.Errorf("%s: buf.Len() == %d, len(buf.Bytes()) == %d", testname, buf.Len(), len(bytes)) 38 } 39 40 if buf.Len() != len(str) { 41 t.Errorf("%s: buf.Len() == %d, len(buf.String()) == %d", testname, buf.Len(), len(str)) 42 } 43 44 if buf.Len() != len(s) { 45 t.Errorf("%s: buf.Len() == %d, len(s) == %d", testname, buf.Len(), len(s)) 46 } 47 48 if string(bytes) != s { 49 t.Errorf("%s: string(buf.Bytes()) == %q, s == %q", testname, string(bytes), s) 50 } 51 } 52 53 // Fill buf through n writes of string fus. 54 // The initial contents of buf corresponds to the string s; 55 // the result is the final contents of buf returned as a string. 56 func fillString(t *testing.T, testname string, buf *Buffer, s string, n int, fus string) string { 57 check(t, testname+" (fill 1)", buf, s) 58 for ; n > 0; n-- { 59 m, err := buf.WriteString(fus) 60 if m != len(fus) { 61 t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fus)) 62 } 63 if err != nil { 64 t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err) 65 } 66 s += fus 67 check(t, testname+" (fill 4)", buf, s) 68 } 69 return s 70 } 71 72 // Fill buf through n writes of byte slice fub. 73 // The initial contents of buf corresponds to the string s; 74 // the result is the final contents of buf returned as a string. 75 func fillBytes(t *testing.T, testname string, buf *Buffer, s string, n int, fub []byte) string { 76 check(t, testname+" (fill 1)", buf, s) 77 for ; n > 0; n-- { 78 m, err := buf.Write(fub) 79 if m != len(fub) { 80 t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fub)) 81 } 82 if err != nil { 83 t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err) 84 } 85 s += string(fub) 86 check(t, testname+" (fill 4)", buf, s) 87 } 88 return s 89 } 90 91 func TestNewBuffer(t *testing.T) { 92 buf := NewBuffer(testBytes) 93 check(t, "NewBuffer", buf, testString) 94 } 95 96 func TestNewBufferString(t *testing.T) { 97 buf := NewBufferString(testString) 98 check(t, "NewBufferString", buf, testString) 99 } 100 101 // Empty buf through repeated reads into fub. 102 // The initial contents of buf corresponds to the string s. 103 func empty(t *testing.T, testname string, buf *Buffer, s string, fub []byte) { 104 check(t, testname+" (empty 1)", buf, s) 105 106 for { 107 n, err := buf.Read(fub) 108 if n == 0 { 109 break 110 } 111 if err != nil { 112 t.Errorf(testname+" (empty 2): err should always be nil, found err == %s", err) 113 } 114 s = s[n:] 115 check(t, testname+" (empty 3)", buf, s) 116 } 117 118 check(t, testname+" (empty 4)", buf, "") 119 } 120 121 func TestBasicOperations(t *testing.T) { 122 var buf Buffer 123 124 for i := 0; i < 5; i++ { 125 check(t, "TestBasicOperations (1)", &buf, "") 126 127 buf.Reset() 128 check(t, "TestBasicOperations (2)", &buf, "") 129 130 buf.Truncate(0) 131 check(t, "TestBasicOperations (3)", &buf, "") 132 133 n, err := buf.Write(testBytes[0:1]) 134 if n != 1 { 135 t.Errorf("wrote 1 byte, but n == %d", n) 136 } 137 if err != nil { 138 t.Errorf("err should always be nil, but err == %s", err) 139 } 140 check(t, "TestBasicOperations (4)", &buf, "a") 141 142 buf.WriteByte(testString[1]) 143 check(t, "TestBasicOperations (5)", &buf, "ab") 144 145 n, err = buf.Write(testBytes[2:26]) 146 if n != 24 { 147 t.Errorf("wrote 24 bytes, but n == %d", n) 148 } 149 check(t, "TestBasicOperations (6)", &buf, testString[0:26]) 150 151 buf.Truncate(26) 152 check(t, "TestBasicOperations (7)", &buf, testString[0:26]) 153 154 buf.Truncate(20) 155 check(t, "TestBasicOperations (8)", &buf, testString[0:20]) 156 157 empty(t, "TestBasicOperations (9)", &buf, testString[0:20], make([]byte, 5)) 158 empty(t, "TestBasicOperations (10)", &buf, "", make([]byte, 100)) 159 160 buf.WriteByte(testString[1]) 161 c, err := buf.ReadByte() 162 if err != nil { 163 t.Error("ReadByte unexpected eof") 164 } 165 if c != testString[1] { 166 t.Errorf("ReadByte wrong value c=%v", c) 167 } 168 c, err = buf.ReadByte() 169 if err == nil { 170 t.Error("ReadByte unexpected not eof") 171 } 172 } 173 } 174 175 func TestLargeStringWrites(t *testing.T) { 176 var buf Buffer 177 limit := 30 178 if testing.Short() { 179 limit = 9 180 } 181 for i := 3; i < limit; i += 3 { 182 s := fillString(t, "TestLargeWrites (1)", &buf, "", 5, testString) 183 empty(t, "TestLargeStringWrites (2)", &buf, s, make([]byte, len(testString)/i)) 184 } 185 check(t, "TestLargeStringWrites (3)", &buf, "") 186 } 187 188 func TestLargeByteWrites(t *testing.T) { 189 var buf Buffer 190 limit := 30 191 if testing.Short() { 192 limit = 9 193 } 194 for i := 3; i < limit; i += 3 { 195 s := fillBytes(t, "TestLargeWrites (1)", &buf, "", 5, testBytes) 196 empty(t, "TestLargeByteWrites (2)", &buf, s, make([]byte, len(testString)/i)) 197 } 198 check(t, "TestLargeByteWrites (3)", &buf, "") 199 } 200 201 func TestLargeStringReads(t *testing.T) { 202 var buf Buffer 203 for i := 3; i < 30; i += 3 { 204 s := fillString(t, "TestLargeReads (1)", &buf, "", 5, testString[0:len(testString)/i]) 205 empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(testString))) 206 } 207 check(t, "TestLargeStringReads (3)", &buf, "") 208 } 209 210 func TestLargeByteReads(t *testing.T) { 211 var buf Buffer 212 for i := 3; i < 30; i += 3 { 213 s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, testBytes[0:len(testBytes)/i]) 214 empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(testString))) 215 } 216 check(t, "TestLargeByteReads (3)", &buf, "") 217 } 218 219 func TestMixedReadsAndWrites(t *testing.T) { 220 var buf Buffer 221 s := "" 222 for i := 0; i < 50; i++ { 223 wlen := rand.Intn(len(testString)) 224 if i%2 == 0 { 225 s = fillString(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, testString[0:wlen]) 226 } else { 227 s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, testBytes[0:wlen]) 228 } 229 230 rlen := rand.Intn(len(testString)) 231 fub := make([]byte, rlen) 232 n, _ := buf.Read(fub) 233 s = s[n:] 234 } 235 empty(t, "TestMixedReadsAndWrites (2)", &buf, s, make([]byte, buf.Len())) 236 } 237 238 func TestCapWithPreallocatedSlice(t *testing.T) { 239 buf := NewBuffer(make([]byte, 10)) 240 n := buf.Cap() 241 if n != 10 { 242 t.Errorf("expected 10, got %d", n) 243 } 244 } 245 246 func TestCapWithSliceAndWrittenData(t *testing.T) { 247 buf := NewBuffer(make([]byte, 0, 10)) 248 buf.Write([]byte("test")) 249 n := buf.Cap() 250 if n != 10 { 251 t.Errorf("expected 10, got %d", n) 252 } 253 } 254 255 func TestNil(t *testing.T) { 256 var b *Buffer 257 if b.String() != "<nil>" { 258 t.Errorf("expected <nil>; got %q", b.String()) 259 } 260 } 261 262 func TestReadFrom(t *testing.T) { 263 var buf Buffer 264 for i := 3; i < 30; i += 3 { 265 s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, testBytes[0:len(testBytes)/i]) 266 var b Buffer 267 b.ReadFrom(&buf) 268 empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(testString))) 269 } 270 } 271 272 type panicReader struct{ panic bool } 273 274 func (r panicReader) Read(p []byte) (int, error) { 275 if r.panic { 276 panic(nil) 277 } 278 return 0, io.EOF 279 } 280 281 // Make sure that an empty Buffer remains empty when 282 // it is "grown" before a Read that panics 283 func TestReadFromPanicReader(t *testing.T) { 284 285 // First verify non-panic behaviour 286 var buf Buffer 287 i, err := buf.ReadFrom(panicReader{}) 288 if err != nil { 289 t.Fatal(err) 290 } 291 if i != 0 { 292 t.Fatalf("unexpected return from bytes.ReadFrom (1): got: %d, want %d", i, 0) 293 } 294 check(t, "TestReadFromPanicReader (1)", &buf, "") 295 296 // Confirm that when Reader panics, the empty buffer remains empty 297 var buf2 Buffer 298 defer func() { 299 recover() 300 check(t, "TestReadFromPanicReader (2)", &buf2, "") 301 }() 302 buf2.ReadFrom(panicReader{panic: true}) 303 } 304 305 func TestReadFromNegativeReader(t *testing.T) { 306 var b Buffer 307 defer func() { 308 switch err := recover().(type) { 309 case nil: 310 t.Fatal("bytes.Buffer.ReadFrom didn't panic") 311 case error: 312 // this is the error string of errNegativeRead 313 wantError := "bytes.Buffer: reader returned negative count from Read" 314 if err.Error() != wantError { 315 t.Fatalf("recovered panic: got %v, want %v", err.Error(), wantError) 316 } 317 default: 318 t.Fatalf("unexpected panic value: %#v", err) 319 } 320 }() 321 322 b.ReadFrom(new(negativeReader)) 323 } 324 325 func TestWriteTo(t *testing.T) { 326 var buf Buffer 327 for i := 3; i < 30; i += 3 { 328 s := fillBytes(t, "TestWriteTo (1)", &buf, "", 5, testBytes[0:len(testBytes)/i]) 329 var b Buffer 330 buf.WriteTo(&b) 331 empty(t, "TestWriteTo (2)", &b, s, make([]byte, len(testString))) 332 } 333 } 334 335 func TestRuneIO(t *testing.T) { 336 const NRune = 1000 337 // Built a test slice while we write the data 338 b := make([]byte, utf8.UTFMax*NRune) 339 var buf Buffer 340 n := 0 341 for r := rune(0); r < NRune; r++ { 342 size := utf8.EncodeRune(b[n:], r) 343 nbytes, err := buf.WriteRune(r) 344 if err != nil { 345 t.Fatalf("WriteRune(%U) error: %s", r, err) 346 } 347 if nbytes != size { 348 t.Fatalf("WriteRune(%U) expected %d, got %d", r, size, nbytes) 349 } 350 n += size 351 } 352 b = b[0:n] 353 354 // Check the resulting bytes 355 if !Equal(buf.Bytes(), b) { 356 t.Fatalf("incorrect result from WriteRune: %q not %q", buf.Bytes(), b) 357 } 358 359 p := make([]byte, utf8.UTFMax) 360 // Read it back with ReadRune 361 for r := rune(0); r < NRune; r++ { 362 size := utf8.EncodeRune(p, r) 363 nr, nbytes, err := buf.ReadRune() 364 if nr != r || nbytes != size || err != nil { 365 t.Fatalf("ReadRune(%U) got %U,%d not %U,%d (err=%s)", r, nr, nbytes, r, size, err) 366 } 367 } 368 369 // Check that UnreadRune works 370 buf.Reset() 371 372 // check at EOF 373 if err := buf.UnreadRune(); err == nil { 374 t.Fatal("UnreadRune at EOF: got no error") 375 } 376 if _, _, err := buf.ReadRune(); err == nil { 377 t.Fatal("ReadRune at EOF: got no error") 378 } 379 if err := buf.UnreadRune(); err == nil { 380 t.Fatal("UnreadRune after ReadRune at EOF: got no error") 381 } 382 383 // check not at EOF 384 buf.Write(b) 385 for r := rune(0); r < NRune; r++ { 386 r1, size, _ := buf.ReadRune() 387 if err := buf.UnreadRune(); err != nil { 388 t.Fatalf("UnreadRune(%U) got error %q", r, err) 389 } 390 r2, nbytes, err := buf.ReadRune() 391 if r1 != r2 || r1 != r || nbytes != size || err != nil { 392 t.Fatalf("ReadRune(%U) after UnreadRune got %U,%d not %U,%d (err=%s)", r, r2, nbytes, r, size, err) 393 } 394 } 395 } 396 397 func TestNext(t *testing.T) { 398 b := []byte{0, 1, 2, 3, 4} 399 tmp := make([]byte, 5) 400 for i := 0; i <= 5; i++ { 401 for j := i; j <= 5; j++ { 402 for k := 0; k <= 6; k++ { 403 // 0 <= i <= j <= 5; 0 <= k <= 6 404 // Check that if we start with a buffer 405 // of length j at offset i and ask for 406 // Next(k), we get the right bytes. 407 buf := NewBuffer(b[0:j]) 408 n, _ := buf.Read(tmp[0:i]) 409 if n != i { 410 t.Fatalf("Read %d returned %d", i, n) 411 } 412 bb := buf.Next(k) 413 want := k 414 if want > j-i { 415 want = j - i 416 } 417 if len(bb) != want { 418 t.Fatalf("in %d,%d: len(Next(%d)) == %d", i, j, k, len(bb)) 419 } 420 for l, v := range bb { 421 if v != byte(l+i) { 422 t.Fatalf("in %d,%d: Next(%d)[%d] = %d, want %d", i, j, k, l, v, l+i) 423 } 424 } 425 } 426 } 427 } 428 } 429 430 var readBytesTests = []struct { 431 buffer string 432 delim byte 433 expected []string 434 err error 435 }{ 436 {"", 0, []string{""}, io.EOF}, 437 {"a\x00", 0, []string{"a\x00"}, nil}, 438 {"abbbaaaba", 'b', []string{"ab", "b", "b", "aaab"}, nil}, 439 {"hello\x01world", 1, []string{"hello\x01"}, nil}, 440 {"foo\nbar", 0, []string{"foo\nbar"}, io.EOF}, 441 {"alpha\nbeta\ngamma\n", '\n', []string{"alpha\n", "beta\n", "gamma\n"}, nil}, 442 {"alpha\nbeta\ngamma", '\n', []string{"alpha\n", "beta\n", "gamma"}, io.EOF}, 443 } 444 445 func TestReadBytes(t *testing.T) { 446 for _, test := range readBytesTests { 447 buf := NewBufferString(test.buffer) 448 var err error 449 for _, expected := range test.expected { 450 var bytes []byte 451 bytes, err = buf.ReadBytes(test.delim) 452 if string(bytes) != expected { 453 t.Errorf("expected %q, got %q", expected, bytes) 454 } 455 if err != nil { 456 break 457 } 458 } 459 if err != test.err { 460 t.Errorf("expected error %v, got %v", test.err, err) 461 } 462 } 463 } 464 465 func TestReadString(t *testing.T) { 466 for _, test := range readBytesTests { 467 buf := NewBufferString(test.buffer) 468 var err error 469 for _, expected := range test.expected { 470 var s string 471 s, err = buf.ReadString(test.delim) 472 if s != expected { 473 t.Errorf("expected %q, got %q", expected, s) 474 } 475 if err != nil { 476 break 477 } 478 } 479 if err != test.err { 480 t.Errorf("expected error %v, got %v", test.err, err) 481 } 482 } 483 } 484 485 func BenchmarkReadString(b *testing.B) { 486 const n = 32 << 10 487 488 data := make([]byte, n) 489 data[n-1] = 'x' 490 b.SetBytes(int64(n)) 491 for i := 0; i < b.N; i++ { 492 buf := NewBuffer(data) 493 _, err := buf.ReadString('x') 494 if err != nil { 495 b.Fatal(err) 496 } 497 } 498 } 499 500 func TestGrow(t *testing.T) { 501 x := []byte{'x'} 502 y := []byte{'y'} 503 tmp := make([]byte, 72) 504 for _, startLen := range []int{0, 100, 1000, 10000, 100000} { 505 xBytes := Repeat(x, startLen) 506 for _, growLen := range []int{0, 100, 1000, 10000, 100000} { 507 buf := NewBuffer(xBytes) 508 // If we read, this affects buf.off, which is good to test. 509 readBytes, _ := buf.Read(tmp) 510 buf.Grow(growLen) 511 yBytes := Repeat(y, growLen) 512 // Check no allocation occurs in write, as long as we're single-threaded. 513 var m1, m2 runtime.MemStats 514 runtime.ReadMemStats(&m1) 515 buf.Write(yBytes) 516 runtime.ReadMemStats(&m2) 517 if runtime.GOMAXPROCS(-1) == 1 && m1.Mallocs != m2.Mallocs { 518 t.Errorf("allocation occurred during write") 519 } 520 // Check that buffer has correct data. 521 if !Equal(buf.Bytes()[0:startLen-readBytes], xBytes[readBytes:]) { 522 t.Errorf("bad initial data at %d %d", startLen, growLen) 523 } 524 if !Equal(buf.Bytes()[startLen-readBytes:startLen-readBytes+growLen], yBytes) { 525 t.Errorf("bad written data at %d %d", startLen, growLen) 526 } 527 } 528 } 529 } 530 531 func TestGrowOverflow(t *testing.T) { 532 defer func() { 533 if err := recover(); err != ErrTooLarge { 534 t.Errorf("after too-large Grow, recover() = %v; want %v", err, ErrTooLarge) 535 } 536 }() 537 538 buf := NewBuffer(make([]byte, 1)) 539 const maxInt = int(^uint(0) >> 1) 540 buf.Grow(maxInt) 541 } 542 543 // Was a bug: used to give EOF reading empty slice at EOF. 544 func TestReadEmptyAtEOF(t *testing.T) { 545 b := new(Buffer) 546 slice := make([]byte, 0) 547 n, err := b.Read(slice) 548 if err != nil { 549 t.Errorf("read error: %v", err) 550 } 551 if n != 0 { 552 t.Errorf("wrong count; got %d want 0", n) 553 } 554 } 555 556 func TestUnreadByte(t *testing.T) { 557 b := new(Buffer) 558 559 // check at EOF 560 if err := b.UnreadByte(); err == nil { 561 t.Fatal("UnreadByte at EOF: got no error") 562 } 563 if _, err := b.ReadByte(); err == nil { 564 t.Fatal("ReadByte at EOF: got no error") 565 } 566 if err := b.UnreadByte(); err == nil { 567 t.Fatal("UnreadByte after ReadByte at EOF: got no error") 568 } 569 570 // check not at EOF 571 b.WriteString("abcdefghijklmnopqrstuvwxyz") 572 573 // after unsuccessful read 574 if n, err := b.Read(nil); n != 0 || err != nil { 575 t.Fatalf("Read(nil) = %d,%v; want 0,nil", n, err) 576 } 577 if err := b.UnreadByte(); err == nil { 578 t.Fatal("UnreadByte after Read(nil): got no error") 579 } 580 581 // after successful read 582 if _, err := b.ReadBytes('m'); err != nil { 583 t.Fatalf("ReadBytes: %v", err) 584 } 585 if err := b.UnreadByte(); err != nil { 586 t.Fatalf("UnreadByte: %v", err) 587 } 588 c, err := b.ReadByte() 589 if err != nil { 590 t.Fatalf("ReadByte: %v", err) 591 } 592 if c != 'm' { 593 t.Errorf("ReadByte = %q; want %q", c, 'm') 594 } 595 } 596 597 // Tests that we occasionally compact. Issue 5154. 598 func TestBufferGrowth(t *testing.T) { 599 var b Buffer 600 buf := make([]byte, 1024) 601 b.Write(buf[0:1]) 602 var cap0 int 603 for i := 0; i < 5<<10; i++ { 604 b.Write(buf) 605 b.Read(buf) 606 if i == 0 { 607 cap0 = b.Cap() 608 } 609 } 610 cap1 := b.Cap() 611 // (*Buffer).grow allows for 2x capacity slop before sliding, 612 // so set our error threshold at 3x. 613 if cap1 > cap0*3 { 614 t.Errorf("buffer cap = %d; too big (grew from %d)", cap1, cap0) 615 } 616 } 617 618 func BenchmarkWriteByte(b *testing.B) { 619 const n = 4 << 10 620 b.SetBytes(n) 621 buf := NewBuffer(make([]byte, n)) 622 for i := 0; i < b.N; i++ { 623 buf.Reset() 624 for i := 0; i < n; i++ { 625 buf.WriteByte('x') 626 } 627 } 628 } 629 630 func BenchmarkWriteRune(b *testing.B) { 631 const n = 4 << 10 632 const r = '☺' 633 b.SetBytes(int64(n * utf8.RuneLen(r))) 634 buf := NewBuffer(make([]byte, n*utf8.UTFMax)) 635 for i := 0; i < b.N; i++ { 636 buf.Reset() 637 for i := 0; i < n; i++ { 638 buf.WriteRune(r) 639 } 640 } 641 } 642 643 // From Issue 5154. 644 func BenchmarkBufferNotEmptyWriteRead(b *testing.B) { 645 buf := make([]byte, 1024) 646 for i := 0; i < b.N; i++ { 647 var b Buffer 648 b.Write(buf[0:1]) 649 for i := 0; i < 5<<10; i++ { 650 b.Write(buf) 651 b.Read(buf) 652 } 653 } 654 } 655 656 // Check that we don't compact too often. From Issue 5154. 657 func BenchmarkBufferFullSmallReads(b *testing.B) { 658 buf := make([]byte, 1024) 659 for i := 0; i < b.N; i++ { 660 var b Buffer 661 b.Write(buf) 662 for b.Len()+20 < b.Cap() { 663 b.Write(buf[:10]) 664 } 665 for i := 0; i < 5<<10; i++ { 666 b.Read(buf[:1]) 667 b.Write(buf[:1]) 668 } 669 } 670 }