OSDN Git Service

eb7a7ec0892f8480fbf2e06d2a1802944c72f7b8
[pf3gnuchains/gcc-fork.git] / libgo / go / compress / gzip / gzip_test.go
1 // Copyright 2010 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.
4
5 package gzip
6
7 import (
8         "bufio"
9         "bytes"
10         "io"
11         "io/ioutil"
12         "testing"
13         "time"
14 )
15
16 // pipe creates two ends of a pipe that gzip and gunzip, and runs dfunc at the
17 // writer end and cfunc at the reader end.
18 func pipe(t *testing.T, dfunc func(*Compressor), cfunc func(*Decompressor)) {
19         piper, pipew := io.Pipe()
20         defer piper.Close()
21         go func() {
22                 defer pipew.Close()
23                 compressor, err := NewWriter(pipew)
24                 if err != nil {
25                         t.Fatalf("%v", err)
26                 }
27                 defer compressor.Close()
28                 dfunc(compressor)
29         }()
30         decompressor, err := NewReader(piper)
31         if err != nil {
32                 t.Fatalf("%v", err)
33         }
34         defer decompressor.Close()
35         cfunc(decompressor)
36 }
37
38 // Tests that an empty payload still forms a valid GZIP stream.
39 func TestEmpty(t *testing.T) {
40         pipe(t,
41                 func(compressor *Compressor) {},
42                 func(decompressor *Decompressor) {
43                         b, err := ioutil.ReadAll(decompressor)
44                         if err != nil {
45                                 t.Fatalf("%v", err)
46                         }
47                         if len(b) != 0 {
48                                 t.Fatalf("did not read an empty slice")
49                         }
50                 })
51 }
52
53 // Tests that gzipping and then gunzipping is the identity function.
54 func TestWriter(t *testing.T) {
55         pipe(t,
56                 func(compressor *Compressor) {
57                         compressor.Comment = "Äußerung"
58                         //compressor.Comment = "comment"
59                         compressor.Extra = []byte("extra")
60                         compressor.ModTime = time.Unix(1e8, 0)
61                         compressor.Name = "name"
62                         _, err := compressor.Write([]byte("payload"))
63                         if err != nil {
64                                 t.Fatalf("%v", err)
65                         }
66                 },
67                 func(decompressor *Decompressor) {
68                         b, err := ioutil.ReadAll(decompressor)
69                         if err != nil {
70                                 t.Fatalf("%v", err)
71                         }
72                         if string(b) != "payload" {
73                                 t.Fatalf("payload is %q, want %q", string(b), "payload")
74                         }
75                         if decompressor.Comment != "Äußerung" {
76                                 t.Fatalf("comment is %q, want %q", decompressor.Comment, "Äußerung")
77                         }
78                         if string(decompressor.Extra) != "extra" {
79                                 t.Fatalf("extra is %q, want %q", decompressor.Extra, "extra")
80                         }
81                         if decompressor.ModTime.Unix() != 1e8 {
82                                 t.Fatalf("mtime is %d, want %d", decompressor.ModTime.Unix(), uint32(1e8))
83                         }
84                         if decompressor.Name != "name" {
85                                 t.Fatalf("name is %q, want %q", decompressor.Name, "name")
86                         }
87                 })
88 }
89
90 func TestLatin1(t *testing.T) {
91         latin1 := []byte{0xc4, 'u', 0xdf, 'e', 'r', 'u', 'n', 'g', 0}
92         utf8 := "Äußerung"
93         z := Decompressor{r: bufio.NewReader(bytes.NewBuffer(latin1))}
94         s, err := z.readString()
95         if err != nil {
96                 t.Fatalf("%v", err)
97         }
98         if s != utf8 {
99                 t.Fatalf("string is %q, want %q", s, utf8)
100         }
101
102         buf := bytes.NewBuffer(make([]byte, 0, len(latin1)))
103         c := Compressor{w: buf}
104         if err = c.writeString(utf8); err != nil {
105                 t.Fatalf("%v", err)
106         }
107         s = buf.String()
108         if s != string(latin1) {
109                 t.Fatalf("string is %v, want %v", s, latin1)
110         }
111         //if s, err = buf.ReadString(0); err != nil {
112         //t.Fatalf("%v", err)
113         //}
114 }