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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations