use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.
the class LineCoverageSuiteFitness 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 LineCoverageSuiteFitness method initializeControlDependencies.
/**
* Add guidance to the fitness function by including branch distances on
* all control dependencies
*/
private void initializeControlDependencies() {
// In case we target more than one class (context, or inner classes)
Set<String> targetClasses = new LinkedHashSet<>();
for (TestFitnessFunction ff : lineGoals.values()) {
targetClasses.add(ff.getTargetClass());
}
for (String className : targetClasses) {
List<BytecodeInstruction> instructions = BytecodeInstructionPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getInstructionsIn(className);
if (instructions == null) {
logger.info("No instructions known for class {} (is it an enum?)", className);
continue;
}
for (BytecodeInstruction bi : instructions) {
if (bi.getBasicBlock() == null) {
// Labels get no basic block. TODO - why?
continue;
}
// The order of CDs may be nondeterminstic
// TODO: A better solution would be to make the CD order deterministic rather than sorting here
List<ControlDependency> cds = new ArrayList<>(bi.getControlDependencies());
Collections.sort(cds);
for (ControlDependency cd : cds) {
if (cd.getBranchExpressionValue()) {
branchesToCoverTrue.add(cd.getBranch().getActualBranchId());
} else {
branchesToCoverFalse.add(cd.getBranch().getActualBranchId());
}
}
}
}
branchesToCoverBoth.addAll(branchesToCoverTrue);
branchesToCoverBoth.retainAll(branchesToCoverFalse);
branchesToCoverTrue.removeAll(branchesToCoverBoth);
branchesToCoverFalse.removeAll(branchesToCoverBoth);
logger.info("Covering branches true: " + branchesToCoverTrue);
logger.info("Covering branches false: " + branchesToCoverFalse);
logger.info("Covering branches both: " + branchesToCoverBoth);
}
use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.
the class LineCoverageSuiteFitness 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 analyzeTraces.
/**
* Iterate over all execution results and summarize statistics
*
* @param results
* @param calledMethods
* @return
*/
protected boolean analyzeTraces(List<ExecutionResult> results, Set<String> calledMethods) {
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 (String methodName : this.methodCoverageMap.keySet()) {
TestFitnessFunction goal = this.methodCoverageMap.get(methodName);
// 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
calledMethods.add(methodName);
// goal to not be considered by the next iteration of the evolutionary algorithm
this.toRemoveMethods.add(methodName);
}
}
// In case there were exceptions in a constructor
handleConstructorExceptions(test, result, calledMethods);
}
return hasTimeoutOrTestException;
}
use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.
the class MethodCoverageSuiteFitness method handleConstructorExceptions.
/**
* If there is an exception in a super-constructor, then the corresponding
* constructor might not be included in the execution trace
*
* @param suite
* @param results
* @param calledMethods
*/
protected void handleConstructorExceptions(TestChromosome test, ExecutionResult result, Set<String> calledMethods) {
if (result.hasTimeout() || result.hasTestException() || result.noThrownExceptions())
return;
Integer exceptionPosition = result.getFirstPositionOfThrownException();
Statement statement = result.test.getStatement(exceptionPosition);
if (statement instanceof ConstructorStatement) {
ConstructorStatement c = (ConstructorStatement) statement;
String className = c.getConstructor().getName();
String methodName = "<init>" + Type.getConstructorDescriptor(c.getConstructor().getConstructor());
String name = className + "." + methodName;
if (methodCoverageMap.containsKey(name) && !calledMethods.contains(name)) {
TestFitnessFunction goal = methodCoverageMap.get(name);
// only include methods being called
test.getTestCase().addCoveredGoal(goal);
calledMethods.add(name);
this.toRemoveMethods.add(name);
if (Properties.TEST_ARCHIVE) {
Archive.getArchiveInstance().updateArchive(goal, test, 0.0);
}
}
}
}
Aggregations