@@ -34,7 +34,7 @@ func TestCompressDecompress_RoundTrip(t *testing.T) {
3434
3535 for _ , tt := range tests {
3636 t .Run (tt .name , func (t * testing.T ) {
37- compressed , err := Compress (tt .data )
37+ compressed , err := Compress (tt .data , LevelDefault )
3838 require .NoError (t , err )
3939
4040 assert .True (t , IsCompressed (compressed ), "compressed data should have magic prefix" )
@@ -47,12 +47,46 @@ func TestCompressDecompress_RoundTrip(t *testing.T) {
4747 }
4848}
4949
50+ func TestCompress_AllLevelsRoundTrip (t * testing.T ) {
51+ data := bytes .Repeat ([]byte ("adaptive compression level test " ), 5000 )
52+ levels := []struct {
53+ name string
54+ level CompressionLevel
55+ }{
56+ {"fastest" , LevelFastest },
57+ {"default" , LevelDefault },
58+ {"best" , LevelBest },
59+ }
60+
61+ var sizes []int
62+ for _ , lvl := range levels {
63+ t .Run (lvl .name , func (t * testing.T ) {
64+ compressed , err := Compress (data , lvl .level )
65+ require .NoError (t , err )
66+ assert .True (t , IsCompressed (compressed ))
67+
68+ sizes = append (sizes , len (compressed ))
69+
70+ decompressed , err := Decompress (compressed )
71+ require .NoError (t , err )
72+ assert .Equal (t , data , decompressed )
73+
74+ t .Logf ("level=%s compressed=%d ratio=%.4f" , lvl .name , len (compressed ), float64 (len (compressed ))/ float64 (len (data )))
75+ })
76+ }
77+
78+ // Best should produce equal or smaller output than Fastest
79+ if len (sizes ) == 3 {
80+ assert .LessOrEqual (t , sizes [2 ], sizes [0 ], "LevelBest should compress at least as well as LevelFastest" )
81+ }
82+ }
83+
5084func TestCompress_Empty (t * testing.T ) {
51- compressed , err := Compress (nil )
85+ compressed , err := Compress (nil , LevelDefault )
5286 require .NoError (t , err )
5387 assert .Nil (t , compressed )
5488
55- compressed , err = Compress ([]byte {})
89+ compressed , err = Compress ([]byte {}, LevelDefault )
5690 require .NoError (t , err )
5791 assert .Empty (t , compressed )
5892}
@@ -112,9 +146,8 @@ func TestIsCompressed(t *testing.T) {
112146}
113147
114148func TestCompress_AchievesCompression (t * testing.T ) {
115- // Highly compressible data should achieve meaningful compression
116149 data := bytes .Repeat ([]byte ("rollkit block data with repeated content " ), 10000 )
117- compressed , err := Compress (data )
150+ compressed , err := Compress (data , LevelDefault )
118151 require .NoError (t , err )
119152
120153 ratio := float64 (len (compressed )) / float64 (len (data ))
@@ -123,12 +156,11 @@ func TestCompress_AchievesCompression(t *testing.T) {
123156}
124157
125158func TestCompress_RandomDataStillWorks (t * testing.T ) {
126- // Random data won't compress well but should still round-trip correctly
127159 data := make ([]byte , 4096 )
128160 _ , err := rand .Read (data )
129161 require .NoError (t , err )
130162
131- compressed , err := Compress (data )
163+ compressed , err := Compress (data , LevelFastest )
132164 require .NoError (t , err )
133165
134166 decompressed , err := Decompress (compressed )
@@ -137,9 +169,18 @@ func TestCompress_RandomDataStillWorks(t *testing.T) {
137169}
138170
139171func TestDecompress_DataStartingWithMagicButUncompressed (t * testing.T ) {
140- // Edge case: data that happens to start with the magic bytes but isn't actually compressed.
141- // This should fail decompression (invalid zstd frame).
142172 fakeCompressed := append ([]byte {0x5A , 0x53 , 0x54 , 0x44 }, bytes .Repeat ([]byte {0x00 }, 100 )... )
143173 _ , err := Decompress (fakeCompressed )
144174 assert .Error (t , err , "data starting with magic but containing invalid zstd should error" )
145175}
176+
177+ func TestCompress_InvalidLevel (t * testing.T ) {
178+ // Out-of-range level should fall back to LevelDefault
179+ data := []byte ("test data for invalid level" )
180+ compressed , err := Compress (data , CompressionLevel (99 ))
181+ require .NoError (t , err )
182+
183+ decompressed , err := Decompress (compressed )
184+ require .NoError (t , err )
185+ assert .Equal (t , data , decompressed )
186+ }
0 commit comments