-
Notifications
You must be signed in to change notification settings - Fork 0
/
gozlib_stream_test.go
229 lines (171 loc) · 5.52 KB
/
gozlib_stream_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
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
package gozlib
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGZipCompressStream(t *testing.T) {
const originalLen = 1023 * 10
const inputBufferSize = 1024
const outputBufferSize = 1024
verifyCompressStream(originalLen, inputBufferSize, outputBufferSize, t)
}
func TestGZipCompressStreamEmptyInput(t *testing.T) {
const originalLen = 0
const inputBufferSize = 1024
const outputBufferSize = 1024
verifyCompressStream(originalLen, inputBufferSize, outputBufferSize, t)
}
func TestGZipCompressStreamOutputBufferSmallerThanInput(t *testing.T) {
const originalLen = 2048
const inputBufferSize = 1024
const outputBufferSize = 64
verifyCompressStream(originalLen, inputBufferSize, outputBufferSize, t)
}
func TestGZipCompressStreamInputBufferSmallerThanOutput(t *testing.T) {
const originalLen = 2048
const inputBufferSize = 64
const outputBufferSize = 1024
verifyCompressStream(originalLen, inputBufferSize, outputBufferSize, t)
}
func TestGZipCompressStreamFailToWrite(t *testing.T) {
inputReader := func(data []byte) uint32 {
// it doesn't really matter what we read here
return uint32(len(data))
}
outputWriter := func(data []byte) uint32 {
// 0 is only returned on failure
return 0
}
total, err := GoGZipCompressStream(CompressionLevelBestCompression, 100, 100, inputReader, outputWriter)
assert.ErrorIs(t, err, StreamCompressError)
assert.Equal(t, total, uint64(0))
}
func TestUncompressStream(t *testing.T) {
const originalLen = 2048 * 7
const inputBufferSize = 1024
const outputBufferSize = 1024
verifyUncompressStream(originalLen, inputBufferSize, outputBufferSize, t)
}
func TestUncompressStreamOutputBufferSmallerThanInput(t *testing.T) {
const originalLen = 2048
const inputBufferSize = 1024
const outputBufferSize = 64
verifyUncompressStream(originalLen, inputBufferSize, outputBufferSize, t)
}
func TestUncompressStreamInputBufferSmallerThanOutput(t *testing.T) {
const originalLen = 2048
const inputBufferSize = 64
const outputBufferSize = 1024
verifyUncompressStream(originalLen, inputBufferSize, outputBufferSize, t)
}
func TestUncompressStreamFailInvalidInput(t *testing.T) {
const originalLen = 2048
const inputBufferSize = 1024
const outputBufferSize = 64
inputReader := func(data []byte) uint32 {
// generate invalid data
for i := 0; i < len(data); i++ {
data[i] = byte(i)
}
return uint32(len(data))
}
outputWriter := func(data []byte) uint32 {
return uint32(len(data))
}
total, err := GoUncompressStream(100, 100, inputReader, outputWriter)
assert.ErrorIs(t, err, StreamUncompressError)
assert.Equal(t, uint64(0), total)
}
func TestCompressUncompressStream(t *testing.T) {
const originalLen = 5729
const inputBufferSize = 733
const outputBufferSize = 691
original := makeTestData(originalLen)
uncompBuffer := bytes.NewBuffer(original)
compressed := bytes.NewBuffer([]byte{})
compInputReader := func(data []byte) uint32 {
read, err := uncompBuffer.Read(data)
if err != nil {
return 0
}
return uint32(read)
}
compOutputWriter := func(data []byte) uint32 {
written, err := compressed.Write(data)
if err != nil {
return 0
}
return uint32(written)
}
_, err := GoGZipCompressStream(CompressionLevelBestCompression, inputBufferSize, outputBufferSize, compInputReader, compOutputWriter)
assert.NoError(t, err)
uncompInputReader := func(data []byte) uint32 {
read, err := compressed.Read(data)
if err != nil {
return 0
}
return uint32(read)
}
uncompressed := bytes.NewBuffer([]byte{})
uncompOutputWriter := func(data []byte) uint32 {
written, err := uncompressed.Write(data)
if err != nil {
return 0
}
return uint32(written)
}
uncompTotal, err := GoUncompressStream(inputBufferSize, outputBufferSize, uncompInputReader, uncompOutputWriter)
assert.NoError(t, err)
assert.Equal(t, uint64(originalLen), uncompTotal)
assert.Equal(t, original, uncompressed.Bytes())
}
func verifyUncompressStream(originalLen uint32, inputBufferSize uint32, outputBufferSize uint32, t *testing.T) {
original := makeTestData(originalLen)
compressed, stdCompErr := stdLibGZipCompress(original)
assert.NoError(t, stdCompErr)
inputReader := func(data []byte) uint32 {
read, err := compressed.Read(data)
if err != nil {
return 0
}
return uint32(read)
}
uncompressed := bytes.NewBuffer([]byte{})
outputWriter := func(data []byte) uint32 {
written, err := uncompressed.Write(data)
if err != nil {
return 0
}
return uint32(written)
}
total, err := GoUncompressStream(inputBufferSize, outputBufferSize, inputReader, outputWriter)
assert.NoError(t, err)
assert.Equal(t, uint64(originalLen), total)
assert.Equal(t, original, uncompressed.Bytes())
}
func verifyCompressStream(originalLen uint32, inputBufferSize uint32, outputBufferSize uint32, t *testing.T) {
original := makeTestData(originalLen)
uncompBuffer := bytes.NewBuffer(original)
compressed := bytes.NewBuffer([]byte{})
inputReader := func(data []byte) uint32 {
read, err := uncompBuffer.Read(data)
if err != nil {
return 0
}
return uint32(read)
}
outputWriter := func(data []byte) uint32 {
written, err := compressed.Write(data)
if err != nil {
return 0
}
return uint32(written)
}
total, err := GoGZipCompressStream(CompressionLevelBestCompression, inputBufferSize, outputBufferSize, inputReader, outputWriter)
assert.NoError(t, err)
assert.Greater(t, total, uint64(0))
stdUncompressed, uncompErr := stdLibGZipUncompress(compressed, int64(originalLen))
assert.NoError(t, uncompErr)
assert.Equal(t, stdUncompressed, original)
}