From 912b3ff1a657e6f8a100d5bcc8ddefd295d52fb3 Mon Sep 17 00:00:00 2001 From: ashahidu <ashahidu@caltech.edu> Date: Sun, 21 Feb 2021 16:06:01 -0700 Subject: [PATCH 1/9] Fix issues --- .../cs2/datastructures/BeaverMapsGraph.java | 15 +---- src/edu/caltech/cs2/datastructures/Graph.java | 2 +- .../caltech/cs2/interfaces/IDictionary.java | 2 + src/edu/caltech/cs2/interfaces/IGraph.java | 24 ++++---- src/edu/caltech/cs2/project08/BeaverMaps.java | 1 + .../cs2/project08/MapsAutoCompleter.java | 2 + .../caltech/cs2/project08/BeavermapTests.java | 60 +++++++++++++++---- .../caltech/cs2/project08/DijkstraTest.java | 19 +++--- .../edu/caltech/cs2/project08/GraphTests.java | 32 ++++++++++ 9 files changed, 113 insertions(+), 44 deletions(-) diff --git a/src/edu/caltech/cs2/datastructures/BeaverMapsGraph.java b/src/edu/caltech/cs2/datastructures/BeaverMapsGraph.java index d824b98..f3372fa 100644 --- a/src/edu/caltech/cs2/datastructures/BeaverMapsGraph.java +++ b/src/edu/caltech/cs2/datastructures/BeaverMapsGraph.java @@ -2,18 +2,13 @@ package edu.caltech.cs2.datastructures; import com.google.gson.JsonElement; import com.google.gson.JsonParser; -import edu.caltech.cs2.interfaces.IDeque; -import edu.caltech.cs2.interfaces.IDictionary; -import edu.caltech.cs2.interfaces.ISet; +import edu.caltech.cs2.interfaces.*; -import java.io.File; import java.io.FileReader; import java.io.IOException; public class BeaverMapsGraph extends Graph<Long, Double> { - private static JsonParser JSON_PARSER = new JsonParser(); - private IDictionary<Long, Location> ids; private ISet<Location> buildings; @@ -117,12 +112,8 @@ public class BeaverMapsGraph extends Graph<Long, Double> { * @return the JSON data from filename */ private static JsonElement fromFile(String filename) { - try { - return JSON_PARSER.parse( - new FileReader( - new File(filename) - ) - ); + try (FileReader reader = new FileReader(filename)) { + return JsonParser.parseReader(reader); } catch (IOException e) { return null; } diff --git a/src/edu/caltech/cs2/datastructures/Graph.java b/src/edu/caltech/cs2/datastructures/Graph.java index da8d092..223b49c 100644 --- a/src/edu/caltech/cs2/datastructures/Graph.java +++ b/src/edu/caltech/cs2/datastructures/Graph.java @@ -2,7 +2,7 @@ package edu.caltech.cs2.datastructures; import edu.caltech.cs2.interfaces.*; -public class Graph<V, E> extends IGraph<V, E> { +public class Graph<V, E> implements IGraph<V, E> { @Override public boolean addVertex(V vertex) { diff --git a/src/edu/caltech/cs2/interfaces/IDictionary.java b/src/edu/caltech/cs2/interfaces/IDictionary.java index 588ec43..04c3f79 100644 --- a/src/edu/caltech/cs2/interfaces/IDictionary.java +++ b/src/edu/caltech/cs2/interfaces/IDictionary.java @@ -1,5 +1,7 @@ package edu.caltech.cs2.interfaces; +import edu.caltech.cs2.datastructures.LinkedDeque; + public interface IDictionary<K, V> extends Iterable<K> { public static class Entry<K, V> { diff --git a/src/edu/caltech/cs2/interfaces/IGraph.java b/src/edu/caltech/cs2/interfaces/IGraph.java index 6e0e757..24a47c8 100644 --- a/src/edu/caltech/cs2/interfaces/IGraph.java +++ b/src/edu/caltech/cs2/interfaces/IGraph.java @@ -1,32 +1,32 @@ package edu.caltech.cs2.interfaces; -public abstract class IGraph<V, E> { +public interface IGraph<V, E> { /** * Add a vertex to the graph. * @param vertex The vertex to add * @return true if vertex was not present already. */ - public abstract boolean addVertex(V vertex); + public boolean addVertex(V vertex); /** * Adds edge e to the graph. * * @param e The edge to add. * @throws IllegalArgumentException - * If e is not a valid edge (eg. refers to vertices not in the graph). - * @return true If e was not already present; false if it was (in which case the graph is still updated). + * If src and dest are not valid vertices (eg. refers to vertices not in the graph). + * @return true If src and dest were not already present; false if it was (in which case the graph is still updated). */ - public abstract boolean addEdge(V src, V dest, E e); + public boolean addEdge(V src, V dest, E e); /** * Adds edge e to the graph in both directionns. * * @param e The edge to add. * @throws IllegalArgumentException - * If e is not a valid edge (eg. refers to vertices not in the graph). - * @return true If e was not already present in either direction; false if it was (in which case the graph is still updated). + * If src and dest are not valid vertices (eg. refers to vertices not in the graph). + * @return true If src and dest were not already present in either direction; false if it was (in which case the graph is still updated). */ - public abstract boolean addUndirectedEdge(V src, V dest, E e); + public boolean addUndirectedEdge(V src, V dest, E e); /** * Remove an edge from src to dest from the graph. @@ -34,13 +34,13 @@ public abstract class IGraph<V, E> { * @throws IllegalArgumentException if src or dest is not in the graph. * @return true if an edge from src to dest edge was present. */ - public abstract boolean removeEdge(V src, V dest); + public boolean removeEdge(V src, V dest); /** * Returns the set of vertices in the graph. * @return The set of all vertices in the graph. */ - public abstract ISet<V> vertices(); + public ISet<V> vertices(); /** * Tests if vertices i and j are adjacent, returning the edge between @@ -50,7 +50,7 @@ public abstract class IGraph<V, E> { * @return The edge from i to j if it exists in the graph; * null otherwise. */ - public abstract E adjacent(V i, V j); + public E adjacent(V i, V j); /** * Return the neighbours of a given vertex when this graph is treated as @@ -60,5 +60,5 @@ public abstract class IGraph<V, E> { * @throws IllegalArgumentException if vertex is not in the graph. * @return The set of neighbors of vertex. */ - public abstract ISet<V> neighbors(V vertex); + public ISet<V> neighbors(V vertex); } \ No newline at end of file diff --git a/src/edu/caltech/cs2/project08/BeaverMaps.java b/src/edu/caltech/cs2/project08/BeaverMaps.java index 3acb98e..e11e8d1 100644 --- a/src/edu/caltech/cs2/project08/BeaverMaps.java +++ b/src/edu/caltech/cs2/project08/BeaverMaps.java @@ -5,6 +5,7 @@ import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; import edu.caltech.cs2.datastructures.BeaverMapsGraph; +import edu.caltech.cs2.datastructures.LinkedDeque; import edu.caltech.cs2.datastructures.Location; import edu.caltech.cs2.interfaces.IDeque; import edu.caltech.cs2.interfaces.ISet; diff --git a/src/edu/caltech/cs2/project08/MapsAutoCompleter.java b/src/edu/caltech/cs2/project08/MapsAutoCompleter.java index db88dfa..7be91f5 100644 --- a/src/edu/caltech/cs2/project08/MapsAutoCompleter.java +++ b/src/edu/caltech/cs2/project08/MapsAutoCompleter.java @@ -1,6 +1,8 @@ package edu.caltech.cs2.project08; +import edu.caltech.cs2.datastructures.LinkedDeque; import edu.caltech.cs2.datastructures.Location; +import edu.caltech.cs2.datastructures.TrieMap; import edu.caltech.cs2.interfaces.IDeque; import edu.caltech.cs2.interfaces.ISet; import edu.caltech.cs2.interfaces.ITrieMap; diff --git a/tests/edu/caltech/cs2/project08/BeavermapTests.java b/tests/edu/caltech/cs2/project08/BeavermapTests.java index bc4fddf..50c7327 100644 --- a/tests/edu/caltech/cs2/project08/BeavermapTests.java +++ b/tests/edu/caltech/cs2/project08/BeavermapTests.java @@ -9,7 +9,6 @@ import edu.caltech.cs2.datastructures.Location; import edu.caltech.cs2.helpers.Inspection; import edu.caltech.cs2.helpers.Reflection; -import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; @@ -22,7 +21,7 @@ import edu.caltech.cs2.interfaces.ISet; import org.hamcrest.MatcherAssert; import org.hamcrest.collection.IsIterableContainingInAnyOrder; import org.hamcrest.collection.IsIterableContainingInOrder; -import org.hamcrest.core.IsCollectionContaining; +import org.hamcrest.core.IsIterableContaining; import org.junit.jupiter.api.*; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.TestMethodOrder; @@ -34,16 +33,12 @@ 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 JsonParser JSON_PARSER = new JsonParser(); private static JsonElement fromFile(String filename) { - try { - return JSON_PARSER.parse( - new FileReader( - new File(filename) - ) - ); - } catch (IOException e) { + try (FileReader reader = new FileReader(filename)) { + return JsonParser.parseReader(reader); + } + catch (IOException e) { return null; } } @@ -130,6 +125,22 @@ public class BeavermapTests { } } + @Tag("C") + @DisplayName("Test addVertex() updates BeaverMapsGraph properly") + @Test + 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"); + } + } + @Tag("C") @DisplayName("BeaverMapsGraph implements required public methods") @Test @@ -143,7 +154,7 @@ public class BeavermapTests { .map(x -> x.getName()) .collect(Collectors.toList())); MatcherAssert.assertThat(new ArrayList<>(actual), - IsCollectionContaining.hasItems((expected.toArray()))); + IsIterableContaining.hasItems((expected.toArray()))); } @@ -226,6 +237,33 @@ public class BeavermapTests { } } + @Tag("A") + @DisplayName("Test buildings are ignored in dijkstra path") + @Test + 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") diff --git a/tests/edu/caltech/cs2/project08/DijkstraTest.java b/tests/edu/caltech/cs2/project08/DijkstraTest.java index 1070100..e8ad31e 100644 --- a/tests/edu/caltech/cs2/project08/DijkstraTest.java +++ b/tests/edu/caltech/cs2/project08/DijkstraTest.java @@ -77,10 +77,12 @@ public class DijkstraTest { Scanner s = new Scanner(new File("data/dijkstra/" + traceFile)); - if (res == null) { - assertEquals(s.nextLine(), "null", "Path exists but was not found"); + String line = s.nextLine(); + if (line.equals("null")) { + assertNull(res, "Path does not exist but was found"); } else { + assertNotNull(res, "Path exists but was not found"); for (Location l : res) { if (prev != null) { pathLen += bmg.adjacent(prev.id, l.id); @@ -88,7 +90,7 @@ public class DijkstraTest { prev = l; } - double expectedLen = s.nextDouble(); + double expectedLen = Double.parseDouble(line); assertEquals(expectedLen, pathLen, "Path lengths are not equivalent"); } @@ -97,7 +99,7 @@ public class DijkstraTest { @Order(3) @DisplayName("Tests Dijkstra on random graph and paths") @Test - public void dijkstraStressTest() throws FileNotFoundException{ + public void dijkstraStressTest() throws FileNotFoundException { final int num_tests = 1000; IGraph<Integer, Integer> refg = new Graph<Integer, Integer>(); @@ -131,10 +133,12 @@ public class DijkstraTest { BeaverMapsGraph bmg = GraphMaker.transformToLocations(refg); IDeque<Location> res = bmg.dijkstra(new Location(startvertex), new Location(endvertex)); - if (res == null) { - assertEquals(s.nextLine(), "null", "Path exists but was not found"); + String line = s.nextLine(); + if (line.equals("null")) { + assertNull(res, "Path does not exist but was found"); } else { + assertNotNull(res, "Path exists but was not found"); double pathLen = 0; Location prev = null; for (Location l : res) { @@ -144,8 +148,7 @@ public class DijkstraTest { prev = l; } - double expectedLen = s.nextDouble(); - s.nextLine(); + double expectedLen = Double.parseDouble(line); assertEquals(expectedLen, pathLen, "Path lengths are not equivalent"); } } diff --git a/tests/edu/caltech/cs2/project08/GraphTests.java b/tests/edu/caltech/cs2/project08/GraphTests.java index c38df58..c15c08c 100644 --- a/tests/edu/caltech/cs2/project08/GraphTests.java +++ b/tests/edu/caltech/cs2/project08/GraphTests.java @@ -187,6 +187,38 @@ public class GraphTests { }); } + @Test + public void undirectedEdgeTest() { + assertTimeout(Duration.ofMillis(SIMPLE_OP_TIMEOUT_MS), () -> { + IGraph<String, Integer> g = new Graph<>(); + + assertTrue(g.addVertex("1"), "Should be able to add a vertex"); + assertEquals(1, g.vertices().size(), "Should have correct number of vertices"); + assertTrue(g.addVertex("2"), "Should be able to add a vertex"); + assertEquals(2, g.vertices().size(), "Should have correct number of vertices"); + assertTrue(g.addVertex("3"), "Should be able to add a vertex"); + assertEquals(3, g.vertices().size(), "Should have correct number of vertices"); + + assertTrue(g.addUndirectedEdge("1", "2", 1), "Should be able to add new edge"); + assertFalse(g.addEdge("1", "2", 1), "Should not be able to add new edge"); + assertFalse(g.addEdge("1", "2", 2), "Should not be able to add new edge"); + assertFalse(g.addEdge("2", "1", 1), "Should not be able to add new edge"); + assertFalse(g.addEdge("2", "1", 2), "Should not be able to add new edge"); + + assertTrue(g.addUndirectedEdge("1", "3", 1), "Should be able to add new edge"); + assertFalse(g.addEdge("1", "3", 1), "Should not be able to add new edge"); + assertFalse(g.addEdge("1", "3", 2), "Should not be able to add new edge"); + assertFalse(g.addEdge("3", "1", 1), "Should not be able to add new edge"); + assertFalse(g.addEdge("3", "1", 2), "Should not be able to add new edge"); + + assertTrue(g.addUndirectedEdge("2", "3", 1), "Should be able to add new edge"); + assertFalse(g.addEdge("2", "3", 1), "Should not be able to add new edge"); + assertFalse(g.addEdge("2", "3", 2), "Should not be able to add new edge"); + assertFalse(g.addEdge("3", "2", 1), "Should not be able to add new edge"); + assertFalse(g.addEdge("3", "2", 2), "Should not be able to add new edge"); + }); + } + @Test public void stringEdgeTest() { assertTimeout(Duration.ofMillis(SIMPLE_OP_TIMEOUT_MS), () -> { -- GitLab From b37e9e2b649b2103dd60d0b9e39db61332a0a4da Mon Sep 17 00:00:00 2001 From: ashahidu <ashahidu@caltech.edu> Date: Sun, 21 Feb 2021 18:33:02 -0700 Subject: [PATCH 2/9] Fix star imports --- src/edu/caltech/cs2/datastructures/BeaverMapsGraph.java | 4 +++- src/edu/caltech/cs2/datastructures/Graph.java | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/edu/caltech/cs2/datastructures/BeaverMapsGraph.java b/src/edu/caltech/cs2/datastructures/BeaverMapsGraph.java index f3372fa..96520b1 100644 --- a/src/edu/caltech/cs2/datastructures/BeaverMapsGraph.java +++ b/src/edu/caltech/cs2/datastructures/BeaverMapsGraph.java @@ -2,7 +2,9 @@ package edu.caltech.cs2.datastructures; import com.google.gson.JsonElement; import com.google.gson.JsonParser; -import edu.caltech.cs2.interfaces.*; +import edu.caltech.cs2.interfaces.IDeque; +import edu.caltech.cs2.interfaces.IDictionary; +import edu.caltech.cs2.interfaces.ISet; import java.io.FileReader; import java.io.IOException; diff --git a/src/edu/caltech/cs2/datastructures/Graph.java b/src/edu/caltech/cs2/datastructures/Graph.java index 223b49c..d24bd1d 100644 --- a/src/edu/caltech/cs2/datastructures/Graph.java +++ b/src/edu/caltech/cs2/datastructures/Graph.java @@ -1,6 +1,8 @@ package edu.caltech.cs2.datastructures; -import edu.caltech.cs2.interfaces.*; +import edu.caltech.cs2.interfaces.IDictionary; +import edu.caltech.cs2.interfaces.IGraph; +import edu.caltech.cs2.interfaces.ISet; public class Graph<V, E> implements IGraph<V, E> { -- GitLab From 5837873bc2f909bb5b0209b80668ac061267dfa4 Mon Sep 17 00:00:00 2001 From: ashahidu <ashahidu@caltech.edu> Date: Sun, 21 Feb 2021 18:52:54 -0700 Subject: [PATCH 3/9] Fix issue 2 --- tests/edu/caltech/cs2/project08/BeavermapTests.java | 10 ++++++---- tests/edu/caltech/cs2/project08/DijkstraTest.java | 8 ++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/edu/caltech/cs2/project08/BeavermapTests.java b/tests/edu/caltech/cs2/project08/BeavermapTests.java index 50c7327..b4ef8be 100644 --- a/tests/edu/caltech/cs2/project08/BeavermapTests.java +++ b/tests/edu/caltech/cs2/project08/BeavermapTests.java @@ -290,7 +290,12 @@ public class BeavermapTests { IDeque<Location> actualPath = bmg.dijkstra(start, target); List<Long> actualPathIDs = new ArrayList<>(); - if (actualPath != null) { + + if (expectedPathIDs.size() == 0) { + assertNull(actualPath, "Path does not exist from " + start.id + " to " + target.id + " but was found"); + } + else { + assertNotNull(actualPath, "Path exists from " + start.id + " to " + target.id + " but was not found"); for (Location l : actualPath) { actualPathIDs.add(l.id); } @@ -298,9 +303,6 @@ public class BeavermapTests { MatcherAssert.assertThat(actualPathIDs, IsIterableContainingInOrder.contains(expectedPathIDs.toArray())); } - else if (expectedPathIDs.size() != 0) { - fail("Found path from " + start.id + " to " + target.id + " where there is none."); - } } } diff --git a/tests/edu/caltech/cs2/project08/DijkstraTest.java b/tests/edu/caltech/cs2/project08/DijkstraTest.java index e8ad31e..533f4dd 100644 --- a/tests/edu/caltech/cs2/project08/DijkstraTest.java +++ b/tests/edu/caltech/cs2/project08/DijkstraTest.java @@ -79,10 +79,10 @@ public class DijkstraTest { String line = s.nextLine(); if (line.equals("null")) { - assertNull(res, "Path does not exist but was found"); + assertNull(res, "Path does not exist from " + start + " to " + end + " but was found"); } else { - assertNotNull(res, "Path exists but was not found"); + assertNotNull(res, "Path exists from " + start + " to " + end + " but was not found"); for (Location l : res) { if (prev != null) { pathLen += bmg.adjacent(prev.id, l.id); @@ -135,10 +135,10 @@ public class DijkstraTest { String line = s.nextLine(); if (line.equals("null")) { - assertNull(res, "Path does not exist but was found"); + assertNull(res, "Path does not exist from " + startvertex + " to " + endvertex + " but was found"); } else { - assertNotNull(res, "Path exists but was not found"); + assertNotNull(res, "Path exists from " + startvertex + " to " + endvertex + " but was not found"); double pathLen = 0; Location prev = null; for (Location l : res) { -- GitLab From 60e0af3229307fa2778bc829fdce6dcade3ac342 Mon Sep 17 00:00:00 2001 From: Ethan Ordentlich <eordentl@Caltech.edu> Date: Thu, 25 Feb 2021 02:41:49 -0800 Subject: [PATCH 4/9] Small updates --- src/edu/caltech/cs2/interfaces/IDeque.java | 4 +-- src/edu/caltech/cs2/interfaces/IGraph.java | 13 +++++--- tests/edu/caltech/cs2/helpers/Inspection.java | 33 ++++++++++++++----- .../caltech/cs2/project08/BeavermapTests.java | 14 +++++++- .../edu/caltech/cs2/project08/GraphTests.java | 15 ++++++++- 5 files changed, 61 insertions(+), 18 deletions(-) diff --git a/src/edu/caltech/cs2/interfaces/IDeque.java b/src/edu/caltech/cs2/interfaces/IDeque.java index e05b17b..03cd010 100644 --- a/src/edu/caltech/cs2/interfaces/IDeque.java +++ b/src/edu/caltech/cs2/interfaces/IDeque.java @@ -23,8 +23,8 @@ public interface IDeque<E> extends ICollection<E> { * @param e Element to add */ @Override - default public void add(E e){ - this.addFront(e); + default public void add(E e) { + this.addBack(e); } /** diff --git a/src/edu/caltech/cs2/interfaces/IGraph.java b/src/edu/caltech/cs2/interfaces/IGraph.java index 24a47c8..1344044 100644 --- a/src/edu/caltech/cs2/interfaces/IGraph.java +++ b/src/edu/caltech/cs2/interfaces/IGraph.java @@ -14,7 +14,8 @@ public interface IGraph<V, E> { * @param e The edge to add. * @throws IllegalArgumentException * If src and dest are not valid vertices (eg. refers to vertices not in the graph). - * @return true If src and dest were not already present; false if it was (in which case the graph is still updated). + * @return true if an edge from src to dest did not exist previously. + * false if an edge from src to dest did exist previously, in which case the edge value is updated */ public boolean addEdge(V src, V dest, E e); @@ -23,16 +24,18 @@ public interface IGraph<V, E> { * * @param e The edge to add. * @throws IllegalArgumentException - * If src and dest are not valid vertices (eg. refers to vertices not in the graph). - * @return true If src and dest were not already present in either direction; false if it was (in which case the graph is still updated). + * If v1 and v2 are not valid vertices (eg. refers to vertices not in the graph). + * @return true if an edge between src and dest did not exist in either direction. + * false if an edge between src and dest existed in either direction, in which case the edge value is + * updated. */ - public boolean addUndirectedEdge(V src, V dest, E e); + public boolean addUndirectedEdge(V v1, V v2, E e); /** * Remove an edge from src to dest from the graph. * * @throws IllegalArgumentException if src or dest is not in the graph. - * @return true if an edge from src to dest edge was present. + * @return true if an edge from src to dest was present. */ public boolean removeEdge(V src, V dest); diff --git a/tests/edu/caltech/cs2/helpers/Inspection.java b/tests/edu/caltech/cs2/helpers/Inspection.java index cdf089e..c3f1fca 100644 --- a/tests/edu/caltech/cs2/helpers/Inspection.java +++ b/tests/edu/caltech/cs2/helpers/Inspection.java @@ -18,11 +18,11 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.fail; public class Inspection { - private static String getUsageOf(List<String> regexps, List<? extends Node> codeObjects) { + private static String[] getUsageOf(List<String> regexps, List<? extends Node> codeObjects) { for (Node d : codeObjects) { for (String regex : regexps) { if (d.toString().replaceAll("\\R", "").matches(".*" + regex + ".*")) { - return regex; + return new String[]{d.toString().replaceAll("\\R", ""), regex}; } } } @@ -32,9 +32,25 @@ public class Inspection { public static void assertNoImportsOf(String filePath, List<String> regexps) { try { CompilationUnit cu = JavaParser.parse(new File(filePath)); - String usage = getUsageOf(regexps, cu.getImports()); + String[] usage = getUsageOf(regexps, cu.getImports()); if (usage != null) { - fail("You may not import " + usage + " in " + Paths.get(filePath).getFileName() + "."); + fail(usage[0] + " is a banned import under " + usage[1] + " in " + Paths.get(filePath).getFileName() + + "."); + } + } catch (FileNotFoundException e) { + fail("Missing Java file: " + Paths.get(filePath).getFileName()); + } + } + + public static void assertNoImportsOfExcept(String filePath, String bannedImport, List<String> allowedClasses) { + try { + CompilationUnit cu = JavaParser.parse(new File(filePath)); + String bannedRegex = bannedImport + "\\.(?!" + String.join("|", allowedClasses) + ")"; + String[] usage = getUsageOf(List.of(bannedRegex), cu.getImports()); + if (usage != null) { + fail(usage[0] + " is a banned import under " + bannedImport + + " and is not an allowed import " + + allowedClasses + " in " + Paths.get(filePath).getFileName() + "."); } } catch (FileNotFoundException e) { fail("Missing Java file: " + Paths.get(filePath).getFileName()); @@ -66,16 +82,16 @@ public class Inspection { List<ConstructorDeclaration> constructors = new ArrayList<>(); CONSTRUCTOR_COLLECTOR.visit(cu, constructors); - String usage = getUsageOf(regexps, constructors); + String[] usage = getUsageOf(regexps, constructors); if (usage != null) { - fail("You may not use " + usage + " in " + Paths.get(filePath).getFileName() + "."); + fail("You may not use " + usage[1] + " in " + Paths.get(filePath).getFileName() + "."); } List<MethodDeclaration> methods = new ArrayList<>(); METHOD_COLLECTOR.visit(cu, methods); usage = getUsageOf(regexps, methods); if (usage != null) { - fail("You may not use " + usage + " in " + Paths.get(filePath).getFileName() + "."); + fail("You may not use " + usage[1] + " in " + Paths.get(filePath).getFileName() + "."); } } catch (FileNotFoundException e) { fail("Missing Java file: " + Paths.get(filePath).getFileName()); @@ -93,8 +109,7 @@ public class Inspection { List<Statement> statements = body.getStatements(); if (statements.size() != 1) { nonThisConstructors++; - } - else if (!statements.get(0).toString().startsWith("this(")) { + } else if (!statements.get(0).toString().startsWith("this(")) { nonThisConstructors++; } diff --git a/tests/edu/caltech/cs2/project08/BeavermapTests.java b/tests/edu/caltech/cs2/project08/BeavermapTests.java index b4ef8be..a592cc1 100644 --- a/tests/edu/caltech/cs2/project08/BeavermapTests.java +++ b/tests/edu/caltech/cs2/project08/BeavermapTests.java @@ -47,11 +47,23 @@ public class BeavermapTests { @DisplayName("Does not use or import disallowed classes") @Test public void testForInvalidClasses() { - List<String> graphDisallow = List.of("java\\.util\\.(?!Iterator)", "java\\.lang\\.reflect"); + List<String> graphDisallow = List.of("java\\.lang\\.reflect"); Inspection.assertNoImportsOf(BEAVERMAP_GRAPH_SOURCE, graphDisallow); Inspection.assertNoUsageOf(BEAVERMAP_GRAPH_SOURCE, graphDisallow); } + @Order(1) + @DisplayName("Does not use or import disallowed classes from java.util") + @Test + @Tag("C") + 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") diff --git a/tests/edu/caltech/cs2/project08/GraphTests.java b/tests/edu/caltech/cs2/project08/GraphTests.java index c15c08c..fb2231c 100644 --- a/tests/edu/caltech/cs2/project08/GraphTests.java +++ b/tests/edu/caltech/cs2/project08/GraphTests.java @@ -11,6 +11,7 @@ import edu.caltech.cs2.interfaces.IGraph; import org.hamcrest.MatcherAssert; import org.hamcrest.collection.IsIterableContainingInAnyOrder; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -26,11 +27,23 @@ public class GraphTests { @DisplayName("Does not use or import disallowed classes") @Test public void testForInvalidClasses() { - List<String> graphDisallow = List.of("java\\.util\\.(?!Iterator)", "java\\.io", "java\\.lang\\.reflect"); + List<String> graphDisallow = List.of("java\\.io", "java\\.lang\\.reflect"); Inspection.assertNoImportsOf(GRAPH_SOURCE, graphDisallow); Inspection.assertNoUsageOf(GRAPH_SOURCE, graphDisallow); } + @Order(1) + @DisplayName("Does not use or import disallowed classes from java.util") + @Test + @Tag("C") + public void testForInvalidImportsJavaUtil() { + List<String> allowed = List.of("Iterator"); + Inspection.assertNoImportsOfExcept(GRAPH_SOURCE, "java\\.util", allowed); + + List<String> bannedUsages = List.of("java\\.util\\.(?!" + String.join("|", allowed) + ")"); + Inspection.assertNoUsageOf(GRAPH_SOURCE, bannedUsages); + } + @Test public void emptyGraphTest() { IGraph<String, String> g = new Graph<>(); -- GitLab From e8ac544e7975e7da038b6e553047e12172bb1428 Mon Sep 17 00:00:00 2001 From: Ethan Ordentlich <eordentl@Caltech.edu> Date: Thu, 25 Feb 2021 03:06:41 -0800 Subject: [PATCH 5/9] Test updates for graphs --- .../caltech/cs2/project08/DijkstraTest.java | 4 +- .../edu/caltech/cs2/project08/GraphMaker.java | 65 ++++---- .../edu/caltech/cs2/project08/GraphTests.java | 140 +++++++++--------- 3 files changed, 108 insertions(+), 101 deletions(-) diff --git a/tests/edu/caltech/cs2/project08/DijkstraTest.java b/tests/edu/caltech/cs2/project08/DijkstraTest.java index 533f4dd..e159e6c 100644 --- a/tests/edu/caltech/cs2/project08/DijkstraTest.java +++ b/tests/edu/caltech/cs2/project08/DijkstraTest.java @@ -31,7 +31,7 @@ public class DijkstraTest { @DisplayName("Loop path should be singleton") @Test public void dijkstraShortTripTest() { - IDeque<Location> res = GraphMaker.transformToLocations(GraphMaker.graph4(10)).dijkstra(new Location(1), new Location(1)); + IDeque<Location> res = GraphMaker.transformToLocations(GraphMaker.generateCompleteGraph(10)).dijkstra(new Location(1), new Location(1)); assertEquals(1, res.size(), "Path from a node to itself should only include the node once"); assertEquals(res.peek(), new Location(1), "Path from a node to itself should only include the node once"); } @@ -40,7 +40,7 @@ public class DijkstraTest { @DisplayName("Disconnected graph should not have a path") @Test public void dijkstraDisconnectedTest() { - IDeque<Location> res = GraphMaker.transformToLocations(GraphMaker.graph5(10)).dijkstra(new Location(1), new Location(9)); + IDeque<Location> res = GraphMaker.transformToLocations(GraphMaker.generateDisjointCompleteGraphs(10)).dijkstra(new Location(1), new Location(9)); assertNull(res, "Disconnected graph should give null path"); } diff --git a/tests/edu/caltech/cs2/project08/GraphMaker.java b/tests/edu/caltech/cs2/project08/GraphMaker.java index a4906f7..770da6d 100644 --- a/tests/edu/caltech/cs2/project08/GraphMaker.java +++ b/tests/edu/caltech/cs2/project08/GraphMaker.java @@ -23,8 +23,11 @@ public class GraphMaker { return ng; } - // A very simple graph. - public static IGraph<Integer, Integer> graph1() { + /** + * Generate a simple graph + * @return graph + */ + public static IGraph<Integer, Integer> generateSimpleGraph() { IGraph<Integer, Integer> g = new Graph<>(); g.addVertex(1); g.addVertex(2); @@ -35,8 +38,12 @@ public class GraphMaker { return g; } - // A linear graph on n vertices. - public static IGraph<Integer, Integer> graph2(int n) { + /** + * Generate a directed linear graph + * @param n - number of vertices in the linear graph + * @return graph, with edges labelled with the source vertex + */ + public static IGraph<Integer, Integer> generateLinearGraph(int n) { IGraph<Integer, Integer> g = new Graph<>(); for (int i = 0; i < n; i++) { assertTrue(g.addVertex(i), "Adding a new vertex should return true"); @@ -56,7 +63,7 @@ public class GraphMaker { } // Verify that a graph we're given looks reasonably like graph 2 - public static void graph2Test(IGraph<Integer, Integer> g, int n) { + public static void verifyLinearGraph(IGraph<Integer, Integer> g, int n) { vertexTest(g, n); for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1; j++) { @@ -69,8 +76,12 @@ public class GraphMaker { } } - // A tournament on n vertices; has an edge from i to j iff j<i - public static IGraph<Integer, Integer> graph3(int n) { + /** + * Construct a tournament graph on n vertices; has an edge from i to j iff j<i + * @param n - number of vertices + * @return graph + */ + public static IGraph<Integer, Integer> generateTournamentGraph(int n) { IGraph<Integer, Integer> g = new Graph<>(); for (int i = 0; i < n; i++) { assertTrue(g.addVertex(i), "Adding a new vertex should return true"); @@ -84,7 +95,7 @@ public class GraphMaker { } // Verify that a graph we're given looks reasonably like graph 3 - public static void graph3Test(IGraph<Integer, Integer> g, int n) { + public static void verifyTournamentGraph(IGraph<Integer, Integer> g, int n) { vertexTest(g, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { @@ -99,8 +110,12 @@ public class GraphMaker { } } - // A complete graph on n vertices - public static IGraph<Integer, Integer> graph4(int n) { + /** + * Construct a complete graph on n vertices + * @param n - number of vertices + * @return graph + */ + public static IGraph<Integer, Integer> generateCompleteGraph(int n) { IGraph<Integer, Integer> g = new Graph<>(); for (int i = 0; i < n; i++) { assertTrue(g.addVertex(i), "Adding a new vertex should return true"); @@ -117,7 +132,7 @@ public class GraphMaker { } // Verify that a graph we're given looks reasonably like graph 4 - public static void graph4Test(IGraph<Integer, Integer> g, int n) { + public static void verifyCompleteGraph(IGraph<Integer, Integer> g, int n) { vertexTest(g, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { @@ -133,8 +148,12 @@ public class GraphMaker { } } - // Two disjoint complete graphs. - public static IGraph<Integer, Integer> graph5(int n) { + /** + * Construct a graph that consists of 2 disjoint complete graphs, each with n vertices + * @param n - number of vertices in each complete component + * @return graph + */ + public static IGraph<Integer, Integer> generateDisjointCompleteGraphs(int n) { IGraph<Integer, Integer> g = new Graph<>(); for (int i = 0; i < n; i++) { assertTrue(g.addVertex(i), "Adding a new vertex should return true"); @@ -162,7 +181,7 @@ public class GraphMaker { } // Verify that a graph we're given looks reasonably like graph 5 - public static void graph5Test(IGraph<Integer, Integer> g, int n) { + public static void verifyDisjointCompleteGraphs(IGraph<Integer, Integer> g, int n) { vertexTest(g, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { @@ -176,22 +195,4 @@ public class GraphMaker { } } } - - /* A graph on which Dijkstra's should fail on a search from - * vertex 0 to vertex 2. Optimal path is 0->3->2 (length 1), but Dijkstra's - * should give us 0->1->2 (length 2). - */ - public static IGraph<Integer, Integer> dijkstraFail() { - IGraph<Integer, Integer> g = new Graph<>(); - g.addVertex(0); - g.addVertex(1); - g.addVertex(2); - g.addVertex(3); - - g.addEdge(0, 1, 1); - g.addEdge(1, 2, 1); - g.addEdge(0, 3, 5); - g.addEdge(3, 2, -4); - return g; - } } \ No newline at end of file diff --git a/tests/edu/caltech/cs2/project08/GraphTests.java b/tests/edu/caltech/cs2/project08/GraphTests.java index fb2231c..c1d52ce 100644 --- a/tests/edu/caltech/cs2/project08/GraphTests.java +++ b/tests/edu/caltech/cs2/project08/GraphTests.java @@ -10,20 +10,19 @@ import edu.caltech.cs2.helpers.Inspection; import edu.caltech.cs2.interfaces.IGraph; import org.hamcrest.MatcherAssert; import org.hamcrest.collection.IsIterableContainingInAnyOrder; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Order; -import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; @Tag("C") +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class GraphTests { private static final String GRAPH_SOURCE = "src/edu/caltech/cs2/datastructures/Graph.java"; private static final int SIMPLE_OP_TIMEOUT_MS = 300; private static final int MEDIUM_OP_TIMEOUT_MS = 500; private static final int STRESS_OP_TIMEOUT_MS = 2200; + @Order(0) @DisplayName("Does not use or import disallowed classes") @Test public void testForInvalidClasses() { @@ -35,7 +34,6 @@ public class GraphTests { @Order(1) @DisplayName("Does not use or import disallowed classes from java.util") @Test - @Tag("C") public void testForInvalidImportsJavaUtil() { List<String> allowed = List.of("Iterator"); Inspection.assertNoImportsOfExcept(GRAPH_SOURCE, "java\\.util", allowed); @@ -44,6 +42,8 @@ public class GraphTests { Inspection.assertNoUsageOf(GRAPH_SOURCE, bannedUsages); } + @Order(2) + @DisplayName("Test empty graph") @Test public void emptyGraphTest() { IGraph<String, String> g = new Graph<>(); @@ -52,9 +52,24 @@ public class GraphTests { ); } + @Test + @Order(3) + @DisplayName("Test creating various graphs") + public void creationTest() { + assertTimeout(Duration.ofMillis(MEDIUM_OP_TIMEOUT_MS), () -> { + GraphMaker.generateSimpleGraph(); + GraphMaker.verifyLinearGraph(GraphMaker.generateLinearGraph(100), 100); + GraphMaker.verifyTournamentGraph(GraphMaker.generateTournamentGraph(100), 100); + GraphMaker.verifyCompleteGraph(GraphMaker.generateCompleteGraph(100), 100); + GraphMaker.verifyDisjointCompleteGraphs(GraphMaker.generateDisjointCompleteGraphs(100), 100); + }); + } + /** * Ensure that we can create a small graph with some edges. */ + @Order(3) + @DisplayName("Test creating a small directed graph") @Test public void secondCreateTest() { IGraph<String, Integer> g = new Graph<>(); @@ -82,7 +97,9 @@ public class GraphTests { }); } + @Order(4) @Test + @DisplayName("Adding an edge with both endpoints missing should fail") public void addIllegalEdgeTest() { // Test adding edge where neither src, dest exists IGraph<String, Integer> g = new Graph<>(); @@ -90,6 +107,8 @@ public class GraphTests { } @Test + @Order(4) + @DisplayName("Adding an edge with dest missing should fail") public void addIllegalEdgeTest2() { // Test adding edge where dest does not exist IGraph<String, Integer> g = new Graph<>(); @@ -98,6 +117,8 @@ public class GraphTests { } @Test + @Order(4) + @DisplayName("Adding an edge with src missing should fail") public void addIllegalEdgeTest3() { // Test adding edge where src does not exist IGraph<String, Integer> g = new Graph<>(); @@ -106,6 +127,8 @@ public class GraphTests { } @Test + @Order(5) + @DisplayName("Test building a simple graph with edges and failures to add edges") public void simpleAddTest() { assertTimeout(Duration.ofMillis(SIMPLE_OP_TIMEOUT_MS), () -> { IGraph<String, Integer> g = new Graph<>(); @@ -138,6 +161,8 @@ public class GraphTests { } @Test + @Order(6) + @DisplayName("Test removing edges from the graph") public void simpleRemoveTest() { assertTimeout(Duration.ofMillis(SIMPLE_OP_TIMEOUT_MS), () -> { IGraph<String, Integer> g = new Graph<>(); @@ -152,33 +177,36 @@ public class GraphTests { } @Test - public void creationTest() { - assertTimeout(Duration.ofMillis(MEDIUM_OP_TIMEOUT_MS), () -> { - GraphMaker.graph1(); - GraphMaker.graph2Test(GraphMaker.graph2(100), 100); - GraphMaker.graph3Test(GraphMaker.graph3(100), 100); - GraphMaker.graph4Test(GraphMaker.graph4(100), 100); - GraphMaker.graph5Test(GraphMaker.graph5(100), 100); - }); - } + @Order(7) + @DisplayName("Test removing edges from the graph again") + public void stringEdgeTest() { + assertTimeout(Duration.ofMillis(SIMPLE_OP_TIMEOUT_MS), () -> { + IGraph<Integer, Integer> g = new Graph<>(); - /* Make sure that they test .equals, rather than ==, to determine - * if an edge is already there. */ - @Test - public void equalsTest() { - IGraph<Integer, Integer> g = new Graph<>(); - // Actually prevent the JVM from caching these values. - // This should be tested in the backing DSs anyway... - Integer i1 = 1000; - Integer i2 = 1000; - assertTrue(g.addVertex(i1), "Attempting to add missing vertex returned false"); - assertFalse(g.addVertex(i2), "Attempting to add present vertex returned true"); + assertTrue(g.addVertex(1), "Should be able to add vertex"); + assertTrue(g.addVertex(2), "Should be able to add vertex"); + assertTrue(g.addVertex(3), "Should be able to add vertex"); + assertTrue(g.addEdge(1, 2, 0), "Should be able to add edge"); + assertTrue(g.addEdge(1, 3, 0), "Should be able to add edge"); + assertTrue(g.addEdge(2, 3, 0), "Should be able to add edge"); + assertNotNull(g.adjacent(1, 2), "Added edge should be present in graph"); + assertNotNull(g.adjacent(1, 3), "Added edge should be present in graph"); + assertNotNull(g.adjacent(2, 3), "Added edge should be present in graph"); + assertTrue(g.removeEdge(1, 2), "Should be able to remove an edge from the graph"); + assertFalse(g.removeEdge(1, 2), "Should not be able to remove already-removed edge"); + assertTrue(g.removeEdge(1, 3), "Should be able to remove an edge from the graph"); + assertFalse(g.removeEdge(1, 3), "Should not be able to remove already-removed edge"); + assertTrue(g.removeEdge(2, 3), "Should be able to remove an edge from the graph"); + assertFalse(g.removeEdge(2, 3), "Should not be able to remove already-removed edge"); + }); } @Test + @Order(8) + @DisplayName("Test that all edges in a complete graph are present") public void adjacentStressTest() { assertTimeout(Duration.ofMillis(STRESS_OP_TIMEOUT_MS), () -> { - IGraph<Integer, Integer> g = GraphMaker.graph4(400); + IGraph<Integer, Integer> g = GraphMaker.generateCompleteGraph(400); for (int i = 0; i < 400; i++) for (int j = 0; j < 400; j++) if (i != j) @@ -187,20 +215,25 @@ public class GraphTests { } @Test + @Order(9) + @DisplayName("Test that directed edges in a tournament graph are correct") public void testNeighbors() { assertTimeout(Duration.ofMillis(SIMPLE_OP_TIMEOUT_MS), () -> { - IGraph<Integer, Integer> g = GraphMaker.graph3(10); + IGraph<Integer, Integer> g = GraphMaker.generateTournamentGraph(10); Set<Integer> vertices = new HashSet<Integer>(); for (int i = 0; i < 10; i++) { - MatcherAssert.assertThat(g.neighbors(i), - IsIterableContainingInAnyOrder.containsInAnyOrder(vertices.toArray())); + MatcherAssert.assertThat("neighbors", g.neighbors(i), + IsIterableContainingInAnyOrder.containsInAnyOrder(vertices.toArray())); + // In a tournament graph, vertices have edges to smaller ones vertices.add(i); } }); } @Test + @Order(10) + @DisplayName("Test adding undirected edges") public void undirectedEdgeTest() { assertTimeout(Duration.ofMillis(SIMPLE_OP_TIMEOUT_MS), () -> { IGraph<String, Integer> g = new Graph<>(); @@ -212,46 +245,19 @@ public class GraphTests { assertTrue(g.addVertex("3"), "Should be able to add a vertex"); assertEquals(3, g.vertices().size(), "Should have correct number of vertices"); - assertTrue(g.addUndirectedEdge("1", "2", 1), "Should be able to add new edge"); - assertFalse(g.addEdge("1", "2", 1), "Should not be able to add new edge"); - assertFalse(g.addEdge("1", "2", 2), "Should not be able to add new edge"); - assertFalse(g.addEdge("2", "1", 1), "Should not be able to add new edge"); - assertFalse(g.addEdge("2", "1", 2), "Should not be able to add new edge"); - - assertTrue(g.addUndirectedEdge("1", "3", 1), "Should be able to add new edge"); - assertFalse(g.addEdge("1", "3", 1), "Should not be able to add new edge"); - assertFalse(g.addEdge("1", "3", 2), "Should not be able to add new edge"); - assertFalse(g.addEdge("3", "1", 1), "Should not be able to add new edge"); - assertFalse(g.addEdge("3", "1", 2), "Should not be able to add new edge"); - - assertTrue(g.addUndirectedEdge("2", "3", 1), "Should be able to add new edge"); - assertFalse(g.addEdge("2", "3", 1), "Should not be able to add new edge"); - assertFalse(g.addEdge("2", "3", 2), "Should not be able to add new edge"); - assertFalse(g.addEdge("3", "2", 1), "Should not be able to add new edge"); - assertFalse(g.addEdge("3", "2", 2), "Should not be able to add new edge"); - }); - } + assertTrue(g.addUndirectedEdge("1", "2", 1), "Adding undirected edge when neither direction exists should return true"); + assertFalse(g.addEdge("1", "2", 1), "Should not be able to add directed edge"); + assertFalse(g.addEdge("2", "1", 1), "Should not be able to add directed edge"); - @Test - public void stringEdgeTest() { - assertTimeout(Duration.ofMillis(SIMPLE_OP_TIMEOUT_MS), () -> { - IGraph<Integer, Integer> g = new Graph<>(); + assertTrue(g.addEdge("1", "3", 1), "Should be able to add new edge"); + assertFalse(g.addUndirectedEdge("3", "1", 2), "Adding an undirected edge when one direction exists should return false"); + assertEquals(g.adjacent("3", "1"), 2, "Adding an undirected edge when one direction exists should update existing edges"); + assertEquals(g.adjacent("1", "3"), 2, "Adding an undirected edge when one direction exists should update existing edges"); - assertTrue(g.addVertex(1), "Should be able to add vertex"); - assertTrue(g.addVertex(2), "Should be able to add vertex"); - assertTrue(g.addVertex(3), "Should be able to add vertex"); - assertTrue(g.addEdge(1, 2, 0), "Should be able to add edge"); - assertTrue(g.addEdge(1, 3, 0), "Should be able to add edge"); - assertTrue(g.addEdge(2, 3, 0), "Should be able to add edge"); - assertNotNull(g.adjacent(1, 2), "Added edge should be present in graph"); - assertNotNull(g.adjacent(1, 3), "Added edge should be present in graph"); - assertNotNull(g.adjacent(2, 3), "Added edge should be present in graph"); - assertTrue(g.removeEdge(1, 2), "Should be able to remove an edge from the graph"); - assertFalse(g.removeEdge(1, 2), "Should not be able to remove already-removed edge"); - assertTrue(g.removeEdge(1, 3), "Should be able to remove an edge from the graph"); - assertFalse(g.removeEdge(1, 3), "Should not be able to remove already-removed edge"); - assertTrue(g.removeEdge(2, 3), "Should be able to remove an edge from the graph"); - assertFalse(g.removeEdge(2, 3), "Should not be able to remove already-removed edge"); + assertTrue(g.addEdge("2", "3", 1), "Should be able to add new directed edge"); + assertFalse(g.addUndirectedEdge("2", "3", 2), "Adding an undirected edge when one direction exists should return false"); + assertEquals(g.adjacent("3", "2"), 2, "Adding an undirected edge when one direction exists should update existing edges"); + assertEquals(g.adjacent("2", "3"), 2, "Adding an undirected edge when one direction exists should update existing edges"); }); } } \ No newline at end of file -- GitLab From 5e8ef6273537f89ceafd82c33b2cc3b381fff20d Mon Sep 17 00:00:00 2001 From: Ethan Ordentlich <eordentl@Caltech.edu> Date: Thu, 25 Feb 2021 03:15:04 -0800 Subject: [PATCH 6/9] Update marvel tests --- .idea/runConfigurations/A_Tests.xml | 13 +++++++++++++ .idea/runConfigurations/B_Tests.xml | 13 +++++++++++++ .idea/runConfigurations/C_Tests.xml | 13 +++++++++++++ .idea/runConfigurations/Graph_Tests__C_.xml | 12 ++++++++++++ .idea/runConfigurations/Marvel_Tests__C_.xml | 12 ++++++++++++ tests/edu/caltech/cs2/project08/MarvelTests.java | 5 +++++ 6 files changed, 68 insertions(+) create mode 100644 .idea/runConfigurations/A_Tests.xml create mode 100644 .idea/runConfigurations/B_Tests.xml create mode 100644 .idea/runConfigurations/C_Tests.xml create mode 100644 .idea/runConfigurations/Graph_Tests__C_.xml create mode 100644 .idea/runConfigurations/Marvel_Tests__C_.xml diff --git a/.idea/runConfigurations/A_Tests.xml b/.idea/runConfigurations/A_Tests.xml new file mode 100644 index 0000000..5eef670 --- /dev/null +++ b/.idea/runConfigurations/A_Tests.xml @@ -0,0 +1,13 @@ +<component name="ProjectRunConfigurationManager"> + <configuration default="false" name="A Tests" type="JUnit" factoryName="JUnit"> + <module name="project08-beavermaps" /> + <option name="MAIN_CLASS_NAME" value="" /> + <option name="METHOD_NAME" value="" /> + <option name="TEST_OBJECT" value="tags" /> + <option name="PARAMETERS" value="" /> + <tag value="A" /> + <method v="2"> + <option name="Make" enabled="true" /> + </method> + </configuration> +</component> \ No newline at end of file diff --git a/.idea/runConfigurations/B_Tests.xml b/.idea/runConfigurations/B_Tests.xml new file mode 100644 index 0000000..0cb51c1 --- /dev/null +++ b/.idea/runConfigurations/B_Tests.xml @@ -0,0 +1,13 @@ +<component name="ProjectRunConfigurationManager"> + <configuration default="false" name="B Tests" type="JUnit" factoryName="JUnit"> + <module name="project08-beavermaps" /> + <option name="MAIN_CLASS_NAME" value="" /> + <option name="METHOD_NAME" value="" /> + <option name="TEST_OBJECT" value="tags" /> + <option name="PARAMETERS" value="" /> + <tag value="B" /> + <method v="2"> + <option name="Make" enabled="true" /> + </method> + </configuration> +</component> \ No newline at end of file diff --git a/.idea/runConfigurations/C_Tests.xml b/.idea/runConfigurations/C_Tests.xml new file mode 100644 index 0000000..d2b12a9 --- /dev/null +++ b/.idea/runConfigurations/C_Tests.xml @@ -0,0 +1,13 @@ +<component name="ProjectRunConfigurationManager"> + <configuration default="false" name="C Tests" type="JUnit" factoryName="JUnit"> + <module name="project08-beavermaps" /> + <option name="MAIN_CLASS_NAME" value="" /> + <option name="METHOD_NAME" value="" /> + <option name="TEST_OBJECT" value="tags" /> + <option name="PARAMETERS" value="" /> + <tag value="C" /> + <method v="2"> + <option name="Make" enabled="true" /> + </method> + </configuration> +</component> \ No newline at end of file diff --git a/.idea/runConfigurations/Graph_Tests__C_.xml b/.idea/runConfigurations/Graph_Tests__C_.xml new file mode 100644 index 0000000..4a81989 --- /dev/null +++ b/.idea/runConfigurations/Graph_Tests__C_.xml @@ -0,0 +1,12 @@ +<component name="ProjectRunConfigurationManager"> + <configuration default="false" name="Graph Tests (C)" type="JUnit" factoryName="JUnit"> + <module name="project08-beavermaps" /> + <option name="PACKAGE_NAME" value="edu.caltech.cs2.project08" /> + <option name="MAIN_CLASS_NAME" value="edu.caltech.cs2.project08.GraphTests" /> + <option name="METHOD_NAME" value="" /> + <option name="TEST_OBJECT" value="class" /> + <method v="2"> + <option name="Make" enabled="true" /> + </method> + </configuration> +</component> \ No newline at end of file diff --git a/.idea/runConfigurations/Marvel_Tests__C_.xml b/.idea/runConfigurations/Marvel_Tests__C_.xml new file mode 100644 index 0000000..25193a3 --- /dev/null +++ b/.idea/runConfigurations/Marvel_Tests__C_.xml @@ -0,0 +1,12 @@ +<component name="ProjectRunConfigurationManager"> + <configuration default="false" name="Marvel Tests (C)" type="JUnit" factoryName="JUnit"> + <module name="project08-beavermaps" /> + <option name="PACKAGE_NAME" value="edu.caltech.cs2.project08" /> + <option name="MAIN_CLASS_NAME" value="edu.caltech.cs2.project08.MarvelTests" /> + <option name="METHOD_NAME" value="" /> + <option name="TEST_OBJECT" value="class" /> + <method v="2"> + <option name="Make" enabled="true" /> + </method> + </configuration> +</component> \ No newline at end of file diff --git a/tests/edu/caltech/cs2/project08/MarvelTests.java b/tests/edu/caltech/cs2/project08/MarvelTests.java index c1f5df3..11a3c19 100644 --- a/tests/edu/caltech/cs2/project08/MarvelTests.java +++ b/tests/edu/caltech/cs2/project08/MarvelTests.java @@ -13,6 +13,7 @@ import java.util.*; import static org.junit.jupiter.api.Assertions.*; @Tag("C") +@DisplayName("Test graph of shared appearances in Marvel comic books") public class MarvelTests { private static Graph<String, Integer> MARVEL_GRAPH; private static final int COMMON_TIMEOUT_MS = 1500; @@ -52,6 +53,7 @@ public class MarvelTests { @Test @Order(0) + @DisplayName("Test that characters with no co-appearances do not have neighbors - neighbor isEmpty") public void testSingletons() { List<String> singletons = new ArrayList<>(); for (String c : MARVEL_GRAPH.vertices()) { @@ -64,6 +66,7 @@ public class MarvelTests { @Test @Order(1) + @DisplayName("Look for characters that appear with many other characters - neighbor size") public void testPopular() { List<String> populars = new ArrayList<>(); for (String c : MARVEL_GRAPH.vertices()) { @@ -76,6 +79,7 @@ public class MarvelTests { @Test @Order(2) + @DisplayName("Test that there are no loops (self-edges) in the graph") public void testNoLoops() { for (String c : MARVEL_GRAPH.vertices()) { assertNull(MARVEL_GRAPH.adjacent(c, c), @@ -87,6 +91,7 @@ public class MarvelTests { // THIS TEST MUST BE RUN LAST - IT HAS SIDE EFFECTS ON THE STATIC GRAPH @Test @Order(3) + @DisplayName("Look for co-appearances that appear in many issues together - edge weights") public void testCommon() { Assertions.assertTimeout(Duration.ofMillis(COMMON_TIMEOUT_MS), this::runCommon); } -- GitLab From 99890b197e37504a0ca16f384d9e79af5e6b68fc Mon Sep 17 00:00:00 2001 From: Ethan Ordentlich <eordentl@Caltech.edu> Date: Thu, 25 Feb 2021 03:34:32 -0800 Subject: [PATCH 7/9] update beavermaps and dijkstra tests --- .../BeaverMapsGraph_Tests__C_.xml | 12 ++++ .../cs2/datastructures/BeaverMapsGraph.java | 3 +- .../caltech/cs2/project08/BeavermapTests.java | 64 +++++++++++++------ .../caltech/cs2/project08/DijkstraTest.java | 37 ++++++----- .../edu/caltech/cs2/project08/GraphMaker.java | 10 +-- .../edu/caltech/cs2/project08/GraphTests.java | 14 ++-- .../caltech/cs2/project08/MarvelTests.java | 2 +- 7 files changed, 88 insertions(+), 54 deletions(-) create mode 100644 .idea/runConfigurations/BeaverMapsGraph_Tests__C_.xml diff --git a/.idea/runConfigurations/BeaverMapsGraph_Tests__C_.xml b/.idea/runConfigurations/BeaverMapsGraph_Tests__C_.xml new file mode 100644 index 0000000..1a7e0ad --- /dev/null +++ b/.idea/runConfigurations/BeaverMapsGraph_Tests__C_.xml @@ -0,0 +1,12 @@ +<component name="ProjectRunConfigurationManager"> + <configuration default="false" name="BeaverMapsGraph Tests (C)" type="JUnit" factoryName="JUnit"> + <module name="project08-beavermaps" /> + <option name="MAIN_CLASS_NAME" value="" /> + <option name="METHOD_NAME" value="" /> + <option name="TEST_OBJECT" value="tags" /> + <tag value="C & Beavermap" /> + <method v="2"> + <option name="Make" enabled="true" /> + </method> + </configuration> +</component> \ No newline at end of file diff --git a/src/edu/caltech/cs2/datastructures/BeaverMapsGraph.java b/src/edu/caltech/cs2/datastructures/BeaverMapsGraph.java index 96520b1..757eb66 100644 --- a/src/edu/caltech/cs2/datastructures/BeaverMapsGraph.java +++ b/src/edu/caltech/cs2/datastructures/BeaverMapsGraph.java @@ -75,8 +75,7 @@ public class BeaverMapsGraph extends Graph<Long, Double> { } /** - * Returns a set of locations which are no more than threshold feet - * away from start. + * Returns a set of locations which are reachable along a path that goes no further than `threshold` feet from start * @param start the location to search around * @param threshold the number of feet in the search radius * @return diff --git a/tests/edu/caltech/cs2/project08/BeavermapTests.java b/tests/edu/caltech/cs2/project08/BeavermapTests.java index a592cc1..081a62b 100644 --- a/tests/edu/caltech/cs2/project08/BeavermapTests.java +++ b/tests/edu/caltech/cs2/project08/BeavermapTests.java @@ -44,6 +44,26 @@ public class BeavermapTests { } @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() { @@ -52,10 +72,11 @@ public class BeavermapTests { Inspection.assertNoUsageOf(BEAVERMAP_GRAPH_SOURCE, graphDisallow); } - @Order(1) + @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); @@ -67,7 +88,10 @@ public class BeavermapTests { // 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", @@ -81,7 +105,10 @@ public class BeavermapTests { } @Tag("C") + @Tag("Beavermap") @Test + @Order(4) + @DisplayName("Test getLocationByName()") public void testGetLocationByName() { BeaverMapsGraph bmg = new BeaverMapsGraph( "data/caltech/caltech.buildings.json", @@ -97,12 +124,14 @@ public class BeavermapTests { } @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); @@ -117,12 +146,14 @@ public class BeavermapTests { } @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); @@ -138,8 +169,10 @@ public class BeavermapTests { } @Tag("C") - @DisplayName("Test addVertex() updates BeaverMapsGraph properly") + @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", @@ -150,33 +183,20 @@ public class BeavermapTests { 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); } } - @Tag("C") - @DisplayName("BeaverMapsGraph implements required public methods") - @Test - 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()))); - } - - // Note: Pasadena map is WAY TOO LARGE to test all edges, don't try @Tag("C") - @DisplayName("Tests nodes and edges in map for filename") + @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); @@ -204,6 +224,7 @@ public class BeavermapTests { } // 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) { @@ -221,6 +242,7 @@ public class BeavermapTests { "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( @@ -252,6 +274,7 @@ public class BeavermapTests { @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", @@ -284,6 +307,7 @@ public class BeavermapTests { "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); diff --git a/tests/edu/caltech/cs2/project08/DijkstraTest.java b/tests/edu/caltech/cs2/project08/DijkstraTest.java index e159e6c..047e86d 100644 --- a/tests/edu/caltech/cs2/project08/DijkstraTest.java +++ b/tests/edu/caltech/cs2/project08/DijkstraTest.java @@ -7,10 +7,7 @@ import edu.caltech.cs2.helpers.Reflection; import edu.caltech.cs2.interfaces.IDeque; import edu.caltech.cs2.interfaces.IGraph; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Order; -import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -25,13 +22,14 @@ import java.util.Scanner; import static org.junit.jupiter.api.Assertions.*; @Tag("A") +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class DijkstraTest { @Order(0) @DisplayName("Loop path should be singleton") @Test public void dijkstraShortTripTest() { - IDeque<Location> res = GraphMaker.transformToLocations(GraphMaker.generateCompleteGraph(10)).dijkstra(new Location(1), new Location(1)); + IDeque<Location> res = GraphMaker.transformToLocations(GraphMaker.completeGraph(10)).dijkstra(new Location(1), new Location(1)); assertEquals(1, res.size(), "Path from a node to itself should only include the node once"); assertEquals(res.peek(), new Location(1), "Path from a node to itself should only include the node once"); } @@ -40,27 +38,27 @@ public class DijkstraTest { @DisplayName("Disconnected graph should not have a path") @Test public void dijkstraDisconnectedTest() { - IDeque<Location> res = GraphMaker.transformToLocations(GraphMaker.generateDisjointCompleteGraphs(10)).dijkstra(new Location(1), new Location(9)); + IDeque<Location> res = GraphMaker.transformToLocations(GraphMaker.disjointCompleteGraphs(10)).dijkstra(new Location(1), new Location(9)); assertNull(res, "Disconnected graph should give null path"); } @Order(2) - @ParameterizedTest + @ParameterizedTest(name = "Graph: {0}, start: {1}, end: {2}, trace file: {3}") @DisplayName("Tests correctness of Dijkstra implementation") @CsvSource({ - "graph1, 1, 3, simple_1_3", - "graph2, 0, 1, line_0_1", - "graph2, 0, 5, line_0_5", - "graph2, 0, 20, line_0_20", - "graph2, 0, 99, line_0_99", - "graph2, 1, 0, line_1_0", - "graph3, 0, 1, graph3_0_1", - "graph3, 99, 0, graph3_99_0" + "simpleGraph, 1, 3, simple_1_3", + "linearGraph, 0, 1, line_0_1", + "linearGraph, 0, 5, line_0_5", + "linearGraph, 0, 20, line_0_20", + "linearGraph, 0, 99, line_0_99", + "linearGraph, 1, 0, line_1_0", + "tournamentGraph, 0, 1, graph3_0_1", + "tournamentGraph, 99, 0, graph3_99_0" }) public void dijkstraTestGraph(String graphName, int start, int end, String traceFile) throws IllegalAccessException, InvocationTargetException, FileNotFoundException { BeaverMapsGraph bmg; - if (graphName.equals("graph1")) { + if (graphName.equals("simpleGraph")) { Method graphGen = Reflection.getMethod(GraphMaker.class, graphName); bmg = GraphMaker.transformToLocations( (IGraph<Integer, Integer>) graphGen.invoke(null)); @@ -121,6 +119,7 @@ public class DijkstraTest { two = (two + 1) % num_vertices; int dice = r.nextInt(5); + // TODO: y if (dice <= 4) { refg.addEdge(one, two, r.nextInt(100)); } else if (dice <= 5) { @@ -135,10 +134,10 @@ public class DijkstraTest { String line = s.nextLine(); if (line.equals("null")) { - assertNull(res, "Path does not exist from " + startvertex + " to " + endvertex + " but was found"); + assertNull(res, "Path does not exist from " + startvertex + " to " + endvertex + " but a path was found"); } else { - assertNotNull(res, "Path exists from " + startvertex + " to " + endvertex + " but was not found"); + assertNotNull(res, "Path exists from " + startvertex + " to " + endvertex + " but a path was not found"); double pathLen = 0; Location prev = null; for (Location l : res) { @@ -149,7 +148,7 @@ public class DijkstraTest { } double expectedLen = Double.parseDouble(line); - assertEquals(expectedLen, pathLen, "Path lengths are not equivalent"); + assertEquals(expectedLen, pathLen, "Path is not the shortest path by length"); } } } diff --git a/tests/edu/caltech/cs2/project08/GraphMaker.java b/tests/edu/caltech/cs2/project08/GraphMaker.java index 770da6d..2f1a5f2 100644 --- a/tests/edu/caltech/cs2/project08/GraphMaker.java +++ b/tests/edu/caltech/cs2/project08/GraphMaker.java @@ -27,7 +27,7 @@ public class GraphMaker { * Generate a simple graph * @return graph */ - public static IGraph<Integer, Integer> generateSimpleGraph() { + public static IGraph<Integer, Integer> simpleGraph() { IGraph<Integer, Integer> g = new Graph<>(); g.addVertex(1); g.addVertex(2); @@ -43,7 +43,7 @@ public class GraphMaker { * @param n - number of vertices in the linear graph * @return graph, with edges labelled with the source vertex */ - public static IGraph<Integer, Integer> generateLinearGraph(int n) { + public static IGraph<Integer, Integer> linearGraph(int n) { IGraph<Integer, Integer> g = new Graph<>(); for (int i = 0; i < n; i++) { assertTrue(g.addVertex(i), "Adding a new vertex should return true"); @@ -81,7 +81,7 @@ public class GraphMaker { * @param n - number of vertices * @return graph */ - public static IGraph<Integer, Integer> generateTournamentGraph(int n) { + public static IGraph<Integer, Integer> tournamentGraph(int n) { IGraph<Integer, Integer> g = new Graph<>(); for (int i = 0; i < n; i++) { assertTrue(g.addVertex(i), "Adding a new vertex should return true"); @@ -115,7 +115,7 @@ public class GraphMaker { * @param n - number of vertices * @return graph */ - public static IGraph<Integer, Integer> generateCompleteGraph(int n) { + public static IGraph<Integer, Integer> completeGraph(int n) { IGraph<Integer, Integer> g = new Graph<>(); for (int i = 0; i < n; i++) { assertTrue(g.addVertex(i), "Adding a new vertex should return true"); @@ -153,7 +153,7 @@ public class GraphMaker { * @param n - number of vertices in each complete component * @return graph */ - public static IGraph<Integer, Integer> generateDisjointCompleteGraphs(int n) { + public static IGraph<Integer, Integer> disjointCompleteGraphs(int n) { IGraph<Integer, Integer> g = new Graph<>(); for (int i = 0; i < n; i++) { assertTrue(g.addVertex(i), "Adding a new vertex should return true"); diff --git a/tests/edu/caltech/cs2/project08/GraphTests.java b/tests/edu/caltech/cs2/project08/GraphTests.java index c1d52ce..bdddca1 100644 --- a/tests/edu/caltech/cs2/project08/GraphTests.java +++ b/tests/edu/caltech/cs2/project08/GraphTests.java @@ -57,11 +57,11 @@ public class GraphTests { @DisplayName("Test creating various graphs") public void creationTest() { assertTimeout(Duration.ofMillis(MEDIUM_OP_TIMEOUT_MS), () -> { - GraphMaker.generateSimpleGraph(); - GraphMaker.verifyLinearGraph(GraphMaker.generateLinearGraph(100), 100); - GraphMaker.verifyTournamentGraph(GraphMaker.generateTournamentGraph(100), 100); - GraphMaker.verifyCompleteGraph(GraphMaker.generateCompleteGraph(100), 100); - GraphMaker.verifyDisjointCompleteGraphs(GraphMaker.generateDisjointCompleteGraphs(100), 100); + GraphMaker.simpleGraph(); + GraphMaker.verifyLinearGraph(GraphMaker.linearGraph(100), 100); + GraphMaker.verifyTournamentGraph(GraphMaker.tournamentGraph(100), 100); + GraphMaker.verifyCompleteGraph(GraphMaker.completeGraph(100), 100); + GraphMaker.verifyDisjointCompleteGraphs(GraphMaker.disjointCompleteGraphs(100), 100); }); } @@ -206,7 +206,7 @@ public class GraphTests { @DisplayName("Test that all edges in a complete graph are present") public void adjacentStressTest() { assertTimeout(Duration.ofMillis(STRESS_OP_TIMEOUT_MS), () -> { - IGraph<Integer, Integer> g = GraphMaker.generateCompleteGraph(400); + IGraph<Integer, Integer> g = GraphMaker.completeGraph(400); for (int i = 0; i < 400; i++) for (int j = 0; j < 400; j++) if (i != j) @@ -219,7 +219,7 @@ public class GraphTests { @DisplayName("Test that directed edges in a tournament graph are correct") public void testNeighbors() { assertTimeout(Duration.ofMillis(SIMPLE_OP_TIMEOUT_MS), () -> { - IGraph<Integer, Integer> g = GraphMaker.generateTournamentGraph(10); + IGraph<Integer, Integer> g = GraphMaker.tournamentGraph(10); Set<Integer> vertices = new HashSet<Integer>(); for (int i = 0; i < 10; i++) { diff --git a/tests/edu/caltech/cs2/project08/MarvelTests.java b/tests/edu/caltech/cs2/project08/MarvelTests.java index 11a3c19..06320c8 100644 --- a/tests/edu/caltech/cs2/project08/MarvelTests.java +++ b/tests/edu/caltech/cs2/project08/MarvelTests.java @@ -13,7 +13,7 @@ import java.util.*; import static org.junit.jupiter.api.Assertions.*; @Tag("C") -@DisplayName("Test graph of shared appearances in Marvel comic books") +@DisplayName("Test graph of shared appearances in Marvel comic books (MarvelTests)") public class MarvelTests { private static Graph<String, Integer> MARVEL_GRAPH; private static final int COMMON_TIMEOUT_MS = 1500; -- GitLab From 01e652a822bb5b0098f2815dd7bf5edd93ece600 Mon Sep 17 00:00:00 2001 From: Ethan Ordentlich <eordentl@Caltech.edu> Date: Thu, 25 Feb 2021 03:36:43 -0800 Subject: [PATCH 8/9] error messages go brrr --- tests/edu/caltech/cs2/project08/DijkstraTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/edu/caltech/cs2/project08/DijkstraTest.java b/tests/edu/caltech/cs2/project08/DijkstraTest.java index 047e86d..27b354c 100644 --- a/tests/edu/caltech/cs2/project08/DijkstraTest.java +++ b/tests/edu/caltech/cs2/project08/DijkstraTest.java @@ -77,10 +77,10 @@ public class DijkstraTest { String line = s.nextLine(); if (line.equals("null")) { - assertNull(res, "Path does not exist from " + start + " to " + end + " but was found"); + assertNull(res, "Path does not exist from " + start + " to " + end + " but a path was found"); } else { - assertNotNull(res, "Path exists from " + start + " to " + end + " but was not found"); + assertNotNull(res, "Path exists from " + start + " to " + end + " but a path was not found"); for (Location l : res) { if (prev != null) { pathLen += bmg.adjacent(prev.id, l.id); -- GitLab From d24edee87cff2c6181ee9afb89a2baeed510f9fc Mon Sep 17 00:00:00 2001 From: ashahidu <ashahidu@caltech.edu> Date: Thu, 25 Feb 2021 09:28:16 -0700 Subject: [PATCH 9/9] error messages go brrr again --- tests/edu/caltech/cs2/project08/BeavermapTests.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/edu/caltech/cs2/project08/BeavermapTests.java b/tests/edu/caltech/cs2/project08/BeavermapTests.java index 081a62b..248ce56 100644 --- a/tests/edu/caltech/cs2/project08/BeavermapTests.java +++ b/tests/edu/caltech/cs2/project08/BeavermapTests.java @@ -328,10 +328,10 @@ public class BeavermapTests { List<Long> actualPathIDs = new ArrayList<>(); if (expectedPathIDs.size() == 0) { - assertNull(actualPath, "Path does not exist from " + start.id + " to " + target.id + " but was found"); + 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 was not found"); + assertNotNull(actualPath, "Path exists from " + start.id + " to " + target.id + " but a path was not found"); for (Location l : actualPath) { actualPathIDs.add(l.id); } -- GitLab