Tests that we occasionally compact. Issue 5154.
(t *testing.T)
| 570 | |
| 571 | // Tests that we occasionally compact. Issue 5154. |
| 572 | func TestBufferGrowth(t *testing.T) { |
| 573 | var b Buffer |
| 574 | buf := make([]byte, 1024) |
| 575 | b.Write(buf[0:1]) |
| 576 | var cap0 int |
| 577 | for i := 0; i < 5<<10; i++ { |
| 578 | b.Write(buf) |
| 579 | b.Read(buf) |
| 580 | if i == 0 { |
| 581 | cap0 = b.Cap() |
| 582 | } |
| 583 | } |
| 584 | cap1 := b.Cap() |
| 585 | // (*Buffer).grow allows for 2x capacity slop before sliding, |
| 586 | // so set our error threshold at 3x. |
| 587 | if cap1 > cap0*3 { |
| 588 | t.Errorf("buffer cap = %d; too big (grew from %d)", cap1, cap0) |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | func BenchmarkWriteByte(b *testing.B) { |
| 593 | const n = 4 << 10 |