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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [compress/] [flate/] [flate_test.go] - Blame information for rev 801

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

Line No. Rev Author Line
1 747 jeremybenn
// 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
// This test tests some internals of the flate package.
6
// The tests in package compress/gzip serve as the
7
// end-to-end test of the decompressor.
8
 
9
package flate
10
 
11
import (
12
        "bytes"
13
        "reflect"
14
        "testing"
15
)
16
 
17
// The Huffman code lengths used by the fixed-format Huffman blocks.
18
var fixedHuffmanBits = [...]int{
19
        // 0-143 length 8
20
        8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
21
        8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
22
        8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
23
        8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
24
        8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
25
        8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
26
        8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
27
        8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
28
        8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
29
 
30
        // 144-255 length 9
31
        9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
32
        9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
33
        9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
34
        9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
35
        9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
36
        9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
37
        9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
38
 
39
        // 256-279 length 7
40
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
41
        7, 7, 7, 7, 7, 7, 7, 7,
42
 
43
        // 280-287 length 8
44
        8, 8, 8, 8, 8, 8, 8, 8,
45
}
46
 
47
type InitDecoderTest struct {
48
        in  []int
49
        out huffmanDecoder
50
        ok  bool
51
}
52
 
53
var initDecoderTests = []*InitDecoderTest{
54
        // Example from Connell 1973,
55
        {
56
                []int{3, 5, 2, 4, 3, 5, 5, 4, 4, 3, 4, 5},
57
                huffmanDecoder{
58
                        2, 5,
59
                        [maxCodeLen + 1]int{2: 0, 4, 13, 31},
60
                        [maxCodeLen + 1]int{2: 0, 1, 6, 20},
61
                        // Paper used different code assignment:
62
                        // 2, 9, 4, 0, 10, 8, 3, 7, 1, 5, 11, 6
63
                        // Reordered here so that codes of same length
64
                        // are assigned to increasing numbers.
65
                        []int{2, 0, 4, 9, 3, 7, 8, 10, 1, 5, 6, 11},
66
                },
67
                true,
68
        },
69
 
70
        // Example from RFC 1951 section 3.2.2
71
        {
72
                []int{2, 1, 3, 3},
73
                huffmanDecoder{
74
                        1, 3,
75
                        [maxCodeLen + 1]int{1: 0, 2, 7},
76
                        [maxCodeLen + 1]int{1: 0, 1, 4},
77
                        []int{1, 0, 2, 3},
78
                },
79
                true,
80
        },
81
 
82
        // Second example from RFC 1951 section 3.2.2
83
        {
84
                []int{3, 3, 3, 3, 3, 2, 4, 4},
85
                huffmanDecoder{
86
                        2, 4,
87
                        [maxCodeLen + 1]int{2: 0, 6, 15},
88
                        [maxCodeLen + 1]int{2: 0, 1, 8},
89
                        []int{5, 0, 1, 2, 3, 4, 6, 7},
90
                },
91
                true,
92
        },
93
 
94
        // Static Huffman codes (RFC 1951 section 3.2.6)
95
        {
96
                fixedHuffmanBits[0:],
97
                fixedHuffmanDecoder,
98
                true,
99
        },
100
 
101
        // Illegal input.
102
        {
103
                []int{},
104
                huffmanDecoder{},
105
                false,
106
        },
107
 
108
        // Illegal input.
109
        {
110
                []int{0, 0, 0, 0, 0, 0, 0},
111
                huffmanDecoder{},
112
                false,
113
        },
114
}
115
 
116
func TestInitDecoder(t *testing.T) {
117
        for i, tt := range initDecoderTests {
118
                var h huffmanDecoder
119
                if h.init(tt.in) != tt.ok {
120
                        t.Errorf("test %d: init = %v", i, !tt.ok)
121
                        continue
122
                }
123
                if !reflect.DeepEqual(&h, &tt.out) {
124
                        t.Errorf("test %d:\nhave %v\nwant %v", i, h, tt.out)
125
                }
126
        }
127
}
128
 
129
func TestUncompressedSource(t *testing.T) {
130
        decoder := NewReader(bytes.NewBuffer([]byte{0x01, 0x01, 0x00, 0xfe, 0xff, 0x11}))
131
        output := make([]byte, 1)
132
        n, error := decoder.Read(output)
133
        if n != 1 || error != nil {
134
                t.Fatalf("decoder.Read() = %d, %v, want 1, nil", n, error)
135
        }
136
        if output[0] != 0x11 {
137
                t.Errorf("output[0] = %x, want 0x11", output[0])
138
        }
139
}

powered by: WebSVN 2.1.0

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