OSDN Git Service

Update to current version of Go library (revision 94d654be2064).
[pf3gnuchains/gcc-fork.git] / libgo / go / compress / lzw / writer_test.go
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.
4
5 package lzw
6
7 import (
8         "io"
9         "io/ioutil"
10         "os"
11         "testing"
12 )
13
14 var filenames = []string{
15         "../testdata/e.txt",
16         "../testdata/pi.txt",
17 }
18
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)
24         if err != nil {
25                 t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err)
26                 return
27         }
28         defer golden.Close()
29
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)
32         if err != nil {
33                 t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err)
34                 return
35         }
36
37         piper, pipew := io.Pipe()
38         defer piper.Close()
39         go func() {
40                 defer raw.Close()
41                 defer pipew.Close()
42                 lzww := NewWriter(pipew, order, litWidth)
43                 defer lzww.Close()
44                 var b [4096]byte
45                 for {
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)
49                                 return
50                         }
51                         _, err1 := lzww.Write(b[:n])
52                         if err1 == os.EPIPE {
53                                 // Fail, but do not report the error, as some other (presumably reportable) error broke the pipe.
54                                 return
55                         }
56                         if err1 != nil {
57                                 t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err1)
58                                 return
59                         }
60                         if err0 == os.EOF {
61                                 break
62                         }
63                 }
64         }()
65         lzwr := NewReader(piper, order, litWidth)
66         defer lzwr.Close()
67
68         // Compare the two.
69         b0, err0 := ioutil.ReadAll(golden)
70         b1, err1 := ioutil.ReadAll(lzwr)
71         if err0 != nil {
72                 t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err0)
73                 return
74         }
75         if err1 != nil {
76                 t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err1)
77                 return
78         }
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))
81                 return
82         }
83         for i := 0; i < len(b0); i++ {
84                 if b0[i] != b1[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])
86                         return
87                 }
88         }
89 }
90
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)
97                         }
98                 }
99         }
100 }
101
102 func BenchmarkEncoder(b *testing.B) {
103         b.StopTimer()
104         buf, _ := ioutil.ReadFile("../testdata/e.txt")
105         b.StartTimer()
106         for i := 0; i < b.N; i++ {
107                 w := NewWriter(devNull{}, LSB, 8)
108                 w.Write(buf)
109                 w.Close()
110         }
111 }