diff options
author | Nigel Tao <nigeltao@golang.org> | 2009-08-17 22:03:13 -0700 |
---|---|---|
committer | Nigel Tao <nigeltao@golang.org> | 2009-08-17 22:03:13 -0700 |
commit | 77b154bbc22c1ee33a50c9d6e26bf80a6ac1ab8e (patch) | |
tree | 24c7ea92f1c112a52feb2ccb7100d5965db246b3 /src/pkg/compress/zlib/reader_test.go | |
parent | 250e21808c33f728bfdf59bc11ba0ceaf37ca1ae (diff) | |
download | golang-77b154bbc22c1ee33a50c9d6e26bf80a6ac1ab8e.tar.gz |
ZLIB reader for go.
R=rsc
APPROVED=rsc
DELTA=204 (204 added, 0 deleted, 0 changed)
OCL=33437
CL=33440
Diffstat (limited to 'src/pkg/compress/zlib/reader_test.go')
-rw-r--r-- | src/pkg/compress/zlib/reader_test.go | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/pkg/compress/zlib/reader_test.go b/src/pkg/compress/zlib/reader_test.go new file mode 100644 index 000000000..f178cb5f0 --- /dev/null +++ b/src/pkg/compress/zlib/reader_test.go @@ -0,0 +1,102 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package zlib + +import ( + "bytes"; + "io"; + "os"; + "testing"; +) + +type zlibTest struct { + desc string; + raw string; + compressed []byte; + err os.Error; +} + +// Compare-to-golden test data was generated by the ZLIB example program at +// http://www.zlib.net/zpipe.c + +var zlibTests = []zlibTest { + zlibTest { + "empty", + "", + []byte { + 0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, + }, + nil + }, + zlibTest { + "goodbye", + "goodbye, world", + []byte { + 0x78, 0x9c, 0x4b, 0xcf, 0xcf, 0x4f, 0x49, 0xaa, + 0x4c, 0xd5, 0x51, 0x28, 0xcf, 0x2f, 0xca, 0x49, + 0x01, 0x00, 0x28, 0xa5, 0x05, 0x5e, + }, + nil + }, + zlibTest { + "bad header", + "", + []byte { + 0x78, 0x9f, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, + }, + HeaderError + }, + zlibTest { + "bad checksum", + "", + []byte { + 0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0xff, + }, + ChecksumError, + }, + zlibTest { + "not enough data", + "", + []byte { + 0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, + }, + io.ErrUnexpectedEOF, + }, + zlibTest { + "excess data is silently ignored", + "", + []byte { + 0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x78, 0x9c, 0xff, + }, + nil, + }, +} + +func TestZlibInflater(t *testing.T) { + b := new(bytes.Buffer); + for i, tt := range zlibTests { + in := io.NewByteReader(tt.compressed); + zlib, err := NewZlibInflater(in); + if err != nil { + if err != tt.err { + t.Errorf("%s: NewZlibInflater: %s", tt.desc, err); + } + continue; + } + b.Reset(); + n, err := io.Copy(zlib, b); + if err != nil { + if err != tt.err { + t.Errorf("%s: io.Copy: %v want %v", tt.desc, err, tt.err); + } + continue; + } + s := string(b.Data()); + if s != tt.raw { + t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.desc, n, s, len(tt.raw), tt.raw); + } + } +} |