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-03/test/ch/usi/inf/sp/cfg/DiGraphTest.java
github-classroom[bot] 61202b8db3
Initial commit
2023-10-09 07:08:58 +00:00

121 lines
No EOL
3.2 KiB
Java

package ch.usi.inf.sp.cfg;
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 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));
}
}