1
0
mirror of https://github.com/golang/go synced 2024-11-22 01:14:40 -07:00

Add a GZIP test for the empty payload.

R=rsc, r
CC=golang-dev
https://golang.org/cl/194131
This commit is contained in:
Nigel Tao 2010-01-30 11:54:39 +11:00
parent 84f9b70280
commit af7e0f1bdd

View File

@ -11,41 +11,60 @@ import (
"testing" "testing"
) )
// Tests that gzipping and then gunzipping is the identity function. // pipe creates two ends of a pipe that gzip and gunzip, and runs dfunc at the
func TestWriter(t *testing.T) { // writer end and ifunc at the reader end.
// Set up the Pipe to do the gzip and gunzip. func pipe(t *testing.T, dfunc func(*Deflater), ifunc func(*Inflater)) {
piper, pipew := io.Pipe() piper, pipew := io.Pipe()
defer piper.Close() defer piper.Close()
go func() { go func() {
defer pipew.Close() defer pipew.Close()
deflater, err := NewDeflater(pipew) deflater, err := NewDeflater(pipew)
if err != nil { if err != nil {
t.Errorf("%v", err) t.Fatalf("%v", err)
return
} }
defer deflater.Close() defer deflater.Close()
dfunc(deflater)
}()
inflater, err := NewInflater(piper)
if err != nil {
t.Fatalf("%v", err)
}
defer inflater.Close()
ifunc(inflater)
}
// 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.Comment = "comment"
deflater.Extra = strings.Bytes("extra") deflater.Extra = strings.Bytes("extra")
deflater.Mtime = 1e8 deflater.Mtime = 1e8
deflater.Name = "name" deflater.Name = "name"
_, err = deflater.Write(strings.Bytes("payload")) _, err := deflater.Write(strings.Bytes("payload"))
if err != nil { if err != nil {
t.Errorf("%v", err) t.Fatalf("%v", err)
return
} }
}() },
inflater, err := NewInflater(piper) func(inflater *Inflater) {
if err != nil {
t.Errorf("%v", err)
return
}
defer inflater.Close()
// Read and compare to the original input.
b, err := ioutil.ReadAll(inflater) b, err := ioutil.ReadAll(inflater)
if err != nil { if err != nil {
t.Errorf(": %v", err) t.Fatalf("%v", err)
return
} }
if string(b) != "payload" { if string(b) != "payload" {
t.Fatalf("payload is %q, want %q", string(b), "payload") t.Fatalf("payload is %q, want %q", string(b), "payload")
@ -62,4 +81,5 @@ func TestWriter(t *testing.T) {
if inflater.Name != "name" { if inflater.Name != "name" {
t.Fatalf("name is %q, want %q", inflater.Name, "name") t.Fatalf("name is %q, want %q", inflater.Name, "name")
} }
})
} }