~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/bytes/buffer_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
        . "bytes"
9
9
        "io"
10
10
        "math/rand"
 
11
        "runtime"
11
12
        "testing"
12
13
        "unicode/utf8"
13
14
)
14
15
 
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.
18
19
 
19
20
func init() {
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)
23
24
        }
24
 
        data = string(bytes)
 
25
        data = string(testBytes)
25
26
}
26
27
 
27
28
// Verify that contents of buf match the string s.
84
85
}
85
86
 
86
87
func TestNewBuffer(t *testing.T) {
87
 
        buf := NewBuffer(bytes)
 
88
        buf := NewBuffer(testBytes)
88
89
        check(t, "NewBuffer", buf, data)
89
90
}
90
91
 
187
188
                limit = 9
188
189
        }
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))
192
193
        }
193
194
        check(t, "TestLargeByteWrites (3)", &buf, "")
205
206
func TestLargeByteReads(t *testing.T) {
206
207
        var buf Buffer
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)))
210
211
        }
211
212
        check(t, "TestLargeByteReads (3)", &buf, "")
219
220
                if i%2 == 0 {
220
221
                        s = fillString(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, data[0:wlen])
221
222
                } else {
222
 
                        s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, bytes[0:wlen])
 
223
                        s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, testBytes[0:wlen])
223
224
                }
224
225
 
225
226
                rlen := rand.Intn(len(data))
240
241
func TestReadFrom(t *testing.T) {
241
242
        var buf Buffer
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])
244
245
                var b Buffer
245
246
                b.ReadFrom(&buf)
246
247
                empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data)))
250
251
func TestWriteTo(t *testing.T) {
251
252
        var buf Buffer
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])
254
255
                var b Buffer
255
256
                buf.WriteTo(&b)
256
 
                empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data)))
 
257
                empty(t, "TestWriteTo (2)", &b, s, make([]byte, len(data)))
257
258
        }
258
259
}
259
260
 
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)
264
265
        var buf Buffer
265
266
        n := 0
374
375
        }
375
376
}
376
377
 
 
378
func TestReadString(t *testing.T) {
 
379
        for _, test := range readBytesTests {
 
380
                buf := NewBufferString(test.buffer)
 
381
                var err error
 
382
                for _, expected := range test.expected {
 
383
                        var s string
 
384
                        s, err = buf.ReadString(test.delim)
 
385
                        if s != expected {
 
386
                                t.Errorf("expected %q, got %q", expected, s)
 
387
                        }
 
388
                        if err != nil {
 
389
                                break
 
390
                        }
 
391
                }
 
392
                if err != test.err {
 
393
                        t.Errorf("expected error %v, got %v", test.err, err)
 
394
                }
 
395
        }
 
396
}
 
397
 
 
398
func BenchmarkReadString(b *testing.B) {
 
399
        const n = 32 << 10
 
400
 
 
401
        data := make([]byte, n)
 
402
        data[n-1] = 'x'
 
403
        b.SetBytes(int64(n))
 
404
        for i := 0; i < b.N; i++ {
 
405
                buf := NewBuffer(data)
 
406
                _, err := buf.ReadString('x')
 
407
                if err != nil {
 
408
                        b.Fatal(err)
 
409
                }
 
410
        }
 
411
}
 
412
 
 
413
func TestGrow(t *testing.T) {
 
414
        x := []byte{'x'}
 
415
        y := []byte{'y'}
 
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)
 
423
                        buf.Grow(growLen)
 
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)
 
428
                        buf.Write(yBytes)
 
429
                        runtime.ReadMemStats(&m2)
 
430
                        if runtime.GOMAXPROCS(-1) == 1 && m1.Mallocs != m2.Mallocs {
 
431
                                t.Errorf("allocation occurred during write")
 
432
                        }
 
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)
 
436
                        }
 
437
                        if !Equal(buf.Bytes()[startLen-readBytes:startLen-readBytes+growLen], yBytes) {
 
438
                                t.Errorf("bad written data at %d %d", startLen, growLen)
 
439
                        }
 
440
                }
 
441
        }
 
442
}
 
443
 
377
444
// Was a bug: used to give EOF reading empty slice at EOF.
378
445
func TestReadEmptyAtEOF(t *testing.T) {
379
446
        b := new(Buffer)
386
453
                t.Errorf("wrong count; got %d want 0", n)
387
454
        }
388
455
}
 
456
 
 
457
func TestUnreadByte(t *testing.T) {
 
458
        b := new(Buffer)
 
459
        b.WriteString("abcdefghijklmnopqrstuvwxyz")
 
460
 
 
461
        _, err := b.ReadBytes('m')
 
462
        if err != nil {
 
463
                t.Fatalf("ReadBytes: %v", err)
 
464
        }
 
465
 
 
466
        err = b.UnreadByte()
 
467
        if err != nil {
 
468
                t.Fatalf("UnreadByte: %v", err)
 
469
        }
 
470
        c, err := b.ReadByte()
 
471
        if err != nil {
 
472
                t.Fatalf("ReadByte: %v", err)
 
473
        }
 
474
        if c != 'm' {
 
475
                t.Errorf("ReadByte = %q; want %q", c, 'm')
 
476
        }
 
477
}
 
478
 
 
479
// Tests that we occasionally compact. Issue 5154.
 
480
func TestBufferGrowth(t *testing.T) {
 
481
        var b Buffer
 
482
        buf := make([]byte, 1024)
 
483
        b.Write(buf[0:1])
 
484
        var cap0 int
 
485
        for i := 0; i < 5<<10; i++ {
 
486
                b.Write(buf)
 
487
                b.Read(buf)
 
488
                if i == 0 {
 
489
                        cap0 = b.Cap()
 
490
                }
 
491
        }
 
492
        cap1 := b.Cap()
 
493
        // (*Buffer).grow allows for 2x capacity slop before sliding,
 
494
        // so set our error threshold at 3x.
 
495
        if cap1 > cap0*3 {
 
496
                t.Errorf("buffer cap = %d; too big (grew from %d)", cap1, cap0)
 
497
        }
 
498
}
 
499
 
 
500
// From Issue 5154.
 
501
func BenchmarkBufferNotEmptyWriteRead(b *testing.B) {
 
502
        buf := make([]byte, 1024)
 
503
        for i := 0; i < b.N; i++ {
 
504
                var b Buffer
 
505
                b.Write(buf[0:1])
 
506
                for i := 0; i < 5<<10; i++ {
 
507
                        b.Write(buf)
 
508
                        b.Read(buf)
 
509
                }
 
510
        }
 
511
}
 
512
 
 
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++ {
 
517
                var b Buffer
 
518
                b.Write(buf)
 
519
                for b.Len()+20 < b.Cap() {
 
520
                        b.Write(buf[:10])
 
521
                }
 
522
                for i := 0; i < 5<<10; i++ {
 
523
                        b.Read(buf[:1])
 
524
                        b.Write(buf[:1])
 
525
                }
 
526
        }
 
527
}