Search in sources :

Example 16 with Branch

use of org.evosuite.coverage.branch.Branch in project evosuite by EvoSuite.

the class TestGenerationResultBuilder method setTestCase.

public void setTestCase(String name, String code, TestCase testCase, String comment, ExecutionResult result) {
    testCode.put(name, code);
    testCases.put(name, testCase);
    Set<Failure> failures = new LinkedHashSet<Failure>();
    for (ContractViolation violation : testCase.getContractViolations()) {
        failures.add(new Failure(violation));
    }
    if (!Properties.CHECK_CONTRACTS && result.hasUndeclaredException()) {
        int position = result.getFirstPositionOfThrownException();
        Throwable exception = result.getExceptionThrownAtPosition(position);
        failures.add(new Failure(exception, position, testCase));
    }
    contractViolations.put(name, failures);
    testComments.put(name, comment);
    testLineCoverage.put(name, result.getTrace().getCoveredLines());
    uncoveredLines.removeAll(result.getTrace().getCoveredLines());
    Set<BranchInfo> branchCoverage = new LinkedHashSet<BranchInfo>();
    for (int branchId : result.getTrace().getCoveredFalseBranches()) {
        Branch branch = BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranch(branchId);
        if (branch == null) {
            LoggingUtils.getEvoLogger().warn("Branch is null: " + branchId);
            continue;
        }
        BranchInfo info = new BranchInfo(branch.getClassName(), branch.getMethodName(), branch.getInstruction().getLineNumber(), false);
        branchCoverage.add(info);
    }
    for (int branchId : result.getTrace().getCoveredTrueBranches()) {
        Branch branch = BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranch(branchId);
        if (branch == null) {
            LoggingUtils.getEvoLogger().warn("Branch is null: " + branchId);
            continue;
        }
        BranchInfo info = new BranchInfo(branch.getClassName(), branch.getMethodName(), branch.getInstruction().getLineNumber(), true);
        branchCoverage.add(info);
    }
    testBranchCoverage.put(name, branchCoverage);
    uncoveredBranches.removeAll(branchCoverage);
    Set<MutationInfo> mutationCoverage = new LinkedHashSet<MutationInfo>();
    for (Assertion assertion : testCase.getAssertions()) {
        for (Mutation m : assertion.getKilledMutations()) {
            mutationCoverage.add(new MutationInfo(m));
        }
    }
    testMutantCoverage.put(name, mutationCoverage);
    uncoveredMutants.removeAll(mutationCoverage);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Assertion(org.evosuite.assertion.Assertion) ContractViolation(org.evosuite.contracts.ContractViolation) Branch(org.evosuite.coverage.branch.Branch) Mutation(org.evosuite.coverage.mutation.Mutation)

Example 17 with Branch

use of org.evosuite.coverage.branch.Branch in project evosuite by EvoSuite.

the class ExecutionTraceImpl method getTraceInDUCounterRange.

/**
 * {@inheritDoc}
 *
 * Returns a copy of this trace where all MethodCall-information associated
 * with duCounters outside the range of the given duCounter-Start and -End
 * is removed from the finished_calls-traces
 *
 * finished_calls without any point in the trace at which the given
 * duCounter range is hit are removed completely
 *
 * Also traces for methods other then the one that holds the given targetDU
 * are removed as well as trace information that would pass the branch of
 * the given targetDU If wantToCoverTargetDU is false instead those
 * targetDUBranch information is removed that would pass the alternative
 * branch of targetDU
 *
 * The latter is because this method only gets called when the given
 * targetDU was not active in the given duCounter-range if and only if
 * wantToCoverTargetDU is set, and since useFitness calculation is on branch
 * level and the branch of the targetDU can be passed before the targetDU is
 * passed this can lead to a flawed branchFitness.
 *
 * WARNING: this will not affect this.true_distances and other fields of
 * ExecutionTrace this only affects the finished_calls field (which should
 * suffice for BranchCoverageFitness-calculation)
 */
@Override
public ExecutionTrace getTraceInDUCounterRange(DefUse targetDU, boolean wantToCoverTargetDU, int duCounterStart, int duCounterEnd) {
    if (duCounterStart > duCounterEnd) {
        throw new IllegalArgumentException("start has to be lesser or equal end");
    /*
			 * // DONE: bug // this still has a major flaw: s.
			 * MeanTestClass.mean(): // right now its like we map branches to
			 * activeDefenitions // but especially in the root branch of a
			 * method // activeDefenitions change during execution time // FIX:
			 * in order to avoid these false positives remove all information //
			 * for a certain branch if some information for that branch is
			 * supposed to be removed // subTodo since branchPassed() only gets
			 * called when a branch is passed initially // fake calls to
			 * branchPassed() have to be made whenever a DU is passed // s.
			 * definitionPassed(), usePassed() and
			 * addFakeActiveMethodCallInformation()
			 * 
			 * // DONE: new bug // turns out thats an over-approximation that
			 * makes it // impossible to cover some potentially coverable goals
			 * 
			 * // completely new: // if your definition gets overwritten in a
			 * trace // the resulting fitness should be the fitness of not
			 * taking the branch with the overwriting definition // DONE: in
			 * order to do that don't remove older trace information for an
			 * overwritten branch // but rather set the true and false distance
			 * of that previous branch information to the distance of not taking
			 * the overwriting branch // done differently: s.
			 * DefUseCoverageTestFitness.getFitness()
			 */
    }
    ExecutionTraceImpl r = clone();
    Branch targetDUBranch = targetDU.getControlDependentBranch();
    ArrayList<Integer> removableCalls = new ArrayList<Integer>();
    for (int callPos = 0; callPos < r.finishedCalls.size(); callPos++) {
        MethodCall call = r.finishedCalls.get(callPos);
        // check if call is for the method of targetDU
        if (!call.methodName.equals(targetDU.getMethodName())) {
            removableCalls.add(callPos);
            continue;
        }
        ArrayList<Integer> removableIndices = new ArrayList<Integer>();
        for (int i = 0; i < call.defuseCounterTrace.size(); i++) {
            int currentDUCounter = call.defuseCounterTrace.get(i);
            int currentBranchBytecode = call.branchTrace.get(i);
            if (currentDUCounter < duCounterStart || currentDUCounter > duCounterEnd)
                removableIndices.add(i);
            else if (currentBranchBytecode == targetDUBranch.getInstruction().getInstructionId()) {
                // only remove this point in the trace if it would cover
                // targetDU
                boolean targetExpressionValue = targetDU.getControlDependentBranchExpressionValue();
                if (targetExpressionValue) {
                    if (call.trueDistanceTrace.get(i) == 0.0)
                        removableIndices.add(i);
                } else {
                    if (call.falseDistanceTrace.get(i) == 0.0)
                        removableIndices.add(i);
                }
            }
        }
        removeFromFinishCall(call, removableIndices);
        if (call.defuseCounterTrace.size() == 0)
            removableCalls.add(callPos);
    }
    removeFinishCalls(r, removableCalls);
    return new ExecutionTraceProxy(r);
}
Also used : Branch(org.evosuite.coverage.branch.Branch) ArrayList(java.util.ArrayList)

Example 18 with Branch

use of org.evosuite.coverage.branch.Branch in project evosuite by EvoSuite.

the class LCSAJPool method add_lcsaj.

/**
 * <p>add_lcsaj</p>
 *
 * @param className a {@link java.lang.String} object.
 * @param methodName a {@link java.lang.String} object.
 * @param lcsaj a {@link org.evosuite.coverage.lcsaj.LCSAJ} object.
 */
public static void add_lcsaj(String className, String methodName, LCSAJ lcsaj) {
    if (!lcsaj_map.containsKey(className))
        lcsaj_map.put(className, new HashMap<String, List<LCSAJ>>());
    if (!lcsaj_map.get(className).containsKey(methodName))
        lcsaj_map.get(className).put(methodName, new ArrayList<LCSAJ>());
    lcsaj_map.get(className).get(methodName).add(lcsaj);
    lcsaj.setID(lcsaj_map.get(className).get(methodName).size());
    Logger logger = LoggerFactory.getLogger(LCSAJPool.class);
    logger.info("Adding LCSAJ: " + lcsaj);
    for (Branch branch : lcsaj.getBranchInstructions()) {
        logger.info(" -> " + branch.getInstruction().getASMNodeString());
    }
}
Also used : HashMap(java.util.HashMap) Branch(org.evosuite.coverage.branch.Branch) ArrayList(java.util.ArrayList) Logger(org.slf4j.Logger)

Example 19 with Branch

use of org.evosuite.coverage.branch.Branch in project evosuite by EvoSuite.

the class LCSAJ method lookupInstruction.

/**
 * <p>
 * lookupInstruction
 * </p>
 *
 * @param id
 *            a int.
 * @param instruction
 *            a {@link org.evosuite.graphs.cfg.BytecodeInstruction} object.
 */
public void lookupInstruction(int id, BytecodeInstruction instruction) {
    lastAccessedNode = instruction.getASMNode();
    if (instruction.isBranch()) {
        Branch branch = BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranchForInstruction(instruction);
        branches.add(branch);
    } else if (instruction.isReturn() || instruction.isThrow() || instruction.isGoto()) {
        if (Properties.STRATEGY != Strategy.EVOSUITE && !BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).isKnownAsBranch(instruction)) {
            instruction.forceBranch();
            BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).registerAsBranch(instruction);
            logger.info("Registering new branch");
        }
        Branch branch = BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranchForInstruction(instruction);
        branches.add(branch);
    }
}
Also used : 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