github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/compress/zlib/writer_test.go (about) 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. 4 5 package zlib 6 7 import ( 8 "bytes" 9 "fmt" 10 "io" 11 "io/ioutil" 12 "os" 13 "testing" 14 ) 15 16 var filenames = []string{ 17 "../testdata/e.txt", 18 "../testdata/pi.txt", 19 } 20 21 var data = []string{ 22 "test a reasonable sized string that can be compressed", 23 } 24 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) 30 if err != nil { 31 t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err) 32 return 33 } 34 defer golden.Close() 35 b0, err0 := ioutil.ReadAll(golden) 36 if err0 != nil { 37 t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err0) 38 return 39 } 40 testLevelDict(t, fn, b0, level, d) 41 } 42 43 func testLevelDict(t *testing.T, fn string, b0 []byte, level int, d string) { 44 // Make dictionary, if given. 45 var dict []byte 46 if d != "" { 47 dict = []byte(d) 48 } 49 50 // Push data through a pipe that compresses at the write end, and decompresses at the read end. 51 piper, pipew := io.Pipe() 52 defer piper.Close() 53 go func() { 54 defer pipew.Close() 55 zlibw, err := NewWriterLevelDict(pipew, level, dict) 56 if err != nil { 57 t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err) 58 return 59 } 60 defer zlibw.Close() 61 _, err = zlibw.Write(b0) 62 if err != nil { 63 t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err) 64 return 65 } 66 }() 67 zlibr, err := NewReaderDict(piper, dict) 68 if err != nil { 69 t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err) 70 return 71 } 72 defer zlibr.Close() 73 74 // Compare the decompressed data. 75 b1, err1 := ioutil.ReadAll(zlibr) 76 if err1 != nil { 77 t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err1) 78 return 79 } 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)) 82 return 83 } 84 for i := 0; i < len(b0); i++ { 85 if b0[i] != b1[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]) 87 return 88 } 89 } 90 } 91 92 func TestWriter(t *testing.T) { 93 for i, s := range data { 94 b := []byte(s) 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, "") 100 } 101 } 102 } 103 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, "") 110 } 111 } 112 } 113 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) 121 } 122 } 123 } 124 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.") 127 var buf bytes.Buffer 128 compressor, err := NewWriterLevelDict(&buf, BestCompression, input) 129 if err != nil { 130 t.Errorf("error in NewWriterLevelDict: %s", err) 131 return 132 } 133 compressor.Write(input) 134 compressor.Close() 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) 139 } 140 }