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, err := NewWriter(&buf, h.level)
86 t.Errorf("NewWriter: %v", err)
91 if !bytes.Equal(buf.Bytes(), h.out) {
92 t.Errorf("Deflate(%d, %x) = %x, want %x", h.level, h.in, buf.Bytes(), h.out)
97 // A sparseReader returns a stream consisting of 0s followed by 1<<16 1s.
98 // This tests missing hash references in a very large input.
99 type sparseReader struct {
104 func (r *sparseReader) Read(b []byte) (n int, err error) {
109 cur := r.cur + int64(n)
114 for i := range b[0:n] {
115 if r.cur+int64(i) >= r.l-1<<16 {
125 func TestVeryLongSparseChunk(t *testing.T) {
127 t.Logf("skipping sparse chunk during short test")
130 w, err := NewWriter(ioutil.Discard, 1)
132 t.Errorf("NewWriter: %v", err)
135 if _, err = io.Copy(w, &sparseReader{l: 23E8}); err != nil {
136 t.Errorf("Compress failed: %v", err)
141 type syncBuffer struct {
148 func newSyncBuffer() *syncBuffer {
149 return &syncBuffer{ready: make(chan bool, 1)}
152 func (b *syncBuffer) Read(p []byte) (n int, err error) {
155 n, err = b.buf.Read(p)
157 if n > 0 || b.closed {
165 func (b *syncBuffer) signal() {
167 case b.ready <- true:
172 func (b *syncBuffer) Write(p []byte) (n int, err error) {
173 n, err = b.buf.Write(p)
178 func (b *syncBuffer) WriteMode() {
182 func (b *syncBuffer) ReadMode() {
187 func (b *syncBuffer) Close() error {
193 func testSync(t *testing.T, level int, input []byte, name string) {
198 t.Logf("--testSync %d, %d, %s", level, len(input), name)
199 buf := newSyncBuffer()
200 buf1 := new(bytes.Buffer)
202 w, err := NewWriter(io.MultiWriter(buf, buf1), level)
204 t.Errorf("NewWriter: %v", err)
209 // Write half the input and read back.
210 for i := 0; i < 2; i++ {
213 lo, hi = 0, (len(input)+1)/2
215 lo, hi = (len(input)+1)/2, len(input)
217 t.Logf("#%d: write %d-%d", i, lo, hi)
218 if _, err := w.Write(input[lo:hi]); err != nil {
219 t.Errorf("testSync: write: %v", err)
223 if err := w.Flush(); err != nil {
224 t.Errorf("testSync: flush: %v", err)
228 if err := w.Close(); err != nil {
229 t.Errorf("testSync: close: %v", err)
233 out := make([]byte, hi-lo+1)
234 m, err := io.ReadAtLeast(r, out, hi-lo)
235 t.Logf("#%d: read %d", i, m)
236 if m != hi-lo || err != nil {
237 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())
240 if !bytes.Equal(input[lo:hi], out[:hi-lo]) {
241 t.Errorf("testSync/%d: read wrong bytes: %x vs %x", i, input[lo:hi], out[:hi-lo])
244 // This test originally checked that after reading
245 // the first half of the input, there was nothing left
246 // in the read buffer (buf.buf.Len() != 0) but that is
247 // not necessarily the case: the write Flush may emit
248 // some extra framing bits that are not necessary
249 // to process to obtain the first half of the uncompressed
250 // data. The test ran correctly most of the time, because
251 // the background goroutine had usually read even
252 // those extra bits by now, but it's not a useful thing to
257 out := make([]byte, 10)
258 if n, err := r.Read(out); n > 0 || err != io.EOF {
259 t.Errorf("testSync (%d, %d, %s): final Read: %d, %v (hex: %x)", level, len(input), name, n, err, out[0:n])
261 if buf.buf.Len() != 0 {
262 t.Errorf("testSync (%d, %d, %s): extra data at end", level, len(input), name)
266 // stream should work for ordinary reader too
268 out, err = ioutil.ReadAll(r)
270 t.Errorf("testSync: read: %s", err)
274 if !bytes.Equal(input, out) {
275 t.Errorf("testSync: decompress(compress(data)) != data: level=%d input=%s", level, name)
279 func testToFromWithLevelAndLimit(t *testing.T, level int, input []byte, name string, limit int) {
280 var buffer bytes.Buffer
281 w, err := NewWriter(&buffer, level)
283 t.Errorf("NewWriter: %v", err)
288 if limit > 0 && buffer.Len() > limit {
289 t.Errorf("level: %d, len(compress(data)) = %d > limit = %d", level, buffer.Len(), limit)
292 r := NewReader(&buffer)
293 out, err := ioutil.ReadAll(r)
295 t.Errorf("read: %s", err)
299 if !bytes.Equal(input, out) {
300 t.Errorf("decompress(compress(data)) != data: level=%d input=%s", level, name)
303 testSync(t, level, input, name)
306 func testToFromWithLimit(t *testing.T, input []byte, name string, limit [10]int) {
307 for i := 0; i < 10; i++ {
308 testToFromWithLevelAndLimit(t, i, input, name, limit[i])
312 func TestDeflateInflate(t *testing.T) {
313 for i, h := range deflateInflateTests {
314 testToFromWithLimit(t, h.in, fmt.Sprintf("#%d", i), [10]int{})
318 func TestReverseBits(t *testing.T) {
319 for _, h := range reverseBitsTests {
320 if v := reverseBits(h.in, h.bitCount); v != h.out {
321 t.Errorf("reverseBits(%v,%v) = %v, want %v",
322 h.in, h.bitCount, v, h.out)
327 type deflateInflateStringTest struct {
333 var deflateInflateStringTests = []deflateInflateStringTest{
337 [...]int{100018, 50650, 50960, 51150, 50930, 50790, 50790, 50790, 50790, 50790},
340 "../testdata/Mark.Twain-Tom.Sawyer.txt",
341 "Mark.Twain-Tom.Sawyer",
342 [...]int{407330, 187598, 180361, 172974, 169160, 163476, 160936, 160506, 160295, 160295},
346 func TestDeflateInflateString(t *testing.T) {
347 for _, test := range deflateInflateStringTests {
348 gold, err := ioutil.ReadFile(test.filename)
352 testToFromWithLimit(t, gold, test.label, test.limit)
359 func TestReaderDict(t *testing.T) {
362 text = "hello again world"
365 w, err := NewWriter(&b, 5)
367 t.Fatalf("NewWriter: %v", err)
369 w.Write([]byte(dict))
372 w.Write([]byte(text))
375 r := NewReaderDict(&b, []byte(dict))
376 data, err := ioutil.ReadAll(r)
380 if string(data) != "hello again world" {
381 t.Fatalf("read returned %q want %q", string(data), text)
385 func TestWriterDict(t *testing.T) {
388 text = "hello again world"
391 w, err := NewWriter(&b, 5)
393 t.Fatalf("NewWriter: %v", err)
395 w.Write([]byte(dict))
398 w.Write([]byte(text))
402 w, _ = NewWriterDict(&b1, 5, []byte(dict))
403 w.Write([]byte(text))
406 if !bytes.Equal(b1.Bytes(), b.Bytes()) {
407 t.Fatalf("writer wrote %q want %q", b1.Bytes(), b.Bytes())
411 // See http://code.google.com/p/go/issues/detail?id=2508
412 func TestRegression2508(t *testing.T) {
414 t.Logf("test disabled with -short")
417 w, err := NewWriter(ioutil.Discard, 1)
419 t.Fatalf("NewWriter: %v", err)
421 buf := make([]byte, 1024)
422 for i := 0; i < 131072; i++ {
423 if _, err := w.Write(buf); err != nil {
424 t.Fatalf("writer failed: %v", err)