Skip to content

Commit ac069a9

Browse files
committed
Fix: Resolve CI failures for compression algorithms
1 parent 0190f6e commit ac069a9

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

src/main/java/com/thealgorithms/compression/RunLengthEncoding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static String decompress(String compressedText) {
7575
for (char ch : compressedText.toCharArray()) {
7676
if (Character.isDigit(ch)) {
7777
// Build the number for runs of 10 or more (e.g., "12A")
78-
count = count * 10 + (ch - '0');
78+
count = count * 10 + ch - '0';
7979
} else {
8080
// Append the character 'count' times
8181
decompressed.append(String.valueOf(ch).repeat(Math.max(0, count)));

src/main/java/com/thealgorithms/compression/ShannonFano.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,10 @@ public static Map<Character, String> generateCodes(String text) {
107107
* @param prefix The current prefix code being built for the symbols in this partition.
108108
*/
109109
private static void buildCodeTree(List<Symbol> symbols, int start, int end, String prefix) {
110-
if (start > end) {
111-
return;
112-
}
113-
110+
// The initial check in generateCodes ensures start <= end is always true here.
111+
// The base case is when a partition has only one symbol.
114112
if (start == end) {
115-
// Base case: single symbol gets the current prefix as its code.
116-
symbols.get(start).code = prefix.isEmpty() ? "0" : prefix;
113+
symbols.get(start).code = prefix;
117114
return;
118115
}
119116

src/test/java/com/thealgorithms/compression/RunLengthEncodingTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66

77
class RunLengthEncodingTest {
88

9+
@Test
10+
void testNullInputs() {
11+
// Test that a null input to compress returns an empty string
12+
assertEquals("", RunLengthEncoding.compress(null));
13+
14+
// Test that a null input to decompress returns an empty string
15+
assertEquals("", RunLengthEncoding.decompress(null));
16+
}
17+
918
@Test
1019
void testCompressionSimple() {
1120
// Test a typical string with multiple runs

src/test/java/com/thealgorithms/compression/ShannonFanoTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
import static org.junit.jupiter.api.Assertions.assertTrue;
55

66
import java.util.Map;
7-
87
import org.junit.jupiter.api.Test;
98

109
class ShannonFanoTest {
1110

11+
@Test
12+
void testNullInput() {
13+
// Test with a null string, should return an empty map
14+
assertTrue(ShannonFano.generateCodes(null).isEmpty());
15+
}
16+
1217
@Test
1318
void testSimpleString() {
1419
// A simple string to test basic code generation
@@ -21,7 +26,6 @@ void testSimpleString() {
2126
assertEquals("11", codes.get('C'));
2227
}
2328

24-
2529
@Test
2630
void testExampleFromStringIssue() {
2731
// Example from the original issue proposal: A:15, B:7, C:6, D:6, E:5

0 commit comments

Comments
 (0)