forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRunLengthEncodingTest.java
More file actions
90 lines (73 loc) · 2.89 KB
/
RunLengthEncodingTest.java
File metadata and controls
90 lines (73 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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)));
}
}