/
bzip2_test.go
41 lines (34 loc) · 1.07 KB
/
bzip2_test.go
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
// Copyright 2015 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bzip2_test
import (
"bytes"
"compress/bzip2" // reader
"io"
"testing"
bzip "github.com/chai2010/bzip2" // writer
)
func TestBzip2(t *testing.T) {
var compressed, uncompressed bytes.Buffer
w := bzip.NewWriter(&compressed)
// Write a repetitive message in a million pieces,
// compressing one copy but not the other.
tee := io.MultiWriter(w, &uncompressed)
for i := 0; i < 1000000; i++ {
io.WriteString(tee, "hello")
}
if err := w.Close(); err != nil {
t.Fatal(err)
}
// Check the size of the compressed stream.
if got, want := compressed.Len(), 255; got != want {
t.Errorf("1 million hellos compressed to %d bytes, want %d", got, want)
}
// Decompress and compare with original.
var decompressed bytes.Buffer
io.Copy(&decompressed, bzip2.NewReader(&compressed))
if !bytes.Equal(uncompressed.Bytes(), decompressed.Bytes()) {
t.Error("decompression yielded a different message")
}
}