File: io_test.go

package info (click to toggle)
golang-github-gitleaks-go-gitdiff 0.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 800 kB
  • sloc: makefile: 2; sh: 1
file content (254 lines) | stat: -rw-r--r-- 5,565 bytes parent folder | download
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package gitdiff

import (
	"bytes"
	"fmt"
	"io"
	"math/rand"
	"testing"
)

func TestLineReaderAt(t *testing.T) {
	const lineTemplate = "generated test line %d\n"

	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,
		},
		"readAllLines": {
			InputLines: 64,
			Offset:     0,
			Count:      64,
		},
		"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,
		},
	}

	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])
				}
			}
		})
	}

	newlineTests := map[string]struct {
		InputSize int
	}{
		"readLinesNoFinalNewline": {
			InputSize: indexBufferSize + indexBufferSize/2,
		},
		"readLinesNoFinalNewlineBufferMultiple": {
			InputSize: 4 * indexBufferSize,
		},
	}

	for name, test := range newlineTests {
		t.Run(name, func(t *testing.T) {
			input := bytes.Repeat([]byte("0"), test.InputSize)

			var output [][]byte
			for i := 0; i < len(input); i++ {
				last := i
				i += rand.Intn(80)
				if i < len(input)-1 { // last character of input must not be a newline
					input[i] = '\n'
					output = append(output, input[last:i+1])
				} else {
					output = append(output, input[last:])
				}
			}

			r := &lineReaderAt{r: bytes.NewReader(input)}
			lines := make([][]byte, len(output))

			n, err := r.ReadLinesAt(lines, 0)
			if err != nil {
				t.Fatalf("unexpected error reading reading lines: %v", err)
			}

			if n != len(output) {
				t.Fatalf("incorrect number of lines read: expected %d, actual %d", len(output), n)
			}

			for i, line := range lines {
				if !bytes.Equal(output[i], line) {
					t.Errorf("incorrect content in line %d:\nexpected: %q\nactual: %q", i, output[i], line)
				}
			}
		})
	}
}

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())
			}
		})
	}
}