MCPcopy
hub / github.com/kopia/kopia / TestCompressor

Function TestCompressor

repo/compression/compressor_test.go:15–87  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

13func TestMain(m *testing.M) { testutil.MyTestMain(m) }
14
15func TestCompressor(t *testing.T) {
16 for id, comp := range ByHeaderID {
17 if isUnsupported[id] {
18 continue
19 }
20
21 t.Run(fmt.Sprintf("compressible-data-%x", id), func(t *testing.T) {
22 // make sure all-zero data is compressed
23 data := make([]byte, 10000)
24
25 var cData bytes.Buffer
26
27 if err := comp.Compress(&cData, bytes.NewReader(data)); err != nil {
28 t.Fatalf("compression error %v", err)
29 return
30 }
31
32 if cData.Len() >= len(data) {
33 t.Errorf("compression not effective for all-zero data (len: %v, expected less than %v)", cData.Len(), len(data))
34 }
35
36 for id2, comp2 := range ByHeaderID {
37 if id != id2 {
38 var dData bytes.Buffer
39
40 if err2 := comp2.Decompress(&dData, bytes.NewReader(cData.Bytes()), true); err2 == nil {
41 t.Errorf("compressor %x was able to decompress results of %x", id2, id)
42 }
43 }
44 }
45
46 var data2 bytes.Buffer
47 if err := comp.Decompress(&data2, bytes.NewReader(cData.Bytes()), true); err != nil {
48 t.Fatalf("decompression error %v", err)
49 }
50
51 if !bytes.Equal(data, data2.Bytes()) {
52 t.Errorf("invalid decompressed data %x, wanted %x", data2, data)
53 }
54
55 t.Logf("compressed %v => %v", len(data), cData.Len())
56 })
57
58 t.Run(fmt.Sprintf("non-compressible-data-%x", id), func(t *testing.T) {
59 // make sure all-random data is not compressed
60 data := make([]byte, 10000)
61 rand.Read(data)
62
63 var cData bytes.Buffer
64
65 err := comp.Compress(&cData, bytes.NewReader(data))
66 if err != nil {
67 t.Fatalf("compression error %v", err)
68 return
69 }
70
71 if cData.Len() < len(data) {
72 t.Errorf("compression magically effective for random data")

Callers

nothing calls this directly

Calls 10

FatalfMethod · 0.80
ErrorfMethod · 0.80
EqualMethod · 0.80
LogfMethod · 0.80
RunMethod · 0.65
CompressMethod · 0.65
DecompressMethod · 0.65
LenMethod · 0.45
BytesMethod · 0.45
ReadMethod · 0.45

Tested by

no test coverage detected