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
package edu.caltech.cs2.project08;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import edu.caltech.cs2.datastructures.BeaverMapsGraph;
import edu.caltech.cs2.datastructures.Location;
import edu.caltech.cs2.helpers.Inspection;
import edu.caltech.cs2.helpers.Reflection;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import edu.caltech.cs2.interfaces.IDeque;
import edu.caltech.cs2.interfaces.ISet;
import org.hamcrest.MatcherAssert;
import org.hamcrest.collection.IsIterableContainingInAnyOrder;
import org.hamcrest.collection.IsIterableContainingInOrder;
import org.hamcrest.core.IsIterableContaining;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.*;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BeavermapTests {
private static String BEAVERMAP_GRAPH_SOURCE = "src/edu/caltech/cs2/datastructures/BeaverMapsGraph.java";
private static JsonElement fromFile(String filename) {
try (FileReader reader = new FileReader(filename)) {
return JsonParser.parseReader(reader);
}
catch (IOException e) {
return null;
}
}
@Tag("C")
@Tag("Beavermap")
@DisplayName("BeaverMapsGraph implements required public methods")
@Test
@Order(0)
public void testMethodsBeaverMapsGraph() {
SortedSet<String> expected = new TreeSet<>(List.of(
"getLocationByName", "getBuildings", "getClosestBuilding", "dfs", "dijkstra", "addVertex"
));
SortedSet<String> actual = new TreeSet<>(
Stream.of(BeaverMapsGraph.class.getDeclaredMethods())
.filter(Reflection.hasModifier("public"))
.map(x -> x.getName())
.collect(Collectors.toList()));
MatcherAssert.assertThat(new ArrayList<>(actual),
IsIterableContaining.hasItems((expected.toArray())));
}
@Tag("C")
@Tag("Beavermap")
@Order(1)
@DisplayName("Does not use or import disallowed classes")
@Test
public void testForInvalidClasses() {
List<String> graphDisallow = List.of("java\\.lang\\.reflect");
Inspection.assertNoImportsOf(BEAVERMAP_GRAPH_SOURCE, graphDisallow);
Inspection.assertNoUsageOf(BEAVERMAP_GRAPH_SOURCE, graphDisallow);
}
@Order(2)
@DisplayName("Does not use or import disallowed classes from java.util")
@Test
@Tag("C")
@Tag("Beavermap")
public void testForInvalidImportsJavaUtil() {
List<String> allowed = List.of("Iterator");
Inspection.assertNoImportsOfExcept(BEAVERMAP_GRAPH_SOURCE, "java\\.util", allowed);
List<String> bannedUsages = List.of("java\\.util\\.(?!" + String.join("|", allowed) + ")");
Inspection.assertNoUsageOf(BEAVERMAP_GRAPH_SOURCE, bannedUsages);
}
// Only use Caltech map and buildings to test for correctness
@Tag("C")
@Tag("Beavermap")
@Test
@Order(3)
@DisplayName("Test getLocationById()")
public void testGetLocationByID() {
BeaverMapsGraph bmg = new BeaverMapsGraph(
"data/caltech/caltech.buildings.json",
"data/caltech/caltech.waypoints.json",
"data/caltech/caltech.roads.json");
JsonElement bs = fromFile("data/caltech/caltech.buildings.json");
for (JsonElement b : bs.getAsJsonArray()) {
Location loc = new Location(b.getAsJsonObject());
assertNotNull(bmg.getLocationByID(loc.id), "Location id " + loc.id + " not found by id");
}
}
@Tag("C")
@Tag("Beavermap")
@Test
@Order(4)
@DisplayName("Test getLocationByName()")
public void testGetLocationByName() {
BeaverMapsGraph bmg = new BeaverMapsGraph(
"data/caltech/caltech.buildings.json",
"data/caltech/caltech.waypoints.json",
"data/caltech/caltech.roads.json");
JsonElement bs = fromFile("data/caltech/caltech.buildings.json");
for (JsonElement b : bs.getAsJsonArray()) {
Location loc = new Location(b.getAsJsonObject());
if (loc.name != null) {
assertNotNull(bmg.getLocationByName(loc.name), "Location " + loc.name + " not found by name");
}
}
}
@Tag("C")
@Tag("Beavermap")
@DisplayName("Test getBuildings()")
@ParameterizedTest(name = "Test getBuildings() on {0}")
@CsvSource({
"caltech/caltech.buildings.json, caltech/caltech.waypoints.json, caltech/caltech.roads.json",
"pasadena/pasadena.buildings.json, pasadena/pasadena.waypoints.json, pasadena/pasadena.roads.json",
})
@Order(5)
public void testGetBuildings(String buildingsFile, String waypointsFile, String roadsFile) {
BeaverMapsGraph bmg = new BeaverMapsGraph(
"data/" + buildingsFile, "data/" + waypointsFile, "data/" + roadsFile);
Set<Location> buildings = new HashSet<>();
JsonElement bs = fromFile("data/" + buildingsFile);
for (JsonElement b : bs.getAsJsonArray()) {
Location loc = new Location(b.getAsJsonObject());
buildings.add(loc);
}
MatcherAssert.assertThat(bmg.getBuildings(),
IsIterableContainingInAnyOrder.containsInAnyOrder(buildings.toArray()));
}
@Tag("C")
@Tag("Beavermap")
@DisplayName("Test getClosestBuilding()")
@ParameterizedTest(name = "Test getClosestBuilding() on {0}")
@CsvSource({
"caltech/caltech.buildings.json, caltech/caltech.waypoints.json, caltech/caltech.roads.json, caltech/caltech.closest_trace.json",
"pasadena/pasadena.buildings.json, pasadena/pasadena.waypoints.json, pasadena/pasadena.roads.json, pasadena/pasadena.closest_trace.json",
})
@Order(6)
public void testGetClosestLocation(String buildingsFile, String waypointsFile, String roadsFile, String traceFile) {
BeaverMapsGraph bmg = new BeaverMapsGraph(
"data/" + buildingsFile, "data/" + waypointsFile, "data/" + roadsFile);
JsonElement bs = fromFile("data/" + traceFile);
for (JsonElement b : bs.getAsJsonArray()) {
JsonObject curr = b.getAsJsonObject();
Location center = bmg.getLocationByID(curr.get("center").getAsLong());
Location closestExpected = bmg.getLocationByID(curr.get("closest").getAsLong());
Location closestActual = bmg.getClosestBuilding(center.lat, center.lon);
assertEquals(closestExpected.lat, closestActual.lat, "Latitudes differ");
assertEquals(closestExpected.lon, closestActual.lon, "Longitudes differ");
}
}
@Tag("C")
@Tag("Beavermap")
@DisplayName("Test addVertex() updates BeaverMapsGraph and underlying Graph properly")
@Test
@Order(7)
public void testAddVertexBeaverMapsGraph() {
BeaverMapsGraph bmg = new BeaverMapsGraph(
"data/caltech/caltech.buildings.json",
"data/caltech/caltech.waypoints.json",
"data/caltech/caltech.roads.json");
JsonElement bs = fromFile("data/pasadena/pasadena.buildings.json");
for (JsonElement b : bs.getAsJsonArray()) {
Location loc = new Location(b.getAsJsonObject());
bmg.addVertex(loc);
assertNotNull(bmg.getLocationByID(loc.id), "Location id " + loc.id + " not found by id");
// Test that the vertex was actually added to the graph
bmg.neighbors(loc.id);
}
}
// Note: Pasadena map is WAY TOO LARGE to test all edges, don't try
@Tag("C")
@Tag("Beavermap")
@DisplayName("Completely check all nodes and edges in BeaverMapsGraph loaded from files")
@ParameterizedTest(name = "Test nodes in file {0}")
@CsvSource({
"caltech/caltech.buildings.json, caltech/caltech.waypoints.json, caltech/caltech.roads.json, caltech/caltech.neighbors_trace.json"
})
@Order(8)
public void testNodesEdgesInMap(String bFile, String wFile, String roadsFile, String traceFile) {
BeaverMapsGraph bmg = new BeaverMapsGraph("data/" + bFile, "data/" + wFile, "data/" + roadsFile);
List<Long> actualNodeIDs = new ArrayList<>();
for (long nid : bmg.vertices()) {
actualNodeIDs.add(nid);
}
JsonElement s = fromFile("data/" + traceFile);
for (JsonElement b : s.getAsJsonArray()) {
JsonObject curr = b.getAsJsonObject();
long locID = curr.get("id").getAsLong();
Location loc = bmg.getLocationByID(locID);
assertNotNull(loc, locID + " should be in graph, but is not");
actualNodeIDs.remove(locID);
JsonArray neighbors = curr.get("neighbors").getAsJsonArray();
ISet<Long> actualNeighbors = bmg.neighbors(locID);
List<Long> missingNeighbors = new ArrayList<>();
for (JsonElement e : neighbors) {
long neighborID = e.getAsLong();
if (!actualNeighbors.remove(neighborID)) {
missingNeighbors.add(neighborID);
}
}
// Use this instead of MatcherAssert to provide better errors (though I doubt they'll be needed)
// Should use Truth instead... but not this year.
if (missingNeighbors.size() > 0) {
fail(locID + " missing neighbors " + missingNeighbors);
} else if (actualNeighbors.size() != 0) {
fail(locID + " has extra neighbors " + actualNeighbors);
}
}
assertEquals(0, actualNodeIDs.size(), "Graph has extra nodes: " + actualNodeIDs);
}
@Tag("B")
@DisplayName("Test DFS radius search")
@ParameterizedTest(name = "Test DFS on graph {0}")
@CsvSource({
"caltech/caltech.buildings.json, caltech/caltech.waypoints.json, caltech/caltech.roads.json, caltech/caltech.radius_trace.json",
"pasadena/pasadena.buildings.json, pasadena/pasadena.waypoints.json, pasadena/pasadena.roads.json, pasadena/pasadena.radius_trace.json",
})
@Order(9)
public void testDFSRadius(String buildingsFile, String waypointsFile, String roadsFile, String traceFile) {
BeaverMapsGraph bmg = new BeaverMapsGraph(
"data/" + buildingsFile, "data/" + waypointsFile, "data/" + roadsFile);
JsonElement s = fromFile("data/" + traceFile);
for (JsonElement b : s.getAsJsonArray()) {
JsonObject curr = b.getAsJsonObject();
long locID = curr.get("center").getAsLong();
Location loc = bmg.getLocationByID(locID);
double dist = curr.get("radius").getAsDouble();
JsonArray locList = curr.get("locations").getAsJsonArray();
Set<Long> expectedLocIDs = new HashSet<>();
for (JsonElement e : locList) {
expectedLocIDs.add(e.getAsLong());
}
ISet<Location> actualLoc = bmg.dfs(loc, dist);
Set<Long> locIDs = new HashSet<>(actualLoc.size());
for (Location l : actualLoc) {
locIDs.add(l.id);
}
MatcherAssert.assertThat(locIDs,
IsIterableContainingInAnyOrder.containsInAnyOrder(expectedLocIDs.toArray()));
}
}
@Tag("A")
@DisplayName("Test buildings are ignored in dijkstra path")
@Test
@Order(10)
public void testDijkstraIgnoreBuildings() {
BeaverMapsGraph bmg = new BeaverMapsGraph(
"data/caltech/caltech.buildings.json",
"data/caltech/caltech.waypoints.json",
"data/caltech/caltech.roads.json");
JsonElement s = fromFile("data/caltech/caltech.paths_trace.json");
for (JsonElement b : s.getAsJsonArray()) {
JsonObject curr = b.getAsJsonObject();
Location start = bmg.getLocationByID(curr.get("start").getAsLong());
Location target = bmg.getLocationByID(curr.get("target").getAsLong());
IDeque<Location> actualPath = bmg.dijkstra(start, target);
if (actualPath == null) {
continue;
}
for (Location loc : actualPath){
if (loc.id == start.id || loc.id == target.id) {
continue;
}
ISet<Location> buildings = Reflection.getFieldValue(BeaverMapsGraph.class, "buildings", bmg);
assertFalse(buildings.contains(loc), "Location " + loc.id + " in path is a building");
}
}
}
@Tag("A")
@DisplayName("Test Dijkstra")
@ParameterizedTest(name = "Test Dijkstra on graph {0}")
@CsvSource({
"caltech/caltech.buildings.json, caltech/caltech.waypoints.json, caltech/caltech.roads.json, caltech/caltech.paths_trace.json",
"pasadena/pasadena.buildings.json, pasadena/pasadena.waypoints.json, pasadena/pasadena.roads.json, pasadena/pasadena.paths_trace.json",
})
@Order(11)
public void testDijkstraBeaverMap(String buildingsFile, String waypointsFile, String roadsFile, String traceFile) throws FileNotFoundException {
BeaverMapsGraph bmg = new BeaverMapsGraph(
"data/" + buildingsFile, "data/" + waypointsFile, "data/" + roadsFile);
JsonElement s = fromFile("data/" + traceFile);
for (JsonElement b : s.getAsJsonArray()) {
JsonObject curr = b.getAsJsonObject();
Location start = bmg.getLocationByID(curr.get("start").getAsLong());
Location target = bmg.getLocationByID(curr.get("target").getAsLong());
// Build expected list
JsonArray pathList = curr.get("path").getAsJsonArray();
List<Long> expectedPathIDs = new ArrayList<>();
for (JsonElement e : pathList) {
expectedPathIDs.add(e.getAsLong());
}
IDeque<Location> actualPath = bmg.dijkstra(start, target);
List<Long> actualPathIDs = new ArrayList<>();
if (expectedPathIDs.size() == 0) {
assertNull(actualPath, "Path does not exist from " + start.id + " to " + target.id + " but a path was found");
}
else {
assertNotNull(actualPath, "Path exists from " + start.id + " to " + target.id + " but a path was not found");
for (Location l : actualPath) {
actualPathIDs.add(l.id);
}
// Check that path is *exactly* equivalent
MatcherAssert.assertThat(actualPathIDs,
IsIterableContainingInOrder.contains(expectedPathIDs.toArray()));
}
}
}
}