-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompression_test.go
More file actions
107 lines (91 loc) · 4.16 KB
/
compression_test.go
File metadata and controls
107 lines (91 loc) · 4.16 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package sentencecipher
import (
"bytes"
"compress/flate"
"os"
"testing"
)
func compressFlate(data []byte) ([]byte, error) {
var buf bytes.Buffer
w, err := flate.NewWriter(&buf, flate.BestCompression)
if err != nil {
return nil, err
}
if _, err := w.Write(data); err != nil {
return nil, err
}
if err := w.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func runComparison(t *testing.T, name string, data []byte) {
t.Run(name, func(t *testing.T) {
if len(data) == 0 {
t.Skip("Empty data")
}
// Brotli compression (current implementation)
brotliCompressed, err := compress(data)
if err != nil {
t.Fatalf("Brotli compression failed: %v", err)
}
// Flate compression
flateCompressed, err := compressFlate(data)
if err != nil {
t.Fatalf("Flate compression failed: %v", err)
}
t.Logf("--- %s Data Analysis ---", name)
t.Logf("Original size: %d bytes", len(data))
t.Logf("Brotli compressed: %d bytes (%.2f%%)", len(brotliCompressed), float64(len(brotliCompressed))/float64(len(data))*100)
t.Logf("Flate compressed: %d bytes (%.2f%%)", len(flateCompressed), float64(len(flateCompressed))/float64(len(data))*100)
if len(brotliCompressed) < len(flateCompressed) {
diff := len(flateCompressed) - len(brotliCompressed)
t.Logf(">> Brotli is smaller by %d bytes", diff)
} else {
diff := len(brotliCompressed) - len(flateCompressed)
t.Logf(">> Flate is smaller by %d bytes", diff)
}
cipher := NewDefaultCipher()
// Compare Standard Encoding (Cipher sentences)
rawStd := cipher.encodeRaw(data)
brotliStd := cipher.encodeRaw(brotliCompressed)
flateStd := cipher.encodeRaw(flateCompressed)
t.Logf("\n[Standard Encoding]")
t.Logf("Raw (No Compress): %d chars", len(rawStd))
t.Logf("Brotli encoded: %d chars (%.2f%% of Raw)", len(brotliStd), float64(len(brotliStd))/float64(len(rawStd))*100)
t.Logf("Flate encoded: %d chars (%.2f%% of Raw)", len(flateStd), float64(len(flateStd))/float64(len(rawStd))*100)
diffStd := len(flateStd) - len(brotliStd)
t.Logf("Brotli vs Flate diff: %d chars", diffStd)
// Compare Natural Encoding (Email style)
rawNat := cipher.encodeNaturalRaw(data)
brotliNat := cipher.encodeNaturalRaw(brotliCompressed)
flateNat := cipher.encodeNaturalRaw(flateCompressed)
t.Logf("\n[Natural Encoding]")
t.Logf("Raw (No Compress): %d chars", len(rawNat))
t.Logf("Brotli encoded: %d chars (%.2f%% of Raw)", len(brotliNat), float64(len(brotliNat))/float64(len(rawNat))*100)
t.Logf("Flate encoded: %d chars (%.2f%% of Raw)", len(flateNat), float64(len(flateNat))/float64(len(rawNat))*100)
diffNat := len(flateNat) - len(brotliNat)
t.Logf("Brotli vs Flate diff: %d chars", diffNat)
t.Logf("---------------------------")
})
}
func TestCompressionComparison(t *testing.T) {
// 1. Text Input
input := `Dashwood contempt on mr unlocked resolved provided of of. Stanhill wondered it it welcomed oh. Hundred no prudent he however smiling at an offence. If earnestly extremity he he propriety something admitting convinced ye. Pleasant in to although as if differed horrible. Mirth his quick its set front enjoy hoped had there. Who connection imprudence middletons too but increasing celebrated principles joy. Herself too improve gay winding ask expense are compact. New all paid few hard pure she.
Insipidity the sufficient discretion imprudence resolution sir him decisively. Proceed how any engaged visitor. Explained propriety off out perpetual his you. Feel sold off felt nay rose met you. We so entreaties cultivated astonished is. Was sister for few longer mrs sudden talent become. Done may bore quit evil old mile. If likely am of beauty tastes.`
runComparison(t, "English Text", []byte(input))
// 2. Binary Input (Image)
// Try to read icon16.png if it exists, otherwise generate random binary data
imgData, err := os.ReadFile("icon16.png")
if err == nil {
runComparison(t, "Image (icon16.png)", imgData)
} else {
t.Log("icon16.png not found, using generated binary data")
// Generate some repetitive binary data (compressible)
binData := make([]byte, 1000)
for i := 0; i < 1000; i++ {
binData[i] = byte(i % 256)
}
runComparison(t, "Generated Binary", binData)
}
}