-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphSearchEngineTester.java
More file actions
162 lines (130 loc) · 4.22 KB
/
GraphSearchEngineTester.java
File metadata and controls
162 lines (130 loc) · 4.22 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
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;
import java.util.*;
import java.io.*;
/**
* Code to test an <tt>GraphSearchEngine</tt> implementation.
*/
public class GraphSearchEngineTester {
private final GraphSearchEngine searchEngine = new GraphSearchEngineImpl();
/*
* checks that the names in the path matches the names in correctNames array
*/
void checkCorrectNamesInPath(List<Node> shortestPath, String[] correctNames) {
int idx = 0;
for (Node node : shortestPath) {
assertEquals(correctNames[idx++], node.getName());
}
}
/*
* makes the graph using the data from the tsv file
*/
private IMDBGraph makeGraph() {
final IMDBGraph graph;
try {
graph = new IMDBGraphImpl(IMDBGraphImpl.IMDB_DIRECTORY + "/testActors.tsv",
IMDBGraphImpl.IMDB_DIRECTORY + "/testMovies.tsv");
} catch (IOException ioe) {
ioe.printStackTrace();
assertTrue(false);
return null;
}
return graph;
}
@Test
@Timeout(5)
void testShortestPath1() {
IMDBGraph graph = makeGraph();
final Node actor1 = graph.getActor("Kris");
final Node actor2 = graph.getActor("Sandy");
final List<Node> shortestPath = searchEngine.findShortestPath(actor1, actor2);
assertEquals(5, shortestPath.size());
final String[] correctNames = { "Kris", "Blah2", "Sara", "Blah3", "Sandy" };
checkCorrectNamesInPath(shortestPath, correctNames);
}
@Test
/*
* tests the path between the same node
*/
void testSameNode() {
IMDBGraph graph = makeGraph();
final Node actor1 = graph.getActor("Uyen");
final Node actor2 = graph.getActor("Uyen");
final List<Node> shortestPath = searchEngine.findShortestPath(actor1, actor2);
assertEquals(1, shortestPath.size());
final String[] correctNames = { "Uyen" };
checkCorrectNamesInPath(shortestPath, correctNames);
}
@Test
/*
* tests for no path between 2 nodes
*/
void testNoPath() {
IMDBGraph graph = makeGraph();
final Node actor1 = graph.getActor("Uyen2");
final Node actor2 = graph.getActor("Kris");
final List<Node> shortestPath = searchEngine.findShortestPath(actor1, actor2);
assertNull(shortestPath);
}
@Test
/*
* tests to make sure there is no path if the start and end nodes are not actors
*/
void testNotAnActor() {
IMDBGraph graph = makeGraph();
final Node actor1 = graph.getActor("Uyen3");
final Node actor2 = graph.getActor("aa");
final List<Node> shortestPath = searchEngine.findShortestPath(actor1, actor2);
assertNull(shortestPath);
}
@Test
/*
* test to make sure there is no path if it starts out as null
*/
void testStartingNodeNull() {
IMDBGraph graph = makeGraph();
final Node actor1 = graph.getActor(null);
final Node actor2 = graph.getActor("aa");
final List<Node> shortestPath = searchEngine.findShortestPath(actor1, actor2);
assertNull(shortestPath);
}
@Test
/*
* tests for a path between two nodes with surrounding neighbors
*/
void testTwoMiddleNodes() {
IMDBGraph graph = makeGraph();
final Node actor1 = graph.getActor("Sara");
final Node actor2 = graph.getActor("Sandy");
final List<Node> shortestPath = searchEngine.findShortestPath(actor1, actor2);
assertEquals(3, shortestPath.size());
final String[] correctNames = { "Sara", "Blah3", "Sandy" };
checkCorrectNamesInPath(shortestPath, correctNames);
}
@Test
/*
* tests for a path between two movie nodes
*/
void testTwoMovies() {
IMDBGraph graph = makeGraph();
final Node movie1 = graph.getMovie("Blah1");
final Node movie2 = graph.getMovie("Blah3");
final List<Node> shortestPath = searchEngine.findShortestPath(movie1, movie2);
assertEquals(5, shortestPath.size());
final String[] correctNames = { "Blah1", "Kris", "Blah2", "Sara", "Blah3" };
checkCorrectNamesInPath(shortestPath, correctNames);
}
@Test
/*
* tests for a path between a movie and actor
*/
void testPathBetweenMovieAndActor() {
IMDBGraph graph = makeGraph();
final Node movie1 = graph.getMovie("Blah1");
final Node actor1 = graph.getActor("Sandy");
final List<Node> shortestPath = searchEngine.findShortestPath(movie1, actor1);
assertEquals(6, shortestPath.size());
final String[] correctNames = { "Blah1", "Kris", "Blah2", "Sara", "Blah3", "Sandy" };
checkCorrectNamesInPath(shortestPath, correctNames);
}
}