package ch.usi.inf.sp.cfg; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; /** * Tests ControlFlowGraphRenderer by * asking it to render simple, artificially generated CFGs. * The test is VERY WEAK. * That is, there are many bugs it will not discover. */ class ControlFlowGraphRendererTest { @Test public void renderEmptyControlFlowGraph() { ControlFlowGraph cfg = new ControlFlowGraph(); String result = ControlFlowGraphRenderer.renderControlFlowGraph("THELABEL", cfg); //System.out.println(result); assertTrue(result.contains("THELABEL"), "must include the given label"); assertTrue(result.startsWith("digraph ")); assertTrue(result.contains("{")); assertTrue(result.contains("}")); assertTrue(result.contains("["), "must set properties of entry/exit nodes"); assertTrue(result.contains("]"), "must set properties of entry/exit nodes"); assertTrue(result.contains("label"), "must set label for graph, and for nodes"); assertTrue(result.contains("-1"), "must contain entry node's ID: -1"); assertTrue(result.contains("-2"), "must contain exit node's ID: -2"); assertTrue(result.contains("shape"), "must set shape for entry/exit nodes"); } @Test public void renderSingleBlockControlFlowGraph() { ControlFlowGraph cfg = new ControlFlowGraph(); BasicBlock bb = new BasicBlock(99); bb.appendInstruction("THEINSTRUCTION"); cfg.addNode(bb); cfg.addEntryEdge(bb); cfg.addExitEdge(bb); String result = ControlFlowGraphRenderer.renderControlFlowGraph("THELABEL", cfg); //System.out.println(result); assertTrue(result.contains("THELABEL"), "must include the given label"); assertTrue(result.startsWith("digraph ")); assertTrue(result.contains("{")); assertTrue(result.contains("}")); assertTrue(result.contains("["), "must set properties of entry/exit nodes"); assertTrue(result.contains("]"), "must set properties of entry/exit nodes"); assertTrue(result.contains("label"), "must set label for graph, and for nodes"); assertTrue(result.contains("-1"), "must contain entry node's ID: -1"); assertTrue(result.contains("-2"), "must contain exit node's ID: -2"); assertTrue(result.contains("shape"), "must set shape for entry/exit nodes"); assertTrue(result.contains("->"), "must contain an edge (->)"); assertTrue(result.contains("99"), "must include the basic block's ID (99)"); assertTrue(result.contains("THEINSTRUCTION"), "must contain THEINSTRUCTION in the label of the basic block"); assertTrue(result.contains("record"), "must use a record shape for the basic block"); } @Test public void renderTwoBlockControlFlowGraph() { ControlFlowGraph cfg = new ControlFlowGraph(); BasicBlock bb1 = new BasicBlock(11); bb1.appendInstruction("I11.1"); bb1.appendInstruction("I11.2"); cfg.addNode(bb1); BasicBlock bb2 = new BasicBlock(22); bb2.appendInstruction("I22.1"); bb2.appendInstruction("I22.2"); cfg.addNode(bb2); cfg.addEntryEdge(bb1); cfg.addFallthroughEdge(bb1, bb2); cfg.addExitEdge(bb2); String result = ControlFlowGraphRenderer.renderControlFlowGraph("THELABEL", cfg); //System.out.println(result); assertTrue(result.contains("THELABEL"), "must include the given label"); assertTrue(result.startsWith("digraph ")); assertTrue(result.contains("{")); assertTrue(result.contains("}")); assertTrue(result.contains("["), "must set properties of entry/exit nodes"); assertTrue(result.contains("]"), "must set properties of entry/exit nodes"); assertTrue(result.contains("label"), "must set label for graph, and for nodes"); assertTrue(result.contains("-1"), "must contain entry node's ID: -1"); assertTrue(result.contains("-2"), "must contain exit node's ID: -2"); assertTrue(result.contains("shape"), "must set shape for entry/exit nodes"); assertTrue(result.contains("->"), "must contain an edge (->)"); assertTrue(result.contains("11"), "must include the first basic block's ID (11)"); assertTrue(result.contains("22"), "must include the second basic block's ID (22)"); assertTrue(result.contains("I11.1"), "must contain instruction I11.1 in the label of the first basic block"); assertTrue(result.contains("I11.2"), "must contain instruction I11.2 in the label of the first basic block"); assertTrue(result.contains("I22.1"), "must contain instruction I22.1 in the label of the first basic block"); assertTrue(result.contains("I22.2"), "must contain instruction I22.2 in the label of the first basic block"); assertTrue(result.contains("record"), "must use a record shape for the basic block"); } }