| 1 |
package huff0 |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"math/rand" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func TestBuildCTableSimple(t *testing.T) { |
| 11 |
var count [256]uint32 |
| 12 |
count['a'] = 60 |
| 13 |
count['b'] = 30 |
| 14 |
count['c'] = 10 |
| 15 |
var s Scratch |
| 16 |
if err := s.BuildCTable(&count); err != nil { |
| 17 |
t.Fatalf("BuildCTable: %v", err) |
| 18 |
} |
| 19 |
if len(s.prevTable) < 4 { |
| 20 |
t.Fatalf("prevTable too short: %d", len(s.prevTable)) |
| 21 |
} |
| 22 |
if s.prevTable['a'].nBits == 0 || s.prevTable['b'].nBits == 0 || s.prevTable['c'].nBits == 0 { |
| 23 |
t.Fatalf("expected non-zero nBits for active symbols") |
| 24 |
} |
| 25 |
// Symbols below the active set are zero in the trimmed prevTable. |
| 26 |
if s.prevTable[0].nBits != 0 { |
| 27 |
t.Fatalf("expected zero nBits for unused symbol 0") |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func TestBuildCTableEmpty(t *testing.T) { |
| 32 |
var count [256]uint32 |
| 33 |
var s Scratch |
| 34 |
if err := s.BuildCTable(&count); err == nil { |
| 35 |
t.Fatalf("expected error for empty histogram") |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
// TestBuildCTableOversizedTotal exercises histograms that sum to more than |
| 40 |
// huff0's internal BlockSizeMax — the case that arises when summing the |
| 41 |
// per-block histograms of a multi-block bitmap. Without scaling, huffSort |
| 42 |
// places nodes at out-of-range rank indices and buildCTable panics on a |
| 43 |
// negative index. |
| 44 |
func TestBuildCTableOversizedTotal(t *testing.T) { |
| 45 |
var count [256]uint32 |
| 46 |
// 4 MB of one dominant symbol, plus a handful of rare singletons. |
| 47 |
count[0] = 4 << 20 |
| 48 |
count[1] = 2 |
| 49 |
count[2] = 1 |
| 50 |
count[128] = 1 |
| 51 |
count[255] = 3 |
| 52 |
var s Scratch |
| 53 |
if err := s.BuildCTable(&count); err != nil { |
| 54 |
t.Fatalf("BuildCTable: %v", err) |
| 55 |
} |
| 56 |
if len(s.prevTable) == 0 { |
| 57 |
t.Fatalf("prevTable not installed") |
| 58 |
} |
| 59 |
// Every originally non-zero symbol must be representable. |
| 60 |
for i, v := range count { |
| 61 |
if v == 0 { |
| 62 |
continue |
| 63 |
} |
| 64 |
if i >= len(s.prevTable) || s.prevTable[i].nBits == 0 { |
| 65 |
t.Fatalf("symbol %d lost during scaling", i) |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func TestBuildCTableNilCount(t *testing.T) { |
| 71 |
var s Scratch |
| 72 |
if err := s.BuildCTable(nil); err == nil { |
| 73 |
t.Fatalf("expected error for nil count") |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func TestEstimateSizeAndCanUseTableNilHist(t *testing.T) { |
| 78 |
var count [256]uint32 |
| 79 |
count['a'] = 10 |
| 80 |
count['b'] = 5 |
| 81 |
var s Scratch |
| 82 |
if err := s.BuildCTable(&count); err != nil { |
| 83 |
t.Fatalf("BuildCTable: %v", err) |
| 84 |
} |
| 85 |
if got := s.EstimateSize(nil); got != -1 { |
| 86 |
t.Fatalf("EstimateSize(nil) = %d, want -1", got) |
| 87 |
} |
| 88 |
if s.CanUseTable(nil) { |
| 89 |
t.Fatalf("CanUseTable(nil) = true, want false") |
| 90 |
} |
| 91 |
// Also exercise the nil-receiver and unloaded-table paths so the guards |
| 92 |
// behave even before BuildCTable has installed prevTable. |
| 93 |
var empty Scratch |
| 94 |
if got := empty.EstimateSize(&count); got != -1 { |
| 95 |
t.Fatalf("EstimateSize on empty Scratch = %d, want -1", got) |
| 96 |
} |
| 97 |
if empty.CanUseTable(&count) { |
| 98 |
t.Fatalf("CanUseTable on empty Scratch = true, want false") |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func TestBuildCTableSingleSymbol(t *testing.T) { |
| 103 |
var count [256]uint32 |
| 104 |
count[42] = 100 |
| 105 |
var s Scratch |
| 106 |
if err := s.BuildCTable(&count); !errors.Is(err, ErrUseRLE) { |
| 107 |
t.Fatalf("expected ErrUseRLE, got %v", err) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func TestBuildCTableEstimateAndCanUse(t *testing.T) { |
| 112 |
var count [256]uint32 |
| 113 |
for i := range 32 { |
| 114 |
count[i] = uint32(32 - i) // skewed |
| 115 |
} |
| 116 |
var s Scratch |
| 117 |
if err := s.BuildCTable(&count); err != nil { |
| 118 |
t.Fatalf("BuildCTable: %v", err) |
| 119 |
} |
| 120 |
if !s.CanUseTable(&count) { |
| 121 |
t.Fatalf("CanUseTable: expected true for source histogram") |
| 122 |
} |
| 123 |
size := s.EstimateSize(&count) |
| 124 |
if size <= 0 { |
| 125 |
t.Fatalf("EstimateSize: expected positive, got %d", size) |
| 126 |
} |
| 127 |
var foreign [256]uint32 |
| 128 |
foreign[200] = 10 // not in original |
| 129 |
if s.CanUseTable(&foreign) { |
| 130 |
t.Fatalf("CanUseTable: expected false for foreign histogram") |
| 131 |
} |
| 132 |
if s.EstimateSize(&foreign) >= 0 { |
| 133 |
t.Fatalf("EstimateSize: expected -1 for foreign histogram") |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
// TestBuildCTableRoundtrip builds a table from a histogram, then encodes data |
| 138 |
// with that prebuilt table (ReusePolicyMust) and verifies the decoder |
| 139 |
// reconstructs the input via the standard table-in-stream format. |
| 140 |
func TestBuildCTableRoundtrip(t *testing.T) { |
| 141 |
rng := rand.New(rand.NewSource(42)) |
| 142 |
// Generate skewed input bytes (so compression yields meaningful tables). |
| 143 |
input := make([]byte, 16<<10) |
| 144 |
for i := range input { |
| 145 |
// 80% zero, 20% in [1,9] |
| 146 |
if rng.Intn(5) == 0 { |
| 147 |
input[i] = byte(1 + rng.Intn(9)) |
| 148 |
} |
| 149 |
} |
| 150 |
var count [256]uint32 |
| 151 |
for _, b := range input { |
| 152 |
count[b]++ |
| 153 |
} |
| 154 |
|
| 155 |
var src Scratch |
| 156 |
if err := src.BuildCTable(&count); err != nil { |
| 157 |
t.Fatalf("BuildCTable: %v", err) |
| 158 |
} |
| 159 |
|
| 160 |
// Hand-off the prebuilt table to the encoder via prevTable + ReusePolicyMust. |
| 161 |
var enc Scratch |
| 162 |
enc.TransferCTable(&src) |
| 163 |
enc.Reuse = ReusePolicyMust |
| 164 |
|
| 165 |
out, reused, err := Compress4X(input, &enc) |
| 166 |
if err != nil { |
| 167 |
t.Fatalf("Compress4X: %v", err) |
| 168 |
} |
| 169 |
if !reused { |
| 170 |
t.Fatalf("expected table reuse") |
| 171 |
} |
| 172 |
if len(enc.OutTable) != 0 { |
| 173 |
t.Fatalf("expected no table emitted on reuse, got %d bytes", len(enc.OutTable)) |
| 174 |
} |
| 175 |
|
| 176 |
// To verify decompression we have to ship the table to the decoder. |
| 177 |
// Encode once more without ReusePolicy to obtain the serialized table, |
| 178 |
// then concatenate that header with the reuse-only data above to mimic |
| 179 |
// a stream that starts with the table. |
| 180 |
var noReuse Scratch |
| 181 |
noReuse.Reuse = ReusePolicyNone |
| 182 |
if _, _, err := Compress4X(input, &noReuse); err != nil { |
| 183 |
t.Fatalf("Compress4X (no reuse): %v", err) |
| 184 |
} |
| 185 |
if len(noReuse.OutTable) == 0 { |
| 186 |
t.Fatalf("expected non-empty OutTable on non-reuse") |
| 187 |
} |
| 188 |
|
| 189 |
// Read a decoder-side scratch from the table header. |
| 190 |
var dec Scratch |
| 191 |
dec.MaxDecodedSize = len(input) |
| 192 |
dec2, rem, err := ReadTable(noReuse.OutTable, &dec) |
| 193 |
if err != nil { |
| 194 |
t.Fatalf("ReadTable: %v", err) |
| 195 |
} |
| 196 |
if len(rem) != 0 { |
| 197 |
t.Fatalf("expected no remainder, got %d", len(rem)) |
| 198 |
} |
| 199 |
|
| 200 |
got, err := dec2.Decompress4X(out, len(input)) |
| 201 |
if err != nil { |
| 202 |
t.Fatalf("Decompress4X: %v", err) |
| 203 |
} |
| 204 |
if !bytes.Equal(got, input) { |
| 205 |
t.Fatalf("roundtrip mismatch: got len=%d want len=%d", len(got), len(input)) |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
// TestAppendTableRoundtrip verifies AppendTable produces a header that |
| 210 |
// ReadTable can parse, restoring an equivalent table. |
| 211 |
func TestAppendTableRoundtrip(t *testing.T) { |
| 212 |
var count [256]uint32 |
| 213 |
rng := rand.New(rand.NewSource(99)) |
| 214 |
for i := range 64 { |
| 215 |
count[i] = uint32(1 + rng.Intn(1000)) |
| 216 |
} |
| 217 |
var src Scratch |
| 218 |
if err := src.BuildCTable(&count); err != nil { |
| 219 |
t.Fatalf("BuildCTable: %v", err) |
| 220 |
} |
| 221 |
hdr, err := src.AppendTable(nil) |
| 222 |
if err != nil { |
| 223 |
t.Fatalf("AppendTable: %v", err) |
| 224 |
} |
| 225 |
if len(hdr) == 0 { |
| 226 |
t.Fatalf("AppendTable produced empty header") |
| 227 |
} |
| 228 |
var dst Scratch |
| 229 |
_, rem, err := ReadTable(hdr, &dst) |
| 230 |
if err != nil { |
| 231 |
t.Fatalf("ReadTable: %v", err) |
| 232 |
} |
| 233 |
if len(rem) != 0 { |
| 234 |
t.Fatalf("expected no remainder, got %d bytes", len(rem)) |
| 235 |
} |
| 236 |
if dst.prevTableLog != src.prevTableLog { |
| 237 |
t.Fatalf("prevTableLog mismatch: src=%d dst=%d", src.prevTableLog, dst.prevTableLog) |
| 238 |
} |
| 239 |
for i := range count { |
| 240 |
if count[i] == 0 { |
| 241 |
continue |
| 242 |
} |
| 243 |
if i >= len(dst.prevTable) || dst.prevTable[i].nBits == 0 { |
| 244 |
t.Fatalf("missing symbol %d in roundtripped table", i) |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// TestBuildCTableMatchesCompress verifies that BuildCTable followed by reuse |
| 250 |
// produces output identical to the table embedded by a regular Compress4X |
| 251 |
// call on the same input (modulo the table header). |
| 252 |
func TestBuildCTableMatchesCompress(t *testing.T) { |
| 253 |
rng := rand.New(rand.NewSource(7)) |
| 254 |
input := make([]byte, 8<<10) |
| 255 |
for i := range input { |
| 256 |
input[i] = byte(rng.Intn(16)) |
| 257 |
} |
| 258 |
var ref Scratch |
| 259 |
ref.Reuse = ReusePolicyNone |
| 260 |
refOut, refReused, err := Compress4X(input, &ref) |
| 261 |
if err != nil { |
| 262 |
t.Fatalf("ref Compress4X: %v", err) |
| 263 |
} |
| 264 |
if refReused { |
| 265 |
t.Fatalf("ref should not reuse") |
| 266 |
} |
| 267 |
refData := refOut[len(ref.OutTable):] |
| 268 |
|
| 269 |
var count [256]uint32 |
| 270 |
for _, b := range input { |
| 271 |
count[b]++ |
| 272 |
} |
| 273 |
var built Scratch |
| 274 |
if err := built.BuildCTable(&count); err != nil { |
| 275 |
t.Fatalf("BuildCTable: %v", err) |
| 276 |
} |
| 277 |
var enc Scratch |
| 278 |
enc.TransferCTable(&built) |
| 279 |
enc.Reuse = ReusePolicyMust |
| 280 |
out, reused, err := Compress4X(input, &enc) |
| 281 |
if err != nil { |
| 282 |
t.Fatalf("Compress4X: %v", err) |
| 283 |
} |
| 284 |
if !reused { |
| 285 |
t.Fatalf("expected reuse") |
| 286 |
} |
| 287 |
if !bytes.Equal(out, refData) { |
| 288 |
t.Fatalf("compressed payload mismatch: built=%d ref=%d", len(out), len(refData)) |
| 289 |
} |
| 290 |
} |