mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-08-18 04:02:40 +00:00
good good study, day day up!
This commit is contained in:
40
vendor/gopl.io/ch13/bzip/bzip2_test.go
generated
vendored
Normal file
40
vendor/gopl.io/ch13/bzip/bzip2_test.go
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
package bzip_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/bzip2" // reader
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"gopl.io/ch13/bzip" // 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")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user