Search in sources :

Example 6 with BranchCoverageTestFitness

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

the class LineCoverageTestFitness method setupDependencies.

private void setupDependencies() {
    goalInstruction = BytecodeInstructionPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getFirstInstructionAtLineNumber(className, methodName, line);
    if (goalInstruction == null)
        return;
    Set<ControlDependency> cds = goalInstruction.getControlDependencies();
    for (ControlDependency cd : cds) {
        BranchCoverageTestFitness fitness = BranchCoverageFactory.createBranchCoverageTestFitness(cd);
        branchFitnesses.add(fitness);
    }
    if (goalInstruction.isRootBranchDependent())
        branchFitnesses.add(BranchCoverageFactory.createRootBranchTestFitness(goalInstruction));
    if (cds.isEmpty() && !goalInstruction.isRootBranchDependent())
        throw new IllegalStateException("expect control dependencies to be empty only for root dependent instructions: " + toString());
    if (branchFitnesses.isEmpty())
        throw new IllegalStateException("an instruction is at least on the root branch of it's method");
    branchFitnesses.sort((a, b) -> a.compareTo(b));
}
Also used : BranchCoverageTestFitness(org.evosuite.coverage.branch.BranchCoverageTestFitness) ControlDependency(org.evosuite.graphs.cfg.ControlDependency)

Example 7 with BranchCoverageTestFitness

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

the class StatementCoverageTestFitness method explain.

/**
 * <p>
 * explain
 * </p>
 *
 * @return a {@link java.lang.String} object.
 */
public String explain() {
    StringBuilder r = new StringBuilder();
    r.append("StatementCoverageTestFitness for ");
    r.append(goalInstruction.toString());
    r.append(" in " + goalInstruction.getMethodName());
    r.append("\n");
    r.append("CDS:\n");
    for (BranchCoverageTestFitness branchFitness : branchFitnesses) {
        r.append("\t" + branchFitness.toString());
    }
    return r.toString();
}
Also used : BranchCoverageTestFitness(org.evosuite.coverage.branch.BranchCoverageTestFitness)

Example 8 with BranchCoverageTestFitness

use of org.evosuite.coverage.branch.BranchCoverageTestFitness 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)

Example 9 with BranchCoverageTestFitness

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

the class SearchStatistics method calculateCoveredBranchesBitString.

private String calculateCoveredBranchesBitString(TestSuiteChromosome best) {
    StringBuffer buffer = new StringBuffer(1024);
    BranchCoverageFactory factory = new BranchCoverageFactory();
    List<BranchCoverageTestFitness> goals = factory.getCoverageGoals();
    Collections.sort(goals);
    for (BranchCoverageTestFitness fitness : goals) {
        boolean covered = false;
        for (TestChromosome test1 : best.tests) {
            if (fitness.isCovered(test1)) {
                covered = true;
                buffer.append("1");
                break;
            }
        }
        if (!covered) {
            buffer.append("0");
        }
    }
    return buffer.toString();
}
Also used : BranchCoverageTestFitness(org.evosuite.coverage.branch.BranchCoverageTestFitness) BranchCoverageFactory(org.evosuite.coverage.branch.BranchCoverageFactory) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 10 with BranchCoverageTestFitness

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

the class SearchStatistics method searchStarted.

/**
 * {@inheritDoc}
 */
@Override
public void searchStarted(GeneticAlgorithm<?> algorithm) {
    super.searchStarted(algorithm);
    StatisticEntry entry = statistics.get(statistics.size() - 1);
    entry.total_branches = Properties.TARGET_CLASS_PREFIX.isEmpty() ? BranchPool.getBranchCountForMemberClasses(Properties.TARGET_CLASS) : BranchPool.getBranchCountForPrefix(Properties.TARGET_CLASS_PREFIX);
    entry.branchless_methods = Properties.TARGET_CLASS_PREFIX.isEmpty() ? BranchPool.getBranchlessMethodsMemberClasses(Properties.TARGET_CLASS).size() : BranchPool.getBranchlessMethodsPrefix(Properties.TARGET_CLASS_PREFIX).size();
    entry.total_methods = Properties.TARGET_CLASS_PREFIX.isEmpty() ? CFGMethodAdapter.getNumMethodsMemberClasses(Properties.TARGET_CLASS) : CFGMethodAdapter.getNumMethodsPrefix(Properties.TARGET_CLASS_PREFIX);
    // FIXME: since this class is classified as 'deprecate' we are just assuming one fitness function
    List<? extends TestFitnessFunction> goals = TestSuiteGenerator.getFitnessFactory().get(0).getCoverageGoals();
    entry.total_goals = goals.size();
    // for (TestFitnessFunction f : TestSuiteGenerator.getFitnessFactory().getCoverageGoals()) {
    for (TestFitnessFunction f : goals) {
        if (f instanceof BranchCoverageTestFitness) {
            BranchCoverageTestFitness b = (BranchCoverageTestFitness) f;
            if (b.getBranch() != null && b.getBranch().isInstrumented()) {
            }
        }
    }
// removed the code below with the one above, in order to have these
// values for other criteria as well
// if (algorithm.getFitnessFunction() instanceof
// BranchCoverageSuiteFitness) {
// BranchCoverageSuiteFitness fitness = (BranchCoverageSuiteFitness)
// algorithm.getFitnessFunction();
// entry.total_branches = fitness.total_branches;
// entry.branchless_methods = fitness.branchless_methods;
// entry.total_methods = fitness.total_methods;
// }
}
Also used : BranchCoverageTestFitness(org.evosuite.coverage.branch.BranchCoverageTestFitness) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction)

Aggregations

BranchCoverageTestFitness (org.evosuite.coverage.branch.BranchCoverageTestFitness)10 ArrayList (java.util.ArrayList)3 BranchCoverageFactory (org.evosuite.coverage.branch.BranchCoverageFactory)3 HashSet (java.util.HashSet)2 BranchCoverageGoal (org.evosuite.coverage.branch.BranchCoverageGoal)2 CallContext (org.evosuite.setup.CallContext)2 CallGraph (org.evosuite.setup.callgraph.CallGraph)2 Branch (org.evosuite.coverage.branch.Branch)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 TestChromosome (org.evosuite.testcase.TestChromosome)1 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)1 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)1 Test (org.junit.Test)1