| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package chunkstream |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 "testing" |
| 10 |
| 11 . "github.com/smartystreets/goconvey/convey" |
| 12 ) |
| 13 |
| 14 func TestBuffer(t *testing.T) { |
| 15 Convey(`An empty Buffer instance`, t, func() { |
| 16 b := Buffer{} |
| 17 |
| 18 Convey(`Has a length of zero.`, func() { |
| 19 So(b.Len(), ShouldEqual, 0) |
| 20 }) |
| 21 |
| 22 Convey(`Has a FirstChunk of nil.`, func() { |
| 23 So(b.FirstChunk(), ShouldEqual, nil) |
| 24 }) |
| 25 |
| 26 Convey(`Will panic if more than zero bytes are consumed.`, func(
) { |
| 27 So(func() { b.Consume(1) }, ShouldPanic) |
| 28 }) |
| 29 |
| 30 Convey(`When Appending an empty chunk`, func() { |
| 31 c := tc() |
| 32 b.Append(c) |
| 33 |
| 34 Convey(`Has a FirstChunk of nil.`, func() { |
| 35 So(b.FirstChunk(), ShouldEqual, nil) |
| 36 }) |
| 37 |
| 38 Convey(`The Chunk is released.`, func() { |
| 39 So(c.released, ShouldBeTrue) |
| 40 }) |
| 41 }) |
| 42 |
| 43 for _, chunks := range [][]*testChunk{ |
| 44 {}, |
| 45 {tc()}, |
| 46 {tc(0, 1, 2), tc(), tc(3, 4, 5)}, |
| 47 } { |
| 48 Convey(fmt.Sprintf(`With chunks %v, can append.`, chunks
), func() { |
| 49 size := int64(0) |
| 50 coalesced := []byte(nil) |
| 51 for _, c := range chunks { |
| 52 b.Append(c) |
| 53 coalesced = append(coalesced, c.Bytes().
..) |
| 54 size += int64(c.Len()) |
| 55 So(b.Len(), ShouldEqual, size) |
| 56 } |
| 57 So(b.Bytes(), ShouldResemble, coalesced) |
| 58 |
| 59 Convey(`Can consume chunk-at-a-time.`, func() { |
| 60 for i, c := range chunks { |
| 61 if c.Len() > 0 { |
| 62 So(b.FirstChunk(), Shoul
dEqual, chunks[i]) |
| 63 } |
| 64 So(b.Len(), ShouldEqual, size) |
| 65 b.Consume(int64(c.Len())) |
| 66 size -= int64(c.Len()) |
| 67 } |
| 68 So(b.Len(), ShouldEqual, 0) |
| 69 |
| 70 Convey(`All chunks are released.`, func(
) { |
| 71 for _, c := range chunks { |
| 72 So(c.released, ShouldBeT
rue) |
| 73 } |
| 74 }) |
| 75 }) |
| 76 |
| 77 Convey(`Can consume byte-at-a-time.`, func() { |
| 78 for i := int64(0); i < size; i++ { |
| 79 So(b.Len(), ShouldEqual, (size -
i)) |
| 80 So(b.Bytes(), ShouldResemble, co
alesced[i:]) |
| 81 b.Consume(1) |
| 82 } |
| 83 So(b.Len(), ShouldEqual, 0) |
| 84 |
| 85 Convey(`All chunks are released.`, func(
) { |
| 86 for _, c := range chunks { |
| 87 So(c.released, ShouldBeT
rue) |
| 88 } |
| 89 }) |
| 90 }) |
| 91 |
| 92 Convey(`Can consume two bytes at a time.`, func(
) { |
| 93 for i := int64(0); i < size; i += 2 { |
| 94 // Final byte(s), make sure we d
on't over-consume. |
| 95 if b.Len() < 2 { |
| 96 i = b.Len() |
| 97 } |
| 98 |
| 99 So(b.Len(), ShouldEqual, (size -
i)) |
| 100 So(b.Bytes(), ShouldResemble, co
alesced[i:]) |
| 101 b.Consume(2) |
| 102 } |
| 103 So(b.Len(), ShouldEqual, 0) |
| 104 |
| 105 Convey(`All chunks are released.`, func(
) { |
| 106 for _, c := range chunks { |
| 107 So(c.released, ShouldBeT
rue) |
| 108 } |
| 109 }) |
| 110 }) |
| 111 |
| 112 Convey(`Can consume all at once.`, func() { |
| 113 b.Consume(size) |
| 114 So(b.Len(), ShouldEqual, 0) |
| 115 |
| 116 Convey(`All chunks are released.`, func(
) { |
| 117 for _, c := range chunks { |
| 118 So(c.released, ShouldBeT
rue) |
| 119 } |
| 120 }) |
| 121 }) |
| 122 }) |
| 123 } |
| 124 }) |
| 125 } |
| OLD | NEW |