1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 var filenames = []string{
22 "test a reasonable sized string that can be compressed",
25 // Tests that compressing and then decompressing the given file at the given compression level and dictionary
26 // yields equivalent bytes to the original file.
27 func testFileLevelDict(t *testing.T, fn string, level int, d string) {
28 // Read the file, as golden output.
29 golden, err := os.Open(fn)
31 t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err)
35 b0, err0 := ioutil.ReadAll(golden)
37 t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err0)
40 testLevelDict(t, fn, b0, level, d)
43 func testLevelDict(t *testing.T, fn string, b0 []byte, level int, d string) {
44 // Make dictionary, if given.
50 // Push data through a pipe that compresses at the write end, and decompresses at the read end.
51 piper, pipew := io.Pipe()
55 zlibw, err := NewWriterLevelDict(pipew, level, dict)
57 t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err)
61 _, err = zlibw.Write(b0)
63 t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err)
67 zlibr, err := NewReaderDict(piper, dict)
69 t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err)
74 // Compare the decompressed data.
75 b1, err1 := ioutil.ReadAll(zlibr)
77 t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err1)
80 if len(b0) != len(b1) {
81 t.Errorf("%s (level=%d, dict=%q): length mismatch %d versus %d", fn, level, d, len(b0), len(b1))
84 for i := 0; i < len(b0); i++ {
86 t.Errorf("%s (level=%d, dict=%q): mismatch at %d, 0x%02x versus 0x%02x\n", fn, level, d, i, b0[i], b1[i])
92 func TestWriter(t *testing.T) {
93 for i, s := range data {
95 tag := fmt.Sprintf("#%d", i)
96 testLevelDict(t, tag, b, DefaultCompression, "")
97 testLevelDict(t, tag, b, NoCompression, "")
98 for level := BestSpeed; level <= BestCompression; level++ {
99 testLevelDict(t, tag, b, level, "")
104 func TestWriterBig(t *testing.T) {
105 for _, fn := range filenames {
106 testFileLevelDict(t, fn, DefaultCompression, "")
107 testFileLevelDict(t, fn, NoCompression, "")
108 for level := BestSpeed; level <= BestCompression; level++ {
109 testFileLevelDict(t, fn, level, "")
114 func TestWriterDict(t *testing.T) {
115 const dictionary = "0123456789."
116 for _, fn := range filenames {
117 testFileLevelDict(t, fn, DefaultCompression, dictionary)
118 testFileLevelDict(t, fn, NoCompression, dictionary)
119 for level := BestSpeed; level <= BestCompression; level++ {
120 testFileLevelDict(t, fn, level, dictionary)
125 func TestWriterDictIsUsed(t *testing.T) {
126 var input = []byte("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
128 compressor, err := NewWriterLevelDict(&buf, BestCompression, input)
130 t.Errorf("error in NewWriterLevelDict: %s", err)
133 compressor.Write(input)
135 const expectedMaxSize = 25
136 output := buf.Bytes()
137 if len(output) > expectedMaxSize {
138 t.Errorf("result too large (got %d, want <= %d bytes). Is the dictionary being used?", len(output), expectedMaxSize)