| 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 "bytes" |
| 9 "fmt" |
| 10 "io" |
| 11 "testing" |
| 12 |
| 13 . "github.com/smartystreets/goconvey/convey" |
| 14 ) |
| 15 |
| 16 func TestView(t *testing.T) { |
| 17 Convey(`An empty Buffer with generation 42`, t, func() { |
| 18 b := Buffer{} |
| 19 |
| 20 for _, chunks := range [][]*testChunk{ |
| 21 []*testChunk(nil), |
| 22 {tc()}, |
| 23 {tc(0)}, |
| 24 {tc(0, 0, 1)}, |
| 25 {tc(0), tc(0), tc(0)}, |
| 26 {tc(1, 2, 3, 0)}, |
| 27 {tc(1, 2), tc(3, 0, 4, 0), tc(0, 5)}, |
| 28 } { |
| 29 Convey(fmt.Sprintf(`With Chunks %v`, chunks), func() { |
| 30 aggregate := []byte{} |
| 31 size := int64(0) |
| 32 for _, c := range chunks { |
| 33 aggregate = append(aggregate, c.Bytes().
..) |
| 34 size += int64(c.Len()) |
| 35 b.Append(c) |
| 36 } |
| 37 |
| 38 // This is pretty stupid to do, but for testing
it's a nice edge case |
| 39 // to hammer. |
| 40 Convey(`A View with limit 0`, func() { |
| 41 br := b.ViewLimit(0) |
| 42 |
| 43 So(br.Remaining(), ShouldEqual, 0) |
| 44 So(br.Consumed(), ShouldEqual, 0) |
| 45 |
| 46 Convey(`Read() returns EOF.`, func() { |
| 47 buf := make([]byte, 16) |
| 48 a, err := br.Read(buf) |
| 49 So(err, ShouldEqual, io.EOF) |
| 50 So(a, ShouldEqual, 0) |
| 51 }) |
| 52 |
| 53 Convey(`ReadByte() returns EOF.`, func()
{ |
| 54 _, err := br.ReadByte() |
| 55 So(err, ShouldEqual, io.EOF) |
| 56 }) |
| 57 |
| 58 Convey(`Skip() panics.`, func() { |
| 59 So(func() { br.Skip(1) }, Should
Panic) |
| 60 }) |
| 61 |
| 62 Convey(`Index() returns -1.`, func() { |
| 63 So(br.Index([]byte{0}), ShouldEq
ual, -1) |
| 64 }) |
| 65 }) |
| 66 |
| 67 Convey(`An unlimited View`, func() { |
| 68 br := b.View() |
| 69 So(br.Remaining(), ShouldEqual, b.Len()) |
| 70 So(br.Consumed(), ShouldEqual, 0) |
| 71 |
| 72 Convey(`Can Read() the full block of dat
a.`, func() { |
| 73 buf := make([]byte, len(aggregat
e)) |
| 74 amt, err := br.Read(buf) |
| 75 So(amt, ShouldEqual, len(aggrega
te)) |
| 76 So(err, ShouldEqual, io.EOF) |
| 77 So(buf[:amt], ShouldResemble, ag
gregate) |
| 78 |
| 79 Convey(`Subsequent Read() will r
eturn io.EOF.`, func() { |
| 80 amt, err := br.Read(buf) |
| 81 So(amt, ShouldEqual, 0) |
| 82 So(err, ShouldEqual, io.
EOF) |
| 83 }) |
| 84 }) |
| 85 |
| 86 Convey(`Can Read() the full block of dat
a byte-by-byte.`, func() { |
| 87 buf := make([]byte, 1) |
| 88 for i, d := range aggregate { |
| 89 amt, err := br.Read(buf) |
| 90 if i == len(aggregate)-1
{ |
| 91 So(err, ShouldEq
ual, io.EOF) |
| 92 } else { |
| 93 So(err, ShouldBe
Nil) |
| 94 } |
| 95 |
| 96 So(amt, ShouldEqual, 1) |
| 97 So(buf[0], ShouldEqual,
d) |
| 98 } |
| 99 |
| 100 Convey(`Subsequent Read() will r
eturn io.EOF.`, func() { |
| 101 amt, err := br.Read(buf) |
| 102 So(amt, ShouldEqual, 0) |
| 103 So(err, ShouldEqual, io.
EOF) |
| 104 }) |
| 105 }) |
| 106 |
| 107 Convey(`Can ReadByte() the full block of
data.`, func() { |
| 108 for _, d := range aggregate { |
| 109 b, err := br.ReadByte() |
| 110 So(err, ShouldBeNil) |
| 111 So(b, ShouldEqual, d) |
| 112 } |
| 113 |
| 114 Convey(`Subsequent ReadByte() wi
ll return io.EOF.`, func() { |
| 115 _, err := br.ReadByte() |
| 116 So(err, ShouldEqual, io.
EOF) |
| 117 }) |
| 118 }) |
| 119 |
| 120 for _, needle := range [][]byte{ |
| 121 {0}, |
| 122 {0, 0}, |
| 123 {0, 0, 0, 0}, |
| 124 } { |
| 125 expected := bytes.Index(aggregat
e, needle) |
| 126 Convey(fmt.Sprintf(`Index() of %
v returns %v.`, needle, expected), func() { |
| 127 So(br.Index(needle), Sho
uldEqual, expected) |
| 128 }) |
| 129 } |
| 130 }) |
| 131 }) |
| 132 } |
| 133 |
| 134 Convey(`An unlimited View`, func() { |
| 135 br := b.View() |
| 136 |
| 137 Convey(`Has chunksRemaining() value of 0.`, func() { |
| 138 So(br.chunkRemaining(), ShouldEqual, 0) |
| 139 }) |
| 140 }) |
| 141 |
| 142 Convey(`With chunks [{0x01, 0x02, 0x00}, {0x00, 0x03}, {0x00}, {
0x00, 0x00}, {0x04}]`, func() { |
| 143 for _, c := range []Chunk{tc(1, 2, 0), tc(0, 3), tc(0),
tc(0, 0), tc(4)} { |
| 144 b.Append(c) |
| 145 } |
| 146 |
| 147 Convey(`An unlimited View`, func() { |
| 148 br := b.View() |
| 149 |
| 150 Convey(`Should have Remaining() value of 9.`, fu
nc() { |
| 151 So(br.Remaining(), ShouldEqual, 9) |
| 152 }) |
| 153 |
| 154 Convey(`Can spawn a limited clone.`, func() { |
| 155 buf := bytes.Buffer{} |
| 156 _, err := buf.ReadFrom(br.CloneLimit(7)) |
| 157 So(err, ShouldBeNil) |
| 158 So(buf.Bytes(), ShouldResemble, []byte{0
x01, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00}) |
| 159 }) |
| 160 |
| 161 for _, s := range []struct { |
| 162 needle []byte |
| 163 index int64 |
| 164 }{ |
| 165 {[]byte(nil), 0}, |
| 166 {[]byte{0x01}, 0}, |
| 167 {[]byte{0x01, 0x02}, 0}, |
| 168 {[]byte{0x02, 0x00}, 1}, |
| 169 {[]byte{0x00}, 2}, |
| 170 {[]byte{0x00, 0x00}, 2}, |
| 171 {[]byte{0x00, 0x00, 0x00}, 5}, |
| 172 {[]byte{0x03, 0x00, 0x00}, 4}, |
| 173 } { |
| 174 Convey(fmt.Sprintf(`Has Index %v for nee
dle %v`, s.index, s.needle), func() { |
| 175 So(br.Index(s.needle), ShouldEqu
al, s.index) |
| 176 }) |
| 177 } |
| 178 }) |
| 179 |
| 180 Convey(`A View with a limit of 6`, func() { |
| 181 br := b.ViewLimit(6) |
| 182 |
| 183 Convey(`Should have Remaining() value of 6.`, fu
nc() { |
| 184 So(br.Remaining(), ShouldEqual, 6) |
| 185 }) |
| 186 |
| 187 Convey(`Has index of -1 for needle [0x00, 0x04]`
, func() { |
| 188 So(br.Index([]byte{0x00, 0x04}), ShouldE
qual, -1) |
| 189 }) |
| 190 }) |
| 191 |
| 192 Convey(`A View with a limit of 20`, func() { |
| 193 br := b.ViewLimit(20) |
| 194 |
| 195 Convey(`Should have Remaining() value of 9.`, fu
nc() { |
| 196 So(br.Remaining(), ShouldEqual, 9) |
| 197 }) |
| 198 }) |
| 199 }) |
| 200 |
| 201 Convey(`With chunks [{0x0F}..{0x00}]`, func() { |
| 202 for i := 0x0F; i >= 0x00; i-- { |
| 203 b.Append(tc(byte(i))) |
| 204 } |
| 205 br := b.View() |
| 206 |
| 207 Convey(`Has index of -1 for needle [0x0f, 0x10]`, func()
{ |
| 208 So(br.Index([]byte{0x0f, 0x10}), ShouldEqual, -1
) |
| 209 }) |
| 210 |
| 211 for _, s := range []struct { |
| 212 needle []byte |
| 213 index int64 |
| 214 }{ |
| 215 {[]byte{0x0F}, 0}, |
| 216 {[]byte{0x04, 0x03, 0x02}, 11}, |
| 217 {[]byte{0x01, 0x00}, 14}, |
| 218 {[]byte{0x00}, 15}, |
| 219 {[]byte{0x00, 0xFF}, -1}, |
| 220 } { |
| 221 Convey(fmt.Sprintf(`Has Index %v for needle %v`,
s.index, s.needle), func() { |
| 222 So(br.Index(s.needle), ShouldEqual, s.in
dex) |
| 223 }) |
| 224 } |
| 225 }) |
| 226 }) |
| 227 } |
| OLD | NEW |