MCPcopy
hub / github.com/klauspost/compress / TestWithEncoderPadding

Function TestWithEncoderPadding

zstd/encoder_test.go:450–537  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

448}
449
450func TestWithEncoderPadding(t *testing.T) {
451 n := 100
452 if testing.Short() {
453 n = 2
454 }
455 rng := rand.New(rand.NewSource(0x1337))
456 d, err := NewReader(nil)
457 if err != nil {
458 t.Fatal(err)
459 }
460 defer d.Close()
461
462 for i := 0; i < n; i++ {
463 padding := (rng.Int() & 0xfff) + 1
464 src := make([]byte, (rng.Int()&0xfffff)+1)
465 for i := range src {
466 src[i] = uint8(rng.Uint32()) & 7
467 }
468 e, err := NewWriter(nil, WithEncoderPadding(padding), WithEncoderCRC(rng.Uint32()&1 == 0))
469 if err != nil {
470 t.Fatal(err)
471 }
472 // Test the added padding is invisible.
473 dst := e.EncodeAll(src, nil)
474 if len(dst)%padding != 0 {
475 t.Fatalf("wanted size to be multiple of %d, got size %d with remainder %d", padding, len(dst), len(dst)%padding)
476 }
477 got, err := d.DecodeAll(dst, nil)
478 if err != nil {
479 t.Fatal(err)
480 }
481 if !bytes.Equal(src, got) {
482 t.Fatal("output mismatch")
483 }
484 // Test when we supply data as well.
485 dst = e.EncodeAll(src, make([]byte, rng.Int()&255))
486 if len(dst)%padding != 0 {
487 t.Fatalf("wanted size to be multiple of %d, got size %d with remainder %d", padding, len(dst), len(dst)%padding)
488 }
489
490 // Test using the writer.
491 var buf bytes.Buffer
492 e.ResetContentSize(&buf, int64(len(src)))
493 _, err = io.Copy(e, bytes.NewBuffer(src))
494 if err != nil {
495 t.Fatal(err)
496 }
497 err = e.Close()
498 if err != nil {
499 t.Fatal(err)
500 }
501 dst = buf.Bytes()
502 if len(dst)%padding != 0 {
503 t.Fatalf("wanted size to be multiple of %d, got size %d with remainder %d", padding, len(dst), len(dst)%padding)
504 }
505 // Test the added padding is invisible.
506 got, err = d.DecodeAll(dst, nil)
507 if err != nil {

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
CloseMethod · 0.95
ResetMethod · 0.95
WithEncoderPaddingFunction · 0.85
WithEncoderCRCFunction · 0.85
FatalMethod · 0.80
EncodeAllMethod · 0.80
DecodeAllMethod · 0.80
ResetContentSizeMethod · 0.80
CopyMethod · 0.80
NewReaderFunction · 0.70
NewWriterFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…