diff options
author | Nigel Tao <nigeltao@golang.org> | 2010-01-30 11:54:39 +1100 |
---|---|---|
committer | Nigel Tao <nigeltao@golang.org> | 2010-01-30 11:54:39 +1100 |
commit | d58b3781ec517269fb54ae2d6d3d32eabba8c91d (patch) | |
tree | 85817bfaf36b98cb288c1567fb4bbbe6596739a2 /src/pkg/compress/gzip/gzip_test.go | |
parent | 58e8cb4e2a0fa2ad4db8a53de275a66d2067900a (diff) | |
download | golang-d58b3781ec517269fb54ae2d6d3d32eabba8c91d.tar.gz |
Add a GZIP test for the empty payload.
R=rsc, r
CC=golang-dev
http://codereview.appspot.com/194131
Diffstat (limited to 'src/pkg/compress/gzip/gzip_test.go')
-rw-r--r-- | src/pkg/compress/gzip/gzip_test.go | 94 |
1 files changed, 57 insertions, 37 deletions
diff --git a/src/pkg/compress/gzip/gzip_test.go b/src/pkg/compress/gzip/gzip_test.go index 292e2b691..3a9843fd5 100644 --- a/src/pkg/compress/gzip/gzip_test.go +++ b/src/pkg/compress/gzip/gzip_test.go @@ -11,55 +11,75 @@ import ( "testing" ) -// Tests that gzipping and then gunzipping is the identity function. -func TestWriter(t *testing.T) { - // Set up the Pipe to do the gzip and gunzip. +// pipe creates two ends of a pipe that gzip and gunzip, and runs dfunc at the +// writer end and ifunc at the reader end. +func pipe(t *testing.T, dfunc func(*Deflater), ifunc func(*Inflater)) { piper, pipew := io.Pipe() defer piper.Close() go func() { defer pipew.Close() deflater, err := NewDeflater(pipew) if err != nil { - t.Errorf("%v", err) - return + t.Fatalf("%v", err) } defer deflater.Close() - deflater.Comment = "comment" - deflater.Extra = strings.Bytes("extra") - deflater.Mtime = 1e8 - deflater.Name = "name" - _, err = deflater.Write(strings.Bytes("payload")) - if err != nil { - t.Errorf("%v", err) - return - } + dfunc(deflater) }() inflater, err := NewInflater(piper) if err != nil { - t.Errorf("%v", err) - return + t.Fatalf("%v", err) } defer inflater.Close() + ifunc(inflater) +} - // Read and compare to the original input. - b, err := ioutil.ReadAll(inflater) - if err != nil { - t.Errorf(": %v", err) - return - } - if string(b) != "payload" { - t.Fatalf("payload is %q, want %q", string(b), "payload") - } - if inflater.Comment != "comment" { - t.Fatalf("comment is %q, want %q", inflater.Comment, "comment") - } - if string(inflater.Extra) != "extra" { - t.Fatalf("extra is %q, want %q", inflater.Extra, "extra") - } - if inflater.Mtime != 1e8 { - t.Fatalf("mtime is %d, want %d", inflater.Mtime, uint32(1e8)) - } - if inflater.Name != "name" { - t.Fatalf("name is %q, want %q", inflater.Name, "name") - } +// Tests that an empty payload still forms a valid GZIP stream. +func TestEmpty(t *testing.T) { + pipe(t, + func(deflater *Deflater) {}, + func(inflater *Inflater) { + b, err := ioutil.ReadAll(inflater) + if err != nil { + t.Fatalf("%v", err) + } + if len(b) != 0 { + t.Fatalf("did not read an empty slice") + } + }) +} + +// Tests that gzipping and then gunzipping is the identity function. +func TestWriter(t *testing.T) { + pipe(t, + func(deflater *Deflater) { + deflater.Comment = "comment" + deflater.Extra = strings.Bytes("extra") + deflater.Mtime = 1e8 + deflater.Name = "name" + _, err := deflater.Write(strings.Bytes("payload")) + if err != nil { + t.Fatalf("%v", err) + } + }, + func(inflater *Inflater) { + b, err := ioutil.ReadAll(inflater) + if err != nil { + t.Fatalf("%v", err) + } + if string(b) != "payload" { + t.Fatalf("payload is %q, want %q", string(b), "payload") + } + if inflater.Comment != "comment" { + t.Fatalf("comment is %q, want %q", inflater.Comment, "comment") + } + if string(inflater.Extra) != "extra" { + t.Fatalf("extra is %q, want %q", inflater.Extra, "extra") + } + if inflater.Mtime != 1e8 { + t.Fatalf("mtime is %d, want %d", inflater.Mtime, uint32(1e8)) + } + if inflater.Name != "name" { + t.Fatalf("name is %q, want %q", inflater.Name, "name") + } + }) } |