-
Notifications
You must be signed in to change notification settings - Fork 16
/
FileFreqWordsIteratorTest.java
150 lines (112 loc) · 3.62 KB
/
FileFreqWordsIteratorTest.java
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.io.File;
import java.io.IOException;
import junit.framework.TestCase;
public class FileFreqWordsIteratorTest extends TestCase {
public void writeToFile(String input, String destination) {
for (int i = 0; i < input.length(); i++) {
String bin = HuffmanEncoding.convertTo8bits(Integer.toBinaryString(input.charAt(i)));
FileOutputHelper.writeBinStrToFile(bin, destination);
}
}
public boolean checkOutput(String[] output, FileFreqWordsIterator it) {
for (int i = 0; i < output.length; i++) {
if (!it.hasNext())
return false;
String check = "";
String character = it.next();
for (int x = 0; x < output[i].length(); x++) {
check += HuffmanEncoding.convertTo8bits(Integer.toBinaryString(output[i].charAt(x)));
}
System.out.println("expect => [" + check + "] = " + character);
if (!check.equals(character))
return false;
}
return true;
}
public void testOneFreq() {
File f = new File("regularFile.txt");
String[] output = {"kitten", " ", "kitten", " ", "kitten", " ", "k", "i", "m", " ", "kitten"};
try {
if (f.exists())
f.delete();
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
writeToFile("kitten kitten kitten kim kitten", f.getAbsolutePath());
assertEquals(true, checkOutput(output, new FileFreqWordsIterator(f.getAbsolutePath(), 1)));
f.delete();
}
public void testThreeFreq() {
File f = new File("threeFreq.txt");
String[] output = {"aaa", " ", "aaa", " ", "bbb", " ", "bbb", " ", "c", "c", "c", " ", "dddd", " ", "dddd"};
try {
if (f.exists())
f.delete();
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
writeToFile("aaa aaa bbb bbb ccc dddd dddd", f.getAbsolutePath());
assertEquals(true, checkOutput(output, new FileFreqWordsIterator(f.getAbsolutePath(), 3)));
f.delete();
}
public void testZeroFreq() {
File f = new File("zeroFreq.txt");
String[] output = {"B", "o", "n", "j", "o", "u", "r", " ", "l", "e", " ", "c", "h", "a", "t"};
try {
if (f.exists())
f.delete();
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
writeToFile("Bonjour le chat", f.getAbsolutePath());
assertEquals(true, checkOutput(output, new FileFreqWordsIterator(f.getAbsolutePath(), 0)));
f.delete();
}
public void testNegativeFreq() {
assertEquals(true, true);
}
public void testBigFreq() {
File f = new File("bigFreq.txt");
String[] output = {"aaa", " ", "aaa", " ", "bbb", " ", "bbb", " ", "ccc", " ", "dddd", " ", "dddd"};
try {
if (f.exists())
f.delete();
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
writeToFile("aaa aaa bbb bbb ccc dddd dddd", f.getAbsolutePath());
assertEquals(true, checkOutput(output, new FileFreqWordsIterator(f.getAbsolutePath(), 4000)));
f.delete();
}
public void testEmptyFile() {
File f = new File("emptyFile.txt");
try {
if (f.exists())
f.delete();
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
FileFreqWordsIterator it = new FileFreqWordsIterator(f.getAbsolutePath(), 2);
assertEquals(false, it.hasNext());
f.delete();
}
public void testWithNoWords() {
File f = new File("bigFreq.txt");
String[] output = {"a", " ", "b", " ", "c", " ", "d", " ", "e", " ", "f", " ", "g"};
try {
if (f.exists())
f.delete();
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
writeToFile("a b c d e f g", f.getAbsolutePath());
assertEquals(true, checkOutput(output, new FileFreqWordsIterator(f.getAbsolutePath(), 5)));
f.delete();
}
}