forked from valyala/gozstd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_test.go
190 lines (167 loc) · 5.03 KB
/
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
package gozstd
import (
"bytes"
"fmt"
"testing"
"time"
)
func TestStreamCompressDecompress(t *testing.T) {
testStreamCompressDecompress(t, "")
testStreamCompressDecompress(t, "a")
testStreamCompressDecompress(t, "foo bar")
for blockSize := range []int{11, 111, 1111, 11111, 111111, 211111} {
data := newTestString(blockSize, 3)
testStreamCompressDecompress(t, data)
}
}
func testStreamCompressDecompress(t *testing.T, data string) {
t.Helper()
// Serial test.
if err := testStreamCompressDecompressSerial(data); err != nil {
t.Fatalf("error in serial test: %s", err)
}
// Concurrent test.
ch := make(chan error, 3)
for i := 0; i < cap(ch); i++ {
go func() {
ch <- testStreamCompressDecompressSerial(data)
}()
}
for i := 0; i < cap(ch); i++ {
select {
case err := <-ch:
if err != nil {
t.Fatalf("error in concurrent test: %s", err)
}
case <-time.After(time.Second):
t.Fatalf("timeout in concurrent test")
}
}
}
func testStreamCompressDecompressSerial(data string) error {
var bbCompress bytes.Buffer
if err := StreamCompress(&bbCompress, bytes.NewBufferString(data)); err != nil {
return fmt.Errorf("cannot compress stream of size %d: %s", len(data), err)
}
var bbDecompress bytes.Buffer
if err := StreamDecompress(&bbDecompress, &bbCompress); err != nil {
return fmt.Errorf("cannot decompress stream of size %d: %s", len(data), err)
}
plainData := bbDecompress.Bytes()
if string(plainData) != data {
return fmt.Errorf("unexpected decompressed data; got\n%q; want\n%q", plainData, data)
}
return nil
}
func TestStreamCompressDecompressLevel(t *testing.T) {
for level := 0; level < 20; level++ {
t.Run(fmt.Sprintf("level_%d", level), func(t *testing.T) {
testStreamCompressDecompressLevel(t, "", level)
testStreamCompressDecompressLevel(t, "a", level)
testStreamCompressDecompressLevel(t, "foo bar", level)
for blockSize := range []int{11, 111, 1111, 11111, 143333} {
data := newTestString(blockSize, 3)
testStreamCompressDecompressLevel(t, data, level)
}
})
}
}
func testStreamCompressDecompressLevel(t *testing.T, data string, level int) {
t.Helper()
// Serial test.
if err := testStreamCompressDecompressLevelSerial(data, level); err != nil {
t.Fatalf("error in serial test: %s", err)
}
// Concurrent test.
ch := make(chan error, 3)
for i := 0; i < cap(ch); i++ {
go func() {
ch <- testStreamCompressDecompressLevelSerial(data, level)
}()
}
for i := 0; i < cap(ch); i++ {
select {
case err := <-ch:
if err != nil {
t.Fatalf("error in concurrent test: %s", err)
}
case <-time.After(5 * time.Second):
t.Fatalf("timeout in concurrent test")
}
}
}
func testStreamCompressDecompressLevelSerial(data string, level int) error {
var bbCompress bytes.Buffer
if err := StreamCompressLevel(&bbCompress, bytes.NewBufferString(data), level); err != nil {
return fmt.Errorf("cannot compress stream of size %d: %s", len(data), err)
}
var bbDecompress bytes.Buffer
if err := StreamDecompress(&bbDecompress, &bbCompress); err != nil {
return fmt.Errorf("cannot decompress stream of size %d: %s", len(data), err)
}
plainData := bbDecompress.Bytes()
if string(plainData) != data {
return fmt.Errorf("unexpected decompressed data; got\n%q; want\n%q", plainData, data)
}
return nil
}
func TestStreamCompressDecompressDict(t *testing.T) {
var samples [][]byte
for i := 0; i < 1000; i++ {
sample := fmt.Sprintf("this is a dict sample line %d", i)
samples = append(samples, []byte(sample))
}
dict := BuildDict(samples, 8*1024)
cd, err := NewCDict(dict)
if err != nil {
t.Fatalf("cannot create CDict: %s", err)
}
defer cd.Release()
dd, err := NewDDict(dict)
if err != nil {
t.Fatalf("cannot create DDict: %s", err)
}
defer dd.Release()
// Create data for the compression.
var bb bytes.Buffer
for bb.Len() < 256*1024 {
fmt.Fprintf(&bb, "dict sample line %d this is", bb.Len())
}
data := bb.Bytes()
// Serial test.
if err := testStreamCompressDecompressDictSerial(cd, dd, data); err != nil {
t.Fatalf("error in serial test: %s", err)
}
// Concurrent test.
ch := make(chan error, 3)
for i := 0; i < cap(ch); i++ {
go func() {
ch <- testStreamCompressDecompressDictSerial(cd, dd, data)
}()
}
for i := 0; i < cap(ch); i++ {
select {
case err := <-ch:
if err != nil {
t.Fatalf("error in concurrent test: %s", err)
}
case <-time.After(time.Second):
t.Fatalf("timeout in concurrent test")
}
}
}
func testStreamCompressDecompressDictSerial(cd *CDict, dd *DDict, data []byte) error {
var bbCompress bytes.Buffer
if err := StreamCompressDict(&bbCompress, bytes.NewReader(data), cd); err != nil {
return fmt.Errorf("cannot compress stream of size %d: %s", len(data), err)
}
var bbDecompress bytes.Buffer
if err := StreamDecompressDict(&bbDecompress, &bbCompress, dd); err != nil {
return fmt.Errorf("cannot decompress stream of size %d: %s", len(data), err)
}
plainData := bbDecompress.Bytes()
if !bytes.Equal(plainData, data) {
return fmt.Errorf("unexpected decompressed data; got\n%q; want\n%q", plainData, data)
}
return nil
}