1 // Copyright 2011 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.
14 var filenames = []string{
19 // testFile tests that compressing and then decompressing the given file with
20 // the given options yields equivalent bytes to the original file.
21 func testFile(t *testing.T, fn string, order Order, litWidth int) {
22 // Read the file, as golden output.
23 golden, err := os.Open(fn, os.O_RDONLY, 0400)
25 t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err)
30 // Read the file again, and push it through a pipe that compresses at the write end, and decompresses at the read end.
31 raw, err := os.Open(fn, os.O_RDONLY, 0400)
33 t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err)
37 piper, pipew := io.Pipe()
42 lzww := NewWriter(pipew, order, litWidth)
46 n, err0 := raw.Read(b[:])
47 if err0 != nil && err0 != os.EOF {
48 t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err0)
51 _, err1 := lzww.Write(b[:n])
53 // Fail, but do not report the error, as some other (presumably reportable) error broke the pipe.
57 t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err1)
65 lzwr := NewReader(piper, order, litWidth)
69 b0, err0 := ioutil.ReadAll(golden)
70 b1, err1 := ioutil.ReadAll(lzwr)
72 t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err0)
76 t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err1)
79 if len(b0) != len(b1) {
80 t.Errorf("%s (order=%d litWidth=%d): length mismatch %d versus %d", fn, order, litWidth, len(b0), len(b1))
83 for i := 0; i < len(b0); i++ {
85 t.Errorf("%s (order=%d litWidth=%d): mismatch at %d, 0x%02x versus 0x%02x\n", fn, order, litWidth, i, b0[i], b1[i])
91 func TestWriter(t *testing.T) {
92 for _, filename := range filenames {
93 for _, order := range [...]Order{LSB, MSB} {
94 // The test data "2.71828 etcetera" is ASCII text requiring at least 6 bits.
95 for _, litWidth := range [...]int{6, 7, 8} {
96 testFile(t, filename, order, litWidth)
102 func BenchmarkEncoder(b *testing.B) {
104 buf, _ := ioutil.ReadFile("../testdata/e.txt")
106 for i := 0; i < b.N; i++ {
107 w := NewWriter(devNull{}, LSB, 8)