| 1 |
// Copyright 2009 The Go Authors. All rights reserved. |
| 2 |
// Copyright (c) 2015 Klaus Post |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
package flate |
| 7 |
|
| 8 |
import ( |
| 9 |
"bytes" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"os" |
| 13 |
"reflect" |
| 14 |
"strings" |
| 15 |
"sync" |
| 16 |
"testing" |
| 17 |
) |
| 18 |
|
| 19 |
type deflateTest struct { |
| 20 |
in []byte |
| 21 |
level int |
| 22 |
out []byte |
| 23 |
} |
| 24 |
|
| 25 |
type deflateInflateTest struct { |
| 26 |
in []byte |
| 27 |
} |
| 28 |
|
| 29 |
type reverseBitsTest struct { |
| 30 |
in uint16 |
| 31 |
bitCount uint8 |
| 32 |
out uint16 |
| 33 |
} |
| 34 |
|
| 35 |
var deflateTests = []*deflateTest{ |
| 36 |
0: {[]byte{}, 0, []byte{0x3, 0x0}}, |
| 37 |
1: {[]byte{0x11}, BestCompression, []byte{0x12, 0x4, 0xc, 0x0}}, |
| 38 |
2: {[]byte{0x11}, BestCompression, []byte{0x12, 0x4, 0xc, 0x0}}, |
| 39 |
3: {[]byte{0x11}, BestCompression, []byte{0x12, 0x4, 0xc, 0x0}}, |
| 40 |
|
| 41 |
4: {[]byte{0x11}, 0, []byte{0x0, 0x1, 0x0, 0xfe, 0xff, 0x11, 0x3, 0x0}}, |
| 42 |
5: {[]byte{0x11, 0x12}, 0, []byte{0x0, 0x2, 0x0, 0xfd, 0xff, 0x11, 0x12, 0x3, 0x0}}, |
| 43 |
6: {[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 0, |
| 44 |
[]byte{0x0, 0x8, 0x0, 0xf7, 0xff, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x3, 0x0}, |
| 45 |
}, |
| 46 |
7: {[]byte{}, 1, []byte{0x3, 0x0}}, |
| 47 |
8: {[]byte{0x11}, BestCompression, []byte{0x12, 0x4, 0xc, 0x0}}, |
| 48 |
9: {[]byte{0x11, 0x12}, BestCompression, []byte{0x12, 0x14, 0x2, 0xc, 0x0}}, |
| 49 |
10: {[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, BestCompression, []byte{0x12, 0x84, 0x1, 0xc0, 0x0}}, |
| 50 |
11: {[]byte{}, 9, []byte{0x3, 0x0}}, |
| 51 |
12: {[]byte{0x11}, 9, []byte{0x12, 0x4, 0xc, 0x0}}, |
| 52 |
13: {[]byte{0x11, 0x12}, 9, []byte{0x12, 0x14, 0x2, 0xc, 0x0}}, |
| 53 |
14: {[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 9, []byte{0x12, 0x84, 0x1, 0xc0, 0x0}}, |
| 54 |
} |
| 55 |
|
| 56 |
var deflateInflateTests = []*deflateInflateTest{ |
| 57 |
{[]byte{}}, |
| 58 |
{[]byte{0x11}}, |
| 59 |
{[]byte{0x11, 0x12}}, |
| 60 |
{[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}}, |
| 61 |
{[]byte{0x11, 0x10, 0x13, 0x41, 0x21, 0x21, 0x41, 0x13, 0x87, 0x78, 0x13}}, |
| 62 |
{largeDataChunk()}, |
| 63 |
} |
| 64 |
|
| 65 |
var reverseBitsTests = []*reverseBitsTest{ |
| 66 |
{1, 1, 1}, |
| 67 |
{1, 2, 2}, |
| 68 |
{1, 3, 4}, |
| 69 |
{1, 4, 8}, |
| 70 |
{1, 5, 16}, |
| 71 |
{17, 5, 17}, |
| 72 |
{257, 9, 257}, |
| 73 |
{29, 5, 23}, |
| 74 |
} |
| 75 |
|
| 76 |
func largeDataChunk() []byte { |
| 77 |
result := make([]byte, 100000) |
| 78 |
for i := range result { |
| 79 |
result[i] = byte(i * i & 0xFF) |
| 80 |
} |
| 81 |
return result |
| 82 |
} |
| 83 |
|
| 84 |
func TestBulkHash4(t *testing.T) { |
| 85 |
for _, x := range deflateTests { |
| 86 |
y := x.out |
| 87 |
if len(y) >= minMatchLength { |
| 88 |
y = append(y, y...) |
| 89 |
for j := 4; j < len(y); j++ { |
| 90 |
y := y[:j] |
| 91 |
dst := make([]uint32, len(y)-minMatchLength+1) |
| 92 |
for i := range dst { |
| 93 |
dst[i] = uint32(i + 100) |
| 94 |
} |
| 95 |
bulkHash4(y, dst) |
| 96 |
for i, val := range dst { |
| 97 |
got := val |
| 98 |
expect := hash4(y[i:]) |
| 99 |
if got != expect && got == uint32(i)+100 { |
| 100 |
t.Errorf("Len:%d Index:%d, expected 0x%08x but not modified", len(y), i, expect) |
| 101 |
} else if got != expect { |
| 102 |
t.Errorf("Len:%d Index:%d, got 0x%08x expected:0x%08x", len(y), i, got, expect) |
| 103 |
} else { |
| 104 |
//t.Logf("Len:%d Index:%d OK (0x%08x)", len(y), i, got) |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
func TestDeflate(t *testing.T) { |
| 113 |
for i, h := range deflateTests { |
| 114 |
var buf bytes.Buffer |
| 115 |
w, err := NewWriter(&buf, h.level) |
| 116 |
if err != nil { |
| 117 |
t.Errorf("NewWriter: %v", err) |
| 118 |
continue |
| 119 |
} |
| 120 |
w.Write(h.in) |
| 121 |
w.Close() |
| 122 |
if !bytes.Equal(buf.Bytes(), h.out) { |
| 123 |
t.Errorf("%d: Deflate(%d, %x) got \n%#v, want \n%#v", i, h.level, h.in, buf.Bytes(), h.out) |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
// A sparseReader returns a stream consisting of 0s followed by 1<<16 1s. |
| 129 |
// This tests missing hash references in a very large input. |
| 130 |
type sparseReader struct { |
| 131 |
l int64 |
| 132 |
cur int64 |
| 133 |
} |
| 134 |
|
| 135 |
func (r *sparseReader) Read(b []byte) (n int, err error) { |
| 136 |
if r.cur >= r.l { |
| 137 |
return 0, io.EOF |
| 138 |
} |
| 139 |
n = len(b) |
| 140 |
cur := r.cur + int64(n) |
| 141 |
if cur > r.l { |
| 142 |
n -= int(cur - r.l) |
| 143 |
cur = r.l |
| 144 |
} |
| 145 |
for i := range b[0:n] { |
| 146 |
if r.cur+int64(i) >= r.l-1<<16 { |
| 147 |
b[i] = 1 |
| 148 |
} else { |
| 149 |
b[i] = 0 |
| 150 |
} |
| 151 |
} |
| 152 |
r.cur = cur |
| 153 |
return |
| 154 |
} |
| 155 |
|
| 156 |
func TestVeryLongSparseChunk(t *testing.T) { |
| 157 |
if testing.Short() { |
| 158 |
t.Skip("skipping sparse chunk during short test") |
| 159 |
} |
| 160 |
var buf bytes.Buffer |
| 161 |
w, err := NewWriter(&buf, 1) |
| 162 |
if err != nil { |
| 163 |
t.Errorf("NewWriter: %v", err) |
| 164 |
return |
| 165 |
} |
| 166 |
if _, err = io.Copy(w, &sparseReader{l: 23e8}); err != nil { |
| 167 |
t.Errorf("Compress failed: %v", err) |
| 168 |
return |
| 169 |
} |
| 170 |
t.Log("Length:", buf.Len()) |
| 171 |
} |
| 172 |
|
| 173 |
func TestOneMByte(t *testing.T) { |
| 174 |
var input [1024 * 1024]byte |
| 175 |
|
| 176 |
var compressedOutput bytes.Buffer |
| 177 |
for level := HuffmanOnly; level <= BestCompression; level++ { |
| 178 |
compressedOutput.Reset() |
| 179 |
compressor, err := NewWriter(&compressedOutput, level) |
| 180 |
if err != nil { |
| 181 |
t.Fatalf("create: %s", err) |
| 182 |
} |
| 183 |
// Use single write... |
| 184 |
if _, err := compressor.Write(input[:]); err != nil { |
| 185 |
t.Fatalf("compress: %s", err) |
| 186 |
} |
| 187 |
|
| 188 |
if err := compressor.Close(); err != nil { |
| 189 |
t.Fatalf("close: %s", err) |
| 190 |
} |
| 191 |
|
| 192 |
var decompressedOutput bytes.Buffer |
| 193 |
|
| 194 |
decompresser := NewReader(&compressedOutput) |
| 195 |
t.Log("level:", level, "compressed:", compressedOutput.Len()) |
| 196 |
if _, err := io.Copy(&decompressedOutput, decompresser); err != nil { |
| 197 |
t.Fatalf("decompress: %s", err) |
| 198 |
} |
| 199 |
|
| 200 |
if !bytes.Equal(input[:], decompressedOutput.Bytes()) { |
| 201 |
t.Fatal("input and output do not match") |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
type syncBuffer struct { |
| 207 |
buf bytes.Buffer |
| 208 |
mu sync.RWMutex |
| 209 |
closed bool |
| 210 |
ready chan bool |
| 211 |
} |
| 212 |
|
| 213 |
func newSyncBuffer() *syncBuffer { |
| 214 |
return &syncBuffer{ready: make(chan bool, 1)} |
| 215 |
} |
| 216 |
|
| 217 |
func (b *syncBuffer) Read(p []byte) (n int, err error) { |
| 218 |
for { |
| 219 |
b.mu.RLock() |
| 220 |
n, err = b.buf.Read(p) |
| 221 |
b.mu.RUnlock() |
| 222 |
if n > 0 || b.closed { |
| 223 |
return |
| 224 |
} |
| 225 |
<-b.ready |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
func (b *syncBuffer) signal() { |
| 230 |
select { |
| 231 |
case b.ready <- true: |
| 232 |
default: |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
func (b *syncBuffer) Write(p []byte) (n int, err error) { |
| 237 |
n, err = b.buf.Write(p) |
| 238 |
b.signal() |
| 239 |
return |
| 240 |
} |
| 241 |
|
| 242 |
func (b *syncBuffer) WriteMode() { |
| 243 |
b.mu.Lock() |
| 244 |
} |
| 245 |
|
| 246 |
func (b *syncBuffer) ReadMode() { |
| 247 |
b.mu.Unlock() |
| 248 |
b.signal() |
| 249 |
} |
| 250 |
|
| 251 |
func (b *syncBuffer) Close() error { |
| 252 |
b.closed = true |
| 253 |
b.signal() |
| 254 |
return nil |
| 255 |
} |
| 256 |
|
| 257 |
func testSync(t *testing.T, level int, input []byte, name string) { |
| 258 |
if len(input) == 0 { |
| 259 |
return |
| 260 |
} |
| 261 |
|
| 262 |
t.Logf("--testSync %d, %d, %s", level, len(input), name) |
| 263 |
buf := newSyncBuffer() |
| 264 |
buf1 := new(bytes.Buffer) |
| 265 |
buf.WriteMode() |
| 266 |
w, err := NewWriter(io.MultiWriter(buf, buf1), level) |
| 267 |
if err != nil { |
| 268 |
t.Errorf("NewWriter: %v", err) |
| 269 |
return |
| 270 |
} |
| 271 |
r := NewReader(buf) |
| 272 |
|
| 273 |
// Write half the input and read back. |
| 274 |
for i := range 2 { |
| 275 |
var lo, hi int |
| 276 |
if i == 0 { |
| 277 |
lo, hi = 0, (len(input)+1)/2 |
| 278 |
} else { |
| 279 |
lo, hi = (len(input)+1)/2, len(input) |
| 280 |
} |
| 281 |
t.Logf("#%d: write %d-%d", i, lo, hi) |
| 282 |
if _, err := w.Write(input[lo:hi]); err != nil { |
| 283 |
t.Errorf("testSync: write: %v", err) |
| 284 |
return |
| 285 |
} |
| 286 |
if i == 0 { |
| 287 |
if err := w.Flush(); err != nil { |
| 288 |
t.Errorf("testSync: flush: %v", err) |
| 289 |
return |
| 290 |
} |
| 291 |
} else { |
| 292 |
if err := w.Close(); err != nil { |
| 293 |
t.Errorf("testSync: close: %v", err) |
| 294 |
} |
| 295 |
} |
| 296 |
buf.ReadMode() |
| 297 |
out := make([]byte, hi-lo+1) |
| 298 |
m, err := io.ReadAtLeast(r, out, hi-lo) |
| 299 |
t.Logf("#%d: read %d", i, m) |
| 300 |
if m != hi-lo || err != nil { |
| 301 |
t.Errorf("testSync/%d (%d, %d, %s): read %d: %d, %v (%d left)", i, level, len(input), name, hi-lo, m, err, buf.buf.Len()) |
| 302 |
return |
| 303 |
} |
| 304 |
if !bytes.Equal(input[lo:hi], out[:hi-lo]) { |
| 305 |
t.Errorf("testSync/%d: read wrong bytes: %x vs %x", i, input[lo:hi], out[:hi-lo]) |
| 306 |
return |
| 307 |
} |
| 308 |
// This test originally checked that after reading |
| 309 |
// the first half of the input, there was nothing left |
| 310 |
// in the read buffer (buf.buf.Len() != 0) but that is |
| 311 |
// not necessarily the case: the write Flush may emit |
| 312 |
// some extra framing bits that are not necessary |
| 313 |
// to process to obtain the first half of the uncompressed |
| 314 |
// data. The test ran correctly most of the time, because |
| 315 |
// the background goroutine had usually read even |
| 316 |
// those extra bits by now, but it's not a useful thing to |
| 317 |
// check. |
| 318 |
buf.WriteMode() |
| 319 |
} |
| 320 |
buf.ReadMode() |
| 321 |
out := make([]byte, 10) |
| 322 |
if n, err := r.Read(out); n > 0 || err != io.EOF { |
| 323 |
t.Errorf("testSync (%d, %d, %s): final Read: %d, %v (hex: %x)", level, len(input), name, n, err, out[0:n]) |
| 324 |
} |
| 325 |
if buf.buf.Len() != 0 { |
| 326 |
t.Errorf("testSync (%d, %d, %s): extra data at end", level, len(input), name) |
| 327 |
} |
| 328 |
r.Close() |
| 329 |
|
| 330 |
// stream should work for ordinary reader too |
| 331 |
r = NewReader(buf1) |
| 332 |
out, err = io.ReadAll(r) |
| 333 |
if err != nil { |
| 334 |
t.Errorf("testSync: read: %s", err) |
| 335 |
return |
| 336 |
} |
| 337 |
r.Close() |
| 338 |
if !bytes.Equal(input, out) { |
| 339 |
t.Errorf("testSync: decompress(compress(data)) != data: level=%d input=%s", level, name) |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
func testToFromWithLevelAndLimit(t *testing.T, level int, input []byte, name string, limit int) { |
| 344 |
var buffer bytes.Buffer |
| 345 |
w, err := NewWriter(&buffer, level) |
| 346 |
if err != nil { |
| 347 |
t.Errorf("NewWriter: %v", err) |
| 348 |
return |
| 349 |
} |
| 350 |
w.Write(input) |
| 351 |
w.Close() |
| 352 |
if limit > 0 { |
| 353 |
t.Logf("level: %d - Size:%.2f%%, %d b\n", level, float64(buffer.Len()*100)/float64(limit), buffer.Len()) |
| 354 |
} |
| 355 |
if limit > 0 && buffer.Len() > limit { |
| 356 |
t.Errorf("level: %d, len(compress(data)) = %d > limit = %d", level, buffer.Len(), limit) |
| 357 |
} |
| 358 |
|
| 359 |
r := NewReader(&buffer) |
| 360 |
out, err := io.ReadAll(r) |
| 361 |
if err != nil { |
| 362 |
t.Errorf("read: %s", err) |
| 363 |
return |
| 364 |
} |
| 365 |
r.Close() |
| 366 |
if !bytes.Equal(input, out) { |
| 367 |
os.WriteFile("testdata/fails/"+t.Name()+".got", out, os.ModePerm) |
| 368 |
os.WriteFile("testdata/fails/"+t.Name()+".want", input, os.ModePerm) |
| 369 |
t.Errorf("decompress(compress(data)) != data: level=%d input=%s", level, name) |
| 370 |
return |
| 371 |
} |
| 372 |
testSync(t, level, input, name) |
| 373 |
} |
| 374 |
|
| 375 |
func testToFromWithLimit(t *testing.T, input []byte, name string, limit [11]int) { |
| 376 |
for i := range 10 { |
| 377 |
testToFromWithLevelAndLimit(t, i, input, name, limit[i]) |
| 378 |
} |
| 379 |
testToFromWithLevelAndLimit(t, -2, input, name, limit[10]) |
| 380 |
} |
| 381 |
|
| 382 |
func TestDeflateInflate(t *testing.T) { |
| 383 |
for i, h := range deflateInflateTests { |
| 384 |
testToFromWithLimit(t, h.in, fmt.Sprintf("#%d", i), [11]int{}) |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
func TestReverseBits(t *testing.T) { |
| 389 |
for _, h := range reverseBitsTests { |
| 390 |
if v := reverseBits(h.in, h.bitCount); v != h.out { |
| 391 |
t.Errorf("reverseBits(%v,%v) = %v, want %v", |
| 392 |
h.in, h.bitCount, v, h.out) |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
type deflateInflateStringTest struct { |
| 398 |
filename string |
| 399 |
label string |
| 400 |
limit [11]int // Number 11 is ConstantCompression |
| 401 |
} |
| 402 |
|
| 403 |
var deflateInflateStringTests = []deflateInflateStringTest{ |
| 404 |
{ |
| 405 |
"../testdata/e.txt", |
| 406 |
"2.718281828...", |
| 407 |
[...]int{100018, 67900, 50960, 51150, 50930, 50790, 50790, 50790, 50790, 50790, 43683 + 100}, |
| 408 |
}, |
| 409 |
{ |
| 410 |
"../testdata/Mark.Twain-Tom.Sawyer.txt", |
| 411 |
"Mark.Twain-Tom.Sawyer", |
| 412 |
[...]int{387999, 185000, 182361, 179974, 174124, 168819, 162936, 160506, 160295, 160295, 233460 + 100}, |
| 413 |
}, |
| 414 |
} |
| 415 |
|
| 416 |
func TestDeflateInflateString(t *testing.T) { |
| 417 |
for _, test := range deflateInflateStringTests { |
| 418 |
gold, err := os.ReadFile(test.filename) |
| 419 |
if err != nil { |
| 420 |
t.Error(err) |
| 421 |
} |
| 422 |
// Remove returns that may be present on Windows |
| 423 |
neutral := strings.Map(func(r rune) rune { |
| 424 |
if r != '\r' { |
| 425 |
return r |
| 426 |
} |
| 427 |
return -1 |
| 428 |
}, string(gold)) |
| 429 |
|
| 430 |
testToFromWithLimit(t, []byte(neutral), test.label, test.limit) |
| 431 |
|
| 432 |
if testing.Short() { |
| 433 |
break |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
func TestReaderDict(t *testing.T) { |
| 439 |
const ( |
| 440 |
dict = "hello world" |
| 441 |
text = "hello again world" |
| 442 |
) |
| 443 |
var b bytes.Buffer |
| 444 |
w, err := NewWriter(&b, 5) |
| 445 |
if err != nil { |
| 446 |
t.Fatalf("NewWriter: %v", err) |
| 447 |
} |
| 448 |
w.Write([]byte(dict)) |
| 449 |
w.Flush() |
| 450 |
b.Reset() |
| 451 |
w.Write([]byte(text)) |
| 452 |
w.Close() |
| 453 |
|
| 454 |
r := NewReaderDict(&b, []byte(dict)) |
| 455 |
data, err := io.ReadAll(r) |
| 456 |
if err != nil { |
| 457 |
t.Fatal(err) |
| 458 |
} |
| 459 |
if string(data) != "hello again world" { |
| 460 |
t.Fatalf("read returned %q want %q", string(data), text) |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
func TestWriterDict(t *testing.T) { |
| 465 |
const ( |
| 466 |
dict = "hello world Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." |
| 467 |
text = "hello world Lorem ipsum dolor sit amet" |
| 468 |
) |
| 469 |
// This test is sensitive to algorithm changes that skip |
| 470 |
// data in favour of speed. Higher levels are less prone to this |
| 471 |
// so we test level 4-9. |
| 472 |
for l := 4; l < 9; l++ { |
| 473 |
var b bytes.Buffer |
| 474 |
w, err := NewWriter(&b, l) |
| 475 |
if err != nil { |
| 476 |
t.Fatalf("level %d, NewWriter: %v", l, err) |
| 477 |
} |
| 478 |
w.Write([]byte(dict)) |
| 479 |
w.Flush() |
| 480 |
b.Reset() |
| 481 |
w.Write([]byte(text)) |
| 482 |
w.Close() |
| 483 |
|
| 484 |
var b1 bytes.Buffer |
| 485 |
w, _ = NewWriterDict(&b1, l, []byte(dict)) |
| 486 |
w.Write([]byte(text)) |
| 487 |
w.Close() |
| 488 |
|
| 489 |
if !bytes.Equal(b1.Bytes(), b.Bytes()) { |
| 490 |
t.Errorf("level %d, writer wrote\n%v\n want\n%v", l, b1.Bytes(), b.Bytes()) |
| 491 |
} |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
// See http://code.google.com/p/go/issues/detail?id=2508 |
| 496 |
func TestRegression2508(t *testing.T) { |
| 497 |
if testing.Short() { |
| 498 |
t.Logf("test disabled with -short") |
| 499 |
return |
| 500 |
} |
| 501 |
w, err := NewWriter(io.Discard, 1) |
| 502 |
if err != nil { |
| 503 |
t.Fatalf("NewWriter: %v", err) |
| 504 |
} |
| 505 |
buf := make([]byte, 1024) |
| 506 |
for range 131072 { |
| 507 |
if _, err := w.Write(buf); err != nil { |
| 508 |
t.Fatalf("writer failed: %v", err) |
| 509 |
} |
| 510 |
} |
| 511 |
w.Close() |
| 512 |
} |
| 513 |
|
| 514 |
func TestWriterReset(t *testing.T) { |
| 515 |
for level := -2; level <= 9; level++ { |
| 516 |
if level == -1 { |
| 517 |
level++ |
| 518 |
} |
| 519 |
if testing.Short() && level > 1 { |
| 520 |
break |
| 521 |
} |
| 522 |
w, err := NewWriter(io.Discard, level) |
| 523 |
if err != nil { |
| 524 |
t.Fatalf("NewWriter: %v", err) |
| 525 |
} |
| 526 |
buf := []byte("hello world") |
| 527 |
for range 1024 { |
| 528 |
w.Write(buf) |
| 529 |
} |
| 530 |
w.Reset(io.Discard) |
| 531 |
|
| 532 |
wref, err := NewWriter(io.Discard, level) |
| 533 |
if err != nil { |
| 534 |
t.Fatalf("NewWriter: %v", err) |
| 535 |
} |
| 536 |
|
| 537 |
// DeepEqual doesn't compare functions. |
| 538 |
w.d.fill, wref.d.fill = nil, nil |
| 539 |
w.d.step, wref.d.step = nil, nil |
| 540 |
w.d.state, wref.d.state = nil, nil |
| 541 |
w.d.fast, wref.d.fast = nil, nil |
| 542 |
|
| 543 |
// hashMatch is always overwritten when used. |
| 544 |
if w.d.tokens.n != 0 { |
| 545 |
t.Errorf("level %d Writer not reset after Reset. %d tokens were present", level, w.d.tokens.n) |
| 546 |
} |
| 547 |
// As long as the length is 0, we don't care about the content. |
| 548 |
w.d.tokens = wref.d.tokens |
| 549 |
|
| 550 |
// We don't care if there are values in the window, as long as it is at d.index is 0 |
| 551 |
w.d.window = wref.d.window |
| 552 |
if !reflect.DeepEqual(w, wref) { |
| 553 |
t.Errorf("level %d Writer not reset after Reset", level) |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
for i := HuffmanOnly; i <= BestCompression; i++ { |
| 558 |
testResetOutput(t, fmt.Sprint("level-", i), func(w io.Writer) (*Writer, error) { return NewWriter(w, i) }) |
| 559 |
} |
| 560 |
dict := []byte(strings.Repeat("we are the world - how are you?", 3)) |
| 561 |
for i := HuffmanOnly; i <= BestCompression; i++ { |
| 562 |
testResetOutput(t, fmt.Sprint("dict-level-", i), func(w io.Writer) (*Writer, error) { return NewWriterDict(w, i, dict) }) |
| 563 |
} |
| 564 |
for i := HuffmanOnly; i <= BestCompression; i++ { |
| 565 |
testResetOutput(t, fmt.Sprint("dict-reset-level-", i), func(w io.Writer) (*Writer, error) { |
| 566 |
w2, err := NewWriter(nil, i) |
| 567 |
if err != nil { |
| 568 |
return w2, err |
| 569 |
} |
| 570 |
w2.ResetDict(w, dict) |
| 571 |
return w2, nil |
| 572 |
}) |
| 573 |
} |
| 574 |
testResetOutput(t, fmt.Sprint("dict-reset-window"), func(w io.Writer) (*Writer, error) { |
| 575 |
w2, err := NewWriterWindow(nil, 1024) |
| 576 |
if err != nil { |
| 577 |
return w2, err |
| 578 |
} |
| 579 |
w2.ResetDict(w, dict) |
| 580 |
return w2, nil |
| 581 |
}) |
| 582 |
} |
| 583 |
|
| 584 |
func testResetOutput(t *testing.T, name string, newWriter func(w io.Writer) (*Writer, error)) { |
| 585 |
t.Run(name, func(t *testing.T) { |
| 586 |
buf := new(bytes.Buffer) |
| 587 |
w, err := newWriter(buf) |
| 588 |
if err != nil { |
| 589 |
t.Fatalf("NewWriter: %v", err) |
| 590 |
} |
| 591 |
b := []byte("hello world - how are you doing?") |
| 592 |
for range 1024 { |
| 593 |
w.Write(b) |
| 594 |
} |
| 595 |
w.Close() |
| 596 |
out1 := buf.Bytes() |
| 597 |
|
| 598 |
buf2 := new(bytes.Buffer) |
| 599 |
w.Reset(buf2) |
| 600 |
for range 1024 { |
| 601 |
w.Write(b) |
| 602 |
} |
| 603 |
w.Close() |
| 604 |
out2 := buf2.Bytes() |
| 605 |
|
| 606 |
if len(out1) != len(out2) { |
| 607 |
t.Errorf("got %d, expected %d bytes", len(out2), len(out1)) |
| 608 |
} |
| 609 |
if !bytes.Equal(out1, out2) { |
| 610 |
mm := 0 |
| 611 |
for i, b := range out1[:len(out2)] { |
| 612 |
if b != out2[i] { |
| 613 |
t.Errorf("mismatch index %d: %02x, expected %02x", i, out2[i], b) |
| 614 |
} |
| 615 |
mm++ |
| 616 |
if mm == 10 { |
| 617 |
t.Fatal("Stopping") |
| 618 |
} |
| 619 |
} |
| 620 |
} |
| 621 |
t.Logf("got %d bytes", len(out1)) |
| 622 |
}) |
| 623 |
} |
| 624 |
|
| 625 |
// TestBestSpeed tests that round-tripping through deflate and then inflate |
| 626 |
// recovers the original input. The Write sizes are near the thresholds in the |
| 627 |
// compressor.encSpeed method (0, 16, 128), as well as near maxStoreBlockSize |
| 628 |
// (65535). |
| 629 |
func TestBestSpeed(t *testing.T) { |
| 630 |
abc := make([]byte, 128) |
| 631 |
for i := range abc { |
| 632 |
abc[i] = byte(i) |
| 633 |
} |
| 634 |
abcabc := bytes.Repeat(abc, 131072/len(abc)) |
| 635 |
var want []byte |
| 636 |
|
| 637 |
testCases := [][]int{ |
| 638 |
{65536, 0}, |
| 639 |
{65536, 1}, |
| 640 |
{65536, 1, 256}, |
| 641 |
{65536, 1, 65536}, |
| 642 |
{65536, 14}, |
| 643 |
{65536, 15}, |
| 644 |
{65536, 16}, |
| 645 |
{65536, 16, 256}, |
| 646 |
{65536, 16, 65536}, |
| 647 |
{65536, 127}, |
| 648 |
{65536, 128}, |
| 649 |
{65536, 128, 256}, |
| 650 |
{65536, 128, 65536}, |
| 651 |
{65536, 129}, |
| 652 |
{65536, 65536, 256}, |
| 653 |
{65536, 65536, 65536}, |
| 654 |
} |
| 655 |
|
| 656 |
for i, tc := range testCases { |
| 657 |
if testing.Short() && i > 5 { |
| 658 |
t.Skip() |
| 659 |
} |
| 660 |
for _, firstN := range []int{1, 65534, 65535, 65536, 65537, 131072} { |
| 661 |
tc[0] = firstN |
| 662 |
outer: |
| 663 |
for _, flush := range []bool{false, true} { |
| 664 |
buf := new(bytes.Buffer) |
| 665 |
want = want[:0] |
| 666 |
|
| 667 |
w, err := NewWriter(buf, BestSpeed) |
| 668 |
if err != nil { |
| 669 |
t.Errorf("i=%d, firstN=%d, flush=%t: NewWriter: %v", i, firstN, flush, err) |
| 670 |
continue |
| 671 |
} |
| 672 |
for _, n := range tc { |
| 673 |
want = append(want, abcabc[:n]...) |
| 674 |
if _, err := w.Write(abcabc[:n]); err != nil { |
| 675 |
t.Errorf("i=%d, firstN=%d, flush=%t: Write: %v", i, firstN, flush, err) |
| 676 |
continue outer |
| 677 |
} |
| 678 |
if !flush { |
| 679 |
continue |
| 680 |
} |
| 681 |
if err := w.Flush(); err != nil { |
| 682 |
t.Errorf("i=%d, firstN=%d, flush=%t: Flush: %v", i, firstN, flush, err) |
| 683 |
continue outer |
| 684 |
} |
| 685 |
} |
| 686 |
if err := w.Close(); err != nil { |
| 687 |
t.Errorf("i=%d, firstN=%d, flush=%t: Close: %v", i, firstN, flush, err) |
| 688 |
continue |
| 689 |
} |
| 690 |
|
| 691 |
r := NewReader(buf) |
| 692 |
got, err := io.ReadAll(r) |
| 693 |
if err != nil { |
| 694 |
t.Errorf("i=%d, firstN=%d, flush=%t: ReadAll: %v", i, firstN, flush, err) |
| 695 |
continue |
| 696 |
} |
| 697 |
r.Close() |
| 698 |
|
| 699 |
if !bytes.Equal(got, want) { |
| 700 |
t.Errorf("i=%d, firstN=%d, flush=%t: corruption during deflate-then-inflate", i, firstN, flush) |
| 701 |
continue |
| 702 |
} |
| 703 |
} |
| 704 |
} |
| 705 |
} |
| 706 |
} |