blob: 9214200ba7162e6044c391202912d9daeaefd826 [file] [log] [blame]
Brad Fitzpatrick1f0d8f22015-09-14 18:33:55 +00001// Copyright 2015 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
5package pargzip
6
7import (
8 "bytes"
9 "compress/gzip"
10 "io"
11 "strings"
12 "testing"
13 "time"
14)
15
16func TestWriter(t *testing.T) {
17 var in bytes.Buffer
18 big := strings.Repeat("a", 4<<10)
Brad Fitzpatrick13282352016-09-20 06:12:04 +000019 for in.Len() < 20<<20 {
Brad Fitzpatrick1f0d8f22015-09-14 18:33:55 +000020 for i := 0; i < 256; i++ {
21 in.WriteByte(byte(i))
22 in.WriteString(big)
23 }
24 }
25 t.Logf("input size = %v", in.Len())
26 var zbuf bytes.Buffer
27 zw := NewWriter(&zbuf)
28 zw.ChunkSize = 1 << 20
29 zw.Parallel = 4
Brad Fitzpatrick1f0d8f22015-09-14 18:33:55 +000030 t0 := time.Now()
31 if n, err := io.Copy(zw, bytes.NewReader(in.Bytes())); err != nil {
32 t.Fatalf("Copy: %v", err)
33 } else {
34 t.Logf("Copied %d bytes", n)
35 }
36 if err := zw.Close(); err != nil {
37 t.Fatalf("Close: %v", err)
38 }
39 td := time.Since(t0)
40 t.Logf("Compressed size: %v (%0.2f%%) in %v", zbuf.Len(), float64(zbuf.Len())/float64(in.Len())*100, td)
41
42 var back bytes.Buffer
43 zr, _ := gzip.NewReader(bytes.NewReader(zbuf.Bytes()))
44 if _, err := io.Copy(&back, zr); err != nil {
45 t.Fatalf("uncompress Copy: %v", err)
46 }
47 if !bytes.Equal(in.Bytes(), back.Bytes()) {
48 t.Error("decompression failed.")
49 }
50 t.Logf("correctly read back %d bytes", back.Len())
51}