Ejemplo n.º 1
0
func TestBzip2(t *testing.T) {
	data := []byte("hello")

	var want []byte
	{
		var out bytes.Buffer
		w := bzip.NewWriter(&out)
		w.Write(data)
		w.Close()
		want = out.Bytes()
	}

	var got []byte
	{
		var out bytes.Buffer
		w := NewWriter(&out)
		w.Write(data)
		w.Close()
		got = out.Bytes()
	}

	if !reflect.DeepEqual(got, want) {
		t.Errorf("got: %d bytes, want: %d byets", len(got), len(want))
	}
}
Ejemplo n.º 2
0
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")
	}
}
Ejemplo n.º 3
0
func main() {
	w := bzip.NewWriter(os.Stdout)
	if _, err := io.Copy(w, os.Stdin); err != nil {
		log.Fatalf("bzipper: %v\n", err)
	}
	if err := w.Close(); err != nil {
		log.Fatalf("bzipper: close: %v\n", err)
	}
}