Skip to content

Fix Huffman edge cases and add CMake tests#4

Open
cynricfu wants to merge 1 commit into
masterfrom
cursor/huffman-fixes-and-tests-a84c
Open

Fix Huffman edge cases and add CMake tests#4
cynricfu wants to merge 1 commit into
masterfrom
cursor/huffman-fixes-and-tests-a84c

Conversation

@cynricfu

Copy link
Copy Markdown
Owner

Summary

This PR hardens the Huffman compressor/decompressor based on the earlier code review, and adds a reproducible build plus regression tests.

Fixes (priority order)

  1. Huffman tree construction — Set root when only one symbol exists; reject empty input files with a clear error.
  2. Single-symbol encoding — Use codeword "0" so encode/decode round-trips work for one-character files.
  3. Safe byte I/O — Read input with unsigned char, reject bytes >= 128 (7-bit ASCII scope), and use binary mode consistently.
  4. Modern C++ compatibilitybinary_to_decimal(const std::string&), #include <cstdlib>, nullptr, and std:: in headers (no using namespace std in .h).
  5. Decode robustness — Null-child checks on invalid bitstreams; fixed off-by-one bug when trimming padding on the last data byte.
  6. Memory management — Destructor frees the Huffman tree and unused node_array slots.

Build & test

cmake -S . -B build -DCMAKE_CXX_COMPILER=g++
cmake --build build
ctest --test-dir build --output-on-failure

tests/run_tests.sh covers sample text round-trip, single/two-character files, DEL (0x7F), empty-file rejection, and non-ASCII rejection.

Notes

  • Compressed files produced by the updated encoder remain compatible with the original on-disk layout for normal multi-symbol inputs; single-symbol files now use codeword "0" (previously broken).
  • Input is explicitly limited to bytes 0–127 to match the original 128-entry symbol table.
Open in Web Open in Cursor 

- Handle empty and single-symbol inputs; assign code "0" for one symbol
- Use unsigned byte reads, validate non-ASCII input, and fix last-byte padding trim
- Add destructor, I/O error handling, and safer decode with null checks
- Add CMakeLists.txt, CTest roundtrip script, and README build instructions

Co-authored-by: Xinyu Fu <fu.xinyu@outlook.com>
@cynricfu

Copy link
Copy Markdown
Owner Author

This is just for trying out the cursor cloud agent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants