lsb-msb-mismatch-corrupting-all-output
Symptom
Decompression produced garbled output on every test file. Round-trip failed
with no error — wrong bytes, no panic.
Root cause
Three directions must be consistent: appendBit builds codes in
one bit order; WriteBits must emit in append order (root decision
first); WriteBit must fill bytes MSB-first so the reader
reconstructs correctly. Any one direction wrong produces corrupted but
structurally valid-looking output.
Fix
Wrote TestBitOrderingExplicit — encodes a known bit pattern,
asserts the exact expected byte value. Pinning the expected byte forced each
direction to be correct rather than just internally consistent with each other.
non-deterministic-tree-corrupting-round-trip-on-tied-frequencies
Symptom
Round-trip passed on most inputs but failed intermittently on inputs with
tied character frequencies. No error — just wrong bytes.
Root cause
Huffman trees are not unique when frequencies tie. Without a consistent
tiebreaker, compression and decompression could build different trees from
the same frequency table and silently assign different codes to each character.
Fix
Sort characters by byte value before enqueuing — applied identically
in BuildHuffmanTree and WriteHeader (which also
sorts before writing frequency entries). Same sort, same input, same tree,
every run.
single-char-file-zero-length-code-writes-nothing
Symptom
Compressing a single-character file produced a non-zero output but
decompression returned empty. The code table was empty.
Root cause
One unique character → one leaf node → root with no branches.
GenerateCodes DFS hit a leaf at the root, appended no bits,
stored a zero-length code. WriteBits with
length = 0 wrote nothing.
Fix
Detect len(freqTable) == 1 in BuildHuffmanTree,
create a dummy leaf, pair it with the real leaf under a synthetic root.
GenerateCodes detects IsDummy() and assigns
{bits: 1, length: 1} to the real character.