-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanbuf_test.go
163 lines (140 loc) · 2.69 KB
/
scanbuf_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
package scanbuf
import (
"bytes"
"io"
"io/ioutil"
"os"
"testing"
)
func openfile() (f *os.File, total int64, err error) {
var fi os.FileInfo
f, err = os.Open("LICENSE")
if err == nil {
fi, err = f.Stat()
if err != nil {
f.Close()
return
}
total = fi.Size()
}
return
}
func TestAdvance(T *testing.T) {
f, total, err := openfile()
if err != nil {
T.Fatal(err)
}
defer f.Close()
ExampleScanbuf_Advance(T, New(nil).Source(f), total)
}
func TestWriteTo(T *testing.T) {
f, total, err := openfile()
if err != nil {
T.Fatal(err)
}
defer f.Close()
ExampleScanbuf_WriteTo(T, New(nil).Source(f), total)
}
func TestBufAdvance(T *testing.T) {
f, total, err := openfile()
if err != nil {
T.Fatal(err)
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
T.Fatal(err)
}
if int64(len(b)) != total {
T.Fatalf("ioutil.ReadAll returns %d bytes but want %d", len(b), total)
}
ExampleScanbuf_Advance(T, New(b), total)
}
func TestBufWriteTo(T *testing.T) {
f, total, err := openfile()
if err != nil {
T.Fatal(err)
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
T.Fatal(err)
}
if int64(len(b)) != total {
T.Fatalf("ioutil.ReadAll returns %d bytes but want %d", len(b), total)
}
ExampleScanbuf_WriteTo(T, New(b), total)
}
func ExampleScanbuf_Advance(T *testing.T, buf *Scanbuf, total int64) {
var (
n, off int
s []byte
err error
stop bool
)
lines := 0
for !stop {
// 尽可能的消耗缓冲, 减少缓冲更新次数
s, err = buf.Advance(off)
if len(s) == 0 {
break // EOF 或者发生错误
}
off = bytes.IndexByte(s, '\n')
if off == -1 {
if err != io.EOF {
off = 0
continue
}
off = len(s)
stop = true
} else {
off++
}
lines++
n += off
}
if err != io.EOF {
T.Fatal("want io.EOF but got", err)
}
if lines != 23 {
T.Fatalf("want lines 23 but got %d", lines)
}
// Unix Line Endings (LF)
if int64(n) != total {
T.Fatalf("want total bytes %d but got %d", total, n)
}
}
func ExampleScanbuf_WriteTo(T *testing.T, buf *Scanbuf, total int64) {
var n int
lines := 0
c, err := buf.WriteTo(WriterFunc(func(s []byte) (int, error) {
if len(s) == 0 {
T.Fatal("unexpected got len(s) == 0")
}
off := bytes.IndexByte(s, '\n')
if off == -1 {
if !buf.IsEOF() {
return 0, nil
}
off = len(s)
} else {
off++
}
lines++
n += off
return off, nil
}))
if err != io.EOF {
T.Fatal("want io.EOF but got", err)
}
if lines != 23 {
T.Fatalf("want lines 23 but got %d", lines)
}
// Unix Line Endings (LF)
if int64(n) != total {
T.Fatalf("want total bytes %d but got %d", total, n)
}
if c != total {
T.Fatalf("want total bytes %d but got %d", total, c)
}
}