Search in sources :

Example 11 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class LineCoverageSuiteFitness method getControlDependencyGuidance.

private double getControlDependencyGuidance(List<ExecutionResult> results) {
    Map<Integer, Integer> predicateCount = new LinkedHashMap<Integer, Integer>();
    Map<Integer, Double> trueDistance = new LinkedHashMap<Integer, Double>();
    Map<Integer, Double> falseDistance = new LinkedHashMap<Integer, Double>();
    for (ExecutionResult result : results) {
        if (result.hasTimeout() || result.hasTestException()) {
            continue;
        }
        for (Entry<Integer, Integer> entry : result.getTrace().getPredicateExecutionCount().entrySet()) {
            if (!predicateCount.containsKey(entry.getKey()))
                predicateCount.put(entry.getKey(), entry.getValue());
            else {
                predicateCount.put(entry.getKey(), predicateCount.get(entry.getKey()) + entry.getValue());
            }
        }
        for (Entry<Integer, Double> entry : result.getTrace().getTrueDistances().entrySet()) {
            if (!trueDistance.containsKey(entry.getKey()))
                trueDistance.put(entry.getKey(), entry.getValue());
            else {
                trueDistance.put(entry.getKey(), Math.min(trueDistance.get(entry.getKey()), entry.getValue()));
            }
        }
        for (Entry<Integer, Double> entry : result.getTrace().getFalseDistances().entrySet()) {
            if (!falseDistance.containsKey(entry.getKey()))
                falseDistance.put(entry.getKey(), entry.getValue());
            else {
                falseDistance.put(entry.getKey(), Math.min(falseDistance.get(entry.getKey()), entry.getValue()));
            }
        }
    }
    double distance = 0.0;
    for (Integer branchId : branchesToCoverBoth) {
        if (!predicateCount.containsKey(branchId)) {
            distance += 2.0;
        } else if (predicateCount.get(branchId) == 1) {
            distance += 1.0;
        } else {
            distance += normalize(trueDistance.get(branchId));
            distance += normalize(falseDistance.get(branchId));
        }
    }
    for (Integer branchId : branchesToCoverTrue) {
        if (!trueDistance.containsKey(branchId)) {
            distance += 1;
        } else {
            distance += normalize(trueDistance.get(branchId));
        }
    }
    for (Integer branchId : branchesToCoverFalse) {
        if (!falseDistance.containsKey(branchId)) {
            distance += 1;
        } else {
            distance += normalize(falseDistance.get(branchId));
        }
    }
    return distance;
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 12 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class OnlyLineCoverageSuiteFitness method analyzeTraces.

/**
 * Iterate over all execution results and summarize statistics
 *
 * @param results
 * @param coveredLines
 * @return
 */
private boolean analyzeTraces(List<ExecutionResult> results, Set<Integer> coveredLines) {
    boolean hasTimeoutOrTestException = false;
    for (ExecutionResult result : results) {
        if (result.hasTimeout() || result.hasTestException()) {
            hasTimeoutOrTestException = true;
            continue;
        }
        TestChromosome test = new TestChromosome();
        test.setTestCase(result.test);
        test.setLastExecutionResult(result);
        test.setChanged(false);
        for (Integer goalID : this.lineGoals.keySet()) {
            TestFitnessFunction goal = this.lineGoals.get(goalID);
            // archive is updated by the TestFitnessFunction class
            double fit = goal.getFitness(test, result);
            if (fit == 0.0) {
                // helper to count the number of covered goals
                coveredLines.add(goalID);
                // goal to not be considered by the next iteration of the evolutionary algorithm
                this.toRemoveLines.add(goalID);
            }
        }
    }
    return hasTimeoutOrTestException;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 13 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class InputCoverageSuiteFitness method getFitness.

/**
 * {@inheritDoc}
 * <p/>
 * Execute all tests and count covered input goals
 */
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> suite) {
    logger.trace("Calculating test suite fitness");
    double fitness = 0.0;
    List<ExecutionResult> results = runTestSuite(suite);
    boolean hasTimeoutOrTestException = false;
    for (ExecutionResult result : results) {
        if (result.hasTimeout() || result.hasTestException()) {
            hasTimeoutOrTestException = true;
            break;
        }
    }
    Set<TestFitnessFunction> setOfCoveredGoals = new LinkedHashSet<>();
    if (hasTimeoutOrTestException) {
        logger.info("Test suite has timed out, setting fitness to max value " + totalGoals);
        fitness = totalGoals;
    } else
        fitness = computeDistance(results, setOfCoveredGoals);
    int coveredGoals = setOfCoveredGoals.size() + removedGoals.size();
    if (totalGoals > 0)
        suite.setCoverage(this, (double) coveredGoals / (double) totalGoals);
    else
        suite.setCoverage(this, 1.0);
    suite.setNumOfCoveredGoals(this, coveredGoals);
    printStatusMessages(suite, coveredGoals, fitness);
    updateIndividual(this, suite, fitness);
    assert (coveredGoals <= totalGoals) : "Covered " + coveredGoals + " vs total goals " + totalGoals;
    assert (fitness >= 0.0);
    assert (fitness != 0.0 || coveredGoals == totalGoals) : "Fitness: " + fitness + ", " + "coverage: " + coveredGoals + "/" + totalGoals;
    assert (suite.getCoverage(this) <= 1.0) && (suite.getCoverage(this) >= 0.0) : "Wrong coverage value " + suite.getCoverage(this);
    return fitness;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 14 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class InputCoverageSuiteFitness method computeDistance.

private double computeDistance(List<ExecutionResult> results, Set<TestFitnessFunction> setOfCoveredGoals) {
    Map<InputCoverageTestFitness, Double> mapDistances = new LinkedHashMap<InputCoverageTestFitness, Double>();
    for (InputCoverageTestFitness testFitness : this.inputCoverageMap) {
        mapDistances.put(testFitness, 1.0);
    }
    for (ExecutionResult result : results) {
        if (result.hasTimeout() || result.hasTestException()) {
            continue;
        }
        TestChromosome test = new TestChromosome();
        test.setTestCase(result.test);
        test.setLastExecutionResult(result);
        test.setChanged(false);
        Iterator<InputCoverageTestFitness> it = this.inputCoverageMap.iterator();
        while (it.hasNext()) {
            InputCoverageTestFitness testFitness = it.next();
            if (!mapDistances.containsKey(testFitness)) {
                continue;
            }
            // archive is updated by the TestFitnessFunction class
            double distance = testFitness.getFitness(test, result);
            mapDistances.put(testFitness, Math.min(distance, mapDistances.get(testFitness)));
            if (distance == 0.0) {
                mapDistances.remove(testFitness);
                // helper to count the number of covered goals
                setOfCoveredGoals.add(testFitness);
                // goal to not be considered by the next iteration of the evolutionary algorithm
                this.toRemoveGoals.add(testFitness);
            }
        }
    }
    double distance = 0.0;
    if (!mapDistances.isEmpty()) {
        distance = mapDistances.values().stream().reduce(Double::sum).get().doubleValue();
    }
    return distance;
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome) LinkedHashMap(java.util.LinkedHashMap)

Example 15 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class IBranchSuiteFitness method getFitness.

public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> suite, boolean updateChromosome) {
    // branchFitness.getFitness(suite);
    double fitness = 0.0;
    List<ExecutionResult> results = runTestSuite(suite);
    Map<IBranchTestFitness, Double> distanceMap = new LinkedHashMap<>();
    Map<IBranchTestFitness, Integer> callCount = new LinkedHashMap<>();
    for (ExecutionResult result : results) {
        if (result.hasTimeout() || result.hasTestException()) {
            continue;
        }
        TestChromosome test = new TestChromosome();
        test.setTestCase(result.test);
        test.setLastExecutionResult(result);
        test.setChanged(false);
        for (Integer branchId : result.getTrace().getTrueDistancesContext().keySet()) {
            Map<CallContext, Double> trueMap = result.getTrace().getTrueDistancesContext().get(branchId);
            for (CallContext context : trueMap.keySet()) {
                IBranchTestFitness goalT = getContextGoal(branchId, context, true);
                if (goalT == null || removedBranchesT.contains(goalT))
                    continue;
                double distanceT = normalize(trueMap.get(context));
                if (distanceMap.get(goalT) == null || distanceMap.get(goalT) > distanceT) {
                    distanceMap.put(goalT, distanceT);
                }
                if (Double.compare(distanceT, 0.0) == 0) {
                    if (updateChromosome)
                        test.getTestCase().addCoveredGoal(goalT);
                    toRemoveBranchesT.add(goalT);
                }
                if (Properties.TEST_ARCHIVE) {
                    Archive.getArchiveInstance().updateArchive(goalT, test, distanceT);
                }
            }
        }
        for (Integer branchId : result.getTrace().getFalseDistancesContext().keySet()) {
            Map<CallContext, Double> falseMap = result.getTrace().getFalseDistancesContext().get(branchId);
            for (CallContext context : falseMap.keySet()) {
                IBranchTestFitness goalF = getContextGoal(branchId, context, false);
                if (goalF == null || removedBranchesF.contains(goalF))
                    continue;
                double distanceF = normalize(falseMap.get(context));
                if (distanceMap.get(goalF) == null || distanceMap.get(goalF) > distanceF) {
                    distanceMap.put(goalF, distanceF);
                }
                if (Double.compare(distanceF, 0.0) == 0) {
                    if (updateChromosome)
                        test.getTestCase().addCoveredGoal(goalF);
                    toRemoveBranchesF.add(goalF);
                }
                if (Properties.TEST_ARCHIVE) {
                    Archive.getArchiveInstance().updateArchive(goalF, test, distanceF);
                }
            }
        }
        for (Entry<String, Map<CallContext, Integer>> entry : result.getTrace().getMethodContextCount().entrySet()) {
            for (Entry<CallContext, Integer> value : entry.getValue().entrySet()) {
                IBranchTestFitness goal = getContextGoal(entry.getKey(), value.getKey());
                if (goal == null || removedRootBranches.contains(goal))
                    continue;
                int count = value.getValue();
                if (callCount.get(goal) == null || callCount.get(goal) < count) {
                    callCount.put(goal, count);
                }
                if (count > 0) {
                    if (updateChromosome)
                        result.test.addCoveredGoal(goal);
                    toRemoveRootBranches.add(goal);
                }
            }
        }
    }
    int numCoveredGoals = 0;
    for (IBranchTestFitness goal : branchGoals) {
        Double distance = distanceMap.get(goal);
        if (distance == null)
            distance = 1.0;
        if (goal.getBranch() == null) {
            Integer count = callCount.get(goal);
            if (count == null || count == 0) {
                fitness += 1;
            } else {
                numCoveredGoals++;
            }
        } else {
            if (distance == 0.0) {
                numCoveredGoals++;
            }
            fitness += distance;
        }
    }
    if (updateChromosome) {
        numCoveredGoals += removedBranchesF.size();
        numCoveredGoals += removedBranchesT.size();
        numCoveredGoals += removedRootBranches.size();
        if (totGoals > 0) {
            suite.setCoverage(this, (double) numCoveredGoals / (double) totGoals);
        }
        suite.setNumOfCoveredGoals(this, numCoveredGoals);
        suite.setNumOfNotCoveredGoals(this, totGoals - numCoveredGoals);
        updateIndividual(this, suite, fitness);
    }
    return fitness;
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) CallContext(org.evosuite.setup.CallContext) LinkedHashMap(java.util.LinkedHashMap) TestChromosome(org.evosuite.testcase.TestChromosome) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)93 TestChromosome (org.evosuite.testcase.TestChromosome)33 HashSet (java.util.HashSet)15 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)15 ArrayList (java.util.ArrayList)11 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)10 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)9 TestCase (org.evosuite.testcase.TestCase)9 VariableReference (org.evosuite.testcase.variable.VariableReference)9 Test (org.junit.Test)9 LinkedHashSet (java.util.LinkedHashSet)8 HashMap (java.util.HashMap)7 LinkedHashMap (java.util.LinkedHashMap)7 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)7 ArrayReference (org.evosuite.testcase.variable.ArrayReference)7 Set (java.util.Set)6 AbstractTestSuiteChromosome (org.evosuite.testsuite.AbstractTestSuiteChromosome)6 Map (java.util.Map)4 Entry (java.util.Map.Entry)3 ExecutionTrace (org.evosuite.testcase.execution.ExecutionTrace)3