use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.
the class OnlyLineCoverageSuiteFitness method updateCoveredGoals.
@Override
public boolean updateCoveredGoals() {
if (!Properties.TEST_ARCHIVE)
return false;
for (Integer goalID : this.toRemoveLines) {
TestFitnessFunction ff = this.lineGoals.remove(goalID);
if (ff != null) {
this.removedLines.add(goalID);
} else {
throw new IllegalStateException("goal to remove not found");
}
}
this.toRemoveLines.clear();
logger.info("Current state of archive: " + Archive.getArchiveInstance().toString());
assert this.numLines == this.lineGoals.size() + this.removedLines.size();
return true;
}
use of org.evosuite.testcase.TestFitnessFunction 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.TestFitnessFunction in project evosuite by EvoSuite.
the class MethodCoverageSuiteFitness method updateCoveredGoals.
@Override
public boolean updateCoveredGoals() {
if (!Properties.TEST_ARCHIVE) {
return false;
}
for (String method : this.toRemoveMethods) {
TestFitnessFunction f = this.methodCoverageMap.remove(method);
if (f != null) {
this.removedMethods.add(method);
} else {
throw new IllegalStateException("Goal to remove not found: " + method + ", candidates: " + methodCoverageMap.keySet());
}
}
this.toRemoveMethods.clear();
logger.info("Current state of archive: " + Archive.getArchiveInstance().toString());
assert this.totalMethods == this.methodCoverageMap.size() + this.removedMethods.size();
return true;
}
use of org.evosuite.testcase.TestFitnessFunction 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.TestFitnessFunction in project evosuite by EvoSuite.
the class WeakMutationSuiteFitness method getFitness.
/* (non-Javadoc)
* @see org.evosuite.ga.FitnessFunction#getFitness(org.evosuite.ga.Chromosome)
*/
/**
* {@inheritDoc}
*/
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> individual) {
/**
* e.g. classes with only static constructors
*/
if (this.numMutants == 0) {
updateIndividual(this, individual, 0.0);
((TestSuiteChromosome) individual).setCoverage(this, 1.0);
((TestSuiteChromosome) individual).setNumOfCoveredGoals(this, 0);
return 0.0;
}
List<ExecutionResult> results = runTestSuite(individual);
// First objective: achieve branch coverage
logger.debug("Calculating branch fitness: ");
/*
* Note: results are cached, so the test suite is not executed again when we
* calculated the branch fitness
*/
double fitness = branchFitness.getFitness(individual);
Map<Integer, Double> mutant_distance = new LinkedHashMap<Integer, Double>();
Set<Integer> touchedMutants = new LinkedHashSet<Integer>();
for (ExecutionResult result : results) {
// use reflection for basic criteria, not for mutation
if (result.hasTimeout() || result.hasTestException() || result.calledReflection()) {
continue;
}
touchedMutants.addAll(result.getTrace().getTouchedMutants());
Map<Integer, Double> touchedMutantsDistances = result.getTrace().getMutationDistances();
if (touchedMutantsDistances.isEmpty()) {
// if 'result' does not touch any mutant, no need to continue
continue;
}
TestChromosome test = new TestChromosome();
test.setTestCase(result.test);
test.setLastExecutionResult(result);
test.setChanged(false);
Iterator<Entry<Integer, MutationTestFitness>> it = this.mutantMap.entrySet().iterator();
while (it.hasNext()) {
Entry<Integer, MutationTestFitness> entry = it.next();
int mutantID = entry.getKey();
TestFitnessFunction goal = entry.getValue();
double fit = 0.0;
if (touchedMutantsDistances.containsKey(mutantID)) {
fit = touchedMutantsDistances.get(mutantID);
if (!mutant_distance.containsKey(mutantID)) {
mutant_distance.put(mutantID, fit);
} else {
mutant_distance.put(mutantID, Math.min(mutant_distance.get(mutantID), fit));
}
} else {
// archive is updated by the TestFitnessFunction class
fit = goal.getFitness(test, result);
}
if (fit == 0.0) {
// update list of covered goals
test.getTestCase().addCoveredGoal(goal);
// goal to not be considered by the next iteration of the evolutionary algorithm
this.toRemoveMutants.add(mutantID);
}
if (Properties.TEST_ARCHIVE) {
Archive.getArchiveInstance().updateArchive(goal, test, fit);
}
}
}
// Second objective: touch all mutants?
fitness += MutationPool.getMutantCounter() - touchedMutants.size();
int covered = removedMutants.size();
for (Double distance : mutant_distance.values()) {
if (distance < 0) {
logger.warn("Distance is " + distance + " / " + Integer.MAX_VALUE + " / " + Integer.MIN_VALUE);
// FIXXME
distance = 0.0;
}
fitness += normalize(distance);
if (distance == 0.0) {
covered++;
}
}
updateIndividual(this, individual, fitness);
((TestSuiteChromosome) individual).setCoverage(this, (double) covered / (double) this.numMutants);
((TestSuiteChromosome) individual).setNumOfCoveredGoals(this, covered);
return fitness;
}
Aggregations