-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestHuffmanCoder.java
More file actions
70 lines (60 loc) · 1.66 KB
/
TestHuffmanCoder.java
File metadata and controls
70 lines (60 loc) · 1.66 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
public class TestHuffmanCoder {
public static void main(String[] args) {
System.out.println("\nTests for class HuffmanCoder");
System.out.println("============================");
HuffmanPair[] h = new HuffmanPair[6];
h[0] = new HuffmanPair('a',5);
h[1] = new HuffmanPair('b',7);
h[2] = new HuffmanPair('c',10);
h[3] = new HuffmanPair('d',15);
h[4] = new HuffmanPair('e',20);
h[5] = new HuffmanPair('f',45);
ArrayOrderedList<HuffmanPair> list = new ArrayOrderedList<HuffmanPair>();
for (int i = 0; i < 6; i++) {
list.add(h[i]);
}
HuffmanCoder coder = new HuffmanCoder(list);
if (coder.encode('a').equals("1010") && coder.encode('f').equals("0")) {
System.out.println("Test 1 passed\n");
}
else {
System.out.println("Test 1 failed\n");
}
if (coder.encode('e').equals("111") && coder.encode('d').equals("110")) {
System.out.println("Test 2 passed\n");
}
else {
System.out.println("Test 2 failed\n");
}
boolean test3 = false;
try {
String s = coder.encode('g');
}
catch (Exception e) {
System.out.println("Test 3 passed\n");
test3 = true;
}
if (!test3) {
System.out.println("Test 3 failed\n");
}
if (coder.decode("1010") == 'a' && coder.decode("0") == 'f') {
System.out.println("Test 4 passed\n");
}
else {
System.out.println("Test 4 failed\n");
}
if (coder.decode("111") == 'e' && coder.decode("110") == 'd') {
System.out.println("Test 5 passed\n");
}
else {
System.out.println("Test 5 failed\n");
}
char a = coder.decode("0101");
if (a == (char)0) {
System.out.println("Test 6 passed\n");
}
else {
System.out.println("Test 6 failed\n");
}
}
}