use of org.evosuite.graphs.cfg.ControlDependency in project evosuite by EvoSuite.
the class LineCoverageTestFitness method setupDependencies.
private void setupDependencies() {
goalInstruction = BytecodeInstructionPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getFirstInstructionAtLineNumber(className, methodName, line);
if (goalInstruction == null)
return;
Set<ControlDependency> cds = goalInstruction.getControlDependencies();
for (ControlDependency cd : cds) {
BranchCoverageTestFitness fitness = BranchCoverageFactory.createBranchCoverageTestFitness(cd);
branchFitnesses.add(fitness);
}
if (goalInstruction.isRootBranchDependent())
branchFitnesses.add(BranchCoverageFactory.createRootBranchTestFitness(goalInstruction));
if (cds.isEmpty() && !goalInstruction.isRootBranchDependent())
throw new IllegalStateException("expect control dependencies to be empty only for root dependent instructions: " + toString());
if (branchFitnesses.isEmpty())
throw new IllegalStateException("an instruction is at least on the root branch of it's method");
branchFitnesses.sort((a, b) -> a.compareTo(b));
}
use of org.evosuite.graphs.cfg.ControlDependency 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.graphs.cfg.ControlDependency in project evosuite by EvoSuite.
the class Mutation method getControlDependencies.
/**
* <p>
* getControlDependencies
* </p>
*
* @return a {@link java.util.Set} object.
*/
public Set<BranchCoverageGoal> getControlDependencies() {
Set<BranchCoverageGoal> goals = new HashSet<BranchCoverageGoal>();
for (ControlDependency cd : original.getControlDependencies()) {
BranchCoverageGoal goal = new BranchCoverageGoal(cd, className, methodName);
goals.add(goal);
}
return goals;
}
use of org.evosuite.graphs.cfg.ControlDependency in project evosuite by EvoSuite.
the class ControlDependenceGraph method getControlDependentBranchIds.
/**
* <p>getControlDependentBranchIds</p>
*
* @param ins a {@link org.evosuite.graphs.cfg.BasicBlock} object.
* @return a {@link java.util.Set} object.
*/
public Set<Integer> getControlDependentBranchIds(BasicBlock ins) {
Set<ControlDependency> dependentBranches = getControlDependentBranches(ins);
Set<Integer> r = new LinkedHashSet<>();
for (ControlDependency cd : dependentBranches) {
if (cd == null)
throw new IllegalStateException("expect set returned by getControlDependentBranches() not to contain null");
r.add(cd.getBranch().getActualBranchId());
}
// meaning entering the method
if (isRootDependent(ins))
r.add(-1);
return r;
}
use of org.evosuite.graphs.cfg.ControlDependency in project evosuite by EvoSuite.
the class ControlFlowDistanceCalculator method getDistancesForControlDependentBranchesOf.
/**
* Returns a set containing the ControlFlowDistances in the given result for
* all branches the given instruction is control dependent on
*
* @param handled
*/
private static Set<ControlFlowDistance> getDistancesForControlDependentBranchesOf(ExecutionResult result, MethodCall call, BytecodeInstruction instruction, String className, String methodName, Set<Branch> handled) {
Set<ControlFlowDistance> r = new HashSet<ControlFlowDistance>();
Set<ControlDependency> nextToLookAt = instruction.getControlDependencies();
for (ControlDependency next : nextToLookAt) {
if (instruction.equals(next.getBranch().getInstruction()))
// avoid loops
continue;
boolean nextValue = next.getBranchExpressionValue();
ControlFlowDistance nextDistance = getNonRootDistance(result, call, next.getBranch(), nextValue, className, methodName, handled);
assert (nextDistance != null);
r.add(nextDistance);
}
if (r.isEmpty()) {
// instruction only dependent on root branch
// since this method is called by getNonRootDistance(MethodCall)
// which in turn is only called when a MethodCall for this branch's
// method was found in the given result, i can safely assume that
// the 0-distance is a control dependence distance for the given
// instruction ... right?
r.add(new ControlFlowDistance());
}
return r;
}
Aggregations