~james-page/ubuntu/trusty/gccgo-go/trunk

« back to all changes in this revision

Viewing changes to src/pkg/compress/lzw/writer_test.go

  • Committer: James Page
  • Date: 2014-01-22 11:10:56 UTC
  • Revision ID: james.page@canonical.com-20140122111056-hfyxm1ozm7if9t3f
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        "runtime"
 
12
        "testing"
 
13
)
 
14
 
 
15
var filenames = []string{
 
16
        "../testdata/e.txt",
 
17
        "../testdata/pi.txt",
 
18
}
 
19
 
 
20
// testFile tests that compressing and then decompressing the given file with
 
21
// the given options yields equivalent bytes to the original file.
 
22
func testFile(t *testing.T, fn string, order Order, litWidth int) {
 
23
        // Read the file, as golden output.
 
24
        golden, err := os.Open(fn)
 
25
        if err != nil {
 
26
                t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err)
 
27
                return
 
28
        }
 
29
        defer golden.Close()
 
30
 
 
31
        // Read the file again, and push it through a pipe that compresses at the write end, and decompresses at the read end.
 
32
        raw, err := os.Open(fn)
 
33
        if err != nil {
 
34
                t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err)
 
35
                return
 
36
        }
 
37
 
 
38
        piper, pipew := io.Pipe()
 
39
        defer piper.Close()
 
40
        go func() {
 
41
                defer raw.Close()
 
42
                defer pipew.Close()
 
43
                lzww := NewWriter(pipew, order, litWidth)
 
44
                defer lzww.Close()
 
45
                var b [4096]byte
 
46
                for {
 
47
                        n, err0 := raw.Read(b[:])
 
48
                        if err0 != nil && err0 != io.EOF {
 
49
                                t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err0)
 
50
                                return
 
51
                        }
 
52
                        _, err1 := lzww.Write(b[:n])
 
53
                        if err1 != nil {
 
54
                                t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err1)
 
55
                                return
 
56
                        }
 
57
                        if err0 == io.EOF {
 
58
                                break
 
59
                        }
 
60
                }
 
61
        }()
 
62
        lzwr := NewReader(piper, order, litWidth)
 
63
        defer lzwr.Close()
 
64
 
 
65
        // Compare the two.
 
66
        b0, err0 := ioutil.ReadAll(golden)
 
67
        b1, err1 := ioutil.ReadAll(lzwr)
 
68
        if err0 != nil {
 
69
                t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err0)
 
70
                return
 
71
        }
 
72
        if err1 != nil {
 
73
                t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err1)
 
74
                return
 
75
        }
 
76
        if len(b1) != len(b0) {
 
77
                t.Errorf("%s (order=%d litWidth=%d): length mismatch %d != %d", fn, order, litWidth, len(b1), len(b0))
 
78
                return
 
79
        }
 
80
        for i := 0; i < len(b0); i++ {
 
81
                if b1[i] != b0[i] {
 
82
                        t.Errorf("%s (order=%d litWidth=%d): mismatch at %d, 0x%02x != 0x%02x\n", fn, order, litWidth, i, b1[i], b0[i])
 
83
                        return
 
84
                }
 
85
        }
 
86
}
 
87
 
 
88
func TestWriter(t *testing.T) {
 
89
        for _, filename := range filenames {
 
90
                for _, order := range [...]Order{LSB, MSB} {
 
91
                        // The test data "2.71828 etcetera" is ASCII text requiring at least 6 bits.
 
92
                        for _, litWidth := range [...]int{6, 7, 8} {
 
93
                                testFile(t, filename, order, litWidth)
 
94
                        }
 
95
                }
 
96
        }
 
97
}
 
98
 
 
99
func TestWriterReturnValues(t *testing.T) {
 
100
        w := NewWriter(ioutil.Discard, LSB, 8)
 
101
        n, err := w.Write([]byte("asdf"))
 
102
        if n != 4 || err != nil {
 
103
                t.Errorf("got %d, %v, want 4, nil", n, err)
 
104
        }
 
105
}
 
106
 
 
107
func benchmarkEncoder(b *testing.B, n int) {
 
108
        b.StopTimer()
 
109
        b.SetBytes(int64(n))
 
110
        buf0, err := ioutil.ReadFile("../testdata/e.txt")
 
111
        if err != nil {
 
112
                b.Fatal(err)
 
113
        }
 
114
        if len(buf0) == 0 {
 
115
                b.Fatalf("test file has no data")
 
116
        }
 
117
        buf1 := make([]byte, n)
 
118
        for i := 0; i < n; i += len(buf0) {
 
119
                if len(buf0) > n-i {
 
120
                        buf0 = buf0[:n-i]
 
121
                }
 
122
                copy(buf1[i:], buf0)
 
123
        }
 
124
        buf0 = nil
 
125
        runtime.GC()
 
126
        b.StartTimer()
 
127
        for i := 0; i < b.N; i++ {
 
128
                w := NewWriter(ioutil.Discard, LSB, 8)
 
129
                w.Write(buf1)
 
130
                w.Close()
 
131
        }
 
132
}
 
133
 
 
134
func BenchmarkEncoder1e4(b *testing.B) {
 
135
        benchmarkEncoder(b, 1e4)
 
136
}
 
137
 
 
138
func BenchmarkEncoder1e5(b *testing.B) {
 
139
        benchmarkEncoder(b, 1e5)
 
140
}
 
141
 
 
142
func BenchmarkEncoder1e6(b *testing.B) {
 
143
        benchmarkEncoder(b, 1e6)
 
144
}