15
const N = 10000 // make this bigger for a larger (and slower) test
16
var data string // test data for write tests
17
var bytes []byte // test data; same as data but as a slice.
16
const N = 10000 // make this bigger for a larger (and slower) test
17
var data string // test data for write tests
18
var testBytes []byte // test data; same as data but as a slice.
20
bytes = make([]byte, N)
21
testBytes = make([]byte, N)
21
22
for i := 0; i < N; i++ {
22
bytes[i] = 'a' + byte(i%26)
23
testBytes[i] = 'a' + byte(i%26)
25
data = string(testBytes)
27
28
// Verify that contents of buf match the string s.
189
190
for i := 3; i < limit; i += 3 {
190
s := fillBytes(t, "TestLargeWrites (1)", &buf, "", 5, bytes)
191
s := fillBytes(t, "TestLargeWrites (1)", &buf, "", 5, testBytes)
191
192
empty(t, "TestLargeByteWrites (2)", &buf, s, make([]byte, len(data)/i))
193
194
check(t, "TestLargeByteWrites (3)", &buf, "")
205
206
func TestLargeByteReads(t *testing.T) {
207
208
for i := 3; i < 30; i += 3 {
208
s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, bytes[0:len(bytes)/i])
209
s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
209
210
empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data)))
211
212
check(t, "TestLargeByteReads (3)", &buf, "")
220
221
s = fillString(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, data[0:wlen])
222
s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, bytes[0:wlen])
223
s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, testBytes[0:wlen])
225
226
rlen := rand.Intn(len(data))
240
241
func TestReadFrom(t *testing.T) {
242
243
for i := 3; i < 30; i += 3 {
243
s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, bytes[0:len(bytes)/i])
244
s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
246
247
empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data)))
250
251
func TestWriteTo(t *testing.T) {
252
253
for i := 3; i < 30; i += 3 {
253
s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, bytes[0:len(bytes)/i])
254
s := fillBytes(t, "TestWriteTo (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
256
empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data)))
257
empty(t, "TestWriteTo (2)", &b, s, make([]byte, len(data)))
260
261
func TestRuneIO(t *testing.T) {
261
262
const NRune = 1000
262
// Built a test array while we write the data
263
// Built a test slice while we write the data
263
264
b := make([]byte, utf8.UTFMax*NRune)
378
func TestReadString(t *testing.T) {
379
for _, test := range readBytesTests {
380
buf := NewBufferString(test.buffer)
382
for _, expected := range test.expected {
384
s, err = buf.ReadString(test.delim)
386
t.Errorf("expected %q, got %q", expected, s)
393
t.Errorf("expected error %v, got %v", test.err, err)
398
func BenchmarkReadString(b *testing.B) {
401
data := make([]byte, n)
404
for i := 0; i < b.N; i++ {
405
buf := NewBuffer(data)
406
_, err := buf.ReadString('x')
413
func TestGrow(t *testing.T) {
416
tmp := make([]byte, 72)
417
for _, startLen := range []int{0, 100, 1000, 10000, 100000} {
418
xBytes := Repeat(x, startLen)
419
for _, growLen := range []int{0, 100, 1000, 10000, 100000} {
420
buf := NewBuffer(xBytes)
421
// If we read, this affects buf.off, which is good to test.
422
readBytes, _ := buf.Read(tmp)
424
yBytes := Repeat(y, growLen)
425
// Check no allocation occurs in write, as long as we're single-threaded.
426
var m1, m2 runtime.MemStats
427
runtime.ReadMemStats(&m1)
429
runtime.ReadMemStats(&m2)
430
if runtime.GOMAXPROCS(-1) == 1 && m1.Mallocs != m2.Mallocs {
431
t.Errorf("allocation occurred during write")
433
// Check that buffer has correct data.
434
if !Equal(buf.Bytes()[0:startLen-readBytes], xBytes[readBytes:]) {
435
t.Errorf("bad initial data at %d %d", startLen, growLen)
437
if !Equal(buf.Bytes()[startLen-readBytes:startLen-readBytes+growLen], yBytes) {
438
t.Errorf("bad written data at %d %d", startLen, growLen)
377
444
// Was a bug: used to give EOF reading empty slice at EOF.
378
445
func TestReadEmptyAtEOF(t *testing.T) {
386
453
t.Errorf("wrong count; got %d want 0", n)
457
func TestUnreadByte(t *testing.T) {
459
b.WriteString("abcdefghijklmnopqrstuvwxyz")
461
_, err := b.ReadBytes('m')
463
t.Fatalf("ReadBytes: %v", err)
468
t.Fatalf("UnreadByte: %v", err)
470
c, err := b.ReadByte()
472
t.Fatalf("ReadByte: %v", err)
475
t.Errorf("ReadByte = %q; want %q", c, 'm')
479
// Tests that we occasionally compact. Issue 5154.
480
func TestBufferGrowth(t *testing.T) {
482
buf := make([]byte, 1024)
485
for i := 0; i < 5<<10; i++ {
493
// (*Buffer).grow allows for 2x capacity slop before sliding,
494
// so set our error threshold at 3x.
496
t.Errorf("buffer cap = %d; too big (grew from %d)", cap1, cap0)
501
func BenchmarkBufferNotEmptyWriteRead(b *testing.B) {
502
buf := make([]byte, 1024)
503
for i := 0; i < b.N; i++ {
506
for i := 0; i < 5<<10; i++ {
513
// Check that we don't compact too often. From Issue 5154.
514
func BenchmarkBufferFullSmallReads(b *testing.B) {
515
buf := make([]byte, 1024)
516
for i := 0; i < b.N; i++ {
519
for b.Len()+20 < b.Cap() {
522
for i := 0; i < 5<<10; i++ {