1 // 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.
16 type deflateTest struct {
22 type deflateInflateTest struct {
26 type reverseBitsTest struct {
32 var deflateTests = []*deflateTest{
33 {[]byte{}, 0, []byte{1, 0, 0, 255, 255}},
34 {[]byte{0x11}, -1, []byte{18, 4, 4, 0, 0, 255, 255}},
35 {[]byte{0x11}, DefaultCompression, []byte{18, 4, 4, 0, 0, 255, 255}},
36 {[]byte{0x11}, 4, []byte{18, 4, 4, 0, 0, 255, 255}},
38 {[]byte{0x11}, 0, []byte{0, 1, 0, 254, 255, 17, 1, 0, 0, 255, 255}},
39 {[]byte{0x11, 0x12}, 0, []byte{0, 2, 0, 253, 255, 17, 18, 1, 0, 0, 255, 255}},
40 {[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 0,
41 []byte{0, 8, 0, 247, 255, 17, 17, 17, 17, 17, 17, 17, 17, 1, 0, 0, 255, 255},
43 {[]byte{}, 1, []byte{1, 0, 0, 255, 255}},
44 {[]byte{0x11}, 1, []byte{18, 4, 4, 0, 0, 255, 255}},
45 {[]byte{0x11, 0x12}, 1, []byte{18, 20, 2, 4, 0, 0, 255, 255}},
46 {[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 1, []byte{18, 132, 2, 64, 0, 0, 0, 255, 255}},
47 {[]byte{}, 9, []byte{1, 0, 0, 255, 255}},
48 {[]byte{0x11}, 9, []byte{18, 4, 4, 0, 0, 255, 255}},
49 {[]byte{0x11, 0x12}, 9, []byte{18, 20, 2, 4, 0, 0, 255, 255}},
50 {[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 9, []byte{18, 132, 2, 64, 0, 0, 0, 255, 255}},
53 var deflateInflateTests = []*deflateInflateTest{
57 {[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}},
58 {[]byte{0x11, 0x10, 0x13, 0x41, 0x21, 0x21, 0x41, 0x13, 0x87, 0x78, 0x13}},
62 var reverseBitsTests = []*reverseBitsTest{
73 func largeDataChunk() []byte {
74 result := make([]byte, 100000)
75 for i := range result {
76 result[i] = byte(i * i & 0xFF)
81 func TestDeflate(t *testing.T) {
82 for _, h := range deflateTests {
84 w := NewWriter(&buf, h.level)
87 if !bytes.Equal(buf.Bytes(), h.out) {
88 t.Errorf("Deflate(%d, %x) = %x, want %x", h.level, h.in, buf.Bytes(), h.out)
93 type syncBuffer struct {
100 func newSyncBuffer() *syncBuffer {
101 return &syncBuffer{ready: make(chan bool, 1)}
104 func (b *syncBuffer) Read(p []byte) (n int, err error) {
107 n, err = b.buf.Read(p)
109 if n > 0 || b.closed {
117 func (b *syncBuffer) signal() {
119 case b.ready <- true:
124 func (b *syncBuffer) Write(p []byte) (n int, err error) {
125 n, err = b.buf.Write(p)
130 func (b *syncBuffer) WriteMode() {
134 func (b *syncBuffer) ReadMode() {
139 func (b *syncBuffer) Close() error {
145 func testSync(t *testing.T, level int, input []byte, name string) {
150 t.Logf("--testSync %d, %d, %s", level, len(input), name)
151 buf := newSyncBuffer()
152 buf1 := new(bytes.Buffer)
154 w := NewWriter(io.MultiWriter(buf, buf1), level)
157 // Write half the input and read back.
158 for i := 0; i < 2; i++ {
161 lo, hi = 0, (len(input)+1)/2
163 lo, hi = (len(input)+1)/2, len(input)
165 t.Logf("#%d: write %d-%d", i, lo, hi)
166 if _, err := w.Write(input[lo:hi]); err != nil {
167 t.Errorf("testSync: write: %v", err)
171 if err := w.Flush(); err != nil {
172 t.Errorf("testSync: flush: %v", err)
176 if err := w.Close(); err != nil {
177 t.Errorf("testSync: close: %v", err)
181 out := make([]byte, hi-lo+1)
182 m, err := io.ReadAtLeast(r, out, hi-lo)
183 t.Logf("#%d: read %d", i, m)
184 if m != hi-lo || err != nil {
185 t.Errorf("testSync/%d (%d, %d, %s): read %d: %d, %v (%d left)", i, level, len(input), name, hi-lo, m, err, buf.buf.Len())
188 if !bytes.Equal(input[lo:hi], out[:hi-lo]) {
189 t.Errorf("testSync/%d: read wrong bytes: %x vs %x", i, input[lo:hi], out[:hi-lo])
192 // This test originally checked that after reading
193 // the first half of the input, there was nothing left
194 // in the read buffer (buf.buf.Len() != 0) but that is
195 // not necessarily the case: the write Flush may emit
196 // some extra framing bits that are not necessary
197 // to process to obtain the first half of the uncompressed
198 // data. The test ran correctly most of the time, because
199 // the background goroutine had usually read even
200 // those extra bits by now, but it's not a useful thing to
205 out := make([]byte, 10)
206 if n, err := r.Read(out); n > 0 || err != io.EOF {
207 t.Errorf("testSync (%d, %d, %s): final Read: %d, %v (hex: %x)", level, len(input), name, n, err, out[0:n])
209 if buf.buf.Len() != 0 {
210 t.Errorf("testSync (%d, %d, %s): extra data at end", level, len(input), name)
214 // stream should work for ordinary reader too
216 out, err := ioutil.ReadAll(r)
218 t.Errorf("testSync: read: %s", err)
222 if !bytes.Equal(input, out) {
223 t.Errorf("testSync: decompress(compress(data)) != data: level=%d input=%s", level, name)
227 func testToFromWithLevel(t *testing.T, level int, input []byte, name string) error {
228 return testToFromWithLevelAndLimit(t, level, input, name, -1)
231 func testToFromWithLevelAndLimit(t *testing.T, level int, input []byte, name string, limit int) error {
232 buffer := bytes.NewBuffer(nil)
233 w := NewWriter(buffer, level)
236 if limit > 0 && buffer.Len() > limit {
237 t.Errorf("level: %d, len(compress(data)) = %d > limit = %d", level, buffer.Len(), limit)
239 r := NewReader(buffer)
240 out, err := ioutil.ReadAll(r)
242 t.Errorf("read: %s", err)
246 if !bytes.Equal(input, out) {
247 t.Errorf("decompress(compress(data)) != data: level=%d input=%s", level, name)
250 testSync(t, level, input, name)
254 func testToFromWithLimit(t *testing.T, input []byte, name string, limit [10]int) {
255 for i := 0; i < 10; i++ {
256 testToFromWithLevelAndLimit(t, i, input, name, limit[i])
260 func testToFrom(t *testing.T, input []byte, name string) {
261 testToFromWithLimit(t, input, name, [10]int{})
264 func TestDeflateInflate(t *testing.T) {
265 for i, h := range deflateInflateTests {
266 testToFrom(t, h.in, fmt.Sprintf("#%d", i))
270 func TestReverseBits(t *testing.T) {
271 for _, h := range reverseBitsTests {
272 if v := reverseBits(h.in, h.bitCount); v != h.out {
273 t.Errorf("reverseBits(%v,%v) = %v, want %v",
274 h.in, h.bitCount, v, h.out)
279 type deflateInflateStringTest struct {
285 var deflateInflateStringTests = []deflateInflateStringTest{
289 [...]int{10013, 5065, 5096, 5115, 5093, 5079, 5079, 5079, 5079, 5079},
292 "../testdata/Mark.Twain-Tom.Sawyer.txt",
293 "Mark.Twain-Tom.Sawyer",
294 [...]int{407330, 187598, 180361, 172974, 169160, 163476, 160936, 160506, 160295, 160295},
298 func TestDeflateInflateString(t *testing.T) {
299 for _, test := range deflateInflateStringTests {
300 gold, err := ioutil.ReadFile(test.filename)
304 testToFromWithLimit(t, gold, test.label, test.limit)
308 func TestReaderDict(t *testing.T) {
311 text = "hello again world"
314 w := NewWriter(&b, 5)
315 w.Write([]byte(dict))
318 w.Write([]byte(text))
321 r := NewReaderDict(&b, []byte(dict))
322 data, err := ioutil.ReadAll(r)
326 if string(data) != "hello again world" {
327 t.Fatalf("read returned %q want %q", string(data), text)
331 func TestWriterDict(t *testing.T) {
334 text = "hello again world"
337 w := NewWriter(&b, 5)
338 w.Write([]byte(dict))
341 w.Write([]byte(text))
345 w = NewWriterDict(&b1, 5, []byte(dict))
346 w.Write([]byte(text))
349 if !bytes.Equal(b1.Bytes(), b.Bytes()) {
350 t.Fatalf("writer wrote %q want %q", b1.Bytes(), b.Bytes())
354 // See http://code.google.com/p/go/issues/detail?id=2508
355 func TestRegression2508(t *testing.T) {
356 w := NewWriter(ioutil.Discard, 1)
357 buf := make([]byte, 1024)
358 for i := 0; i < 131072; i++ {
359 if _, err := w.Write(buf); err != nil {
360 t.Fatalf("writer failed: %v", err)