This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
sp-04/test/ch/usi/inf/sp/graph/DiGraphTest.java

147 lines
3.9 KiB
Java
Raw Normal View History

2023-10-18 19:57:49 +00:00
package ch.usi.inf.sp.graph;
import ch.usi.inf.sp.graph.DiGraph;
import ch.usi.inf.sp.graph.Edge;
import ch.usi.inf.sp.graph.Node;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class DiGraphTest {
@Test
void newDiGraph() {
DiGraph g = new DiGraph();
assertEquals(0, g.getNodes().size());
assertEquals(0, g.getEdges().size());
}
@Test
void toStringEmpty() {
DiGraph g = new DiGraph();
assertEquals("digraph G {\n}\n", g.toString());
}
@Test
void toStringOneEdge() {
DiGraph g = new DiGraph();
Node n1 = new Node();
Node n2 = new Node();
Edge e = new Edge();
g.addNode(n1);
g.addNode(n2);
g.addEdge(e);
g.connect(n1, e, n2);
assertEquals("digraph G {\n"+
" "+n1.toString()+"\n"+
" "+n2.toString()+"\n"+
" "+e.toString()+"\n"+
"}\n", g.toString());
}
@Test
void addNode() {
DiGraph g = new DiGraph();
Node n = new Node();
g.addNode(n);
assertEquals(1, g.getNodes().size());
assertSame(n, g.getNodes().get(0));
assertEquals(0, g.getEdges().size());
}
@Test
void addEdge() {
DiGraph g = new DiGraph();
Edge e = new Edge();
g.addEdge(e);
assertEquals(0, g.getNodes().size());
assertEquals(1, g.getEdges().size());
assertSame(e, g.getEdges().get(0));
}
@Test
void connect() {
DiGraph g = new DiGraph();
Edge e = new Edge();
Node n1 = new Node();
Node n2 = new Node();
g.addNode(n1);
g.addNode(n2);
g.addEdge(e);
g.connect(n1, e, n2);
assertEquals(2, g.getNodes().size());
assertTrue(g.getNodes().contains(n1));
assertTrue(g.getNodes().contains(n2));
assertEquals(1, g.getEdges().size());
assertSame(e, g.getEdges().get(0));
assertSame(e, n1.getOutEdges().get(0));
assertSame(e, n2.getInEdges().get(0));
assertSame(n1, e.getFrom());
assertSame(n2, e.getTo());
}
@Test
void connectEdgeNotContained() {
DiGraph g = new DiGraph();
Edge e = new Edge();
Node n1 = new Node();
Node n2 = new Node();
g.addNode(n1);
g.addNode(n2);
assertThrows(IllegalStateException.class, () -> g.connect(n1, e, n2));
}
@Test
void connectFromNodeNotContained() {
DiGraph g = new DiGraph();
Edge e = new Edge();
Node n1 = new Node();
Node n2 = new Node();
g.addNode(n2);
g.addEdge(e);
assertThrows(IllegalStateException.class, () -> g.connect(n1, e, n2));
}
@Test
void connectToNodeNotContained() {
DiGraph g = new DiGraph();
Edge e = new Edge();
Node n1 = new Node();
Node n2 = new Node();
g.addNode(n1);
g.addEdge(e);
assertThrows(IllegalStateException.class, () -> g.connect(n1, e, n2));
}
@Test
void connectSelfLoop() {
DiGraph g = new DiGraph();
Edge e = new Edge();
Node n = new Node();
g.addNode(n);
g.addEdge(e);
g.connect(n, e, n);
assertEquals(1, g.getNodes().size());
assertTrue(g.getNodes().contains(n));
assertEquals(1, g.getEdges().size());
assertSame(e, g.getEdges().get(0));
assertSame(e, n.getOutEdges().get(0));
assertSame(e, n.getInEdges().get(0));
assertSame(n, e.getFrom());
assertSame(n, e.getTo());
}
@Test
void connectSameEdgeTwice() {
DiGraph g = new DiGraph();
Edge e = new Edge();
Node n1 = new Node();
Node n2 = new Node();
g.addNode(n1);
g.addNode(n2);
g.addEdge(e);
g.connect(n1, e, n2);
assertThrows(IllegalStateException.class, () -> g.connect(n1, e, n2));
}
}