forked from kothar/brotli-go
/
brotli_test.go
150 lines (119 loc) · 3.35 KB
/
brotli_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
package brotli
import (
"bytes"
"io"
"io/ioutil"
"testing"
"gopkg.in/kothar/brotli-go.v0/dec"
"gopkg.in/kothar/brotli-go.v0/enc"
)
func TestSimpleString(T *testing.T) {
testCompress([]byte("Hello Hello Hello, Hello Hello Hello"), T)
}
func TestShortString(T *testing.T) {
s := []byte("The quick brown fox")
l := len(s)
// Brotli will not compress arrays shorter than 3 characters
for ; l > 3; l-- {
testCompress(s[:l], T)
}
}
func testCompress(s []byte, T *testing.T) {
T.Logf("Compressing: %s\n", s)
params := enc.NewBrotliParams()
buffer1 := make([]byte, len(s)*2)
encoded, cerr := enc.CompressBuffer(params, s, buffer1)
if cerr != nil {
T.Error(cerr)
}
buffer2 := make([]byte, len(s))
decoded, derr := dec.DecompressBuffer(encoded, buffer2)
if derr != nil {
T.Error(derr)
}
if !bytes.Equal(s, decoded) {
T.Logf("Decompressed: %s\n", decoded)
T.Error("Decoded output does not match original input")
}
}
// Run roundtrip tests from Brotli repository
func TestRoundtrip(T *testing.T) {
inputs := []string{
"testdata/alice29.txt",
"testdata/asyoulik.txt",
"testdata/lcet10.txt",
"testdata/plrabn12.txt",
"enc/encode.cc",
"shared/dictionary.h",
"dec/decode.c",
}
for _, file := range inputs {
input, err := ioutil.ReadFile(file)
if err != nil {
T.Error(err)
}
for _, quality := range []int{1, 6, 9, 11} {
T.Logf("Roundtrip testing %s at quality %d", file, quality)
params := enc.NewBrotliParams()
params.SetQuality(quality)
bro := testCompressBuffer(params, input, T)
testDecompressBuffer(input, bro, T)
testDecompressStream(input, bytes.NewReader(bro), T)
// Stream compress
buffer := new(bytes.Buffer)
testCompressStream(params, input, buffer, T)
testDecompressBuffer(input, buffer.Bytes(), T)
// Stream roundtrip
reader, writer := io.Pipe()
go testCompressStream(params, input, writer, T)
testDecompressStream(input, reader, T)
}
}
}
func testCompressBuffer(params *enc.BrotliParams, input []byte, T *testing.T) []byte {
// Test buffer compression
bro, err := enc.CompressBuffer(params, input, nil)
if err != nil {
T.Error(err)
}
T.Logf(" Compressed from %d to %d bytes, %.1f%%", len(input), len(bro), (float32(len(bro))/float32(len(input)))*100)
return bro
}
func testDecompressBuffer(input, bro []byte, T *testing.T) {
// Buffer decompression
unbro, err := dec.DecompressBuffer(bro, nil)
if err != nil {
T.Error(err)
}
check("Buffer decompress", input, unbro, T)
}
func testDecompressStream(input []byte, reader io.Reader, T *testing.T) {
// Stream decompression
streamUnbro, err := ioutil.ReadAll(dec.NewBrotliReader(reader))
if err != nil {
T.Error(err)
}
check("Stream decompress", input, streamUnbro, T)
}
func testCompressStream(params *enc.BrotliParams, input []byte, writer io.Writer, T *testing.T) {
bwriter := enc.NewBrotliWriter(params, writer)
n, err := bwriter.Write(input)
if err != nil {
T.Error(err)
}
err = bwriter.Close()
if err != nil {
T.Error(err)
}
if n != len(input) {
T.Error("Not all input was consumed")
}
}
func check(test string, input, output []byte, T *testing.T) {
if len(input) != len(output) {
T.Errorf(" %s: Length of decompressed output (%d) doesn't match input (%d)", test, len(output), len(input))
}
if !bytes.Equal(input, output) {
T.Errorf(" %s: Input does not match decompressed output", test)
}
}