-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathio_test.go
202 lines (184 loc) · 4.2 KB
/
io_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package gitdiff
import (
"bytes"
"fmt"
"io"
"math/rand"
"testing"
)
func TestLineReaderAt(t *testing.T) {
tests := map[string]struct {
InputLines int
Offset int64
Count int
Err bool
EOF bool
EOFCount int
}{
"readLines": {
InputLines: 32,
Offset: 0,
Count: 4,
},
"readLinesOffset": {
InputLines: 32,
Offset: 8,
Count: 4,
},
"readLinesLargeOffset": {
InputLines: 8192,
Offset: 4096,
Count: 64,
},
"readSingleLine": {
InputLines: 4,
Offset: 2,
Count: 1,
},
"readZeroLines": {
InputLines: 4,
Offset: 2,
Count: 0,
},
"readThroughEOF": {
InputLines: 16,
Offset: 12,
Count: 8,
EOF: true,
EOFCount: 4,
},
"emptyInput": {
InputLines: 0,
Offset: 0,
Count: 2,
EOF: true,
EOFCount: 0,
},
"offsetAfterEOF": {
InputLines: 8,
Offset: 10,
Count: 2,
EOF: true,
EOFCount: 0,
},
"offsetNegative": {
InputLines: 8,
Offset: -1,
Count: 2,
Err: true,
},
}
const lineTemplate = "generated test line %d\n"
for name, test := range tests {
t.Run(name, func(t *testing.T) {
var input bytes.Buffer
for i := 0; i < test.InputLines; i++ {
fmt.Fprintf(&input, lineTemplate, i)
}
output := make([][]byte, test.Count)
for i := 0; i < test.Count; i++ {
output[i] = []byte(fmt.Sprintf(lineTemplate, test.Offset+int64(i)))
}
r := &lineReaderAt{r: bytes.NewReader(input.Bytes())}
lines := make([][]byte, test.Count)
n, err := r.ReadLinesAt(lines, test.Offset)
if test.Err {
if err == nil {
t.Fatal("expected error reading lines, but got nil")
}
return
}
if err != nil && (!test.EOF || err != io.EOF) {
t.Fatalf("unexpected error reading lines: %v", err)
}
count := test.Count
if test.EOF {
count = test.EOFCount
}
if n != count {
t.Fatalf("incorrect number of lines read: expected %d, actual %d", count, n)
}
for i := 0; i < n; i++ {
if !bytes.Equal(output[i], lines[i]) {
t.Errorf("incorrect content in line %d:\nexpected: %q\nactual: %q", i, output[i], lines[i])
}
}
})
}
}
func TestCopyFrom(t *testing.T) {
tests := map[string]struct {
Bytes int64
Offset int64
}{
"copyAll": {
Bytes: byteBufferSize / 2,
},
"copyPartial": {
Bytes: byteBufferSize / 2,
Offset: byteBufferSize / 4,
},
"copyLarge": {
Bytes: 8 * byteBufferSize,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
data := make([]byte, test.Bytes)
rand.Read(data)
var dst bytes.Buffer
n, err := copyFrom(&dst, bytes.NewReader(data), test.Offset)
if err != nil {
t.Fatalf("unexpected error copying data: %v", err)
}
if n != test.Bytes-test.Offset {
t.Fatalf("incorrect number of bytes copied: expected %d, actual %d", test.Bytes-test.Offset, n)
}
expected := data[test.Offset:]
if !bytes.Equal(expected, dst.Bytes()) {
t.Fatalf("incorrect data copied:\nexpected: %v\nactual: %v", expected, dst.Bytes())
}
})
}
}
func TestCopyLinesFrom(t *testing.T) {
tests := map[string]struct {
Lines int64
Offset int64
}{
"copyAll": {
Lines: lineBufferSize / 2,
},
"copyPartial": {
Lines: lineBufferSize / 2,
Offset: lineBufferSize / 4,
},
"copyLarge": {
Lines: 8 * lineBufferSize,
},
}
const lineLength = 128
for name, test := range tests {
t.Run(name, func(t *testing.T) {
data := make([]byte, test.Lines*lineLength)
for i := range data {
data[i] = byte(32 + rand.Intn(95)) // ascii letters, numbers, symbols
if i%lineLength == lineLength-1 {
data[i] = '\n'
}
}
var dst bytes.Buffer
n, err := copyLinesFrom(&dst, &lineReaderAt{r: bytes.NewReader(data)}, test.Offset)
if err != nil {
t.Fatalf("unexpected error copying data: %v", err)
}
if n != test.Lines-test.Offset {
t.Fatalf("incorrect number of lines copied: expected %d, actual %d", test.Lines-test.Offset, n)
}
expected := data[test.Offset*lineLength:]
if !bytes.Equal(expected, dst.Bytes()) {
t.Fatalf("incorrect data copied:\nexpected: %v\nactual: %v", expected, dst.Bytes())
}
})
}
}