Search in sources :

Example 1 with CaseStatement

use of de.fraunhofer.aisec.cpg.graph.statements.CaseStatement in project cpg by Fraunhofer-AISEC.

the class CXXLanguageFrontendTest method testSwitch.

@Test
void testSwitch() throws Exception {
    File file = new File("src/test/resources/cfg/switch.cpp");
    TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
    List<Node> graphNodes = SubgraphWalker.flattenAST(declaration);
    graphNodes.sort(new NodeComparator());
    assertTrue(graphNodes.size() != 0);
    List<SwitchStatement> switchStatements = Util.filterCast(graphNodes, SwitchStatement.class);
    assertTrue(switchStatements.size() == 3);
    SwitchStatement switchStatement = switchStatements.get(0);
    assertTrue(((CompoundStatement) switchStatement.getStatement()).getStatements().size() == 11);
    List<CaseStatement> caseStatements = Util.filterCast(SubgraphWalker.flattenAST(switchStatement), CaseStatement.class);
    assertTrue(caseStatements.size() == 4);
    List<DefaultStatement> defaultStatements = Util.filterCast(SubgraphWalker.flattenAST(switchStatement), DefaultStatement.class);
    assertTrue(defaultStatements.size() == 1);
}
Also used : SwitchStatement(de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement) NodeComparator(de.fraunhofer.aisec.cpg.helpers.NodeComparator) CaseStatement(de.fraunhofer.aisec.cpg.graph.statements.CaseStatement) Node(de.fraunhofer.aisec.cpg.graph.Node) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) DefaultStatement(de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

BaseTest (de.fraunhofer.aisec.cpg.BaseTest)1 Node (de.fraunhofer.aisec.cpg.graph.Node)1 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)1 CaseStatement (de.fraunhofer.aisec.cpg.graph.statements.CaseStatement)1 DefaultStatement (de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement)1 SwitchStatement (de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement)1 NodeComparator (de.fraunhofer.aisec.cpg.helpers.NodeComparator)1 File (java.io.File)1 Test (org.junit.jupiter.api.Test)1