Search in sources :

Example 1 with ControlFlowDistance

use of org.evosuite.coverage.ControlFlowDistance in project evosuite by EvoSuite.

the class BranchCoverageTestFitness method getFitness.

/**
 * {@inheritDoc}
 *
 * Calculate approach level + branch distance
 */
@Override
public double getFitness(TestChromosome individual, ExecutionResult result) {
    ControlFlowDistance distance = goal.getDistance(result);
    double fitness = distance.getResultingBranchFitness();
    if (logger.isDebugEnabled()) {
        logger.debug("Goal at line " + goal.getLineNumber() + ": approach level = " + distance.getApproachLevel() + " / branch distance = " + distance.getBranchDistance() + ", fitness = " + fitness);
    }
    updateIndividual(this, individual, fitness);
    if (fitness == 0.0) {
        individual.getTestCase().addCoveredGoal(this);
    }
    if (Properties.TEST_ARCHIVE) {
        Archive.getArchiveInstance().updateArchive(this, individual, fitness);
    }
    return fitness;
}
Also used : ControlFlowDistance(org.evosuite.coverage.ControlFlowDistance)

Example 2 with ControlFlowDistance

use of org.evosuite.coverage.ControlFlowDistance in project evosuite by EvoSuite.

the class ControlFlowDistanceCalculator method getRootDistance.

private static ControlFlowDistance getRootDistance(ExecutionResult result, String className, String methodName) {
    ControlFlowDistance d = new ControlFlowDistance();
    if (result.getTrace().getCoveredMethods().contains(className + "." + methodName)) {
        return d;
    }
    if (hasConstructorException(result, className, methodName)) {
        return d;
    }
    d.increaseApproachLevel();
    return d;
}
Also used : ControlFlowDistance(org.evosuite.coverage.ControlFlowDistance)

Example 3 with ControlFlowDistance

use of org.evosuite.coverage.ControlFlowDistance in project evosuite by EvoSuite.

the class ControlFlowDistanceCalculator method getNonRootDistance.

private static ControlFlowDistance getNonRootDistance(ExecutionResult result, MethodCall call, Branch branch, boolean value, String className, String methodName, Set<Branch> handled) {
    if (branch == null)
        throw new IllegalStateException("expect getNonRootDistance() to only be called if this goal's branch is not a root branch");
    if (call == null)
        throw new IllegalArgumentException("null given");
    if (handled.contains(branch)) {
        // if(r== null)
        return worstPossibleDistanceForMethod(branch);
    // else {
    // return r;
    // }
    }
    handled.add(branch);
    List<Double> trueDistances = call.trueDistanceTrace;
    List<Double> falseDistances = call.falseDistanceTrace;
    // IDEA:
    // if this goal's branch is traced in the given path, return the
    // true_/false_distance, depending on this.value
    // otherwise, look at all Branches this.branch is control dependent on
    // and return 1 + minimum of the branch coverage goal distance over all
    // such branches taking as value the branchExpressionValue
    Set<Integer> branchTracePositions = determineBranchTracePositions(call, branch);
    if (!branchTracePositions.isEmpty()) {
        // branch was traced in given path
        ControlFlowDistance r = new ControlFlowDistance(0, Double.MAX_VALUE);
        for (Integer branchTracePosition : branchTracePositions) if (value)
            r.setBranchDistance(Math.min(r.getBranchDistance(), trueDistances.get(branchTracePosition)));
        else
            r.setBranchDistance(Math.min(r.getBranchDistance(), falseDistances.get(branchTracePosition)));
        if (r.getBranchDistance() == Double.MAX_VALUE)
            throw new IllegalStateException("should be impossible");
        // result.intermediateDistances.put(branch, r);
        return r;
    }
    ControlFlowDistance controlDependenceDistance = getControlDependenceDistancesFor(result, call, branch.getInstruction(), className, methodName, handled);
    controlDependenceDistance.increaseApproachLevel();
    return controlDependenceDistance;
}
Also used : ControlFlowDistance(org.evosuite.coverage.ControlFlowDistance)

Example 4 with ControlFlowDistance

use of org.evosuite.coverage.ControlFlowDistance in project evosuite by EvoSuite.

the class OnlyBranchCoverageTestFitness method getFitness.

/**
 * {@inheritDoc}
 *
 * Calculate approach level + branch distance
 */
@Override
public double getFitness(TestChromosome individual, ExecutionResult result) {
    ControlFlowDistance distance = goal.getDistance(result);
    double fitness = distance.getResultingBranchFitness();
    // If there is an undeclared exception it is a failing test
    // if (result.hasUndeclaredException())
    // fitness += 1;
    logger.debug("Approach level: " + distance.getApproachLevel() + " / branch distance: " + distance.getBranchDistance() + ", fitness = " + fitness);
    updateIndividual(this, individual, fitness);
    if (fitness == 0.0) {
        individual.getTestCase().addCoveredGoal(this);
    }
    if (Properties.TEST_ARCHIVE) {
        Archive.getArchiveInstance().updateArchive(this, individual, fitness);
    }
    return fitness;
}
Also used : ControlFlowDistance(org.evosuite.coverage.ControlFlowDistance)

Example 5 with ControlFlowDistance

use of org.evosuite.coverage.ControlFlowDistance in project evosuite by EvoSuite.

the class MutationTestFitness method getExecutionDistance.

/**
 * <p>
 * getExecutionDistance
 * </p>
 *
 * @param result
 *            a {@link org.evosuite.testcase.execution.ExecutionResult} object.
 * @return a double.
 */
protected double getExecutionDistance(ExecutionResult result) {
    double fitness = 0.0;
    if (!result.getTrace().wasMutationTouched(mutation.getId()))
        fitness += diameter;
    // Get control flow distance
    if (controlDependencies.isEmpty()) {
        // If mutant was not executed, this can be either because of an exception, or because the method was not executed
        String key = mutation.getClassName() + "." + mutation.getMethodName();
        if (result.getTrace().getCoveredMethods().contains(key)) {
            logger.debug("Target method " + key + " was executed");
        } else {
            logger.debug("Target method " + key + " was not executed");
            fitness += diameter;
        }
    } else {
        ControlFlowDistance cfgDistance = null;
        for (BranchCoverageGoal dependency : controlDependencies) {
            logger.debug("Checking dependency...");
            ControlFlowDistance distance = dependency.getDistance(result);
            if (cfgDistance == null)
                cfgDistance = distance;
            else {
                if (distance.compareTo(cfgDistance) < 0)
                    cfgDistance = distance;
            }
        }
        if (cfgDistance != null) {
            logger.debug("Found control dependency");
            fitness += cfgDistance.getResultingBranchFitness();
        }
    }
    return fitness;
}
Also used : ControlFlowDistance(org.evosuite.coverage.ControlFlowDistance) BranchCoverageGoal(org.evosuite.coverage.branch.BranchCoverageGoal)

Aggregations

ControlFlowDistance (org.evosuite.coverage.ControlFlowDistance)7 BranchCoverageGoal (org.evosuite.coverage.branch.BranchCoverageGoal)1 ControlDependency (org.evosuite.graphs.cfg.ControlDependency)1 MethodCall (org.evosuite.testcase.execution.MethodCall)1