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/ControlFlowGraphRendererTest.java
github-classroom[bot] 61202b8db3
Initial commit
2023-10-09 07:08:58 +00:00

100 lines
No EOL
5 KiB
Java

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");
assertTrue(result.contains("circle"), "must use circle 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("circle"), "must use circle 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("circle"), "must use circle 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|I11.2"), "must contain the sequence of instructions I11.1|I11.2 in the label of the first basic block");
assertTrue(result.contains("I22.1|I22.2"), "must contain the sequence of instructions I22.1|I22.2 in the label of the first basic block");
assertTrue(result.contains("record"), "must use a record shape for the basic block");
}
}