| 129 | } |
| 130 | |
| 131 | func (s) TestBufferSlice_Reader(t *testing.T) { |
| 132 | bs := mem.BufferSlice{ |
| 133 | newBuffer([]byte("abcd"), nil), |
| 134 | newBuffer([]byte("abcd"), nil), |
| 135 | newBuffer([]byte("abcd"), nil), |
| 136 | } |
| 137 | wantData := []byte("abcdabcdabcd") |
| 138 | |
| 139 | reader := bs.Reader() |
| 140 | var gotData []byte |
| 141 | // Read into a buffer of size 1 until EOF, and verify that the data matches. |
| 142 | for { |
| 143 | buf := make([]byte, 1) |
| 144 | n, err := reader.Read(buf) |
| 145 | if n > 0 { |
| 146 | gotData = append(gotData, buf[:n]...) |
| 147 | } |
| 148 | if err == io.EOF { |
| 149 | break |
| 150 | } |
| 151 | if err != nil { |
| 152 | t.Fatalf("BufferSlice.Reader() failed unexpectedly: %v", err) |
| 153 | } |
| 154 | } |
| 155 | if !bytes.Equal(gotData, wantData) { |
| 156 | t.Errorf("BufferSlice.Reader() returned data %v, want %v", string(gotData), string(wantData)) |
| 157 | } |
| 158 | |
| 159 | // Reader should have released its references to the underlying buffers, but |
| 160 | // bs still holds its reference and it should be able to read data from it. |
| 161 | gotData = bs.Materialize() |
| 162 | if !bytes.Equal(gotData, wantData) { |
| 163 | t.Errorf("BufferSlice.Materialize() = %s, want %s", string(gotData), string(wantData)) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // TestBufferSlice_ReadAll_Reads exercises ReadAll by allowing it to read |
| 168 | // various combinations of data, empty data, EOF. |