-
Notifications
You must be signed in to change notification settings - Fork 10
/
reader_test.go
91 lines (71 loc) · 1.42 KB
/
reader_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
package zlib
import (
"bytes"
"compress/zlib"
"io"
"testing"
)
// UNIT TESTS
func TestReadBytes(t *testing.T) {
b := &bytes.Buffer{}
w := zlib.NewWriter(b)
w.Write(longString)
w.Close()
r, err := NewReader(nil)
if err != nil {
t.Error(err)
}
defer r.Close()
_, act, err := r.ReadBuffer(b.Bytes(), nil)
if err != nil {
t.Error(err)
}
sliceEquals(t, longString, act)
}
func initTestRead(t *testing.T, bufferSize int) (*bytes.Buffer, *zlib.Writer, *Reader, func(r *Reader) error) {
b := &bytes.Buffer{}
out := &bytes.Buffer{}
w := zlib.NewWriter(b)
r, err := NewReader(b)
if err != nil {
t.Error(err)
t.FailNow()
}
read := func(r *Reader) error {
p := make([]byte, bufferSize)
n, err := r.Read(p)
if err != nil && err != io.EOF {
t.Error(err)
t.Error(n)
t.FailNow()
}
out.Write(p[:n])
return err // io.EOF or nil
}
return out, w, r, read
}
func TestRead_SufficientBuffer(t *testing.T) {
out, w, r, read := initTestRead(t, 1e+4)
defer r.Close()
w.Write(shortString)
w.Flush()
read(r)
w.Write(shortString)
w.Close()
read(r)
sliceEquals(t, append(shortString, shortString...), out.Bytes())
}
func TestRead_SmallBuffer(t *testing.T) {
out, w, r, read := initTestRead(t, 1)
defer r.Close()
w.Write(shortString)
w.Write(shortString)
w.Close()
for {
err := read(r)
if err == io.EOF {
break
}
}
sliceEquals(t, append(shortString, shortString...), out.Bytes())
}