-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGraphProcessorTest.java
More file actions
291 lines (254 loc) · 8.77 KB
/
Copy pathGraphProcessorTest.java
File metadata and controls
291 lines (254 loc) · 8.77 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
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import org.junit.BeforeClass;
import org.junit.Test;
///////////////////////////////////////////////////////////////////////////////
//assignment name: p4
//Author: Jichen Zhang, Junge Zhang
//Partner: Bowen Zhang, Griff Zhang, Tianyuan(Rainer) Yuan
//Email : jzhang877@wisc.edu, jzhang875@wisc.edu
//due date: April 15th 2018
//Credits: none
//known bugs: none
//////////////////////////////////////////////////////////////////////////////
/**
* This is a test class for the graph processor functionalities, from a given
* path of a dictionary file
*/
public class GraphProcessorTest {
/**
* The path of the file which contains the dictionary to be tested
*/
private static String filepath;
/**
* This method sets the testing path used in the rest of the class. It is easy
* to change
*/
@BeforeClass
public static void setUpBeforeClass() {
filepath = "word_list.txt";
}
/**
* This test tests the getWordStream method. We construct an array of String
* from the path in a helper method in this class, and another array of String
* from WordProcessor.getWordStream(path). This fails whenever the size, or any
* element of these arrays are not equal
*
* @throws IOException
*/
@Test
public void getWordStreamTest_ForFile() throws IOException {
String[] expectedWords = readFromFile(filepath);
String[] actualWords = WordProcessor.getWordStream(filepath).toArray(String[]::new);
assertEquals("words length", expectedWords.length, actualWords.length);
for (int i = 0; i < expectedWords.length; i++) {
assertEquals("word", expectedWords[i], actualWords[i]);
}
}
/**
* This test is trying to make sure that the isAdjacent method would always
* return false for 2 same words
*/
@Test
public void isAdjacent_FalseForSame() {
String[] word1 = { "", "abc", "3333333333333333", "!!!" };
String[] word2 = word1.clone();
boolean expected = false;
for (int i = 0; i < word1.length; i++) {
assertEquals(expected, WordProcessor.isAdjacent(word1[i], word2[i]));
}
}
/**
* This method tests the isAdjacent() method. We construct an array of String
* from the path in a helper method in this class. We use another helper method
* to determine the truth value of adjacencies between any 2 words. This fails
* whenever the truth value is different for any of the pairs of 2 words in the
* dictionary
*
* @throws IOException
*/
@Test
public void isAdjacent_ForFile() throws IOException {
String[] words = readFromFile(filepath);
words = removeDuplicate(words);
for (int i = 0; i < words.length; i++) {
for (int j = i; j < words.length; j++) {
// every pair is only tested once. i.e. (i,j) = (j,i)
String prompt = String.format("is adjacent between %1$s and %2$s", words[i], words[j]);
assertEquals(prompt, testAdjacent(words[i], words[j]), WordProcessor.isAdjacent(words[i], words[j]));
}
}
}
/**
* This test uses an unique test file, that has only 2 words but has a lot of
* them. Fails if the numberof vertices returned is not 2.
*
* @throws IOException
*/
@Test
public void pupulateGraph_duplicateWords_correctNumVertices() throws IOException {
String path = "duplicate_test.txt"; // This is a different file. Need upload
GraphProcessor p = new GraphProcessor();
int actual = p.populateGraph(path);
int expected = 2;
assertEquals("Number of vertices ", expected, actual);
}
/**
* This test tests whether the populateGraph() method returns the true number of
* vertices added to the graph. We use WordProcessor.getWordStream method to
* construct a String array, and then compare its size with the return of
* populateGraph().
*
* Precondition: WordProcessor.getWordStream() works fine
*
* @throws IOException
*/
@Test
public void populateGraph_correctNumVertices_ForFile() throws IOException {
GraphProcessor p = new GraphProcessor();
Integer actual = p.populateGraph(filepath);
String[] words = WordProcessor.getWordStream(filepath).toArray(String[]::new);
words = removeDuplicate(words);
Integer expected = words.length;
assertEquals("number of vertices", expected, actual);
}
/**
* This is a test which test the getShortestPath method of GraphProcessor clsss
* by loading the file into a String[] and use our own helper method to
* determine the shortest path. We randomly choose a word and calculate all the
* paths to it, comparing the results between our method and getShortestPath().
* It fails if any of the shortest paths are different.
*
* Precondition: populateGraph(), shortestPathPrecomputation() works fine
* WordProcessor.getWordStream(), isAdjacent() works fine
*
*
* @throws IOException
*/
@Test
public void correctShortestDistance_ForFile() throws IOException {
GraphProcessor p = new GraphProcessor();
p.populateGraph(filepath);
p.shortestPathPrecomputation();
String[] words = WordProcessor.getWordStream(filepath).toArray(String[]::new);
words = removeDuplicate(words);
Graph<String> g = new Graph<String>();
for (String s : words) {
g.addVertex(s);
}
for (int i = 0; i < words.length; i++) {
for (int j = i; j < words.length; j++) {
// everything is counted once
if (WordProcessor.isAdjacent(words[i], words[j])) {
g.addEdge(words[i], words[j]);
}
}
}
// check for a random word that every pair involving it has the same length
// result from bfsSearch in this class is the same as getShortestPath()
int i = (int) (Math.random() * words.length);
for (int j = 0; i < words.length; i++) {
assertEquals("path length", shortestPath(i, words)[j], p.getShortestPath(words[i], words[j]));
}
}
/**
* This method calculates all the shortest paths from a single word to the rest
* of the graph
*
* @param index,
* of the word in the array
* @param words,
* the array of words
* @return the shortest path from one word to the rest in an array for each
* pair. If the two words are unreachable
*/
private Integer[] shortestPath(int index, String[] words) {
// pre-cache edges
boolean[][] edges = new boolean[words.length][words.length];
for (int i = 0; i < words.length; i++)
for (int j = 0; j < words.length; j++)
edges[i][j] = WordProcessor.isAdjacent(words[i], words[j]);
Integer[] distance = new Integer[edges.length];
for (int t = 0; t < distance.length; t++)
distance[t] = -1;
distance[index] = 0;
for (int i = 0; i < edges.length; i++)
for (int j = 0; j < edges.length; j++)
for (int k = 0; k < edges.length; k++)
if (edges[j][k])
if ((distance[k] == null || distance[j] + 1 < distance[k]) && distance[j] != -1)
distance[k] = distance[j] + 1;
// a conversion between -1 and null
for (int i = 0; i < distance.length; i++) {
distance[i] = distance[i] == -1 ? null : distance[i];
}
return distance;
}
/**
* The helper method that determine whether 2 words are adjacent
*
* @param str0
* @param str1
* @return true if str0 and str1 are adjacent
*/
private boolean testAdjacent(String str0, String str1) {
if (str0.equals(str1))
return false;
int dif = str0.length() - str1.length();
if (dif > 1 || dif < -1)
return false;
int shorter = Math.min(str0.length(), str1.length());
int t0 = 0;
int t1 = 0;
for (; t0 < shorter && str0.charAt(t0) == str1.charAt(t0); t0++)
;
for (; t1 < shorter - t0 && str0.charAt(str0.length() - t1 - 1) == str1.charAt(str1.length() - t1 - 1); t1++)
;
int d0 = str0.length() - t0 - t1;
int d1 = str1.length() - t0 - t1;
return (d0 == 0 || d0 == 1) && (d1 == 0 || d1 == 1);
}
@SuppressWarnings("resource")
/**
* The method convert the dictionary file into an array of Strings
*
* @param path
* @return an array, in which each entry represents a word
*/
private String[] readFromFile(String path) {
// Read file
String rawWords = null;
try {
rawWords = new Scanner(new File(path)).useDelimiter("\\Z").next();
} catch (FileNotFoundException e) {
fail("File not found!");
}
// Split into an array
String words[] = rawWords.toUpperCase().trim().split("\n");
return words;
}
/**
* This method removes duplicate members of a given string
* @param lines
* @return a new string array without duplicates and nulls
*/
private static String[] removeDuplicate(String[] lines) {
int repeat = 0;
for (int t = 0; t < lines.length; t++) {
lines[t - repeat] = lines[t].trim();
for (int t1 = 0; t1 < t - repeat; t1++) {
if (lines[t1].equals(lines[t]))
repeat++;
}
}
int size = lines.length - repeat;
String[] nlines = new String[size];
for (int t = 0; t < size; t++)
nlines[t] = lines[t];
return nlines;
}
}