Search in sources :

Example 1 with MethodCall

use of org.evosuite.testcase.MethodCall in project evosuite by EvoSuite.

the class LCSAJCoverageTestFitness method getFitness.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.evosuite.testcase.TestFitnessFunction#getFitness(de.unisb
	 * .cs.st.evosuite.testcase.TestChromosome,
	 * org.evosuite.testcase.ExecutionResult)
	 */
/**
 * {@inheritDoc}
 */
@Override
public double getFitness(TestChromosome individual, ExecutionResult result) {
    approach = lcsaj.length();
    double savedFitness = approach;
    logger.debug("Evaluating fitness for " + lcsaj);
    // for all method calls:
    for (MethodCall call : result.getTrace().getMethodCalls()) {
        double currentFitness = approach;
        // if method call is the method of the LCSAJ
        if (call.className.equals(lcsaj.getClassName()) && call.methodName.equals(lcsaj.getMethodName())) {
            int lcsaj_position = 0;
            boolean found = false;
            logger.debug("New call: " + call.className + "." + call.methodName + " " + call.branchTrace.size());
            // For each branch that was passed in this call
            for (int i = 0; i < call.branchTrace.size(); i++) {
                int actualBranch = call.branchTrace.get(i);
                int lcsaj_branchID = lcsaj.getBranchID(lcsaj_position);
                double false_distance = call.falseDistanceTrace.get(i);
                double true_distance = call.trueDistanceTrace.get(i);
                logger.debug("Current branch: " + call.branchTrace.get(i) + ": " + true_distance + "/" + false_distance + ", need " + lcsaj.getBranchID(lcsaj_position));
                if (actualBranch == lcsaj_branchID) {
                    if (lcsaj_position == 0)
                        found = true;
                    currentFitness -= 1.0;
                    if (actualBranch == lcsaj_finalBranchID) {
                        currentFitness += normalize(true_distance);
                        if (currentFitness < savedFitness) {
                            savedFitness = currentFitness;
                        }
                        if (lcsaj_position > lcsaj.getdPositionReached())
                            lcsaj.setPositionReached(lcsaj_position);
                        lcsaj_position = 0;
                        currentFitness = approach;
                        continue;
                    } else if (false_distance > 0) {
                        logger.debug("Took a wrong turn with false distance " + false_distance);
                        currentFitness += normalize(false_distance);
                        if (currentFitness < savedFitness)
                            savedFitness = currentFitness;
                        if (lcsaj_position > lcsaj.getdPositionReached())
                            lcsaj.setPositionReached(lcsaj_position);
                        lcsaj_position = 0;
                        currentFitness = approach;
                        continue;
                    }
                    lcsaj_position++;
                    logger.debug("LCSAJ position: " + lcsaj_position);
                } else {
                    if (LCSAJPool.isLCSAJBranch(BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranch(actualBranch))) {
                        // logger.debug("Skipping pseudo branch");
                        continue;
                    }
                    if (lcsaj_position > lcsaj.getdPositionReached())
                        lcsaj.setPositionReached(lcsaj_position);
                    lcsaj_position = 0;
                    currentFitness = approach;
                }
            // }
            }
            if (Properties.STRATEGY != Strategy.EVOSUITE) {
                if (!found) {
                    logger.debug("Looking for approach to initial branch: " + lcsaj.getStartBranch() + " with ID " + lcsaj.getStartBranch().getActualBranchId());
                    BranchCoverageGoal goal = new BranchCoverageGoal(lcsaj.getStartBranch(), true, lcsaj.getClassName(), lcsaj.getMethodName());
                    BranchCoverageTestFitness dependentFitness = new BranchCoverageTestFitness(goal);
                    assert (currentFitness == approach);
                    currentFitness += dependentFitness.getFitness(individual, result);
                    if (currentFitness < savedFitness)
                        savedFitness = currentFitness;
                    // logger.debug("Initial branch has distance: "
                    // + dependentFitness.getFitness(individual, result));
                    // logger.debug("Dependencies of initial branch: ");
                    // for (Branch branch : lcsaj.getStartBranch().getAllControlDependentBranches()) {
                    // logger.debug(branch);
                    // }
                    logger.debug("Resulting fitness: " + savedFitness);
                } else {
                    logger.debug("Call does not match: " + call.className + "." + call.methodName + " " + call.branchTrace.size());
                }
            }
        }
    }
    updateIndividual(this, individual, savedFitness);
    return savedFitness;
}
Also used : BranchCoverageTestFitness(org.evosuite.coverage.branch.BranchCoverageTestFitness) BranchCoverageGoal(org.evosuite.coverage.branch.BranchCoverageGoal) MethodCall(org.evosuite.testcase.MethodCall)

Example 2 with MethodCall

use of org.evosuite.testcase.MethodCall in project evosuite by EvoSuite.

the class PrimePathTestFitness method getFitness.

/* (non-Javadoc)
	 * @see org.evosuite.testcase.TestFitnessFunction#getFitness(org.evosuite.testcase.TestChromosome, org.evosuite.testcase.ExecutionResult)
	 */
/**
 * {@inheritDoc}
 */
@Override
public double getFitness(TestChromosome individual, ExecutionResult result) {
    double minMatch = length;
    for (MethodCall call : result.getTrace().getMethodCalls()) {
        if (call.className.equals(className) && call.methodName.equals(methodName)) {
            double matches = 0.0;
            for (Integer i : call.branchTrace) {
                logger.debug(" |-> " + i);
            }
            logger.debug("-------");
            logger.debug(path.toString());
            for (int i = 0; i < path.branches.size(); i++) {
                logger.debug(" -> " + path.branches.get(i).branch + " " + path.branches.get(i).value);
            }
            logger.debug("Length: " + length);
            int pos_path = 0;
            int pos_trace = 0;
            while (pos_path < path.branches.size()) {
                logger.debug("Current matches: " + matches);
                if (pos_trace >= call.branchTrace.size()) {
                    logger.debug("End of trace?" + ": " + (normalize(call.trueDistanceTrace.get(pos_trace - 1)) + normalize(call.falseDistanceTrace.get(pos_trace - 1))));
                    matches += 1 - (normalize(call.trueDistanceTrace.get(pos_trace - 1)) + normalize(call.falseDistanceTrace.get(pos_trace - 1)));
                    break;
                } else if (path.branches.get(pos_path).branch.getActualBranchId() == call.branchTrace.get(pos_trace)) {
                    logger.debug("Found branch match: " + path.branches.get(pos_path).branch);
                    matches++;
                    if (path.branches.get(pos_path).value == true) {
                        if (call.trueDistanceTrace.get(pos_trace) == 0.0) {
                            logger.debug("[True] Truth value match");
                            pos_path++;
                            pos_trace++;
                        } else {
                            logger.debug("[True] Truth value mismatch: " + (normalize(call.trueDistanceTrace.get(pos_trace))) + " / " + (normalize(call.falseDistanceTrace.get(pos_trace))));
                            matches -= (normalize(call.trueDistanceTrace.get(pos_trace)));
                            break;
                        }
                    } else {
                        if (call.falseDistanceTrace.get(pos_trace) == 0.0) {
                            logger.debug("[False] Truth value match");
                            pos_path++;
                            pos_trace++;
                        } else {
                            logger.debug("[False] Truth value mismatch: " + (normalize(call.falseDistanceTrace.get(pos_trace))) + " / " + (normalize(call.trueDistanceTrace.get(pos_trace))));
                            matches -= (normalize(call.falseDistanceTrace.get(pos_trace)));
                            break;
                        }
                    }
                } else {
                    logger.warn("Size of trace: " + call.trueDistanceTrace.size());
                    logger.warn("Position: " + pos_trace);
                    logger.debug("Found mismatch at " + pos_path + " / " + path.getSize() + ": " + (normalize(call.trueDistanceTrace.get(pos_trace - 1)) + normalize(call.falseDistanceTrace.get(pos_trace - 1))));
                    matches += 1 - (normalize(call.trueDistanceTrace.get(pos_trace - 1)) + normalize(call.falseDistanceTrace.get(pos_trace - 1)));
                    // -1?
                    break;
                }
            }
            matches = length - matches;
            logger.debug("Current fitness: " + matches);
            minMatch = Math.min(minMatch, matches);
            logger.debug("Current best fitness: " + minMatch);
            assert (minMatch >= 0) : "Fitness is " + minMatch;
        }
    }
    logger.debug("Final Fitness: " + minMatch);
    return minMatch;
}
Also used : MethodCall(org.evosuite.testcase.MethodCall)

Aggregations

MethodCall (org.evosuite.testcase.MethodCall)2 BranchCoverageGoal (org.evosuite.coverage.branch.BranchCoverageGoal)1 BranchCoverageTestFitness (org.evosuite.coverage.branch.BranchCoverageTestFitness)1