-
Notifications
You must be signed in to change notification settings - Fork 2
/
write_read_test.go
67 lines (54 loc) · 1.69 KB
/
write_read_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
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
package huffman
import (
"bytes"
"io"
"testing"
)
func FuzzWriteRead(f *testing.F) {
f.Add([]byte("hello"))
f.Add([]byte("1234567890"))
f.Add([]byte("1234567890abcdef"))
f.Add([]byte("1234567890abcdef1234567890abcdef"))
f.Add([]byte("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"))
f.Add([]byte("cfgvlhböjnhavUBÖObv)öob"))
f.Fuzz(writeReadTest)
}
func writeReadTest(t *testing.T, data []byte) {
huff := NewHuffman()
for i := 1; i < len(data)+1; i++ {
var (
smallBuffer = make([]byte, i)
inputStream = bytes.NewReader(data)
compressedStream = bytes.NewBuffer(make([]byte, 0, len(data)))
w = NewWriter(compressedStream)
decompressedStream = bytes.NewBuffer(make([]byte, 0, len(data)))
)
n, err := io.CopyBuffer(w, inputStream, smallBuffer)
if err != nil {
t.Fatalf("error writing: %v", err)
}
if n != int64(len(data)) {
t.Fatalf("expected to write %d bytes, wrote %d bytes", len(data), n)
}
compressed, err := huff.Compress(data)
if err != nil {
t.Fatalf("error compressing: %v", err)
}
if !bytes.Equal(compressed, compressedStream.Bytes()) {
t.Fatalf("expected %v(%s), got %v(%s)", compressed, string(compressed), compressedStream.Bytes(), compressedStream.String())
}
var (
r = NewReader(compressedStream)
)
n, err = io.CopyBuffer(decompressedStream, r, smallBuffer)
if err != nil {
t.Fatalf("error reading: %v", err)
}
if n != int64(len(data)) {
t.Fatalf("expected to read %d bytes, read %d (buffer size = %d)", len(data), n, i)
}
if !bytes.Equal(data, decompressedStream.Bytes()) {
t.Fatalf("decompressed: expected %v, got %v", data, decompressedStream.Bytes())
}
}
}