use of org.evosuite.testcase.TestChromosome 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;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class MutationTestPool method searchFinished.
/* (non-Javadoc)
* @see org.evosuite.ga.SearchListener#searchFinished(org.evosuite.ga.GeneticAlgorithm)
*/
/**
* {@inheritDoc}
*/
@Override
public void searchFinished(GeneticAlgorithm<?> algorithm) {
TestSuiteChromosome solution = (TestSuiteChromosome) algorithm.getBestIndividual();
logger.info("Search finished with size " + solution.size());
for (TestChromosome test : testMap.values()) solution.addTest(test);
logger.info("Adding mutation tests to size " + solution.size());
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class ArchiveTestChromosomeFactory method getChromosome.
@Override
public TestChromosome getChromosome() {
if (seededTests != null && !seededTests.isEmpty()) {
/*
Ideally, we should populate the archive directly when EvoSuite starts.
But might be bit tricky based on current archive implementation (which needs executed tests).
So, easiest approach is to just return tests here, with no mutation on those.
However, this is done just once per test, as anyway those will end up
in archive.
*/
TestChromosome test = seededTests.remove(// pull out one element, 'last' just for efficiency
seededTests.size() - 1);
// no assertions are used during search
test.getTestCase().removeAssertions();
return test;
}
TestChromosome test = null;
// double P = (double)Archive.getArchiveInstance().getNumberOfCoveredTargets() / (double)Archive.getArchiveInstance().getNumberOfTargets();
if (!Archive.getArchiveInstance().isArchiveEmpty() && Randomness.nextDouble() < Properties.SEED_CLONE) {
logger.info("Creating test based on archive");
test = new TestChromosome();
test.setTestCase(Archive.getArchiveInstance().getRandomSolution().getTestCase());
int mutations = Randomness.nextInt(Properties.SEED_MUTATIONS);
for (int i = 0; i < mutations; i++) {
test.mutate();
}
} else {
logger.info("Creating random test");
test = defaultFactory.getChromosome();
}
// be sure that the factory returned a valid test
assert ConstraintVerifier.verifyTest(test);
assert !ConstraintVerifier.hasAnyOnlyForAssertionMethod(test.getTestCase());
return test;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class DSETestCaseLocalSearch method doSearch.
/**
* Applies DSE on a test case using the passed local search objective. The
* local search <b>modifies</b> the test chromosome, it cannot create a
* fresh new test chromosome. If the test case has no symbolic variables, or
* it does not reach an uncovered branch, DSE is skipped.
*
* @param test
* the test case chromosome.
*
* @param objective
* the fitness functions for the test chromosome
*/
@Override
public boolean doSearch(TestChromosome test, LocalSearchObjective<TestChromosome> objective) {
logger.info("Test before local search: " + test.getTestCase().toCode());
// gather covered branches true/false branch indexes
final Set<Branch> coveredBranches;
if (suite != null) {
coveredBranches = collectCoveredBranches(suite);
} else {
coveredBranches = getCoveredBranches(test);
}
final Set<Branch> uncoveredBranches = collectUncoveredBranches(coveredBranches);
if (uncoveredBranches.isEmpty()) {
/*
* As there are no branches uncovered (true or false branch missing)
* in this suite, there is no point in continuing DSE. Therefore, we
* should stop DSE (no DSE improvement)
*/
return false;
}
if (!hasUncoveredBranch(test, uncoveredBranches)) {
/*
* As there are uncovered branches, but none is reached by this
* test, the DSE is skipped and we return false (no DSE improvement)
*/
return false;
}
Set<Integer> targetStatementIndexes = collectStatementIndexesWithSymbolicVariables(test, objective);
final boolean fitnessHasImproved;
if (targetStatementIndexes.isEmpty()) {
// Cannot apply DSE because there are no symbolic variables
// Therefore, no improvement on objective.
fitnessHasImproved = false;
} else {
logger.info("Yes, now applying the search at positions {}!", targetStatementIndexes);
DSETestGenerator dseTestGenerator;
if (suite != null) {
dseTestGenerator = new DSETestGenerator(suite);
} else {
dseTestGenerator = new DSETestGenerator();
}
final TestChromosome newTest = dseTestGenerator.generateNewTest(test, targetStatementIndexes, objective);
if (newTest != null) {
fitnessHasImproved = true;
} else {
fitnessHasImproved = false;
}
}
LocalSearchBudget.getInstance().countLocalSearchOnTest();
// Return true iff search was successful
return fitnessHasImproved;
// TODO: Handle arrays in local search
// TODO: mutating an int might have an effect on array lengths
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class DSETestCaseLocalSearch method collectCoveredBranches.
/**
* Returns the set covered branches by this suite
*
* @param suite
* @return
*/
private static Set<Branch> collectCoveredBranches(TestSuiteChromosome suite) {
final Set<Branch> suiteCoveredBranches = new HashSet<Branch>();
for (TestChromosome test : suite.getTestChromosomes()) {
final Set<Branch> testCoveredBranches = getCoveredBranches(test);
suiteCoveredBranches.addAll(testCoveredBranches);
}
return suiteCoveredBranches;
}
Aggregations