mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
feat: Add RLE and Shannon-Fano compression algorithms (#6779)
* feat: Add RLE and Shannon-Fano compression algorithms * Fix: Resolve CI failures for compression algorithms * chore: trigger CI rebuild
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package com.thealgorithms.compression;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class RunLengthEncodingTest {
|
||||
|
||||
@Test
|
||||
void testNullInputs() {
|
||||
// Test that a null input to compress returns an empty string
|
||||
assertEquals("", RunLengthEncoding.compress(null));
|
||||
|
||||
// Test that a null input to decompress returns an empty string
|
||||
assertEquals("", RunLengthEncoding.decompress(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCompressionSimple() {
|
||||
// Test a typical string with multiple runs
|
||||
String input = "AAAABBBCCDAA";
|
||||
String expected = "4A3B2C1D2A";
|
||||
assertEquals(expected, RunLengthEncoding.compress(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCompressionWithNoRuns() {
|
||||
// Test a string with no consecutive characters
|
||||
String input = "ABCDE";
|
||||
String expected = "1A1B1C1D1E";
|
||||
assertEquals(expected, RunLengthEncoding.compress(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCompressionEdgeCases() {
|
||||
// Test with an empty string
|
||||
assertEquals("", RunLengthEncoding.compress(""));
|
||||
|
||||
// Test with a single character
|
||||
assertEquals("1A", RunLengthEncoding.compress("A"));
|
||||
|
||||
// Test with a long run of a single character
|
||||
assertEquals("10Z", RunLengthEncoding.compress("ZZZZZZZZZZ"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDecompressionSimple() {
|
||||
// Test decompression of a typical RLE string
|
||||
String input = "4A3B2C1D2A";
|
||||
String expected = "AAAABBBCCDAA";
|
||||
assertEquals(expected, RunLengthEncoding.decompress(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDecompressionWithNoRuns() {
|
||||
// Test decompression of a string with single characters
|
||||
String input = "1A1B1C1D1E";
|
||||
String expected = "ABCDE";
|
||||
assertEquals(expected, RunLengthEncoding.decompress(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDecompressionWithMultiDigitCount() {
|
||||
// Test decompression where a run count is greater than 9
|
||||
String input = "12A1B3C";
|
||||
String expected = "AAAAAAAAAAAABCCC";
|
||||
assertEquals(expected, RunLengthEncoding.decompress(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDecompressionEdgeCases() {
|
||||
// Test with an empty string
|
||||
assertEquals("", RunLengthEncoding.decompress(""));
|
||||
|
||||
// Test with a single character run
|
||||
assertEquals("A", RunLengthEncoding.decompress("1A"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSymmetry() {
|
||||
// Test that compressing and then decompressing returns the original string
|
||||
String original1 = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB";
|
||||
String compressed = RunLengthEncoding.compress(original1);
|
||||
String decompressed = RunLengthEncoding.decompress(compressed);
|
||||
assertEquals(original1, decompressed);
|
||||
|
||||
String original2 = "A";
|
||||
assertEquals(original2, RunLengthEncoding.decompress(RunLengthEncoding.compress(original2)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.thealgorithms.compression;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ShannonFanoTest {
|
||||
|
||||
@Test
|
||||
void testNullInput() {
|
||||
// Test with a null string, should return an empty map
|
||||
assertTrue(ShannonFano.generateCodes(null).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSimpleString() {
|
||||
// A simple string to test basic code generation
|
||||
String text = "AAABBC";
|
||||
Map<Character, String> codes = ShannonFano.generateCodes(text);
|
||||
|
||||
assertEquals(3, codes.size());
|
||||
assertEquals("0", codes.get('A'));
|
||||
assertEquals("10", codes.get('B'));
|
||||
assertEquals("11", codes.get('C'));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExampleFromStringIssue() {
|
||||
// Example from the original issue proposal: A:15, B:7, C:6, D:6, E:5
|
||||
// The code finds a more optimal split: {A,B} | {C,D,E} -> |22-17|=5
|
||||
// instead of {A} | {B,C,D,E} -> |15-24|=9.
|
||||
String text = "AAAAAAAAAAAAAAABBBBBBBCCCCCCDDDDDDEEEEE";
|
||||
Map<Character, String> codes = ShannonFano.generateCodes(text);
|
||||
|
||||
assertEquals(5, codes.size());
|
||||
assertEquals("00", codes.get('A'));
|
||||
assertEquals("01", codes.get('B'));
|
||||
assertEquals("10", codes.get('C'));
|
||||
assertEquals("110", codes.get('D'));
|
||||
assertEquals("111", codes.get('E'));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEdgeCases() {
|
||||
// Test with an empty string
|
||||
assertTrue(ShannonFano.generateCodes("").isEmpty());
|
||||
|
||||
// Test with a single character
|
||||
Map<Character, String> singleCharCodes = ShannonFano.generateCodes("AAAAA");
|
||||
assertEquals(1, singleCharCodes.size());
|
||||
assertEquals("0", singleCharCodes.get('A')); // A single symbol gets code "0"
|
||||
|
||||
// Test with all unique characters
|
||||
String uniqueCharsText = "ABCDEF";
|
||||
Map<Character, String> uniqueCharCodes = ShannonFano.generateCodes(uniqueCharsText);
|
||||
assertEquals(6, uniqueCharCodes.size());
|
||||
// Check that codes are unique and have varying lengths as expected
|
||||
assertEquals(6, uniqueCharCodes.values().stream().distinct().count());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStringWithTwoChars() {
|
||||
String text = "ABABAB";
|
||||
Map<Character, String> codes = ShannonFano.generateCodes(text);
|
||||
|
||||
assertEquals(2, codes.size());
|
||||
assertTrue(codes.get('A').equals("0") && codes.get('B').equals("1") || codes.get('A').equals("1") && codes.get('B').equals("0"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user