-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathcompress_test.go
55 lines (48 loc) · 1.3 KB
/
compress_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
package gws
import (
"bytes"
"compress/flate"
"testing"
"github.com/lxzan/gws/internal"
"github.com/stretchr/testify/assert"
)
func TestFlate(t *testing.T) {
var as = assert.New(t)
t.Run("ok", func(t *testing.T) {
for i := 0; i < 100; i++ {
var cps = newCompressor(flate.BestSpeed)
var dps = newDecompressor()
var n = internal.AlphabetNumeric.Intn(1024)
var rawText = internal.AlphabetNumeric.Generate(n)
var compressedBuf = bytes.NewBufferString("")
if err := cps.Compress(rawText, compressedBuf); err != nil {
as.NoError(err)
return
}
var buf = bytes.NewBufferString("")
buf.Write(compressedBuf.Bytes())
plainText, _, err := dps.Decompress(buf)
if err != nil {
as.NoError(err)
return
}
as.Equal(string(rawText), plainText.String())
}
})
t.Run("deflate error", func(t *testing.T) {
var cps = newCompressor(flate.BestSpeed)
var dps = newDecompressor()
var n = internal.AlphabetNumeric.Intn(1024)
var rawText = internal.AlphabetNumeric.Generate(n)
var compressedBuf = bytes.NewBufferString("")
if err := cps.Compress(rawText, compressedBuf); err != nil {
as.NoError(err)
return
}
var buf = bytes.NewBufferString("")
buf.Write(compressedBuf.Bytes())
buf.WriteString("1234")
_, _, err := dps.Decompress(buf)
as.Error(err)
})
}