Search in sources :

Example 1 with BranchCoverageGoal

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

the class TryCatchCoverageFactory method getCoverageGoals.

/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.evosuite.coverage.TestCoverageFactory#getCoverageGoals()
	 */
/**
 * {@inheritDoc}
 */
@Override
public List<TryCatchCoverageTestFitness> getCoverageGoals() {
    List<TryCatchCoverageTestFitness> goals = new ArrayList<>();
    // logger.info("Getting branches");
    for (String className : BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).knownClasses()) {
        final MethodNameMatcher matcher = new MethodNameMatcher();
        // Branches
        for (String methodName : BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).knownMethods(className)) {
            if (!matcher.methodMatches(methodName)) {
                logger.info("Method " + methodName + " does not match criteria. ");
                continue;
            }
            for (Branch b : BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).retrieveBranchesInMethod(className, methodName)) {
                if (b.isInstrumented()) {
                    goals.add(new TryCatchCoverageTestFitness(new BranchCoverageGoal(b, true, b.getClassName(), b.getMethodName())));
                    goals.add(new TryCatchCoverageTestFitness(new BranchCoverageGoal(b, false, b.getClassName(), b.getMethodName())));
                }
            }
        }
    }
    return goals;
}
Also used : MethodNameMatcher(org.evosuite.coverage.MethodNameMatcher) Branch(org.evosuite.coverage.branch.Branch) ArrayList(java.util.ArrayList) BranchCoverageGoal(org.evosuite.coverage.branch.BranchCoverageGoal)

Example 2 with BranchCoverageGoal

use of org.evosuite.coverage.branch.BranchCoverageGoal 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 3 with BranchCoverageGoal

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

the class MutationTestFitness method getExecutionDistance.

/**
 * <p>
 * getExecutionDistance
 * </p>
 *
 * @param result
 *            a {@link org.evosuite.testcase.execution.ExecutionResult} object.
 * @return a double.
 */
protected double getExecutionDistance(ExecutionResult result) {
    double fitness = 0.0;
    if (!result.getTrace().wasMutationTouched(mutation.getId()))
        fitness += diameter;
    // Get control flow distance
    if (controlDependencies.isEmpty()) {
        // If mutant was not executed, this can be either because of an exception, or because the method was not executed
        String key = mutation.getClassName() + "." + mutation.getMethodName();
        if (result.getTrace().getCoveredMethods().contains(key)) {
            logger.debug("Target method " + key + " was executed");
        } else {
            logger.debug("Target method " + key + " was not executed");
            fitness += diameter;
        }
    } else {
        ControlFlowDistance cfgDistance = null;
        for (BranchCoverageGoal dependency : controlDependencies) {
            logger.debug("Checking dependency...");
            ControlFlowDistance distance = dependency.getDistance(result);
            if (cfgDistance == null)
                cfgDistance = distance;
            else {
                if (distance.compareTo(cfgDistance) < 0)
                    cfgDistance = distance;
            }
        }
        if (cfgDistance != null) {
            logger.debug("Found control dependency");
            fitness += cfgDistance.getResultingBranchFitness();
        }
    }
    return fitness;
}
Also used : ControlFlowDistance(org.evosuite.coverage.ControlFlowDistance) BranchCoverageGoal(org.evosuite.coverage.branch.BranchCoverageGoal)

Example 4 with BranchCoverageGoal

use of org.evosuite.coverage.branch.BranchCoverageGoal 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;
}
Also used : BranchCoverageGoal(org.evosuite.coverage.branch.BranchCoverageGoal) HashSet(java.util.HashSet) ControlDependency(org.evosuite.graphs.cfg.ControlDependency)

Example 5 with BranchCoverageGoal

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

the class LCSAJCoverageTestFitness method getFitness.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.evosuite.testcase.TestFitnessFunction#getFitness(de.unisb
	 * .cs.st.evosuite.testcase.TestChromosome,
	 * org.evosuite.testcase.ExecutionResult)
	 */
/**
 * {@inheritDoc}
 */
@Override
public double getFitness(TestChromosome individual, ExecutionResult result) {
    approach = lcsaj.length();
    double savedFitness = approach;
    logger.debug("Evaluating fitness for " + lcsaj);
    // for all method calls:
    for (MethodCall call : result.getTrace().getMethodCalls()) {
        double currentFitness = approach;
        // if method call is the method of the LCSAJ
        if (call.className.equals(lcsaj.getClassName()) && call.methodName.equals(lcsaj.getMethodName())) {
            int lcsaj_position = 0;
            boolean found = false;
            logger.debug("New call: " + call.className + "." + call.methodName + " " + call.branchTrace.size());
            // For each branch that was passed in this call
            for (int i = 0; i < call.branchTrace.size(); i++) {
                int actualBranch = call.branchTrace.get(i);
                int lcsaj_branchID = lcsaj.getBranchID(lcsaj_position);
                double false_distance = call.falseDistanceTrace.get(i);
                double true_distance = call.trueDistanceTrace.get(i);
                logger.debug("Current branch: " + call.branchTrace.get(i) + ": " + true_distance + "/" + false_distance + ", need " + lcsaj.getBranchID(lcsaj_position));
                if (actualBranch == lcsaj_branchID) {
                    if (lcsaj_position == 0)
                        found = true;
                    currentFitness -= 1.0;
                    if (actualBranch == lcsaj_finalBranchID) {
                        currentFitness += normalize(true_distance);
                        if (currentFitness < savedFitness) {
                            savedFitness = currentFitness;
                        }
                        if (lcsaj_position > lcsaj.getdPositionReached())
                            lcsaj.setPositionReached(lcsaj_position);
                        lcsaj_position = 0;
                        currentFitness = approach;
                        continue;
                    } else if (false_distance > 0) {
                        logger.debug("Took a wrong turn with false distance " + false_distance);
                        currentFitness += normalize(false_distance);
                        if (currentFitness < savedFitness)
                            savedFitness = currentFitness;
                        if (lcsaj_position > lcsaj.getdPositionReached())
                            lcsaj.setPositionReached(lcsaj_position);
                        lcsaj_position = 0;
                        currentFitness = approach;
                        continue;
                    }
                    lcsaj_position++;
                    logger.debug("LCSAJ position: " + lcsaj_position);
                } else {
                    if (LCSAJPool.isLCSAJBranch(BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranch(actualBranch))) {
                        // logger.debug("Skipping pseudo branch");
                        continue;
                    }
                    if (lcsaj_position > lcsaj.getdPositionReached())
                        lcsaj.setPositionReached(lcsaj_position);
                    lcsaj_position = 0;
                    currentFitness = approach;
                }
            // }
            }
            if (Properties.STRATEGY != Strategy.EVOSUITE) {
                if (!found) {
                    logger.debug("Looking for approach to initial branch: " + lcsaj.getStartBranch() + " with ID " + lcsaj.getStartBranch().getActualBranchId());
                    BranchCoverageGoal goal = new BranchCoverageGoal(lcsaj.getStartBranch(), true, lcsaj.getClassName(), lcsaj.getMethodName());
                    BranchCoverageTestFitness dependentFitness = new BranchCoverageTestFitness(goal);
                    assert (currentFitness == approach);
                    currentFitness += dependentFitness.getFitness(individual, result);
                    if (currentFitness < savedFitness)
                        savedFitness = currentFitness;
                    // logger.debug("Initial branch has distance: "
                    // + dependentFitness.getFitness(individual, result));
                    // logger.debug("Dependencies of initial branch: ");
                    // for (Branch branch : lcsaj.getStartBranch().getAllControlDependentBranches()) {
                    // logger.debug(branch);
                    // }
                    logger.debug("Resulting fitness: " + savedFitness);
                } else {
                    logger.debug("Call does not match: " + call.className + "." + call.methodName + " " + call.branchTrace.size());
                }
            }
        }
    }
    updateIndividual(this, individual, savedFitness);
    return savedFitness;
}
Also used : BranchCoverageTestFitness(org.evosuite.coverage.branch.BranchCoverageTestFitness) BranchCoverageGoal(org.evosuite.coverage.branch.BranchCoverageGoal) MethodCall(org.evosuite.testcase.MethodCall)

Aggregations

BranchCoverageGoal (org.evosuite.coverage.branch.BranchCoverageGoal)5 ArrayList (java.util.ArrayList)2 Branch (org.evosuite.coverage.branch.Branch)2 BranchCoverageTestFitness (org.evosuite.coverage.branch.BranchCoverageTestFitness)2 HashSet (java.util.HashSet)1 ControlFlowDistance (org.evosuite.coverage.ControlFlowDistance)1 MethodNameMatcher (org.evosuite.coverage.MethodNameMatcher)1 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)1 BytecodeInstruction (org.evosuite.graphs.cfg.BytecodeInstruction)1 ControlDependency (org.evosuite.graphs.cfg.ControlDependency)1 CoverageGoalTestNameGenerationStrategy (org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy)1 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)1 MethodCall (org.evosuite.testcase.MethodCall)1 TestCase (org.evosuite.testcase.TestCase)1 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)1 Test (org.junit.Test)1