File: compress_test.go

package info (click to toggle)
golang-github-valyala-fasthttp 20160617-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 980 kB
  • sloc: makefile: 18
file content (89 lines) | stat: -rw-r--r-- 2,470 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package fasthttp

import (
	"bytes"
	"io/ioutil"
	"testing"
)

func TestGzipBytes(t *testing.T) {
	testGzipBytes(t, "")
	testGzipBytes(t, "foobar")
	testGzipBytes(t, "выфаодлодл одлфываыв sd2 k34")
}

func testGzipBytes(t *testing.T, s string) {
	prefix := []byte("foobar")
	gzippedS := AppendGzipBytes(prefix, []byte(s))
	if !bytes.Equal(gzippedS[:len(prefix)], prefix) {
		t.Fatalf("unexpected prefix when compressing %q: %q. Expecting %q", s, gzippedS[:len(prefix)], prefix)
	}

	gunzippedS, err := AppendGunzipBytes(prefix, gzippedS[len(prefix):])
	if err != nil {
		t.Fatalf("unexpected error when uncompressing %q: %s", s, err)
	}
	if !bytes.Equal(gunzippedS[:len(prefix)], prefix) {
		t.Fatalf("unexpected prefix when uncompressing %q: %q. Expecting %q", s, gunzippedS[:len(prefix)], prefix)
	}
	gunzippedS = gunzippedS[len(prefix):]
	if string(gunzippedS) != s {
		t.Fatalf("unexpected uncompressed string %q. Expecting %q", gunzippedS, s)
	}
}

func TestGzipCompress(t *testing.T) {
	testGzipCompress(t, "")
	testGzipCompress(t, "foobar")
	testGzipCompress(t, "ajjnkn asdlkjfqoijfw  jfqkwj foj  eowjiq")
}

func TestFlateCompress(t *testing.T) {
	testFlateCompress(t, "")
	testFlateCompress(t, "foobar")
	testFlateCompress(t, "adf asd asd fasd fasd")
}

func testGzipCompress(t *testing.T, s string) {
	var buf bytes.Buffer
	zw := acquireGzipWriter(&buf, CompressDefaultCompression)
	if _, err := zw.Write([]byte(s)); err != nil {
		t.Fatalf("unexpected error: %s. s=%q", err, s)
	}
	releaseGzipWriter(zw)

	zr, err := acquireGzipReader(&buf)
	if err != nil {
		t.Fatalf("unexpected error: %s. s=%q", err, s)
	}
	body, err := ioutil.ReadAll(zr)
	if err != nil {
		t.Fatalf("unexpected error: %s. s=%q", err, s)
	}
	if string(body) != s {
		t.Fatalf("unexpected string after decompression: %q. Expecting %q", body, s)
	}
	releaseGzipReader(zr)
}

func testFlateCompress(t *testing.T, s string) {
	var buf bytes.Buffer
	zw := acquireFlateWriter(&buf, CompressDefaultCompression)
	if _, err := zw.Write([]byte(s)); err != nil {
		t.Fatalf("unexpected error: %s. s=%q", err, s)
	}
	releaseFlateWriter(zw)

	zr, err := acquireFlateReader(&buf)
	if err != nil {
		t.Fatalf("unexpected error: %s. s=%q", err, s)
	}
	body, err := ioutil.ReadAll(zr)
	if err != nil {
		t.Fatalf("unexpected error: %s. s=%q", err, s)
	}
	if string(body) != s {
		t.Fatalf("unexpected string after decompression: %q. Expecting %q", body, s)
	}
	releaseFlateReader(zr)
}