forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffmanCodingTest.java
More file actions
110 lines (86 loc) · 3.76 KB
/
HuffmanCodingTest.java
File metadata and controls
110 lines (86 loc) · 3.76 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.thealgorithms.compression;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class HuffmanCodingTest {
@Test
void testStandardLifecycle() {
String input = "efficiency is key";
HuffmanCoding huffman = new HuffmanCoding(input);
String encoded = huffman.encode(input);
assertNotNull(encoded);
assertTrue(encoded.matches("[01]+"));
assertEquals(input, huffman.decode(encoded));
}
@Test
void testNullAndEmptyHandling() {
HuffmanCoding huffman = new HuffmanCoding("");
assertEquals("", huffman.encode(""));
assertEquals("", huffman.decode(""));
HuffmanCoding huffmanNull = new HuffmanCoding(null);
assertEquals("", huffmanNull.encode(null));
assertEquals("", huffmanNull.decode(null));
}
@Test
void testSingleCharacterEdgeCase() {
String input = "aaaaa";
HuffmanCoding huffman = new HuffmanCoding(input);
String encoded = huffman.encode(input);
assertEquals("00000", encoded);
assertEquals(input, huffman.decode(encoded));
}
@Test
void testUnicodeAndSpecialCharacters() {
// Tests spacing, symbols, non-latin alphabets, and surrogate pairs (emojis)
String input = "Hello, World! 🚀\nLine 2: こんにちは";
HuffmanCoding huffman = new HuffmanCoding(input);
String encoded = huffman.encode(input);
assertEquals(input, huffman.decode(encoded));
}
@Test
void testFailFastOnUnseenCharacter() {
HuffmanCoding huffman = new HuffmanCoding("abc");
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> huffman.encode("abcd") // 'd' was not in the original tree
);
assertTrue(exception.getMessage().contains("not found in Huffman dictionary"));
}
@Test
void testFailFastOnInvalidBinaryCharacter() {
HuffmanCoding huffman = new HuffmanCoding("abc");
String encoded = huffman.encode("abc");
// Inject a '2' into the binary stream
String corruptedEncoded = encoded + "2";
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> huffman.decode(corruptedEncoded));
assertTrue(exception.getMessage().contains("contains invalid characters"));
}
@Test
void testFailFastOnIncompleteSequence() {
HuffmanCoding huffman = new HuffmanCoding("abcd");
String encoded = huffman.encode("abc");
// Truncate the last bit to simulate an incomplete byte/sequence transfer
String truncatedEncoded = encoded.substring(0, encoded.length() - 1);
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> huffman.decode(truncatedEncoded));
assertTrue(exception.getMessage().contains("incomplete sequence"));
}
@Test
void testImmutabilityOfDictionary() {
HuffmanCoding huffman = new HuffmanCoding("abc");
var codes = huffman.getHuffmanCodes();
assertThrows(UnsupportedOperationException.class, () -> codes.put('z', "0101"));
}
@Test
void testStressVolume() {
StringBuilder sb = new StringBuilder();
// Generate a 100,000 character string
for (int i = 0; i < 100000; i++) {
sb.append((char) ('a' + (i % 26)));
}
String largeInput = sb.toString();
HuffmanCoding huffman = new HuffmanCoding(largeInput);
String encoded = huffman.encode(largeInput);
assertEquals(largeInput, huffman.decode(encoded));
}
}