Skip to content

Commit 9f0df7e

Browse files
committed
Test: Increase coverage for huffman_encoding.rs decode method
Adds two new test cases to ensure 100% patch coverage for HuffmanEncoding::decode: 1. test_decode_empty_encoding_struct: Covers the edge case where num_bits == 0. 2. minimal_decode_end_check: Ensures the final 'if self.num_bits > 0' check in the multi-symbol decode path is fully covered.
1 parent 44d02d7 commit 9f0df7e

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/general/huffman_encoding.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,39 @@ mod tests {
267267
assert_eq!(decoded, bytes);
268268
}
269269

270+
#[test]
271+
fn test_decode_empty_encoding_struct() {
272+
// Create a minimal but VALID HuffmanDictionary.
273+
// This is required because decode() expects a dictionary, even though
274+
// the content of the dictionary doesn't matter when num_bits == 0.
275+
let freq = vec![('a' as u8, 1)];
276+
let dict = HuffmanDictionary::new(&freq).unwrap();
277+
278+
// Manually create the target state: an encoding with 0 bits.
279+
let empty_encoding = HuffmanEncoding {
280+
data: vec![],
281+
num_bits: 0,
282+
};
283+
284+
let result = empty_encoding.decode(&dict);
285+
286+
assert_eq!(result, Some(vec![]));
287+
}
288+
289+
#[test]
290+
fn minimal_decode_end_check() {
291+
let freq = vec![('a' as u8, 1), ('b' as u8, 1)];
292+
let bytes = b"ab";
293+
294+
let dict = HuffmanDictionary::new(&freq).unwrap();
295+
let encoded = dict.encode(bytes);
296+
297+
// This decode will go through the main loop and hit the final 'if self.num_bits > 0' check.
298+
let decoded = encoded.decode(&dict).unwrap();
299+
300+
assert_eq!(decoded, bytes);
301+
}
302+
270303
#[test]
271304
fn small_text() {
272305
let text = "Hello world";

0 commit comments

Comments
 (0)