OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // This test tests some internals of the flate package. | 5 // This test tests some internals of the flate package. |
6 // The tests in package compress/gzip serve as the | 6 // The tests in package compress/gzip serve as the |
7 // end-to-end test of the inflater. | 7 // end-to-end test of the inflater. |
8 | 8 |
9 package flate | 9 package flate |
10 | 10 |
11 import ( | 11 import ( |
12 » "bytes"; | 12 » "bytes" |
13 » "reflect"; | 13 » "reflect" |
14 » "testing"; | 14 » "testing" |
15 ) | 15 ) |
16 | 16 |
17 // The Huffman code lengths used by the fixed-format Huffman blocks. | 17 // The Huffman code lengths used by the fixed-format Huffman blocks. |
18 var fixedHuffmanBits = [...]int{ | 18 var fixedHuffmanBits = [...]int{ |
19 // 0-143 length 8 | 19 // 0-143 length 8 |
20 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 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, | 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, | 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, | 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, | 24 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
(...skipping 13 matching lines...) Expand all Loading... |
38 | 38 |
39 // 256-279 length 7 | 39 // 256-279 length 7 |
40 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 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, | 41 7, 7, 7, 7, 7, 7, 7, 7, |
42 | 42 |
43 // 280-287 length 8 | 43 // 280-287 length 8 |
44 8, 8, 8, 8, 8, 8, 8, 8, | 44 8, 8, 8, 8, 8, 8, 8, 8, |
45 } | 45 } |
46 | 46 |
47 type InitDecoderTest struct { | 47 type InitDecoderTest struct { |
48 » in» []int; | 48 » in []int |
49 » out» huffmanDecoder; | 49 » out huffmanDecoder |
50 » ok» bool; | 50 » ok bool |
51 } | 51 } |
52 | 52 |
53 var initDecoderTests = []*InitDecoderTest{ | 53 var initDecoderTests = []*InitDecoderTest{ |
54 // Example from Connell 1973, | 54 // Example from Connell 1973, |
55 &InitDecoderTest{ | 55 &InitDecoderTest{ |
56 []int{3, 5, 2, 4, 3, 5, 5, 4, 4, 3, 4, 5}, | 56 []int{3, 5, 2, 4, 3, 5, 5, 4, 4, 3, 4, 5}, |
57 huffmanDecoder{ | 57 huffmanDecoder{ |
58 2, 5, | 58 2, 5, |
59 [maxCodeLen + 1]int{2: 0, 4, 13, 31}, | 59 [maxCodeLen + 1]int{2: 0, 4, 13, 31}, |
60 [maxCodeLen + 1]int{2: 0, 1, 6, 20}, | 60 [maxCodeLen + 1]int{2: 0, 1, 6, 20}, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 // Illegal input. | 108 // Illegal input. |
109 &InitDecoderTest{ | 109 &InitDecoderTest{ |
110 []int{0, 0, 0, 0, 0, 0, 0}, | 110 []int{0, 0, 0, 0, 0, 0, 0}, |
111 huffmanDecoder{}, | 111 huffmanDecoder{}, |
112 false, | 112 false, |
113 }, | 113 }, |
114 } | 114 } |
115 | 115 |
116 func TestInitDecoder(t *testing.T) { | 116 func TestInitDecoder(t *testing.T) { |
117 for i, tt := range initDecoderTests { | 117 for i, tt := range initDecoderTests { |
118 » » var h huffmanDecoder; | 118 » » var h huffmanDecoder |
119 if h.init(tt.in) != tt.ok { | 119 if h.init(tt.in) != tt.ok { |
120 » » » t.Errorf("test %d: init = %v", i, !tt.ok); | 120 » » » t.Errorf("test %d: init = %v", i, !tt.ok) |
121 » » » continue; | 121 » » » continue |
122 } | 122 } |
123 if !reflect.DeepEqual(&h, &tt.out) { | 123 if !reflect.DeepEqual(&h, &tt.out) { |
124 t.Errorf("test %d:\nhave %v\nwant %v", i, h, tt.out) | 124 t.Errorf("test %d:\nhave %v\nwant %v", i, h, tt.out) |
125 } | 125 } |
126 } | 126 } |
127 } | 127 } |
128 | 128 |
129 func TestUncompressedSource(t *testing.T) { | 129 func TestUncompressedSource(t *testing.T) { |
130 » decoder := NewInflater(bytes.NewBuffer([]byte{0x01, 0x01, 0x00, 0xfe, 0x
ff, 0x11})); | 130 » decoder := NewInflater(bytes.NewBuffer([]byte{0x01, 0x01, 0x00, 0xfe, 0x
ff, 0x11})) |
131 » output := make([]byte, 1); | 131 » output := make([]byte, 1) |
132 » n, error := decoder.Read(output); | 132 » n, error := decoder.Read(output) |
133 if n != 1 || error != nil { | 133 if n != 1 || error != nil { |
134 t.Fatalf("decoder.Read() = %d, %v, want 1, nil", n, error) | 134 t.Fatalf("decoder.Read() = %d, %v, want 1, nil", n, error) |
135 } | 135 } |
136 if output[0] != 0x11 { | 136 if output[0] != 0x11 { |
137 t.Errorf("output[0] = %x, want 0x11", output[0]) | 137 t.Errorf("output[0] = %x, want 0x11", output[0]) |
138 } | 138 } |
139 } | 139 } |
OLD | NEW |