OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [compress/] [lzw/] [writer_test.go] - Blame information for rev 774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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 benchmarkEncoder(b *testing.B, n int) {
100
        b.StopTimer()
101
        b.SetBytes(int64(n))
102
        buf0, _ := ioutil.ReadFile("../testdata/e.txt")
103
        buf0 = buf0[:10000]
104
        buf1 := make([]byte, n)
105
        for i := 0; i < n; i += len(buf0) {
106
                copy(buf1[i:], buf0)
107
        }
108
        buf0 = nil
109
        runtime.GC()
110
        b.StartTimer()
111
        for i := 0; i < b.N; i++ {
112
                w := NewWriter(ioutil.Discard, LSB, 8)
113
                w.Write(buf1)
114
                w.Close()
115
        }
116
}
117
 
118
func BenchmarkEncoder1e4(b *testing.B) {
119
        benchmarkEncoder(b, 1e4)
120
}
121
 
122
func BenchmarkEncoder1e5(b *testing.B) {
123
        benchmarkEncoder(b, 1e5)
124
}
125
 
126
func BenchmarkEncoder1e6(b *testing.B) {
127
        benchmarkEncoder(b, 1e6)
128
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.