-
Notifications
You must be signed in to change notification settings - Fork 9
/
compressor_test.go
166 lines (134 loc) · 3.29 KB
/
compressor_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
package libdeflate
import (
"bytes"
"compress/flate"
"compress/gzip"
"compress/zlib"
"io"
"testing"
)
var shortString = []byte("hello, world\nhello, world\nhello, world\nhello, world\nhello, world\nhello, world\nhello, world\nhello, world\nhello, world\nhello, world\nhello, world\n")
/*---------------------
UNIT TESTS
-----------------------*/
func TestNewCompressor(t *testing.T) {
c, err := NewCompressor()
if err != nil {
t.Error(err)
}
defer c.Close()
c, err = NewCompressorLevel(30)
if err == nil {
t.Fail()
}
}
func TestCompressDEFLATE(t *testing.T) {
c, _ := NewCompressor()
defer c.Close()
_, comp, err := c.Compress(shortString, nil, ModeDEFLATE)
if err != nil {
t.Error(err)
}
b := bytes.NewBuffer(comp)
r := flate.NewReader(b)
defer r.Close()
dc := &bytes.Buffer{}
io.Copy(dc, r)
slicesEqual(shortString, dc.Bytes(), t)
}
func TestCompressGzip(t *testing.T) {
c, _ := NewCompressor()
defer c.Close()
_, comp, err := c.Compress(shortString, nil, ModeGzip)
if err != nil {
t.Error(err)
}
b := bytes.NewBuffer(comp)
r, err := gzip.NewReader(b)
if err != nil {
t.Error(err)
}
defer r.Close()
dc := &bytes.Buffer{}
io.Copy(dc, r)
slicesEqual(shortString, dc.Bytes(), t)
}
func TestCompressZlibMaxComp(t *testing.T) {
c, _ := NewCompressorLevel(MaxStdZlibCompressionLevel)
defer c.Close()
_, comp, err := c.CompressZlib(shortString, nil)
if err != nil {
t.Error(err)
}
r, _ := zlib.NewReader(bytes.NewBuffer(comp))
defer r.Close()
decomp := make([]byte, len(shortString))
r.Read(decomp)
slicesEqual(shortString, decomp, t)
}
func TestCompressZlib(t *testing.T) {
c, _ := NewCompressor()
defer c.Close()
_, comp, err := c.CompressZlib(shortString, nil)
if err != nil {
t.Error(err)
}
r, _ := zlib.NewReader(bytes.NewBuffer(comp))
defer r.Close()
decomp := make([]byte, len(shortString))
r.Read(decomp)
slicesEqual([]byte(shortString), decomp, t)
}
// this test doesn't really say as much as TestCompress
func TestCompressMeta(t *testing.T) {
c, _ := NewCompressor()
defer c.Close()
if _, _, err := c.CompressZlib(make([]byte, 0), nil); err == nil {
t.Error("expected error")
}
n, out, err := c.CompressZlib(shortString, nil)
if err != nil || n == 0 || n >= len(shortString) || n != len(out) {
t.Error(err)
t.Error(n)
}
out2 := make([]byte, len(shortString))
n, _, err = c.CompressZlib(shortString, out2)
if err != nil || n == 0 {
t.Error(err)
t.Error(n)
}
slicesEqual(out, out2[:n], t) //tests if rep produces same results
}
/*---------------------
INTEGRATION TESTS
-----------------------*/
func TestCompressDecompress(t *testing.T) {
c, _ := NewCompressor()
defer c.Close()
_, comp, err := c.CompressZlib(shortString, nil)
if err != nil {
t.Error(err)
}
out := make([]byte, len(shortString))
dc, _ := NewDecompressor()
defer dc.Close()
if _, err := dc.DecompressZlib(comp, out); err != nil {
t.Error(err)
}
slicesEqual([]byte(shortString), out, t)
}
/*---------------------
HELPER
-----------------------*/
func slicesEqual(expected, actual []byte, t *testing.T) {
if len(expected) != len(actual) {
t.Error("len of slices unequal")
t.FailNow()
}
for i := range expected {
if expected[i] != actual[i] {
t.Errorf("slices differ at %d: want: %d, got: %d", i, expected[i], actual[i])
t.FailNow()
}
}
}