-
Notifications
You must be signed in to change notification settings - Fork 142
/
compressing_reader_test.go
78 lines (67 loc) · 1.7 KB
/
compressing_reader_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
package lz4_test
import (
"bytes"
"fmt"
"io/ioutil"
"strings"
"testing"
"github.com/pierrec/lz4/v4"
)
func TestCompressingReader(t *testing.T) {
goldenFiles := []string{
"testdata/e.txt",
"testdata/gettysburg.txt",
"testdata/Mark.Twain-Tom.Sawyer.txt",
"testdata/Mark.Twain-Tom.Sawyer_long.txt",
"testdata/pg1661.txt",
"testdata/pi.txt",
"testdata/random.data",
"testdata/repeat.txt",
"testdata/issue102.data",
}
for _, fname := range goldenFiles {
for _, option := range []lz4.Option{
lz4.BlockChecksumOption(true),
lz4.SizeOption(123),
} {
label := fmt.Sprintf("%s/%s", fname, option)
t.Run(label, func(t *testing.T) {
fname := fname
option := option
t.Parallel()
raw, err := ioutil.ReadFile(fname)
if err != nil {
t.Fatal(err)
}
r := ioutil.NopCloser(bytes.NewReader(raw))
// Compress.
zcomp := lz4.NewCompressingReader(r)
if err := zcomp.Apply(option, lz4.CompressionLevelOption(lz4.Level1)); err != nil {
t.Fatal(err)
}
zout, err := ioutil.ReadAll(zcomp)
if err != nil {
t.Fatal(err)
}
// Uncompress.
zr := lz4.NewReader(bytes.NewReader(zout))
out, err := ioutil.ReadAll(zr)
if err != nil {
t.Fatal(err)
}
// The uncompressed data must be the same as the initial input.
if got, want := len(out), len(raw); got != want {
t.Errorf("invalid sizes: got %d; want %d", got, want)
}
if !bytes.Equal(out, raw) {
t.Fatal("uncompressed data does not match original")
}
if strings.Contains(option.String(), "SizeOption") {
if got, want := zr.Size(), 123; got != want {
t.Errorf("invalid sizes: got %d; want %d", got, want)
}
}
})
}
}
}