use of org.evosuite.graphs.cfg.BytecodeInstruction 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));
}
use of org.evosuite.graphs.cfg.BytecodeInstruction 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.BytecodeInstruction in project evosuite by EvoSuite.
the class StatementCoverageFactory method computeGoals.
private static void computeGoals() {
if (called)
return;
long start = System.currentTimeMillis();
String targetClass = Properties.TARGET_CLASS;
final MethodNameMatcher matcher = new MethodNameMatcher();
for (String className : BytecodeInstructionPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).knownClasses()) {
if (!(targetClass.equals("") || className.endsWith(targetClass)))
continue;
for (String methodName : BytecodeInstructionPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).knownMethods(className)) {
if (!matcher.methodMatches(methodName))
continue;
for (BytecodeInstruction ins : BytecodeInstructionPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getInstructionsIn(className, methodName)) if (isUsable(ins))
goals.add(new StatementCoverageTestFitness(ins));
}
}
long end = System.currentTimeMillis();
goalComputationTime = end - start;
called = true;
}
use of org.evosuite.graphs.cfg.BytecodeInstruction in project evosuite by EvoSuite.
the class ClassControlFlowGraph method importCFGNodes.
/**
* import CFGs nodes. If the node is a method call to a method of a field
* class, a new CCFGFieldClassCallNode is created. Otherwise, a normal
* CCFGCodeNode is created
*
* @param cfg
* @param temp
*/
private void importCFGNodes(RawControlFlowGraph cfg, Map<BytecodeInstruction, CCFGCodeNode> temp) {
// add BytecodeInstructions as CCFGCodeNodes
for (BytecodeInstruction code : cfg.vertexSet()) {
CCFGCodeNode node;
if (code.isMethodCallOfField()) {
node = new CCFGFieldClassCallNode(code, code.getCalledMethodsClass(), code.getCalledMethodName(), code.getMethodCallDescriptor());
} else {
node = new CCFGCodeNode(code);
}
addVertex(node);
temp.put(code, node);
}
}
use of org.evosuite.graphs.cfg.BytecodeInstruction in project evosuite by EvoSuite.
the class ClassCallGraph method compute.
private void compute() {
Map<String, RawControlFlowGraph> cfgs = GraphPool.getInstance(classLoader).getRawCFGs(className);
if (cfgs == null)
throw new IllegalStateException("did not find CFGs for a class I was supposed to compute the CCG of");
// add nodes
for (String method : cfgs.keySet()) addVertex(new ClassCallNode(method));
// add vertices
for (ClassCallNode methodNode : graph.vertexSet()) {
RawControlFlowGraph rcfg = cfgs.get(methodNode.getMethod());
List<BytecodeInstruction> calls = rcfg.determineMethodCallsToOwnClass();
// System.out.println(calls.size()+" method calls from "+methodNode);
for (BytecodeInstruction call : calls) {
// System.out.println(" to "+call.getCalledMethod()+" in "+call.getCalledMethodsClass());
ClassCallNode calledMethod = getNodeByMethodName(call.getCalledMethod());
if (calledMethod != null) {
ClassCallEdge e = new ClassCallEdge(call);
addEdge(methodNode, calledMethod, e);
}
}
}
}
Aggregations