use of org.evosuite.testsuite.AbstractTestSuiteChromosome in project evosuite by EvoSuite.
the class AllDefsCoverageSuiteFitness method getFitness.
/*
* (non-Javadoc)
*
* @see
* org.evosuite.ga.FitnessFunction#getFitness(org.
* evosuite.ga.Chromosome)
*/
/**
* {@inheritDoc}
*/
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> individual) {
logger.trace("Calculating defuse fitness");
TestSuiteChromosome suite = (TestSuiteChromosome) individual;
List<ExecutionResult> results = runTestSuite(suite);
double fitness = 0.0;
Set<TestFitnessFunction> coveredGoals = new HashSet<TestFitnessFunction>();
for (TestFitnessFunction goal : goals) {
if (coveredGoals.contains(goal))
continue;
double goalFitness = 2.0;
for (ExecutionResult result : results) {
TestChromosome tc = new TestChromosome();
tc.setTestCase(result.test);
double resultFitness = goal.getFitness(tc, result);
if (resultFitness < goalFitness)
goalFitness = resultFitness;
if (goalFitness == 0.0) {
result.test.addCoveredGoal(goal);
// System.out.println(goal.toString());
// System.out.println(result.test.toCode());
// System.out.println(resultFitness);
coveredGoals.add(goal);
break;
}
}
fitness += goalFitness;
}
updateIndividual(this, individual, fitness);
setSuiteCoverage(suite, coveredGoals);
return fitness;
}
use of org.evosuite.testsuite.AbstractTestSuiteChromosome 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;
}
use of org.evosuite.testsuite.AbstractTestSuiteChromosome in project evosuite by EvoSuite.
the class StrongMutationSuiteFitness method getFitness.
/* (non-Javadoc)
* @see org.evosuite.ga.FitnessFunction#getFitness(org.evosuite.ga.Chromosome)
*/
/**
* {@inheritDoc}
*/
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> individual) {
runTestSuite(individual);
// Set<MutationTestFitness> uncoveredMutants = MutationTestPool.getUncoveredFitnessFunctions();
TestSuiteChromosome suite = (TestSuiteChromosome) individual;
for (TestChromosome test : suite.getTestChromosomes()) {
ExecutionResult result = test.getLastExecutionResult();
if (result.hasTimeout() || result.hasTestException()) {
logger.debug("Skipping test with timeout");
double fitness = branchFitness.totalGoals * 2 + branchFitness.totalMethods + 3 * this.numMutants;
updateIndividual(this, individual, fitness);
suite.setCoverage(this, 0.0);
logger.info("Test case has timed out, setting fitness to max value " + fitness);
return fitness;
}
}
// First objective: achieve branch coverage
logger.debug("Calculating branch fitness: ");
double fitness = branchFitness.getFitness(individual);
Set<Integer> touchedMutants = new LinkedHashSet<Integer>();
Map<Mutation, Double> minMutantFitness = new LinkedHashMap<Mutation, Double>();
// 0..1 -> propagation distance
for (Integer mutantId : this.mutantMap.keySet()) {
MutationTestFitness mutantFitness = mutantMap.get(mutantId);
minMutantFitness.put(mutantFitness.getMutation(), 3.0);
}
int mutantsChecked = 0;
int numKilled = removedMutants.size();
Set<Integer> newKilled = new LinkedHashSet<Integer>();
// Quicker tests first
List<TestChromosome> executionOrder = prioritizeTests(suite);
for (TestChromosome test : executionOrder) {
ExecutionResult result = test.getLastExecutionResult();
// use reflection for basic criteria, not for mutation
if (result.calledReflection())
continue;
ExecutionTrace trace = result.getTrace();
touchedMutants.addAll(trace.getTouchedMutants());
logger.debug("Tests touched " + touchedMutants.size() + " mutants");
Map<Integer, Double> touchedMutantsDistances = trace.getMutationDistances();
if (touchedMutantsDistances.isEmpty()) {
// if 'result' does not touch any mutant, no need to continue
continue;
}
Iterator<Entry<Integer, MutationTestFitness>> it = this.mutantMap.entrySet().iterator();
while (it.hasNext()) {
Entry<Integer, MutationTestFitness> entry = it.next();
int mutantID = entry.getKey();
if (newKilled.contains(mutantID)) {
continue;
}
MutationTestFitness goal = entry.getValue();
if (MutationTimeoutStoppingCondition.isDisabled(goal.getMutation())) {
logger.debug("Skipping timed out mutation " + goal.getMutation().getId());
continue;
}
mutantsChecked++;
double mutantInfectionDistance = 3.0;
boolean hasBeenTouched = touchedMutantsDistances.containsKey(mutantID);
if (hasBeenTouched) {
// Infection happened, so we need to check propagation
if (touchedMutantsDistances.get(mutantID) == 0.0) {
logger.debug("Executing test against mutant " + goal.getMutation());
// archive is updated by the TestFitnessFunction class
mutantInfectionDistance = goal.getFitness(test, result);
} else {
// We can skip calling the test fitness function since we already know
// fitness is 1.0 (for propagation) + infection distance
mutantInfectionDistance = 1.0 + normalize(touchedMutantsDistances.get(mutantID));
}
} else {
// archive is updated by the TestFitnessFunction class
mutantInfectionDistance = goal.getFitness(test, result);
}
if (mutantInfectionDistance == 0.0) {
numKilled++;
newKilled.add(mutantID);
// update list of covered goals
result.test.addCoveredGoal(goal);
// goal to not be considered by the next iteration of the evolutionary algorithm
this.toRemoveMutants.add(mutantID);
} else {
minMutantFitness.put(goal.getMutation(), Math.min(mutantInfectionDistance, minMutantFitness.get(goal.getMutation())));
}
}
}
// logger.info("Fitness values for " + minMutantFitness.size() + " mutants");
for (Double fit : minMutantFitness.values()) {
fitness += fit;
}
logger.debug("Mutants killed: {}, Checked: {}, Goals: {})", numKilled, mutantsChecked, this.numMutants);
updateIndividual(this, individual, fitness);
assert numKilled == newKilled.size() + removedMutants.size();
assert numKilled <= this.numMutants;
double coverage = (double) numKilled / (double) this.numMutants;
assert coverage >= 0.0 && coverage <= 1.0;
suite.setCoverage(this, coverage);
suite.setNumOfCoveredGoals(this, numKilled);
return fitness;
}
use of org.evosuite.testsuite.AbstractTestSuiteChromosome in project evosuite by EvoSuite.
the class RegressionTestSuiteChromosome method getTestSuiteForTheOtherClassLoader.
public AbstractTestSuiteChromosome<TestChromosome> getTestSuiteForTheOtherClassLoader() {
AbstractTestSuiteChromosome<TestChromosome> suite = new TestSuiteChromosome();
for (TestChromosome regressionTest : tests) {
RegressionTestChromosome rtc = (RegressionTestChromosome) regressionTest;
suite.addTest(rtc.getTheSameTestForTheOtherClassLoader());
}
return suite;
}
use of org.evosuite.testsuite.AbstractTestSuiteChromosome in project evosuite by EvoSuite.
the class OnlyMutationSuiteFitness 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);
double fitness = 0.0;
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 = this.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