-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.java
More file actions
407 lines (339 loc) · 13.4 KB
/
test.java
File metadata and controls
407 lines (339 loc) · 13.4 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import DictionaryTypes.*;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertFalse;
import java.net.URL;
import java.util.ArrayList;
import java.io.*;
import java.nio.channels.*;
import org.junit.Test;
public class test {
static final int UNIQUE_WORDS = 58108;
static final int ARR_MAX_WORDS = 30000;
static final int LINES = 60000;
static DictionaryInterface dict;
static final String TEST_FILE_URL = "https://gist.githubusercontent.com/NotNoshy/1a464ce1d54a5fffb2a322321b26747b/raw/ac648c9123ed87768cd5d268cde9d45d6a2a8523/corncobInput.txt";
static final String TEST_FILE = "corncobInput.txt";
static final String EMPTY_TEST_FILE = "empty.txt";
// Write code to return the array size of the ResizingArrayDictionary - you
// could do this my making another method in your ResizingArrayDictionary or by
// making the array public and reading its value here.
private static int getResizingArrLen(ResizingArrayDictionary a) {
// replace this with your own code
return a.arrSize();
}
// #region Run this to download the test files & create an empty file
public static void main(String[] args) {
if (!(new File(TEST_FILE).exists())) {
try {
System.out.println("File downloading...");
ReadableByteChannel readableByteChannel = Channels.newChannel(new URL(TEST_FILE_URL).openStream());
FileOutputStream fileOutputStream = new FileOutputStream(TEST_FILE);
fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
System.out.println("File downloaded");
} catch (Exception e) {
System.err.println("File could not be downloaded");
}
}
try {
new File(EMPTY_TEST_FILE).createNewFile();
System.out.println("Empty file created");
} catch (Exception e) {
System.err.println("Empty file could not be created");
}
}
// #endregion
// #region Helper methods
private static boolean hasAllWords(String[] words, ArrayList<String> dictionary) {
boolean has = true;
for (String word : words) {
has &= dictionary.contains(word);
}
return has;
}
// #endregion
// #region Test 1: test create dictionary and check all words added
@Test
public void trieTest1() {
dict = new TrieDictionary();
dict.CreateDictionary(TEST_FILE);
ArrayList<String> list = dict.getDictionaryWords();
String[] words = { "abundantly", "crowbars", "danceable", "infinitives" };
assertEquals(UNIQUE_WORDS, dict.getNumberOfElements());
assertEquals(UNIQUE_WORDS, list.size());
assertTrue(hasAllWords(words, list));
}
@Test
public void arrTest1() {
// Tests if words from first half are added
dict = new ArrayDictionary();
dict.CreateDictionary(TEST_FILE);
ArrayList<String> list = dict.getDictionaryWords();
String[] words = { "footballer", "deference", "commercialise", "surrogate" };
assertEquals(ARR_MAX_WORDS, dict.getNumberOfElements());
assertEquals(ARR_MAX_WORDS, list.size());
assertTrue(hasAllWords(words, list));
}
@Test
public void reArrTest1() {
dict = new ResizingArrayDictionary();
dict.CreateDictionary(TEST_FILE);
ArrayList<String> list = dict.getDictionaryWords();
String[] words = { "abundantly", "crowbars", "danceable", "infinitives" };
assertEquals(UNIQUE_WORDS, dict.getNumberOfElements());
assertEquals(UNIQUE_WORDS, list.size());
assertTrue(hasAllWords(words, list));
assertEquals(UNIQUE_WORDS + 5, getResizingArrLen((ResizingArrayDictionary) dict));
}
// #endregion
// #region Test 2A: test empty input file
@Test
public void trieTest2A() {
dict = new TrieDictionary();
dict.CreateDictionary(EMPTY_TEST_FILE);
ArrayList<String> list = dict.getDictionaryWords();
assertEquals(0, dict.getNumberOfElements());
assertEquals(0, list.size());
}
@Test
public void arrTest2A() {
dict = new ArrayDictionary();
dict.CreateDictionary(EMPTY_TEST_FILE);
ArrayList<String> list = dict.getDictionaryWords();
assertEquals(0, dict.getNumberOfElements());
assertEquals(0, list.size());
}
@Test
public void reArrTest2A() {
dict = new ResizingArrayDictionary();
dict.CreateDictionary(EMPTY_TEST_FILE);
ArrayList<String> list = dict.getDictionaryWords();
assertEquals(0, dict.getNumberOfElements());
assertEquals(0, list.size());
;
assertEquals(5, getResizingArrLen((ResizingArrayDictionary) dict));
}
// #endregion
// #region Test 2B: test empty input file and add words
@Test
public void trieTest2B() {
dict = new TrieDictionary();
dict.CreateDictionary(EMPTY_TEST_FILE);
String[] words = { "shrimp", "shrimps", "chimp", "crimp", "ship", "shriek", "shrift", "shrill", "shrine",
"shrink" };
for (String word : words) {
dict.addNewWord(word);
}
ArrayList<String> list = dict.getDictionaryWords();
assertEquals(words.length, dict.getNumberOfElements());
assertEquals(words.length, list.size());
assertTrue(hasAllWords(words, list));
}
@Test
public void arrTest2B() {
dict = new ArrayDictionary();
dict.CreateDictionary(EMPTY_TEST_FILE);
String[] words = { "shrimp", "shrimps", "chimp", "crimp", "ship", "shriek", "shrift", "shrill", "shrine",
"shrink" };
for (String word : words) {
dict.addNewWord(word);
}
ArrayList<String> list = dict.getDictionaryWords();
assertEquals(words.length, dict.getNumberOfElements());
assertEquals(words.length, list.size());
assertTrue(hasAllWords(words, list));
}
@Test
public void reArrTest2B() {
dict = new ResizingArrayDictionary();
dict.CreateDictionary(EMPTY_TEST_FILE);
String[] words = { "shrimp", "shrimps", "chimp", "crimp", "ship", "shriek", "shrift", "shrill", "shrine",
"shrink" };
for (String word : words) {
dict.addNewWord(word);
}
ArrayList<String> list = dict.getDictionaryWords();
assertEquals(words.length, dict.getNumberOfElements());
assertEquals(words.length, list.size());
assertTrue(hasAllWords(words, list));
assertEquals(10, getResizingArrLen((ResizingArrayDictionary) dict));
}
// #endregion
// #region Test 2C: test no input file and add words
@Test
public void trieTest2C() {
dict = new TrieDictionary();
String[] words = { "shrimp", "shrimps", "chimp", "crimp", "ship", "shriek", "shrift", "shrill", "shrine",
"shrink" };
for (String word : words) {
dict.addNewWord(word);
}
ArrayList<String> list = dict.getDictionaryWords();
assertEquals(words.length, dict.getNumberOfElements());
assertEquals(words.length, list.size());
assertTrue(hasAllWords(words, list));
}
@Test
public void arrTest2C() {
dict = new ArrayDictionary();
String[] words = { "shrimp", "shrimps", "chimp", "crimp", "ship", "shriek", "shrift", "shrill", "shrine",
"shrink" };
for (String word : words) {
dict.addNewWord(word);
}
ArrayList<String> list = dict.getDictionaryWords();
assertEquals(words.length, dict.getNumberOfElements());
assertEquals(words.length, list.size());
assertTrue(hasAllWords(words, list));
}
@Test
public void reArrTest2C() {
dict = new ResizingArrayDictionary();
String[] words = { "shrimp", "shrimps", "chimp", "crimp", "ship", "shriek", "shrift", "shrill", "shrine",
"shrink" };
for (String word : words) {
dict.addNewWord(word);
}
ArrayList<String> list = dict.getDictionaryWords();
assertEquals(words.length, dict.getNumberOfElements());
assertEquals(words.length, list.size());
assertTrue(hasAllWords(words, list));
assertEquals(10, getResizingArrLen((ResizingArrayDictionary) dict));
}
// #endregion
// Test 3: skipped, content covered in other tests (isWord that is in
// dictionary)
// #region Test 4: test isWord that is not in dictionary
@Test
public void trieTest4() {
dict = new TrieDictionary();
String[] words = { "shrimp", "shrimps", "chimp", "crimp", "ship", "shriek", "shrift", "shrill", "shrine",
"shrink" };
for (String word : words) {
dict.addNewWord(word);
}
assertFalse(dict.isWord("shrip"));
}
@Test
public void arrTest4() {
dict = new ArrayDictionary();
String[] words = { "shrimp", "shrimps", "chimp", "crimp", "ship", "shriek", "shrift", "shrill", "shrine",
"shrink" };
for (String word : words) {
dict.addNewWord(word);
}
assertFalse(dict.isWord("shrip"));
}
@Test
public void reArrTest4() {
dict = new ResizingArrayDictionary();
String[] words = { "shrimp", "shrimps", "chimp", "crimp", "ship", "shriek", "shrift", "shrill", "shrine",
"shrink" };
for (String word : words) {
dict.addNewWord(word);
}
assertFalse(dict.isWord("shrip"));
}
// #endregion
// Test 5: skipped, content covered in other tests (test addNewWord)
// #region Test 6: test removeWord
@Test
public void trieTest6() {
dict = new TrieDictionary();
String[] words = { "shrimp", "shrimp", "shrimps", "chimp", "crimp", "ship", "shriek", "shrift", "shrill", "shrine",
"shrink" };
for (String word : words) {
dict.addNewWord(word);
}
dict.removeWord("shrimp");
assertFalse(dict.isWord("shrimp"));
}
@Test
public void arrTest6() {
dict = new ArrayDictionary();
String[] words = { "shrimp", "shrimp", "shrimps", "chimp", "crimp", "ship", "shriek", "shrift", "shrill", "shrine",
"shrink" };
for (String word : words) {
dict.addNewWord(word);
}
dict.removeWord("shrimp");
assertFalse(dict.isWord("shrimp"));
}
@Test
public void reArrTest6() {
dict = new ResizingArrayDictionary();
String[] words = { "shrimp", "shrimp", "shrimps", "chimp", "crimp", "ship", "shriek", "shrift", "shrill", "shrine",
"shrink" };
for (String word : words) {
dict.addNewWord(word);
}
dict.removeWord("shrimp");
assertFalse(dict.isWord("shrimp"));
}
// #endregion
// Tests 7, 8, 9 skipped. (levenshtein distance)
// #region Test 10: test duplicate words from input file and add words twice
@Test
public void trieTest10() {
dict = new TrieDictionary();
dict.CreateDictionary(TEST_FILE);
dict.addNewWord("firearm"); // word in file
dict.removeWord("firearm");
dict.removeWord("patriotic"); // duplicate word in file
assertFalse(dict.isWord("patriotic"));
assertFalse(dict.isWord("firearm"));
}
@Test
public void arrTest10() {
// Tests if words from first half are added
dict = new ArrayDictionary();
dict.CreateDictionary(TEST_FILE);
dict.addNewWord("firearm"); // word in file
dict.removeWord("firearm");
dict.removeWord("patriotic"); // duplicate word in file
assertFalse(dict.isWord("patriotic"));
assertFalse(dict.isWord("firearm"));
}
@Test
public void reArrTest10() {
dict = new ResizingArrayDictionary();
dict.CreateDictionary(TEST_FILE);
dict.addNewWord("firearm"); // word in file
dict.removeWord("firearm");
dict.removeWord("patriotic"); // duplicate word in file
assertFalse(dict.isWord("patriotic"));
assertFalse(dict.isWord("firearm"));
}
// #endregion
// #region TireTestDFS: dfs using given example
// (https://learn.sun.ac.za/mod/page/view.php?id=1574200)
@Test
public void trieTestDFS() {
TrieDictionary trie = new TrieDictionary();
String[] words = { "ab", "ba", "bo", "cd" };
for (String word : words) {
trie.addNewWord(word);
}
ArrayList<Character> list = trie.DFS();
Character[] expected = { 'a', 'b', 'b', 'a', 'o', 'c', 'd' };
Character[] output = new Character[list.size()];
output = list.toArray(output);
assertArrayEquals(expected, output);
}
// #endregion
// #region TireTestBFS: bfs using given example
// (https://learn.sun.ac.za/mod/page/view.php?id=1574200)
@Test
public void trieTestBFS() {
TrieDictionary trie = new TrieDictionary();
String[] words = { "ab", "ba", "bo", "cd" };
for (String word : words) {
trie.addNewWord(word);
}
ArrayList<Character> list = trie.BFS();
Character[] expected = { 'a', 'b', 'c', 'b', 'a', 'o', 'd' };
Character[] output = new Character[list.size()];
output = list.toArray(output);
assertArrayEquals(expected, output);
}
// #endregion
}