-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcompressioncodec_test.go
83 lines (71 loc) · 1.73 KB
/
compressioncodec_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
package orc
import (
"bytes"
"io"
"io/ioutil"
"math/rand"
"testing"
)
var (
_ CompressionCodec = (*CompressionNone)(nil)
_ CompressionCodec = (*CompressionSnappy)(nil)
_ CompressionCodec = (*CompressionZlib)(nil)
_ io.WriteCloser = (*CompressionZlibEncoder)(nil)
_ io.WriteCloser = (*CompressionZlibEncoder)(nil)
_ io.WriteCloser = (*CompressionZlibEncoder)(nil)
)
func TestCompressionHeader(t *testing.T) {
testcases := []struct {
chunkSize int
isOriginal bool
expected []byte
isError bool
}{
{9000000, false, []byte{}, true},
{100000, false, []byte{0x40, 0x0d, 0x03}, false},
{5, true, []byte{0x0b, 0x00, 0x00}, false},
}
for _, v := range testcases {
header, err := compressionHeader(v.chunkSize, v.isOriginal)
if err != nil && !v.isError {
t.Error(err)
continue
}
if err == nil && v.isError {
t.Errorf("On input: Length %d and isOriginal %t -> Expected an error, but got none.", v.chunkSize, v.isOriginal)
}
if bytes.Compare(header, v.expected) != 0 {
t.Errorf("On input: Length %d and isOriginal %t -> Expected header %x got %x", v.chunkSize, v.isOriginal, v.expected, header)
}
}
}
func TestCompressionZlib(t *testing.T) {
c := CompressionZlib{}
buf := make([]byte, 1<<17)
_, err := rand.Read(buf)
if err != nil {
t.Fatal(err)
}
w := &bytes.Buffer{}
r := w
enc := c.Encoder(w)
dec := c.Decoder(r)
n, err := enc.Write(buf)
if err != nil {
t.Fatal(err)
}
if n != len(buf) {
t.Errorf("Buffer underflow. Expected to write %d, wrote %d", len(buf), n)
}
err = enc.Close()
if err != nil {
t.Fatal(err)
}
got, err := ioutil.ReadAll(dec)
if err != nil {
t.Fatal(err)
}
if bytes.Compare(buf, got) != 0 {
t.Errorf("Input and output don't match: %v vs %v", buf, got)
}
}