| 472 | } |
| 473 | |
| 474 | func TestGrow(t *testing.T) { |
| 475 | x := []byte{'x'} |
| 476 | y := []byte{'y'} |
| 477 | tmp := make([]byte, 72) |
| 478 | for _, startLen := range []int{0, 100, 1000, 10000, 100000} { |
| 479 | xBytes := bytes.Repeat(x, startLen) |
| 480 | for _, growLen := range []int{0, 100, 1000, 10000, 100000} { |
| 481 | buf := NewBuffer(xBytes) |
| 482 | // If we read, this affects buf.off, which is good to test. |
| 483 | readBytes, _ := buf.Read(tmp) |
| 484 | buf.Grow(growLen) |
| 485 | yBytes := bytes.Repeat(y, growLen) |
| 486 | // Check no allocation occurs in write, as long as we're single-threaded. |
| 487 | var m1, m2 runtime.MemStats |
| 488 | runtime.ReadMemStats(&m1) |
| 489 | buf.Write(yBytes) |
| 490 | runtime.ReadMemStats(&m2) |
| 491 | if runtime.GOMAXPROCS(-1) == 1 && m1.Mallocs != m2.Mallocs { |
| 492 | t.Errorf("allocation occurred during write") |
| 493 | } |
| 494 | // Check that buffer has correct data. |
| 495 | if !bytes.Equal(buf.Bytes()[0:startLen-readBytes], xBytes[readBytes:]) { |
| 496 | t.Errorf("bad initial data at %d %d", startLen, growLen) |
| 497 | } |
| 498 | if !bytes.Equal(buf.Bytes()[startLen-readBytes:startLen-readBytes+growLen], yBytes) { |
| 499 | t.Errorf("bad written data at %d %d", startLen, growLen) |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | func TestGrowOverflow(t *testing.T) { |
| 506 | defer func() { |