Search in sources :

Example 16 with BytecodeInstruction

use of org.evosuite.graphs.cfg.BytecodeInstruction in project evosuite by EvoSuite.

the class TestCoverageGoalNameGeneration method testTwoTestsDifferOnlyInBranches.

@Test
public void testTwoTestsDifferOnlyInBranches() {
    Branch b1 = mock(Branch.class);
    BytecodeInstruction bi = mock(BytecodeInstruction.class);
    when(b1.getInstruction()).thenReturn(bi);
    TestCase test1 = new DefaultTestCase();
    MethodCoverageTestFitness methodGoal = new MethodCoverageTestFitness("FooClass", "toString");
    test1.addCoveredGoal(methodGoal);
    BranchCoverageTestFitness branchGoal1 = new BranchCoverageTestFitness(new BranchCoverageGoal(b1, true, "FooClass", "toStringBarFooBlubb", 0));
    test1.addCoveredGoal(branchGoal1);
    TestCase test2 = new DefaultTestCase();
    test2.addCoveredGoal(methodGoal);
    // Need to add statements to change hashCode
    test2.addStatement(new IntPrimitiveStatement(test2, 0));
    BranchCoverageTestFitness branchGoal2 = new BranchCoverageTestFitness(new BranchCoverageGoal(b1, false, "FooClass", "toString", 0));
    test2.addCoveredGoal(branchGoal2);
    List<TestCase> tests = new ArrayList<>();
    tests.add(test1);
    tests.add(test2);
    CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests);
    assertEquals("testToString0", naming.getName(test1));
    assertEquals("testToString1", naming.getName(test2));
}
Also used : BranchCoverageTestFitness(org.evosuite.coverage.branch.BranchCoverageTestFitness) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Branch(org.evosuite.coverage.branch.Branch) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) CoverageGoalTestNameGenerationStrategy(org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy) ArrayList(java.util.ArrayList) BytecodeInstruction(org.evosuite.graphs.cfg.BytecodeInstruction) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) BranchCoverageGoal(org.evosuite.coverage.branch.BranchCoverageGoal) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 17 with BytecodeInstruction

use of org.evosuite.graphs.cfg.BytecodeInstruction in project evosuite by EvoSuite.

the class LineCoverageSuiteFitness method initializeControlDependencies.

/**
 * Add guidance to the fitness function by including branch distances on
 * all control dependencies
 */
private void initializeControlDependencies() {
    // In case we target more than one class (context, or inner classes)
    Set<String> targetClasses = new LinkedHashSet<>();
    for (TestFitnessFunction ff : lineGoals.values()) {
        targetClasses.add(ff.getTargetClass());
    }
    for (String className : targetClasses) {
        List<BytecodeInstruction> instructions = BytecodeInstructionPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getInstructionsIn(className);
        if (instructions == null) {
            logger.info("No instructions known for class {} (is it an enum?)", className);
            continue;
        }
        for (BytecodeInstruction bi : instructions) {
            if (bi.getBasicBlock() == null) {
                // Labels get no basic block. TODO - why?
                continue;
            }
            // The order of CDs may be nondeterminstic
            // TODO: A better solution would be to make the CD order deterministic rather than sorting here
            List<ControlDependency> cds = new ArrayList<>(bi.getControlDependencies());
            Collections.sort(cds);
            for (ControlDependency cd : cds) {
                if (cd.getBranchExpressionValue()) {
                    branchesToCoverTrue.add(cd.getBranch().getActualBranchId());
                } else {
                    branchesToCoverFalse.add(cd.getBranch().getActualBranchId());
                }
            }
        }
    }
    branchesToCoverBoth.addAll(branchesToCoverTrue);
    branchesToCoverBoth.retainAll(branchesToCoverFalse);
    branchesToCoverTrue.removeAll(branchesToCoverBoth);
    branchesToCoverFalse.removeAll(branchesToCoverBoth);
    logger.info("Covering branches true: " + branchesToCoverTrue);
    logger.info("Covering branches false: " + branchesToCoverFalse);
    logger.info("Covering branches both: " + branchesToCoverBoth);
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) BytecodeInstruction(org.evosuite.graphs.cfg.BytecodeInstruction) ControlDependency(org.evosuite.graphs.cfg.ControlDependency)

Example 18 with BytecodeInstruction

use of org.evosuite.graphs.cfg.BytecodeInstruction in project evosuite by EvoSuite.

the class StatementCoverageFactory method computeGoals.

private static void computeGoals() {
    if (called)
        return;
    long start = System.currentTimeMillis();
    String targetClass = Properties.TARGET_CLASS;
    final MethodNameMatcher matcher = new MethodNameMatcher();
    for (String className : BytecodeInstructionPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).knownClasses()) {
        if (!(targetClass.equals("") || className.endsWith(targetClass)))
            continue;
        for (String methodName : BytecodeInstructionPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).knownMethods(className)) {
            if (!matcher.methodMatches(methodName))
                continue;
            for (BytecodeInstruction ins : BytecodeInstructionPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getInstructionsIn(className, methodName)) if (isUsable(ins))
                goals.add(new StatementCoverageTestFitness(ins));
        }
    }
    long end = System.currentTimeMillis();
    goalComputationTime = end - start;
    called = true;
}
Also used : MethodNameMatcher(org.evosuite.coverage.MethodNameMatcher) BytecodeInstruction(org.evosuite.graphs.cfg.BytecodeInstruction)

Example 19 with BytecodeInstruction

use of org.evosuite.graphs.cfg.BytecodeInstruction in project evosuite by EvoSuite.

the class ClassControlFlowGraph method importCFGNodes.

/**
 * import CFGs nodes. If the node is a method call to a method of a field
 * class, a new CCFGFieldClassCallNode is created. Otherwise, a normal
 * CCFGCodeNode is created
 *
 * @param cfg
 * @param temp
 */
private void importCFGNodes(RawControlFlowGraph cfg, Map<BytecodeInstruction, CCFGCodeNode> temp) {
    // add BytecodeInstructions as CCFGCodeNodes
    for (BytecodeInstruction code : cfg.vertexSet()) {
        CCFGCodeNode node;
        if (code.isMethodCallOfField()) {
            node = new CCFGFieldClassCallNode(code, code.getCalledMethodsClass(), code.getCalledMethodName(), code.getMethodCallDescriptor());
        } else {
            node = new CCFGCodeNode(code);
        }
        addVertex(node);
        temp.put(code, node);
    }
}
Also used : BytecodeInstruction(org.evosuite.graphs.cfg.BytecodeInstruction)

Example 20 with BytecodeInstruction

use of org.evosuite.graphs.cfg.BytecodeInstruction in project evosuite by EvoSuite.

the class ClassCallGraph method compute.

private void compute() {
    Map<String, RawControlFlowGraph> cfgs = GraphPool.getInstance(classLoader).getRawCFGs(className);
    if (cfgs == null)
        throw new IllegalStateException("did not find CFGs for a class I was supposed to compute the CCG of");
    // add nodes
    for (String method : cfgs.keySet()) addVertex(new ClassCallNode(method));
    // add vertices
    for (ClassCallNode methodNode : graph.vertexSet()) {
        RawControlFlowGraph rcfg = cfgs.get(methodNode.getMethod());
        List<BytecodeInstruction> calls = rcfg.determineMethodCallsToOwnClass();
        // System.out.println(calls.size()+" method calls from "+methodNode);
        for (BytecodeInstruction call : calls) {
            // System.out.println("  to "+call.getCalledMethod()+" in "+call.getCalledMethodsClass());
            ClassCallNode calledMethod = getNodeByMethodName(call.getCalledMethod());
            if (calledMethod != null) {
                ClassCallEdge e = new ClassCallEdge(call);
                addEdge(methodNode, calledMethod, e);
            }
        }
    }
}
Also used : BytecodeInstruction(org.evosuite.graphs.cfg.BytecodeInstruction) RawControlFlowGraph(org.evosuite.graphs.cfg.RawControlFlowGraph)

Aggregations

BytecodeInstruction (org.evosuite.graphs.cfg.BytecodeInstruction)28 RawControlFlowGraph (org.evosuite.graphs.cfg.RawControlFlowGraph)10 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)6 ControlDependency (org.evosuite.graphs.cfg.ControlDependency)5 InsnList (org.objectweb.asm.tree.InsnList)5 HashSet (java.util.HashSet)4 LabelNode (org.objectweb.asm.tree.LabelNode)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Branch (org.evosuite.coverage.branch.Branch)3 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 CCFGCodeNode (org.evosuite.graphs.ccfg.CCFGCodeNode)2 ControlDependenceGraph (org.evosuite.graphs.cdg.ControlDependenceGraph)2 BasicBlock (org.evosuite.graphs.cfg.BasicBlock)2 ControlFlowEdge (org.evosuite.graphs.cfg.ControlFlowEdge)2 AnnotatedLabel (org.evosuite.runtime.instrumentation.AnnotatedLabel)2 Label (org.objectweb.asm.Label)2 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)2