|
7 | 7 | "testing"
|
8 | 8 | )
|
9 | 9 |
|
10 |
| -func FuzzWrite(f *testing.F) { |
11 |
| - refHuffman := NewHuffman() |
12 |
| - |
| 10 | +func FuzzWriterWrite(f *testing.F) { |
13 | 11 | f.Add([]byte{})
|
14 | 12 | f.Add([]byte("hello"))
|
15 | 13 | f.Add([]byte("1234567890"))
|
@@ -96,35 +94,37 @@ func FuzzWrite(f *testing.F) {
|
96 | 94 | 0x03, 0x22, 0x02, 0x00, 0x00, 0x17,
|
97 | 95 | })
|
98 | 96 |
|
99 |
| - dict := NewDictionary() |
100 |
| - |
101 | 97 | f.Fuzz(func(t *testing.T, data []byte) {
|
102 |
| - // TODO: except for the case where no data is passed, Compress and Write should do the same. |
103 |
| - if len(data) == 0 { |
104 |
| - return |
105 |
| - } |
106 |
| - inputStream := bytes.NewReader(data) |
107 |
| - compressedStream := bytes.NewBuffer(make([]byte, 0, len(data))) |
| 98 | + testWriteAndCompareWithCompress(t, data) |
| 99 | + }) |
| 100 | +} |
108 | 101 |
|
109 |
| - w := NewWriterDict(dict, compressedStream) |
110 |
| - n, err := io.CopyBuffer(w, inputStream, make([]byte, 2)) // we want to test with a small buffer explicitly |
111 |
| - if err != nil { |
112 |
| - t.Fatalf("error writing: %v", err) |
113 |
| - } |
| 102 | +func testWriteAndCompareWithCompress(t *testing.T, data []byte) { |
114 | 103 |
|
115 |
| - if n != int64(len(data)) { |
116 |
| - t.Fatalf("expected to write %d bytes, wrote %d", len(data), n) |
117 |
| - } |
| 104 | + var ( |
| 105 | + huff = NewHuffman() |
| 106 | + inputStream = bytes.NewReader(data) |
| 107 | + compressedStream = bytes.NewBuffer(make([]byte, 0, len(data))) |
| 108 | + w = NewWriter(compressedStream) |
| 109 | + ) |
118 | 110 |
|
119 |
| - compressed, err := refHuffman.Compress(data) |
120 |
| - if err != nil { |
121 |
| - t.Fatalf("error compressing: %v", err) |
122 |
| - } |
| 111 | + n, err := io.CopyBuffer(w, inputStream, make([]byte, 1)) // we want to test with a small buffer explicitly |
| 112 | + if err != nil { |
| 113 | + t.Fatalf("error writing: %v", err) |
| 114 | + } |
123 | 115 |
|
124 |
| - if !bytes.Equal(compressed, compressedStream.Bytes()) { |
125 |
| - t.Fatalf("expected %v(%s), got %v(%s)", compressed, string(compressed), compressedStream.Bytes(), string(compressedStream.Bytes())) |
126 |
| - } |
127 |
| - }) |
| 116 | + if n != int64(len(data)) { |
| 117 | + t.Fatalf("expected to write %d bytes, wrote %d", len(data), n) |
| 118 | + } |
| 119 | + |
| 120 | + compressed, err := huff.Compress(data) |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("error compressing: %v", err) |
| 123 | + } |
| 124 | + |
| 125 | + if !bytes.Equal(compressed, compressedStream.Bytes()) { |
| 126 | + t.Fatalf("expected %v(%s), got %v(%s)", compressed, string(compressed), compressedStream.Bytes(), compressedStream.String()) |
| 127 | + } |
128 | 128 | }
|
129 | 129 |
|
130 | 130 | func BenchmarkCompression(b *testing.B) {
|
|
0 commit comments