use of org.evosuite.coverage.branch.Branch in project evosuite by EvoSuite.
the class LCSAJ method toString.
/**
* {@inheritDoc}
*/
@Override
public String toString() {
String output = "LCSAJ no.: " + this.id;
output += " in " + this.className + "/" + this.methodName + ". Branches passed: ";
for (Branch b : branches) output += " -> " + b.getActualBranchId();
// output += " -> " + b.getASMNodeString() + "\n";
return output;
}
use of org.evosuite.coverage.branch.Branch in project evosuite by EvoSuite.
the class BranchInstrumentation method addInstrumentationForSwitchCases.
/**
* For each actual case <key>: of a switch this method adds instrumentation
* for the Branch corresponding to that case to the given instruction list.
*
* @param v
* a {@link org.evosuite.graphs.cfg.BytecodeInstruction} object.
* @param instrumentation
* a {@link org.objectweb.asm.tree.InsnList} object.
* @param className
* a {@link java.lang.String} object.
* @param methodName
* a {@link java.lang.String} object.
*/
protected void addInstrumentationForSwitchCases(BytecodeInstruction v, InsnList instrumentation, String className, String methodName) {
if (!v.isSwitch())
throw new IllegalArgumentException("switch instruction expected");
List<Branch> caseBranches = BranchPool.getInstance(classLoader).getCaseBranchesForSwitch(v);
if (caseBranches == null || caseBranches.isEmpty())
throw new IllegalStateException("expect BranchPool to know at least one Branch for each switch instruction");
for (Branch targetCaseBranch : caseBranches) {
if (targetCaseBranch.isDefaultCase())
// handled elsewhere
continue;
Integer targetCaseValue = targetCaseBranch.getTargetCaseValue();
Integer targetCaseBranchId = targetCaseBranch.getActualBranchId();
instrumentation.add(new InsnNode(Opcodes.DUP));
instrumentation.add(new LdcInsnNode(targetCaseValue));
instrumentation.add(new LdcInsnNode(Opcodes.IF_ICMPEQ));
instrumentation.add(new LdcInsnNode(targetCaseBranchId));
instrumentation.add(new LdcInsnNode(v.getInstructionId()));
instrumentation.add(new MethodInsnNode(Opcodes.INVOKESTATIC, EXECUTION_TRACER, "passedBranch", "(IIIII)V", false));
}
}
use of org.evosuite.coverage.branch.Branch 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.coverage.branch.Branch in project evosuite by EvoSuite.
the class TestSuiteGeneratorHelper method getBytecodeStatistics.
static void getBytecodeStatistics() {
if (Properties.TRACK_BOOLEAN_BRANCHES) {
int gradientBranchCount = ExecutionTraceImpl.gradientBranches.size() * 2;
ClientServices.track(RuntimeVariable.Gradient_Branches, gradientBranchCount);
}
if (Properties.TRACK_COVERED_GRADIENT_BRANCHES) {
int coveredGradientBranchCount = ExecutionTraceImpl.gradientBranchesCoveredTrue.size() + ExecutionTraceImpl.gradientBranchesCoveredFalse.size();
ClientServices.track(RuntimeVariable.Gradient_Branches_Covered, coveredGradientBranchCount);
}
if (Properties.BRANCH_COMPARISON_TYPES) {
int cmp_intzero = 0, cmp_intint = 0, cmp_refref = 0, cmp_refnull = 0;
int bc_lcmp = 0, bc_fcmpl = 0, bc_fcmpg = 0, bc_dcmpl = 0, bc_dcmpg = 0;
for (Branch b : BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getAllBranches()) {
int branchOpCode = b.getInstruction().getASMNode().getOpcode();
int previousOpcode = -2;
if (b.getInstruction().getASMNode().getPrevious() != null) {
previousOpcode = b.getInstruction().getASMNode().getPrevious().getOpcode();
}
switch(previousOpcode) {
case Opcodes.LCMP:
bc_lcmp++;
break;
case Opcodes.FCMPL:
bc_fcmpl++;
break;
case Opcodes.FCMPG:
bc_fcmpg++;
break;
case Opcodes.DCMPL:
bc_dcmpl++;
break;
case Opcodes.DCMPG:
bc_dcmpg++;
break;
}
switch(branchOpCode) {
// copmpare int with zero
case Opcodes.IFEQ:
case Opcodes.IFNE:
case Opcodes.IFLT:
case Opcodes.IFGE:
case Opcodes.IFGT:
case Opcodes.IFLE:
cmp_intzero++;
break;
// copmpare int with int
case Opcodes.IF_ICMPEQ:
case Opcodes.IF_ICMPNE:
case Opcodes.IF_ICMPLT:
case Opcodes.IF_ICMPGE:
case Opcodes.IF_ICMPGT:
case Opcodes.IF_ICMPLE:
cmp_intint++;
break;
// copmpare reference with reference
case Opcodes.IF_ACMPEQ:
case Opcodes.IF_ACMPNE:
cmp_refref++;
break;
// compare reference with null
case Opcodes.IFNULL:
case Opcodes.IFNONNULL:
cmp_refnull++;
break;
}
}
ClientServices.track(RuntimeVariable.Cmp_IntZero, cmp_intzero);
ClientServices.track(RuntimeVariable.Cmp_IntInt, cmp_intint);
ClientServices.track(RuntimeVariable.Cmp_RefRef, cmp_refref);
ClientServices.track(RuntimeVariable.Cmp_RefNull, cmp_refnull);
ClientServices.track(RuntimeVariable.BC_lcmp, bc_lcmp);
ClientServices.track(RuntimeVariable.BC_fcmpl, bc_fcmpl);
ClientServices.track(RuntimeVariable.BC_fcmpg, bc_fcmpg);
ClientServices.track(RuntimeVariable.BC_dcmpl, bc_dcmpl);
ClientServices.track(RuntimeVariable.BC_dcmpg, bc_dcmpg);
RuntimeVariable[] bytecodeVarsCovered = new RuntimeVariable[] { RuntimeVariable.Covered_lcmp, RuntimeVariable.Covered_fcmpl, RuntimeVariable.Covered_fcmpg, RuntimeVariable.Covered_dcmpl, RuntimeVariable.Covered_dcmpg, RuntimeVariable.Covered_IntInt, RuntimeVariable.Covered_IntInt, RuntimeVariable.Covered_IntZero, RuntimeVariable.Covered_RefRef, RuntimeVariable.Covered_RefNull };
for (RuntimeVariable bcvar : bytecodeVarsCovered) {
ClientServices.track(bcvar, getBytecodeCount(bcvar, ExecutionTraceImpl.bytecodeInstructionCoveredFalse) + getBytecodeCount(bcvar, ExecutionTraceImpl.bytecodeInstructionCoveredTrue));
}
RuntimeVariable[] bytecodeVarsReached = new RuntimeVariable[] { RuntimeVariable.Reached_lcmp, RuntimeVariable.Reached_fcmpl, RuntimeVariable.Reached_fcmpg, RuntimeVariable.Reached_dcmpl, RuntimeVariable.Reached_dcmpg, RuntimeVariable.Reached_IntInt, RuntimeVariable.Reached_IntInt, RuntimeVariable.Reached_IntZero, RuntimeVariable.Reached_RefRef, RuntimeVariable.Reached_RefNull };
for (RuntimeVariable bcvar : bytecodeVarsReached) {
ClientServices.track(bcvar, getBytecodeCount(bcvar, ExecutionTraceImpl.bytecodeInstructionReached) * 2);
}
}
}
use of org.evosuite.coverage.branch.Branch in project evosuite by EvoSuite.
the class RawControlFlowGraph method addSwitchBranchEdge.
private ControlFlowEdge addSwitchBranchEdge(BytecodeInstruction src, BytecodeInstruction target, boolean isExceptionEdge) {
if (!target.isLabel())
throw new IllegalStateException("expect control flow edges from switch statements to always target labelNodes");
LabelNode label = (LabelNode) target.getASMNode();
List<Branch> switchCaseBranches = BranchPool.getInstance(classLoader).getBranchForLabel(label);
if (switchCaseBranches == null) {
logger.debug("not a switch case label: " + label.toString() + " " + target.toString());
return internalAddEdge(src, target, new ControlFlowEdge(isExceptionEdge));
}
for (Branch switchCaseBranch : switchCaseBranches) {
// TODO n^2
Set<ControlFlowEdge> soFar = incomingEdgesOf(target);
boolean handled = false;
for (ControlFlowEdge old : soFar) if (switchCaseBranch.equals(old.getBranchInstruction()))
handled = true;
if (handled)
continue;
/*
* previous try to add fake intermediate nodes for each empty case
* block to help the CDG - unsuccessful:
* if(switchCaseBranches.size()>1) { // // e = new
* ControlFlowEdge(isExceptionEdge); //
* e.setBranchInstruction(switchCaseBranch); //
* e.setBranchExpressionValue(true); // BytecodeInstruction
* fakeInstruction =
* BytecodeInstructionPool.createFakeInstruction(className
* ,methodName); // addVertex(fakeInstruction); //
* internalAddEdge(src,fakeInstruction,e); // // e = new
* ControlFlowEdge(isExceptionEdge); //
* e.setBranchInstruction(switchCaseBranch); //
* e.setBranchExpressionValue(true); // // e =
* internalAddEdge(fakeInstruction,target,e); // } else {
*/
ControlDependency cd = new ControlDependency(switchCaseBranch, true);
ControlFlowEdge e = new ControlFlowEdge(cd, isExceptionEdge);
e = internalAddEdge(src, target, e);
}
return new ControlFlowEdge(isExceptionEdge);
}
Aggregations