use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.
the class MutationAssertionGenerator method runTest.
/**
* Execute a test case on a mutant
*
* @param test
* The test case that should be executed
* @param mutant
* The mutant on which the test case shall be executed
*/
protected ExecutionResult runTest(TestCase test, Mutation mutant) {
ExecutionResult result = new ExecutionResult(test, mutant);
// resetObservers();
comparisonObserver.clear();
sameObserver.clear();
primitiveObserver.clear();
inspectorObserver.clear();
fieldObserver.clear();
nullObserver.clear();
arrayObserver.clear();
arrayLengthObserver.clear();
containsTraceObserver.clear();
try {
logger.debug("Executing test");
if (mutant == null) {
MutationObserver.deactivateMutation();
} else {
MutationObserver.activateMutation(mutant);
}
result = TestCaseExecutor.getInstance().execute(test);
MutationObserver.deactivateMutation(mutant);
int num = test.size();
MaxStatementsStoppingCondition.statementsExecuted(num);
result.setTrace(comparisonObserver.getTrace(), ComparisonTraceEntry.class);
result.setTrace(sameObserver.getTrace(), SameTraceEntry.class);
result.setTrace(primitiveObserver.getTrace(), PrimitiveTraceEntry.class);
result.setTrace(inspectorObserver.getTrace(), InspectorTraceEntry.class);
result.setTrace(fieldObserver.getTrace(), PrimitiveFieldTraceEntry.class);
result.setTrace(nullObserver.getTrace(), NullTraceEntry.class);
result.setTrace(arrayObserver.getTrace(), ArrayTraceEntry.class);
result.setTrace(arrayLengthObserver.getTrace(), ArrayLengthTraceEntry.class);
result.setTrace(containsTraceObserver.getTrace(), ContainsTraceEntry.class);
} catch (Exception e) {
throw new Error(e);
}
return result;
}
use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.
the class AssertionGenerator method filterFailingAssertions.
protected void filterFailingAssertions(TestCase test) {
// Make sure we are not keeping assertions influenced by static state
// TODO: Need to handle statically initialized classes
ExecutionResult result = runTest(test);
Set<Assertion> invalidAssertions = new HashSet<Assertion>();
for (Assertion assertion : test.getAssertions()) {
for (OutputTrace<?> outputTrace : result.getTraces()) {
if (outputTrace.isDetectedBy(assertion)) {
invalidAssertions.add(assertion);
break;
}
}
}
logger.info("Removing {} nondeterministic assertions", invalidAssertions.size());
for (Assertion assertion : invalidAssertions) {
test.removeAssertion(assertion);
}
}
use of org.evosuite.testcase.execution.ExecutionResult 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.testcase.execution.ExecutionResult in project evosuite by EvoSuite.
the class CBranchSuiteFitness method getFitness.
/*
* (non-Javadoc)
*
* @see
* org.evosuite.ga.FitnessFunction#getFitness(org.evosuite.ga.Chromosome)
*/
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> suite) {
// branchFitness.getFitness(suite);
double fitness = 0.0;
List<ExecutionResult> results = runTestSuite(suite);
Map<CBranchTestFitness, Double> distanceMap = new LinkedHashMap<>();
Map<Integer, Integer> callCounter = new LinkedHashMap<>();
Map<Integer, Integer> branchCounter = new LinkedHashMap<>();
for (ExecutionResult result : results) {
if (result.hasTimeout() || result.hasTestException()) {
continue;
}
// Determine minimum branch distance for each branch in each context
assert (result.getTrace().getTrueDistancesContext().keySet().size() == result.getTrace().getFalseDistancesContext().keySet().size());
TestChromosome test = new TestChromosome();
test.setTestCase(result.test);
test.setLastExecutionResult(result);
test.setChanged(false);
for (Integer branchId : result.getTrace().getTrueDistancesContext().keySet()) {
Map<CallContext, Double> trueMap = result.getTrace().getTrueDistancesContext().get(branchId);
Map<CallContext, Double> falseMap = result.getTrace().getFalseDistancesContext().get(branchId);
for (CallContext context : trueMap.keySet()) {
CBranchTestFitness goalT = getContextGoal(branchId, context, true);
if (goalT == null)
continue;
double distanceT = normalize(trueMap.get(context));
if (distanceMap.get(goalT) == null || distanceMap.get(goalT) > distanceT) {
distanceMap.put(goalT, distanceT);
}
if (Double.compare(distanceT, 0.0) == 0) {
if (removedGoals.contains(goalT))
continue;
test.getTestCase().addCoveredGoal(goalT);
toRemoveGoals.add(goalT);
}
if (Properties.TEST_ARCHIVE) {
Archive.getArchiveInstance().updateArchive(goalT, test, distanceT);
}
}
for (CallContext context : falseMap.keySet()) {
CBranchTestFitness goalF = getContextGoal(branchId, context, false);
if (goalF == null)
continue;
double distanceF = normalize(falseMap.get(context));
if (distanceMap.get(goalF) == null || distanceMap.get(goalF) > distanceF) {
distanceMap.put(goalF, distanceF);
}
if (Double.compare(distanceF, 0.0) == 0) {
if (removedGoals.contains(goalF))
continue;
test.getTestCase().addCoveredGoal(goalF);
toRemoveGoals.add(goalF);
}
if (Properties.TEST_ARCHIVE) {
Archive.getArchiveInstance().updateArchive(goalF, test, distanceF);
}
}
}
for (Entry<Integer, Map<CallContext, Integer>> entry : result.getTrace().getPredicateContextExecutionCount().entrySet()) {
for (Entry<CallContext, Integer> value : entry.getValue().entrySet()) {
int count = value.getValue();
CBranchTestFitness goalT = getContextGoal(entry.getKey(), value.getKey(), true);
if (goalT != null) {
if (branchCounter.get(goalT.getGenericContextBranchIdentifier()) == null || branchCounter.get(goalT.getGenericContextBranchIdentifier()) < count) {
branchCounter.put(goalT.getGenericContextBranchIdentifier(), count);
}
} else {
CBranchTestFitness goalF = getContextGoal(entry.getKey(), value.getKey(), false);
if (goalF != null) {
if (branchCounter.get(goalF.getGenericContextBranchIdentifier()) == null || branchCounter.get(goalF.getGenericContextBranchIdentifier()) < count) {
branchCounter.put(goalF.getGenericContextBranchIdentifier(), count);
}
} else
continue;
}
}
}
for (Entry<String, Map<CallContext, Integer>> entry : result.getTrace().getMethodContextCount().entrySet()) {
for (Entry<CallContext, Integer> value : entry.getValue().entrySet()) {
CBranchTestFitness goal = getContextGoal(entry.getKey(), value.getKey());
if (goal == null)
continue;
int count = value.getValue();
if (callCounter.get(goal.hashCode()) == null || callCounter.get(goal.hashCode()) < count) {
callCounter.put(goal.hashCode(), count);
}
if (count > 0) {
if (removedGoals.contains(goal))
continue;
test.getTestCase().addCoveredGoal(goal);
toRemoveGoals.add(goal);
}
if (Properties.TEST_ARCHIVE) {
Archive.getArchiveInstance().updateArchive(goal, test, count == 0 ? 1.0 : 0.0);
}
}
}
}
int numCoveredGoals = removedGoals.size();
for (CBranchTestFitness goal : branchGoals) {
if (removedGoals.contains(goal))
continue;
Double distance = distanceMap.get(goal);
if (distance == null)
distance = 1.0;
if (goal.getBranch() == null) {
Integer count = callCounter.get(goal.hashCode());
if (count == null || count == 0) {
fitness += 1;
} else {
numCoveredGoals++;
}
} else {
Integer count = branchCounter.get(goal.getGenericContextBranchIdentifier());
if (count == null || count == 0)
fitness += 1;
else if (count == 1)
fitness += 0.5;
else {
if (Double.compare(distance, 0.0) == 0) {
numCoveredGoals++;
}
fitness += distance;
}
}
}
if (!branchGoals.isEmpty()) {
suite.setCoverage(this, (double) numCoveredGoals / (double) branchGoals.size());
} else {
suite.setCoverage(this, 1);
}
suite.setNumOfCoveredGoals(this, numCoveredGoals);
suite.setNumOfNotCoveredGoals(this, branchGoals.size() - numCoveredGoals);
updateIndividual(this, suite, fitness);
return fitness;
}
use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.
the class OutputCoverageSuiteFitness method computeDistance.
public double computeDistance(List<ExecutionResult> results, Set<TestFitnessFunction> setOfCoveredGoals) {
Map<OutputCoverageTestFitness, Double> mapDistances = new LinkedHashMap<OutputCoverageTestFitness, Double>();
for (OutputCoverageTestFitness testFitness : this.outputCoverageGoals) {
mapDistances.put(testFitness, 1.0);
}
for (ExecutionResult result : results) {
if (result.hasTimeout() || result.hasTestException()) {
continue;
}
TestChromosome test = new TestChromosome();
test.setTestCase(result.test);
test.setLastExecutionResult(result);
test.setChanged(false);
Iterator<OutputCoverageTestFitness> it = this.outputCoverageGoals.iterator();
while (it.hasNext()) {
OutputCoverageTestFitness testFitness = it.next();
if (!mapDistances.containsKey(testFitness)) {
continue;
}
// archive is updated by the TestFitnessFunction class
double distance = testFitness.getFitness(test, result);
mapDistances.put(testFitness, Math.min(distance, mapDistances.get(testFitness)));
if (distance == 0.0) {
mapDistances.remove(testFitness);
// helper to count the number of covered goals
setOfCoveredGoals.add(testFitness);
// goal to not be considered by the next iteration of the evolutionary algorithm
this.toRemoveGoals.add(testFitness);
}
}
}
double distance = 0.0;
if (!mapDistances.isEmpty()) {
distance = mapDistances.values().stream().reduce(Double::sum).get().doubleValue();
}
return distance;
}
Aggregations