use of org.evosuite.testcase.execution.ExecutionResult 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.testcase.execution.ExecutionResult in project evosuite by EvoSuite.
the class StatementCoverageSuiteFitness method getFitness.
/**
* {@inheritDoc}
*/
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> suite) {
List<ExecutionResult> results = runTestSuite(suite);
double fitness = 0.0;
// first simple and naive idea:
// just take each goal, calculate the minimal fitness over all results in the suite
// once a goal is covered don't check for it again
// in the end sum up all those fitness and it's the resulting suite-fitness
// guess this is horribly inefficient but it's a start
List<? extends TestFitnessFunction> totalGoals = StatementCoverageFactory.retrieveCoverageGoals();
Set<TestFitnessFunction> coveredGoals = new HashSet<TestFitnessFunction>();
for (TestFitnessFunction goal : totalGoals) {
double goalFitness = Double.MAX_VALUE;
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);
coveredGoals.add(goal);
break;
}
}
fitness += goalFitness;
}
if (totalGoals.size() > 0)
suite.setCoverage(this, coveredGoals.size() / (double) totalGoals.size());
else
suite.setCoverage(this, 1.0);
suite.setNumOfCoveredGoals(this, coveredGoals.size());
updateIndividual(this, suite, fitness);
return fitness;
}
use of org.evosuite.testcase.execution.ExecutionResult 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.testcase.execution.ExecutionResult in project evosuite by EvoSuite.
the class FixedNumRandomTestStrategy method generateTests.
@Override
public TestSuiteChromosome generateTests() {
LoggingUtils.getEvoLogger().info("* Generating fixed number of random tests");
RandomLengthTestFactory factory = new org.evosuite.testcase.factories.RandomLengthTestFactory();
TestSuiteChromosome suite = new TestSuiteChromosome();
if (!canGenerateTestsForSUT()) {
LoggingUtils.getEvoLogger().info("* Found no testable methods in the target class " + Properties.TARGET_CLASS);
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, 0);
return suite;
}
for (int i = 0; i < Properties.NUM_RANDOM_TESTS; i++) {
logger.info("Current test: " + i + "/" + Properties.NUM_RANDOM_TESTS);
TestChromosome test = factory.getChromosome();
ExecutionResult result = TestCaseExecutor.runTest(test.getTestCase());
Integer pos = result.getFirstPositionOfThrownException();
if (pos != null) {
if (result.getExceptionThrownAtPosition(pos) instanceof CodeUnderTestException || result.getExceptionThrownAtPosition(pos) instanceof UncompilableCodeException || result.getExceptionThrownAtPosition(pos) instanceof TestCaseExecutor.TimeoutExceeded) {
// Filter invalid tests
continue;
} else {
// Remove anything that follows an exception
test.getTestCase().chop(pos + 1);
}
test.setChanged(true);
} else {
test.setLastExecutionResult(result);
}
suite.addTest(test);
}
// Search is finished, send statistics
sendExecutionStatistics();
return suite;
}
use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.
the class IndividualTestStrategy method getAdditionallyCoveredGoals.
private Set<Integer> getAdditionallyCoveredGoals(List<? extends TestFitnessFunction> goals, Set<Integer> covered, TestChromosome best) {
Set<Integer> r = new HashSet<Integer>();
ExecutionResult result = best.getLastExecutionResult();
assert (result != null);
// if (result == null) {
// result = TestCaseExecutor.getInstance().execute(best.test);
// }
int num = -1;
for (TestFitnessFunction goal : goals) {
num++;
if (covered.contains(num))
continue;
if (goal.isCovered(best, result)) {
r.add(num);
if (Properties.PRINT_COVERED_GOALS)
LoggingUtils.getEvoLogger().info("* Additionally covered: " + goal.toString());
}
}
return r;
}
Aggregations