Search in sources :

Example 1 with BranchCoverageTestFitness

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

the class CBranchFitnessFactory method getCoverageGoals.

/* (non-Javadoc)
	 * @see org.evosuite.coverage.TestFitnessFactory#getCoverageGoals()
	 */
@Override
public List<CBranchTestFitness> getCoverageGoals() {
    // TODO this creates duplicate goals. Momentary fixed using a Set, but it should be optimised
    Set<CBranchTestFitness> goals = new HashSet<>();
    // retrieve set of branches
    BranchCoverageFactory branchFactory = new BranchCoverageFactory();
    List<BranchCoverageTestFitness> branchGoals = branchFactory.getCoverageGoals();
    CallGraph callGraph = DependencyAnalysis.getCallGraph();
    // try to find all occurrences of this branch in the call tree
    for (BranchCoverageTestFitness branchGoal : branchGoals) {
        logger.info("Adding context branches for " + branchGoal.toString());
        for (CallContext context : callGraph.getMethodEntryPoint(branchGoal.getClassName(), branchGoal.getMethod())) {
            goals.add(new CBranchTestFitness(branchGoal.getBranchGoal(), context));
        }
    }
    logger.info("Created " + goals.size() + " goals");
    return new ArrayList<CBranchTestFitness>(goals);
}
Also used : BranchCoverageTestFitness(org.evosuite.coverage.branch.BranchCoverageTestFitness) BranchCoverageFactory(org.evosuite.coverage.branch.BranchCoverageFactory) CallGraph(org.evosuite.setup.callgraph.CallGraph) ArrayList(java.util.ArrayList) CallContext(org.evosuite.setup.CallContext) HashSet(java.util.HashSet)

Example 2 with BranchCoverageTestFitness

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

the class LineCoverageTestFitness method getFitness.

/**
 * {@inheritDoc}
 *
 * Calculate fitness
 *
 * @param individual
 *            a {@link org.evosuite.testcase.ExecutableChromosome} object.
 * @param result
 *            a {@link org.evosuite.testcase.execution.ExecutionResult} object.
 * @return a double.
 */
@Override
public double getFitness(TestChromosome individual, ExecutionResult result) {
    double fitness = 1.0;
    // Deactivate coverage archive while measuring fitness, since branchcoverage fitness
    // evaluating will attempt to claim coverage for it in the archive
    boolean archive = Properties.TEST_ARCHIVE;
    Properties.TEST_ARCHIVE = false;
    if (result.getTrace().getCoveredLines().contains(this.line)) {
        fitness = 0.0;
    } else {
        double r = Double.MAX_VALUE;
        // Find minimum distance to satisfying any of the control dependencies
        for (BranchCoverageTestFitness branchFitness : branchFitnesses) {
            double newFitness = branchFitness.getFitness(individual, result);
            if (newFitness == 0.0) {
                // If the control dependency was covered, then likely
                // an exception happened before the line was reached
                newFitness = 1.0;
            } else {
                newFitness = 1.0 + normalize(newFitness);
            }
            if (newFitness < r)
                r = newFitness;
        }
        fitness = r;
    }
    Properties.TEST_ARCHIVE = archive;
    updateIndividual(this, individual, fitness);
    if (fitness == 0.0) {
        individual.getTestCase().addCoveredGoal(this);
    }
    if (Properties.TEST_ARCHIVE) {
        Archive.getArchiveInstance().updateArchive(this, individual, fitness);
    }
    return fitness;
}
Also used : BranchCoverageTestFitness(org.evosuite.coverage.branch.BranchCoverageTestFitness)

Example 3 with BranchCoverageTestFitness

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

the class IBranchFitnessFactory method getCoverageGoals.

/* (non-Javadoc)
	 * @see org.evosuite.coverage.TestFitnessFactory#getCoverageGoals()
	 */
@Override
public List<IBranchTestFitness> getCoverageGoals() {
    // TODO this creates duplicate goals. Momentary fixed using a Set.
    Set<IBranchTestFitness> goals = new HashSet<IBranchTestFitness>();
    // retrieve set of branches
    BranchCoverageFactory branchFactory = new BranchCoverageFactory();
    List<BranchCoverageTestFitness> branchGoals = branchFactory.getCoverageGoalsForAllKnownClasses();
    CallGraph callGraph = DependencyAnalysis.getCallGraph();
    // try to find all occurrences of this branch in the call tree
    for (BranchCoverageTestFitness branchGoal : branchGoals) {
        logger.info("Adding context branches for " + branchGoal.toString());
        for (CallContext context : callGraph.getAllContextsFromTargetClass(branchGoal.getClassName(), branchGoal.getMethod())) {
            // if is not possible to reach this branch from the target class, continue.
            if (context.isEmpty())
                continue;
            goals.add(new IBranchTestFitness(branchGoal.getBranchGoal(), context));
        }
    }
    assert (goals.size() >= branchFactory.getCoverageGoals().size());
    logger.info("Created " + goals.size() + " goals");
    return new ArrayList<IBranchTestFitness>(goals);
}
Also used : BranchCoverageTestFitness(org.evosuite.coverage.branch.BranchCoverageTestFitness) BranchCoverageFactory(org.evosuite.coverage.branch.BranchCoverageFactory) CallGraph(org.evosuite.setup.callgraph.CallGraph) ArrayList(java.util.ArrayList) CallContext(org.evosuite.setup.CallContext) HashSet(java.util.HashSet)

Example 4 with BranchCoverageTestFitness

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

the class StatementCoverageTestFitness method getFitness.

/**
 * {@inheritDoc}
 */
@Override
public double getFitness(TestChromosome individual, ExecutionResult result) {
    if (branchFitnesses.isEmpty())
        throw new IllegalStateException("expect to know at least one fitness for goalInstruction");
    if (result.hasTimeout() || result.hasTestException()) {
        updateIndividual(this, individual, Double.MAX_VALUE);
        return Double.MAX_VALUE;
    }
    double r = Double.MAX_VALUE;
    for (BranchCoverageTestFitness branchFitness : branchFitnesses) {
        double newFitness = branchFitness.getFitness(individual, result);
        if (newFitness == 0.0) {
            lastCoveringFitness = branchFitness;
            return 0.0;
        }
        if (newFitness < r)
            r = newFitness;
    }
    lastCoveringFitness = null;
    updateIndividual(this, individual, r);
    return r;
}
Also used : BranchCoverageTestFitness(org.evosuite.coverage.branch.BranchCoverageTestFitness)

Example 5 with BranchCoverageTestFitness

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

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