Search in sources :

Example 11 with Branch

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;
}
Also used : Branch(org.evosuite.coverage.branch.Branch)

Example 12 with Branch

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));
    }
}
Also used : MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TableSwitchInsnNode(org.objectweb.asm.tree.TableSwitchInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) LookupSwitchInsnNode(org.objectweb.asm.tree.LookupSwitchInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) Branch(org.evosuite.coverage.branch.Branch) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode)

Example 13 with Branch

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));
}
Also used : BranchCoverageTestFitness(org.evosuite.coverage.branch.BranchCoverageTestFitness) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Branch(org.evosuite.coverage.branch.Branch) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) CoverageGoalTestNameGenerationStrategy(org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy) ArrayList(java.util.ArrayList) BytecodeInstruction(org.evosuite.graphs.cfg.BytecodeInstruction) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) BranchCoverageGoal(org.evosuite.coverage.branch.BranchCoverageGoal) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 14 with Branch

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);
        }
    }
}
Also used : Branch(org.evosuite.coverage.branch.Branch) RuntimeVariable(org.evosuite.statistics.RuntimeVariable)

Example 15 with Branch

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);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) Branch(org.evosuite.coverage.branch.Branch)

Aggregations

Branch (org.evosuite.coverage.branch.Branch)19 ArrayList (java.util.ArrayList)4 BytecodeInstruction (org.evosuite.graphs.cfg.BytecodeInstruction)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 BranchCoverageGoal (org.evosuite.coverage.branch.BranchCoverageGoal)2 Mutation (org.evosuite.coverage.mutation.Mutation)2 ControlFlowEdge (org.evosuite.graphs.cfg.ControlFlowEdge)2 TestCase (org.evosuite.testcase.TestCase)2 TestChromosome (org.evosuite.testcase.TestChromosome)2 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)2 LabelNode (org.objectweb.asm.tree.LabelNode)2 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 Set (java.util.Set)1 Assertion (org.evosuite.assertion.Assertion)1 ContractViolation (org.evosuite.contracts.ContractViolation)1 MethodNameMatcher (org.evosuite.coverage.MethodNameMatcher)1