Search in sources :

Example 6 with ControlDependency

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

the class LineCoverageTestFitness method setupDependencies.

private void setupDependencies() {
    goalInstruction = BytecodeInstructionPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getFirstInstructionAtLineNumber(className, methodName, line);
    if (goalInstruction == null)
        return;
    Set<ControlDependency> cds = goalInstruction.getControlDependencies();
    for (ControlDependency cd : cds) {
        BranchCoverageTestFitness fitness = BranchCoverageFactory.createBranchCoverageTestFitness(cd);
        branchFitnesses.add(fitness);
    }
    if (goalInstruction.isRootBranchDependent())
        branchFitnesses.add(BranchCoverageFactory.createRootBranchTestFitness(goalInstruction));
    if (cds.isEmpty() && !goalInstruction.isRootBranchDependent())
        throw new IllegalStateException("expect control dependencies to be empty only for root dependent instructions: " + toString());
    if (branchFitnesses.isEmpty())
        throw new IllegalStateException("an instruction is at least on the root branch of it's method");
    branchFitnesses.sort((a, b) -> a.compareTo(b));
}
Also used : BranchCoverageTestFitness(org.evosuite.coverage.branch.BranchCoverageTestFitness) ControlDependency(org.evosuite.graphs.cfg.ControlDependency)

Example 7 with ControlDependency

use of org.evosuite.graphs.cfg.ControlDependency 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 8 with ControlDependency

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

the class Mutation method getControlDependencies.

/**
 * <p>
 * getControlDependencies
 * </p>
 *
 * @return a {@link java.util.Set} object.
 */
public Set<BranchCoverageGoal> getControlDependencies() {
    Set<BranchCoverageGoal> goals = new HashSet<BranchCoverageGoal>();
    for (ControlDependency cd : original.getControlDependencies()) {
        BranchCoverageGoal goal = new BranchCoverageGoal(cd, className, methodName);
        goals.add(goal);
    }
    return goals;
}
Also used : BranchCoverageGoal(org.evosuite.coverage.branch.BranchCoverageGoal) HashSet(java.util.HashSet) ControlDependency(org.evosuite.graphs.cfg.ControlDependency)

Example 9 with ControlDependency

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

the class ControlDependenceGraph method getControlDependentBranchIds.

/**
 * <p>getControlDependentBranchIds</p>
 *
 * @param ins a {@link org.evosuite.graphs.cfg.BasicBlock} object.
 * @return a {@link java.util.Set} object.
 */
public Set<Integer> getControlDependentBranchIds(BasicBlock ins) {
    Set<ControlDependency> dependentBranches = getControlDependentBranches(ins);
    Set<Integer> r = new LinkedHashSet<>();
    for (ControlDependency cd : dependentBranches) {
        if (cd == null)
            throw new IllegalStateException("expect set returned by getControlDependentBranches() not to contain null");
        r.add(cd.getBranch().getActualBranchId());
    }
    // meaning entering the method
    if (isRootDependent(ins))
        r.add(-1);
    return r;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ControlDependency(org.evosuite.graphs.cfg.ControlDependency)

Example 10 with ControlDependency

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

the class ControlFlowDistanceCalculator method getDistancesForControlDependentBranchesOf.

/**
 * Returns a set containing the ControlFlowDistances in the given result for
 * all branches the given instruction is control dependent on
 *
 * @param handled
 */
private static Set<ControlFlowDistance> getDistancesForControlDependentBranchesOf(ExecutionResult result, MethodCall call, BytecodeInstruction instruction, String className, String methodName, Set<Branch> handled) {
    Set<ControlFlowDistance> r = new HashSet<ControlFlowDistance>();
    Set<ControlDependency> nextToLookAt = instruction.getControlDependencies();
    for (ControlDependency next : nextToLookAt) {
        if (instruction.equals(next.getBranch().getInstruction()))
            // avoid loops
            continue;
        boolean nextValue = next.getBranchExpressionValue();
        ControlFlowDistance nextDistance = getNonRootDistance(result, call, next.getBranch(), nextValue, className, methodName, handled);
        assert (nextDistance != null);
        r.add(nextDistance);
    }
    if (r.isEmpty()) {
        // instruction only dependent on root branch
        // since this method is called by getNonRootDistance(MethodCall)
        // which in turn is only called when a MethodCall for this branch's
        // method was found in the given result, i can safely assume that
        // the 0-distance is a control dependence distance for the given
        // instruction ... right?
        r.add(new ControlFlowDistance());
    }
    return r;
}
Also used : ControlFlowDistance(org.evosuite.coverage.ControlFlowDistance) ControlDependency(org.evosuite.graphs.cfg.ControlDependency)

Aggregations

ControlDependency (org.evosuite.graphs.cfg.ControlDependency)10 BytecodeInstruction (org.evosuite.graphs.cfg.BytecodeInstruction)5 BasicBlock (org.evosuite.graphs.cfg.BasicBlock)3 InsnList (org.objectweb.asm.tree.InsnList)3 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)3 LinkedHashSet (java.util.LinkedHashSet)2 ControlDependenceGraph (org.evosuite.graphs.cdg.ControlDependenceGraph)2 Label (org.objectweb.asm.Label)2 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)2 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)2 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)2 LabelNode (org.objectweb.asm.tree.LabelNode)2 HashSet (java.util.HashSet)1 ControlFlowDistance (org.evosuite.coverage.ControlFlowDistance)1 BranchCoverageGoal (org.evosuite.coverage.branch.BranchCoverageGoal)1 BranchCoverageTestFitness (org.evosuite.coverage.branch.BranchCoverageTestFitness)1 ControlFlowEdge (org.evosuite.graphs.cfg.ControlFlowEdge)1 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)1 FrameNode (org.objectweb.asm.tree.FrameNode)1 LineNumberNode (org.objectweb.asm.tree.LineNumberNode)1