-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
156 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
strict digraph G { | ||
A; | ||
B; | ||
C; | ||
D; | ||
Z; | ||
A -> B; | ||
A -> C; | ||
A -> D; | ||
Z -> C; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import org.jgrapht.graph.DefaultEdge; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class GraphDataTest { | ||
GraphData graphApi = new GraphData(); | ||
@BeforeEach | ||
public void init_graph() { | ||
assertTrue(graphApi.parseGraph("src/test/test_graph.dot")); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test initial graph parsing") | ||
public void TestParseGraph() { | ||
Set<String> nodes = new HashSet<String>(); | ||
nodes.add("A"); | ||
nodes.add("B"); | ||
nodes.add("C"); | ||
nodes.add("D"); | ||
assertEquals(nodes, graphApi.graphObject.vertexSet()); | ||
Set<String> edges = new HashSet<String>(); | ||
Set<String> expected_edges = new HashSet<String>(); | ||
edges.add("(A : B)"); | ||
edges.add("(A : C)"); | ||
edges.add("(A : D)"); | ||
for (DefaultEdge e: graphApi.graphObject.edgeSet()) { | ||
expected_edges.add(e.toString()); | ||
} | ||
assertEquals(expected_edges, edges); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test toString") | ||
public void TestToString() { | ||
String expected_value = "Number of nodes: 4\n" + | ||
"Node labels: [A, B, C, D]\n" + | ||
"Number of edges: 3\n" + | ||
"Node and edge directions: (A -> B), (A -> C), (A -> D)\n"; | ||
|
||
assertEquals(expected_value, graphApi.toString()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test outputGraph") | ||
public void TestOutputGraph() throws IOException { | ||
String expected_value = """ | ||
Number of nodes: 4 | ||
Node labels: [A, B, C, D] | ||
Number of edges: 3 | ||
Node and edge directions: (A -> B), (A -> C), (A -> D) | ||
"""; | ||
assertTrue(graphApi.outputGraph("src/test/output.txt")); | ||
assertEquals(expected_value, Files.readString(Paths.get("src/test/output.txt"))); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test outputGraph if node does not exist already") | ||
public void TestAddNode(){ | ||
assertTrue(graphApi.addNode("Z")); | ||
assertTrue(graphApi.graphObject.vertexSet().stream().anyMatch(v -> (v.equals("Z")))); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test outputGraph if node exists already") | ||
public void TestAddNodeIfExists(){ | ||
graphApi.addNode("Z"); | ||
assertFalse(graphApi.addNode("Z")); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test addition of multiple nodes") | ||
public void TestAddNodes(){ | ||
String[] nodes = {"Z", "X", "Y"}; | ||
graphApi.addNodes(nodes); | ||
for(String node:nodes) { | ||
assertTrue(graphApi.graphObject.vertexSet().stream().anyMatch(v -> (v.equals(node)))); | ||
} | ||
} | ||
|
||
@Test | ||
@DisplayName("Test addition of edge") | ||
public void TestAddEdge(){ | ||
assertTrue(graphApi.addEdge("B","C")); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test addition of alreardy existing edge") | ||
public void TestAddEdgeIfExists(){ | ||
assertFalse(graphApi.addEdge("A","C")); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test addition of edge") | ||
public void TestAddEdgeIfNodeDoesNotExist(){ | ||
assertFalse(graphApi.addEdge("Z","C")); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test DOT graph generation") | ||
public void TestOutputDOTGraph() throws IOException { | ||
graphApi.addNode("Z"); | ||
graphApi.addEdge("Z","C"); | ||
String expected_value = Files.readString(Paths.get("src/test/expected.dot")); | ||
assertTrue(graphApi.outputDOTGraph("src/test/gen_graph.dot")); | ||
assertEquals(expected_value, Files.readString(Paths.get("src/test/gen_graph.dot"))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
strict digraph D { | ||
|
||
A | ||
B | ||
C | ||
|
||
A -> B | ||
A -> C | ||
A -> D | ||
} |