use of org.evosuite.coverage.branch.BranchCoverageTestFitness in project evosuite by EvoSuite.
the class CBranchFitnessFactory method getCoverageGoals.
/* (non-Javadoc)
* @see org.evosuite.coverage.TestFitnessFactory#getCoverageGoals()
*/
@Override
public List<CBranchTestFitness> getCoverageGoals() {
// TODO this creates duplicate goals. Momentary fixed using a Set, but it should be optimised
Set<CBranchTestFitness> goals = new HashSet<>();
// retrieve set of branches
BranchCoverageFactory branchFactory = new BranchCoverageFactory();
List<BranchCoverageTestFitness> branchGoals = branchFactory.getCoverageGoals();
CallGraph callGraph = DependencyAnalysis.getCallGraph();
// try to find all occurrences of this branch in the call tree
for (BranchCoverageTestFitness branchGoal : branchGoals) {
logger.info("Adding context branches for " + branchGoal.toString());
for (CallContext context : callGraph.getMethodEntryPoint(branchGoal.getClassName(), branchGoal.getMethod())) {
goals.add(new CBranchTestFitness(branchGoal.getBranchGoal(), context));
}
}
logger.info("Created " + goals.size() + " goals");
return new ArrayList<CBranchTestFitness>(goals);
}
use of org.evosuite.coverage.branch.BranchCoverageTestFitness in project evosuite by EvoSuite.
the class LineCoverageTestFitness method getFitness.
/**
* {@inheritDoc}
*
* Calculate fitness
*
* @param individual
* a {@link org.evosuite.testcase.ExecutableChromosome} object.
* @param result
* a {@link org.evosuite.testcase.execution.ExecutionResult} object.
* @return a double.
*/
@Override
public double getFitness(TestChromosome individual, ExecutionResult result) {
double fitness = 1.0;
// Deactivate coverage archive while measuring fitness, since branchcoverage fitness
// evaluating will attempt to claim coverage for it in the archive
boolean archive = Properties.TEST_ARCHIVE;
Properties.TEST_ARCHIVE = false;
if (result.getTrace().getCoveredLines().contains(this.line)) {
fitness = 0.0;
} else {
double r = Double.MAX_VALUE;
// Find minimum distance to satisfying any of the control dependencies
for (BranchCoverageTestFitness branchFitness : branchFitnesses) {
double newFitness = branchFitness.getFitness(individual, result);
if (newFitness == 0.0) {
// If the control dependency was covered, then likely
// an exception happened before the line was reached
newFitness = 1.0;
} else {
newFitness = 1.0 + normalize(newFitness);
}
if (newFitness < r)
r = newFitness;
}
fitness = r;
}
Properties.TEST_ARCHIVE = archive;
updateIndividual(this, individual, fitness);
if (fitness == 0.0) {
individual.getTestCase().addCoveredGoal(this);
}
if (Properties.TEST_ARCHIVE) {
Archive.getArchiveInstance().updateArchive(this, individual, fitness);
}
return fitness;
}
use of org.evosuite.coverage.branch.BranchCoverageTestFitness in project evosuite by EvoSuite.
the class IBranchFitnessFactory method getCoverageGoals.
/* (non-Javadoc)
* @see org.evosuite.coverage.TestFitnessFactory#getCoverageGoals()
*/
@Override
public List<IBranchTestFitness> getCoverageGoals() {
// TODO this creates duplicate goals. Momentary fixed using a Set.
Set<IBranchTestFitness> goals = new HashSet<IBranchTestFitness>();
// retrieve set of branches
BranchCoverageFactory branchFactory = new BranchCoverageFactory();
List<BranchCoverageTestFitness> branchGoals = branchFactory.getCoverageGoalsForAllKnownClasses();
CallGraph callGraph = DependencyAnalysis.getCallGraph();
// try to find all occurrences of this branch in the call tree
for (BranchCoverageTestFitness branchGoal : branchGoals) {
logger.info("Adding context branches for " + branchGoal.toString());
for (CallContext context : callGraph.getAllContextsFromTargetClass(branchGoal.getClassName(), branchGoal.getMethod())) {
// if is not possible to reach this branch from the target class, continue.
if (context.isEmpty())
continue;
goals.add(new IBranchTestFitness(branchGoal.getBranchGoal(), context));
}
}
assert (goals.size() >= branchFactory.getCoverageGoals().size());
logger.info("Created " + goals.size() + " goals");
return new ArrayList<IBranchTestFitness>(goals);
}
use of org.evosuite.coverage.branch.BranchCoverageTestFitness in project evosuite by EvoSuite.
the class StatementCoverageTestFitness method getFitness.
/**
* {@inheritDoc}
*/
@Override
public double getFitness(TestChromosome individual, ExecutionResult result) {
if (branchFitnesses.isEmpty())
throw new IllegalStateException("expect to know at least one fitness for goalInstruction");
if (result.hasTimeout() || result.hasTestException()) {
updateIndividual(this, individual, Double.MAX_VALUE);
return Double.MAX_VALUE;
}
double r = Double.MAX_VALUE;
for (BranchCoverageTestFitness branchFitness : branchFitnesses) {
double newFitness = branchFitness.getFitness(individual, result);
if (newFitness == 0.0) {
lastCoveringFitness = branchFitness;
return 0.0;
}
if (newFitness < r)
r = newFitness;
}
lastCoveringFitness = null;
updateIndividual(this, individual, r);
return r;
}
use of org.evosuite.coverage.branch.BranchCoverageTestFitness in project evosuite by EvoSuite.
the class TestCoverageGoalNameGeneration method testTwoTestsDifferOnlyInBranches.
@Test
public void testTwoTestsDifferOnlyInBranches() {
Branch b1 = mock(Branch.class);
BytecodeInstruction bi = mock(BytecodeInstruction.class);
when(b1.getInstruction()).thenReturn(bi);
TestCase test1 = new DefaultTestCase();
MethodCoverageTestFitness methodGoal = new MethodCoverageTestFitness("FooClass", "toString");
test1.addCoveredGoal(methodGoal);
BranchCoverageTestFitness branchGoal1 = new BranchCoverageTestFitness(new BranchCoverageGoal(b1, true, "FooClass", "toStringBarFooBlubb", 0));
test1.addCoveredGoal(branchGoal1);
TestCase test2 = new DefaultTestCase();
test2.addCoveredGoal(methodGoal);
// Need to add statements to change hashCode
test2.addStatement(new IntPrimitiveStatement(test2, 0));
BranchCoverageTestFitness branchGoal2 = new BranchCoverageTestFitness(new BranchCoverageGoal(b1, false, "FooClass", "toString", 0));
test2.addCoveredGoal(branchGoal2);
List<TestCase> tests = new ArrayList<>();
tests.add(test1);
tests.add(test2);
CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests);
assertEquals("testToString0", naming.getName(test1));
assertEquals("testToString1", naming.getName(test2));
}
Aggregations