171 lines
5.8 KiB
Java
171 lines
5.8 KiB
Java
|
package ch.usi.inf.sp.cfg;
|
||
|
|
||
|
import org.junit.jupiter.api.Test;
|
||
|
|
||
|
import java.util.Collection;
|
||
|
import java.util.HashSet;
|
||
|
import java.util.Set;
|
||
|
|
||
|
import static org.junit.jupiter.api.Assertions.*;
|
||
|
|
||
|
class ControlFlowGraphTest {
|
||
|
|
||
|
private final void assertConnected(BasicBlock from, ControlFlowEdge e, BasicBlock to) {
|
||
|
assertTrue(from.getOutEdges().contains(e));
|
||
|
assertTrue(to.getInEdges().contains(e));
|
||
|
assertSame(from, e.getFrom());
|
||
|
assertSame(to, e.getTo());
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
void newControlFlowGraph() {
|
||
|
ControlFlowGraph g = new ControlFlowGraph();
|
||
|
assertNotNull(g.getEntry());
|
||
|
assertNotNull(g.getExit());
|
||
|
assertNotSame(g.getEntry(), g.getExit());
|
||
|
assertEquals(2, g.getNodes().size());
|
||
|
assertTrue(g.getNodes().contains(g.getEntry()));
|
||
|
assertTrue(g.getNodes().contains(g.getExit()));
|
||
|
assertEquals(0, g.getEdges().size());
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
void addEntryEdge() {
|
||
|
ControlFlowGraph g = new ControlFlowGraph();
|
||
|
BasicBlock bb = new BasicBlock(1);
|
||
|
g.addNode(bb);
|
||
|
ControlFlowEdge e = g.addEntryEdge(bb);
|
||
|
assertNotNull(e);
|
||
|
assertEquals(3, g.getNodes().size());
|
||
|
assertTrue(g.getNodes().contains(bb));
|
||
|
assertEquals(1, g.getEdges().size());
|
||
|
assertTrue(g.getEdges().contains(g.getEntry().getOutEdges().get(0)));
|
||
|
assertSame(bb, g.getEntry().getOutEdges().get(0).getTo());
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
void addEntryEdgeToNonContainedBasicBlock() {
|
||
|
ControlFlowGraph g = new ControlFlowGraph();
|
||
|
BasicBlock bb = new BasicBlock(1);
|
||
|
assertThrows(IllegalStateException.class, () -> g.addEntryEdge(bb));
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
void addTwoEntryEdges() {
|
||
|
ControlFlowGraph g = new ControlFlowGraph();
|
||
|
BasicBlock bb = new BasicBlock(1);
|
||
|
g.addNode(bb);
|
||
|
g.addEntryEdge(bb);
|
||
|
assertThrows(IllegalStateException.class, () -> g.addEntryEdge(bb));
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
void addExitEdge() {
|
||
|
ControlFlowGraph g = new ControlFlowGraph();
|
||
|
BasicBlock bb = new BasicBlock(1);
|
||
|
g.addNode(bb);
|
||
|
ControlFlowEdge e = g.addExitEdge(bb);
|
||
|
assertNotNull(e);
|
||
|
assertEquals(3, g.getNodes().size());
|
||
|
assertTrue(g.getNodes().contains(bb));
|
||
|
assertEquals(1, g.getEdges().size());
|
||
|
assertTrue(g.getEdges().contains(g.getExit().getInEdges().get(0)));
|
||
|
assertSame(bb, g.getExit().getInEdges().get(0).getFrom());
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
void addExitEdgeFromNonContainedBasicBlock() {
|
||
|
ControlFlowGraph g = new ControlFlowGraph();
|
||
|
BasicBlock bb = new BasicBlock(1);
|
||
|
assertThrows(IllegalStateException.class, () -> g.addExitEdge(bb));
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
void addTwoExitEdges() {
|
||
|
ControlFlowGraph g = new ControlFlowGraph();
|
||
|
BasicBlock bb = new BasicBlock(1);
|
||
|
g.addNode(bb);
|
||
|
g.addExitEdge(bb);
|
||
|
g.addExitEdge(bb);
|
||
|
assertEquals(3, g.getNodes().size());
|
||
|
assertTrue(g.getNodes().contains(bb));
|
||
|
assertEquals(2, g.getEdges().size());
|
||
|
assertTrue(g.getEdges().contains(g.getExit().getInEdges().get(0)));
|
||
|
assertTrue(g.getEdges().contains(g.getExit().getInEdges().get(1)));
|
||
|
assertSame(bb, g.getExit().getInEdges().get(0).getFrom());
|
||
|
assertSame(bb, g.getExit().getInEdges().get(1).getFrom());
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
void addFallthroughEdge() {
|
||
|
ControlFlowGraph g = new ControlFlowGraph();
|
||
|
BasicBlock from = new BasicBlock(1);
|
||
|
BasicBlock to = new BasicBlock(2);
|
||
|
g.addNode(from);
|
||
|
g.addNode(to);
|
||
|
ControlFlowEdge e = g.addFallthroughEdge(from, to);
|
||
|
assertNotNull(e);
|
||
|
assertEquals("", e.getLabel());
|
||
|
assertEquals(4, g.getNodes().size());
|
||
|
assertTrue(g.getNodes().contains(from));
|
||
|
assertTrue(g.getNodes().contains(to));
|
||
|
assertEquals(1, g.getEdges().size());
|
||
|
assertTrue(g.getEdges().contains(e));
|
||
|
assertConnected(from, e, to);
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
void addBranchTakenEdge() {
|
||
|
ControlFlowGraph g = new ControlFlowGraph();
|
||
|
BasicBlock from = new BasicBlock(1);
|
||
|
BasicBlock to = new BasicBlock(2);
|
||
|
g.addNode(from);
|
||
|
g.addNode(to);
|
||
|
ControlFlowEdge e = g.addBranchTakenEdge(from, to);
|
||
|
assertNotNull(e);
|
||
|
assertEquals("T", e.getLabel());
|
||
|
assertEquals(4, g.getNodes().size());
|
||
|
assertTrue(g.getNodes().contains(from));
|
||
|
assertTrue(g.getNodes().contains(to));
|
||
|
assertEquals(1, g.getEdges().size());
|
||
|
assertTrue(g.getEdges().contains(e));
|
||
|
assertConnected(from, e, to);
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
void addCaseEdge() {
|
||
|
ControlFlowGraph g = new ControlFlowGraph();
|
||
|
BasicBlock from = new BasicBlock(1);
|
||
|
BasicBlock to = new BasicBlock(2);
|
||
|
g.addNode(from);
|
||
|
g.addNode(to);
|
||
|
ControlFlowEdge e = g.addCaseEdge(from, to, 99);
|
||
|
assertNotNull(e);
|
||
|
assertEquals("99", e.getLabel());
|
||
|
assertEquals(4, g.getNodes().size());
|
||
|
assertTrue(g.getNodes().contains(from));
|
||
|
assertTrue(g.getNodes().contains(to));
|
||
|
assertEquals(1, g.getEdges().size());
|
||
|
assertTrue(g.getEdges().contains(e));
|
||
|
assertConnected(from, e, to);
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
void addDefaultEdge() {
|
||
|
ControlFlowGraph g = new ControlFlowGraph();
|
||
|
BasicBlock from = new BasicBlock(1);
|
||
|
BasicBlock to = new BasicBlock(2);
|
||
|
g.addNode(from);
|
||
|
g.addNode(to);
|
||
|
ControlFlowEdge e = g.addDefaultEdge(from, to);
|
||
|
assertNotNull(e);
|
||
|
assertEquals("default", e.getLabel());
|
||
|
assertEquals(4, g.getNodes().size());
|
||
|
assertTrue(g.getNodes().contains(from));
|
||
|
assertTrue(g.getNodes().contains(to));
|
||
|
assertEquals(1, g.getEdges().size());
|
||
|
assertTrue(g.getEdges().contains(e));
|
||
|
assertConnected(from, e, to);
|
||
|
}
|
||
|
|
||
|
}
|