garydgregory commented on code in PR #795:
URL: https://github.com/apache/commons-compress/pull/795#discussion_r3741653409


##########
src/test/java/org/apache/commons/compress/huffman/HuffmanDecoderTest.java:
##########
@@ -99,4 +132,81 @@ void testDecodeSymbols(final int[] codeLengths, final 
byte[] inputData, final Li
         }
         assertEquals(expectedSymbols, actualSymbols, "Decoded symbols do not 
match expected symbols");
     }
+
+    @Test
+    void testNoCodeLengths() throws Exception {
+        try {
+            new HuffmanDecoder(new int[0]);
+            fail("Expected IllegalArgumentException for empty code length 
list");

Review Comment:
   Use `assertThrows()`.



##########
src/test/java/org/apache/commons/compress/huffman/HuffmanDecoderTest.java:
##########
@@ -99,4 +132,81 @@ void testDecodeSymbols(final int[] codeLengths, final 
byte[] inputData, final Li
         }
         assertEquals(expectedSymbols, actualSymbols, "Decoded symbols do not 
match expected symbols");
     }
+
+    @Test
+    void testNoCodeLengths() throws Exception {
+        try {
+            new HuffmanDecoder(new int[0]);
+            fail("Expected IllegalArgumentException for empty code length 
list");
+        } catch (final IllegalArgumentException e) {
+            assertEquals("codeLengthSize must be > 0; was 0", e.getMessage());
+        }
+    }
+
+    @Test
+    void testSingleCodeLength() throws Exception {
+        final int[] length = { 1 };
+        // Value: 0
+        final HuffmanDecoder decoder = new HuffmanDecoder(length);
+        assertEquals(0, decodeSymbol(decoder, (byte) 0x00)); // 0xxx xxxx
+        try {
+            decodeSymbol(decoder, (byte) 0x80); // 1xxx xxxx
+            fail("Expected CompressorException for invalid bitstream");
+        } catch (final CompressorException e) {
+            assertEquals("Invalid Huffman code: 2", e.getMessage());
+        }
+    }
+
+    @Test
+    void testNoLeafNodes() throws Exception {
+        final HuffmanDecoder decoder = new HuffmanDecoder(new int[] { 0, 0, 0, 
0, 0 });
+
+        try {
+            decodeSymbol(decoder, (byte) 0, (byte) 0, (byte) 0, (byte) 0);
+            fail("Expected CompressorException when decoding symbols for tree 
with no leaf nodes");
+        } catch (final CompressorException e) {
+            assertEquals("Invalid Huffman code: 0", e.getMessage());
+        }
+    }
+
+    @Test
+    void testInvalidBitstream() throws Exception {
+        final int[] length = { 4, 2, 3, 0, 5, 0, 1 };
+        // Value: 0 1 2 3 4 5 6
+        final HuffmanDecoder decoder = new HuffmanDecoder(length);
+        assertEquals(6, decodeSymbol(decoder, (byte) 0x00)); // 0xxx xxxx
+        assertEquals(1, decodeSymbol(decoder, (byte) 0x80)); // 10xx xxxx
+        assertEquals(2, decodeSymbol(decoder, (byte) 0xc0)); // 110x xxxx
+        assertEquals(0, decodeSymbol(decoder, (byte) 0xe0)); // 1110 xxxx
+        assertEquals(4, decodeSymbol(decoder, (byte) 0xf0)); // 1111 0xxx
+        try {
+            decodeSymbol(decoder, (byte) 0xf8); // 1111 1xxx
+            fail("Expected CompressorException for invalid bitstream");
+        } catch (final CompressorException e) {
+            assertEquals("Invalid Huffman code: 62", e.getMessage());
+        }
+    }
+
+    @Test
+    void testReadEof() throws Exception {
+        final int[] length = { 4, 2, 3, 0, 5, 5, 1 };
+        // Value: 0 1 2 3 4 5 6
+        final HuffmanDecoder decoder = new HuffmanDecoder(length);
+        try (BitInputStream in = new BitInputStream(new 
ByteArrayInputStream(new byte[] { (byte) 0b11111_110 }), ByteOrder.BIG_ENDIAN)) 
{
+            assertEquals(5, decoder.decodeSymbol(in)); // 1111 1xxx
+            assertEquals(2, decoder.decodeSymbol(in)); // 110x xxxx
+            try {
+                decoder.decodeSymbol(in); // EOF
+                fail("Expected EOFException for end of stream");

Review Comment:
   Use `assertThrows()`.



##########
src/test/java/org/apache/commons/compress/huffman/HuffmanDecoderTest.java:
##########
@@ -99,4 +132,81 @@ void testDecodeSymbols(final int[] codeLengths, final 
byte[] inputData, final Li
         }
         assertEquals(expectedSymbols, actualSymbols, "Decoded symbols do not 
match expected symbols");
     }
+
+    @Test
+    void testNoCodeLengths() throws Exception {
+        try {
+            new HuffmanDecoder(new int[0]);
+            fail("Expected IllegalArgumentException for empty code length 
list");
+        } catch (final IllegalArgumentException e) {
+            assertEquals("codeLengthSize must be > 0; was 0", e.getMessage());
+        }
+    }
+
+    @Test
+    void testSingleCodeLength() throws Exception {
+        final int[] length = { 1 };
+        // Value: 0
+        final HuffmanDecoder decoder = new HuffmanDecoder(length);
+        assertEquals(0, decodeSymbol(decoder, (byte) 0x00)); // 0xxx xxxx
+        try {
+            decodeSymbol(decoder, (byte) 0x80); // 1xxx xxxx
+            fail("Expected CompressorException for invalid bitstream");

Review Comment:
   Use `assertThrows()`.



##########
src/test/java/org/apache/commons/compress/huffman/HuffmanDecoderTest.java:
##########
@@ -99,4 +132,81 @@ void testDecodeSymbols(final int[] codeLengths, final 
byte[] inputData, final Li
         }
         assertEquals(expectedSymbols, actualSymbols, "Decoded symbols do not 
match expected symbols");
     }
+
+    @Test
+    void testNoCodeLengths() throws Exception {
+        try {
+            new HuffmanDecoder(new int[0]);
+            fail("Expected IllegalArgumentException for empty code length 
list");
+        } catch (final IllegalArgumentException e) {
+            assertEquals("codeLengthSize must be > 0; was 0", e.getMessage());
+        }
+    }
+
+    @Test
+    void testSingleCodeLength() throws Exception {
+        final int[] length = { 1 };
+        // Value: 0
+        final HuffmanDecoder decoder = new HuffmanDecoder(length);
+        assertEquals(0, decodeSymbol(decoder, (byte) 0x00)); // 0xxx xxxx
+        try {
+            decodeSymbol(decoder, (byte) 0x80); // 1xxx xxxx
+            fail("Expected CompressorException for invalid bitstream");
+        } catch (final CompressorException e) {
+            assertEquals("Invalid Huffman code: 2", e.getMessage());
+        }
+    }
+
+    @Test
+    void testNoLeafNodes() throws Exception {
+        final HuffmanDecoder decoder = new HuffmanDecoder(new int[] { 0, 0, 0, 
0, 0 });
+
+        try {
+            decodeSymbol(decoder, (byte) 0, (byte) 0, (byte) 0, (byte) 0);
+            fail("Expected CompressorException when decoding symbols for tree 
with no leaf nodes");
+        } catch (final CompressorException e) {
+            assertEquals("Invalid Huffman code: 0", e.getMessage());
+        }
+    }
+
+    @Test
+    void testInvalidBitstream() throws Exception {
+        final int[] length = { 4, 2, 3, 0, 5, 0, 1 };
+        // Value: 0 1 2 3 4 5 6
+        final HuffmanDecoder decoder = new HuffmanDecoder(length);
+        assertEquals(6, decodeSymbol(decoder, (byte) 0x00)); // 0xxx xxxx
+        assertEquals(1, decodeSymbol(decoder, (byte) 0x80)); // 10xx xxxx
+        assertEquals(2, decodeSymbol(decoder, (byte) 0xc0)); // 110x xxxx
+        assertEquals(0, decodeSymbol(decoder, (byte) 0xe0)); // 1110 xxxx
+        assertEquals(4, decodeSymbol(decoder, (byte) 0xf0)); // 1111 0xxx
+        try {
+            decodeSymbol(decoder, (byte) 0xf8); // 1111 1xxx
+            fail("Expected CompressorException for invalid bitstream");

Review Comment:
   Use `assertThrows()`.



##########
src/test/java/org/apache/commons/compress/huffman/HuffmanDecoderTest.java:
##########
@@ -99,4 +132,81 @@ void testDecodeSymbols(final int[] codeLengths, final 
byte[] inputData, final Li
         }
         assertEquals(expectedSymbols, actualSymbols, "Decoded symbols do not 
match expected symbols");
     }
+
+    @Test
+    void testNoCodeLengths() throws Exception {
+        try {
+            new HuffmanDecoder(new int[0]);
+            fail("Expected IllegalArgumentException for empty code length 
list");
+        } catch (final IllegalArgumentException e) {
+            assertEquals("codeLengthSize must be > 0; was 0", e.getMessage());
+        }
+    }
+
+    @Test
+    void testSingleCodeLength() throws Exception {
+        final int[] length = { 1 };
+        // Value: 0
+        final HuffmanDecoder decoder = new HuffmanDecoder(length);
+        assertEquals(0, decodeSymbol(decoder, (byte) 0x00)); // 0xxx xxxx
+        try {
+            decodeSymbol(decoder, (byte) 0x80); // 1xxx xxxx
+            fail("Expected CompressorException for invalid bitstream");
+        } catch (final CompressorException e) {
+            assertEquals("Invalid Huffman code: 2", e.getMessage());
+        }
+    }
+
+    @Test
+    void testNoLeafNodes() throws Exception {
+        final HuffmanDecoder decoder = new HuffmanDecoder(new int[] { 0, 0, 0, 
0, 0 });
+
+        try {
+            decodeSymbol(decoder, (byte) 0, (byte) 0, (byte) 0, (byte) 0);
+            fail("Expected CompressorException when decoding symbols for tree 
with no leaf nodes");

Review Comment:
   Use `assertThrows()`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to