|
4 | 4 | "bytes"
|
5 | 5 | "fmt"
|
6 | 6 | "io"
|
| 7 | + "math/rand" |
7 | 8 | "testing"
|
8 | 9 | )
|
9 | 10 |
|
@@ -114,3 +115,88 @@ func TestLineReaderAt(t *testing.T) {
|
114 | 115 | })
|
115 | 116 | }
|
116 | 117 | }
|
| 118 | + |
| 119 | +func TestCopyFrom(t *testing.T) { |
| 120 | + tests := map[string]struct { |
| 121 | + Bytes int64 |
| 122 | + Offset int64 |
| 123 | + }{ |
| 124 | + "copyAll": { |
| 125 | + Bytes: byteBufferSize / 2, |
| 126 | + }, |
| 127 | + "copyPartial": { |
| 128 | + Bytes: byteBufferSize / 2, |
| 129 | + Offset: byteBufferSize / 4, |
| 130 | + }, |
| 131 | + "copyLarge": { |
| 132 | + Bytes: 8 * byteBufferSize, |
| 133 | + }, |
| 134 | + } |
| 135 | + |
| 136 | + for name, test := range tests { |
| 137 | + t.Run(name, func(t *testing.T) { |
| 138 | + data := make([]byte, test.Bytes) |
| 139 | + rand.Read(data) |
| 140 | + |
| 141 | + var dst bytes.Buffer |
| 142 | + n, err := copyFrom(&dst, bytes.NewReader(data), test.Offset) |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("unexpected error copying data: %v", err) |
| 145 | + } |
| 146 | + if n != test.Bytes-test.Offset { |
| 147 | + t.Fatalf("incorrect number of bytes copied: expected %d, actual %d", test.Bytes-test.Offset, n) |
| 148 | + } |
| 149 | + |
| 150 | + expected := data[test.Offset:] |
| 151 | + if !bytes.Equal(expected, dst.Bytes()) { |
| 152 | + t.Fatalf("incorrect data copied:\nexpected: %v\nactual: %v", expected, dst.Bytes()) |
| 153 | + } |
| 154 | + }) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func TestCopyLinesFrom(t *testing.T) { |
| 159 | + tests := map[string]struct { |
| 160 | + Lines int64 |
| 161 | + Offset int64 |
| 162 | + }{ |
| 163 | + "copyAll": { |
| 164 | + Lines: lineBufferSize / 2, |
| 165 | + }, |
| 166 | + "copyPartial": { |
| 167 | + Lines: lineBufferSize / 2, |
| 168 | + Offset: lineBufferSize / 4, |
| 169 | + }, |
| 170 | + "copyLarge": { |
| 171 | + Lines: 8 * lineBufferSize, |
| 172 | + }, |
| 173 | + } |
| 174 | + |
| 175 | + const lineLength = 128 |
| 176 | + |
| 177 | + for name, test := range tests { |
| 178 | + t.Run(name, func(t *testing.T) { |
| 179 | + data := make([]byte, test.Lines*lineLength) |
| 180 | + for i := range data { |
| 181 | + data[i] = byte(32 + rand.Intn(95)) // ascii letters, numbers, symbols |
| 182 | + if i%lineLength == lineLength-1 { |
| 183 | + data[i] = '\n' |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + var dst bytes.Buffer |
| 188 | + n, err := copyLinesFrom(&dst, &lineReaderAt{r: bytes.NewReader(data)}, test.Offset) |
| 189 | + if err != nil { |
| 190 | + t.Fatalf("unexpected error copying data: %v", err) |
| 191 | + } |
| 192 | + if n != test.Lines-test.Offset { |
| 193 | + t.Fatalf("incorrect number of lines copied: expected %d, actual %d", test.Lines-test.Offset, n) |
| 194 | + } |
| 195 | + |
| 196 | + expected := data[test.Offset*lineLength:] |
| 197 | + if !bytes.Equal(expected, dst.Bytes()) { |
| 198 | + t.Fatalf("incorrect data copied:\nexpected: %v\nactual: %v", expected, dst.Bytes()) |
| 199 | + } |
| 200 | + }) |
| 201 | + } |
| 202 | +} |
0 commit comments