-
Notifications
You must be signed in to change notification settings - Fork 14
/
buffered_test.go
101 lines (84 loc) · 2.64 KB
/
buffered_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
package readahead
import (
"bufio"
"rare/pkg/testutil"
"strings"
"testing"
"testing/iotest"
"github.com/stretchr/testify/assert"
)
func TestBasicReadingShortBuf(t *testing.T) {
r := strings.NewReader("Hello there you\nthis is line 2\n")
ra := NewBuffered(r, 3)
assert.Equal(t, []byte("Hello there you"), ra.ReadLine())
assert.Equal(t, []byte("this is line 2"), ra.ReadLine())
assert.Nil(t, ra.ReadLine())
}
func TestBasicReadingLongBuf(t *testing.T) {
r := strings.NewReader("Hello there you\nthis is line 2\n")
ra := NewBuffered(r, 1024)
assert.Equal(t, []byte("Hello there you"), ra.ReadLine())
assert.Equal(t, []byte("this is line 2"), ra.ReadLine())
assert.Nil(t, ra.ReadLine())
}
func TestBasicReadingMidBuf(t *testing.T) {
r := strings.NewReader("Hello there you\nthis is line 2\n")
ra := NewBuffered(r, 20) // Just enough to read first line, but not both
assert.Equal(t, []byte("Hello there you"), ra.ReadLine())
assert.Equal(t, []byte("this is line 2"), ra.ReadLine())
assert.Nil(t, ra.ReadLine())
}
func TestBasicReadingNoNewTerm(t *testing.T) {
r := strings.NewReader("Hello there you\nthis is line 2")
ra := NewBuffered(r, 3)
assert.Equal(t, []byte("Hello there you"), ra.ReadLine())
assert.Equal(t, []byte("this is line 2"), ra.ReadLine())
assert.Nil(t, ra.ReadLine())
}
func TestReadEmptyString(t *testing.T) {
r := strings.NewReader("")
ra := NewBuffered(r, 3)
assert.Nil(t, ra.ReadLine())
}
func TestReadSingleCharString(t *testing.T) {
r := strings.NewReader("A")
ra := NewBuffered(r, 3)
assert.Equal(t, []byte("A"), ra.ReadLine())
}
func TestBufferedDropCR(t *testing.T) {
r := strings.NewReader("test\r\nthing")
ra := NewBuffered(r, 3)
assert.Equal(t, []byte("test"), ra.ReadLine())
assert.Equal(t, []byte("thing"), ra.ReadLine())
assert.Nil(t, ra.ReadLine())
}
func TestErrorHandling(t *testing.T) {
errReader := iotest.TimeoutReader(strings.NewReader("Hello there you\nthis is a line\n"))
ra := NewBuffered(errReader, 20)
var hadError bool
ra.OnError(func(e error) {
hadError = true
})
assert.Equal(t, []byte("Hello there you"), ra.ReadLine())
assert.Equal(t, []byte("this"), ra.ReadLine()) // up to twentyith char
assert.Nil(t, ra.ReadLine())
assert.True(t, hadError)
}
func BenchmarkBuffered(b *testing.B) {
r := testutil.NewTextGenerator(1024)
ra := NewBuffered(r, 128*128)
for i := 0; i < b.N; i++ {
ra.Scan()
}
}
func BenchmarkScanner(b *testing.B) {
r := testutil.NewTextGenerator(1024)
s := bufio.NewScanner(r)
for i := 0; i < b.N; i++ {
s.Scan()
// Copy into a new memory slot as practically that's needed for how its consumed
r := s.Bytes()
data := make([]byte, len(r))
copy(data, r)
}
}