Skip to content

Commit

Permalink
Unit test fidelity of graph implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
hxtk committed Nov 29, 2017
1 parent 5c5d202 commit b009ed1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/main/java/me/psanders/graph/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ java_library(
"//src/main/java/me/psanders:__pkg__",
"//src/main/java/me/psanders/graph/path:__pkg__",
"//src/test/java/me/psanders/graph/path:__pkg__",
"//src/test/java/me/psanders/graph:__pkg__",
]
)
17 changes: 17 additions & 0 deletions src/test/java/me/psanders/graph/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
java_library(
name = "utils_tests",
srcs = glob(["*.java"]),
deps = [
"//src/main/java/me/psanders/graph:graph",
"@junit//jar"
],
visibility = []
)

java_test(
name = "GraphTest",
size = "small",
runtime_deps = [
":utils_tests"
]
)
39 changes: 21 additions & 18 deletions src/test/java/me/psanders/graph/GraphTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright: Peter Sanders. All rights reserved.
// Date: 2017-10-13
// Date: 2017-11-29

package me.psanders.graph;

Expand All @@ -10,7 +10,10 @@

import me.psanders.graph.Graph;
import me.psanders.graph.MatrixGraph;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class GraphTest {

@Test
Expand All @@ -26,23 +29,23 @@ public void linkedGraphIsFaithful() {
// If this fails then our insertion functions do not match our retrieval functions
// i.e., `Graph.getWeight(X,Y)` searches
private void testFidelity(Graph graph) {
assertNull(graph.getWeight(0, 0));
assertNull(graph.getWeight(0, 3));
assertNull(graph.getWeight(1, 1));
assertNull(graph.getWeight(2, 0));
assertNull(graph.getWeight(2, 2));
assertNull(graph.getWeight(3, 1));
assertNull(graph.getWeight(3, 2));
assertNull(graph.getWeight(3, 3));

assertEquals(graph.getWeight(0, 1), 2);
assertEquals(graph.getWeight(0, 2), 3);
assertEquals(graph.getWeight(1, 0), 1);
assertEquals(graph.getWeight(1, 2), 2);
assertEquals(graph.getWeight(1, 3), 3);
assertEquals(graph.getWeight(2, 1), 3);
assertEquals(graph.getWeight(2, 3), 4);
assertEquals(graph.getWeight(3, 0), 1);
assertNull(graph.getWeight("a", "a"));
assertNull(graph.getWeight("a", "d"));
assertNull(graph.getWeight("b", "b"));
assertNull(graph.getWeight("c", "a"));
assertNull(graph.getWeight("c", "c"));
assertNull(graph.getWeight("d", "b"));
assertNull(graph.getWeight("d", "c"));
assertNull(graph.getWeight("d", "d"));

assertEquals(2, graph.getWeight("a", "b"));
assertEquals(3, graph.getWeight("a", "c"));
assertEquals(1, graph.getWeight("b", "a"));
assertEquals(2, graph.getWeight("b", "c"));
assertEquals(3, graph.getWeight("b", "d"));
assertEquals(3, graph.getWeight("c", "b"));
assertEquals(4, graph.getWeight("c", "d"));
assertEquals(1, graph.getWeight("d", "a"));
}

}
2 changes: 1 addition & 1 deletion src/test/java/me/psanders/graph/TestGraphFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Graph<String, Integer> getMatrixGraph() {

vertices.put("a", 0);
vertices.put("b", 1);
vertices.put("b", 2);
vertices.put("c", 2);
vertices.put("d", 3);

Integer[][] adjMatrix = {
Expand Down

0 comments on commit b009ed1

Please sign in to comment.