From 562a6298244056f7fecd64f8dcf4d2181fd9a4e9 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 8 Apr 2026 23:42:39 +0000 Subject: [PATCH] Add test for LZ77 backreference boundary conditions Added tests to `test_compression_lz77_edge_cases.cpp` and `test_compression_lz77.cpp` to explicitly cover the exact boundary condition where the requested backreference distance exceeds the available output history by exactly 1 byte. Co-authored-by: segin <480709+segin@users.noreply.github.com> --- run_lz77_tests.sh | 1 + tests/test_compression_lz77.cpp | 22 ++++++++++++++++++ tests/test_compression_lz77_edge_cases.cpp | 26 ++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100755 run_lz77_tests.sh diff --git a/run_lz77_tests.sh b/run_lz77_tests.sh new file mode 100755 index 00000000..d48f1533 --- /dev/null +++ b/run_lz77_tests.sh @@ -0,0 +1 @@ +make -j$(nproc) check TESTS="test_compression_lz77 test_compression_lz77_edge_cases" diff --git a/tests/test_compression_lz77.cpp b/tests/test_compression_lz77.cpp index 0d00495a..5565ed41 100644 --- a/tests/test_compression_lz77.cpp +++ b/tests/test_compression_lz77.cpp @@ -184,6 +184,28 @@ int edge_case_tests() { ASSERT_EQ(output[1], 0x42); } + + // 7. Exact Boundary Distance (Distance == Output Size + 1) + { + // Block 1: 8 Literals ('A'...'H') -> Output "ABCDEFGH" (size 8) + // Block 2: Ref (Dist 9, Len 3) + // Flags: 0 (all literals) + // Data: 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' + // Flags: 1 (Ref at bit 0) + // Data: Dist 9, Len 3. Dist 9 -> 0x009. + // b1 = (9 >> 4) = 0. + // b2 = ((9 & 0xF) << 4) | 0 = 0x90. + std::vector input = { + 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // Block 1 + 0x01, 0x00, 0x90 // Block 2 + }; + auto output = decompressor.decompress(input.data(), input.size()); + + ASSERT_EQ(output.size(), 11); + std::string s(output.begin(), output.end()); + ASSERT_EQ(s, "ABCDEFGHABC"); + } + std::cout << "[EDGE] Passed.\n"; return 0; } diff --git a/tests/test_compression_lz77_edge_cases.cpp b/tests/test_compression_lz77_edge_cases.cpp index 34cbce46..07a0388f 100644 --- a/tests/test_compression_lz77_edge_cases.cpp +++ b/tests/test_compression_lz77_edge_cases.cpp @@ -118,6 +118,32 @@ int main() { std::cout << "Passed.\n"; } + + // Test 7: Exact Boundary Backreference (Distance == Output Size + 1) + // We need some initial data to be valid and a full block to reset flags. + // Block 1: 8 Literals [0x00, 'A'...'H'] -> Output "ABCDEFGH" (size 8) + // Block 2: Flags [0x01] (1st item Ref) + // Ref bytes [0x00, 0x90] -> Dist 9, Len 3. + // + // Distance 9 == Output Size 8 + 1. + // Implementation clamps distance to output.size() = 8. + // Start = 8 - 8 = 0. + // Copies output[0]...output[2] -> "ABC". + // Result: "ABCDEFGHABC". + { + std::cout << " Test 7: Exact Boundary Backreference... "; + std::vector input = { + 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // Block 1 + 0x01, 0x00, 0x90 // Block 2 + }; + auto output = decompressor.decompress(input.data(), input.size()); + + ASSERT_EQ(output.size(), 11); + std::string s(output.begin(), output.end()); + ASSERT_EQ(s, "ABCDEFGHABC"); + std::cout << "Passed.\n"; + } + std::cout << "[EDGE] All tests passed.\n"; return 0; }