Search in sources :

Example 1 with CallContext

use of org.evosuite.setup.CallContext 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 CallContext

use of org.evosuite.setup.CallContext in project evosuite by EvoSuite.

the class CBranchSuiteFitness method getFitness.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.evosuite.ga.FitnessFunction#getFitness(org.evosuite.ga.Chromosome)
	 */
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> suite) {
    // branchFitness.getFitness(suite);
    double fitness = 0.0;
    List<ExecutionResult> results = runTestSuite(suite);
    Map<CBranchTestFitness, Double> distanceMap = new LinkedHashMap<>();
    Map<Integer, Integer> callCounter = new LinkedHashMap<>();
    Map<Integer, Integer> branchCounter = new LinkedHashMap<>();
    for (ExecutionResult result : results) {
        if (result.hasTimeout() || result.hasTestException()) {
            continue;
        }
        // Determine minimum branch distance for each branch in each context
        assert (result.getTrace().getTrueDistancesContext().keySet().size() == result.getTrace().getFalseDistancesContext().keySet().size());
        TestChromosome test = new TestChromosome();
        test.setTestCase(result.test);
        test.setLastExecutionResult(result);
        test.setChanged(false);
        for (Integer branchId : result.getTrace().getTrueDistancesContext().keySet()) {
            Map<CallContext, Double> trueMap = result.getTrace().getTrueDistancesContext().get(branchId);
            Map<CallContext, Double> falseMap = result.getTrace().getFalseDistancesContext().get(branchId);
            for (CallContext context : trueMap.keySet()) {
                CBranchTestFitness goalT = getContextGoal(branchId, context, true);
                if (goalT == null)
                    continue;
                double distanceT = normalize(trueMap.get(context));
                if (distanceMap.get(goalT) == null || distanceMap.get(goalT) > distanceT) {
                    distanceMap.put(goalT, distanceT);
                }
                if (Double.compare(distanceT, 0.0) == 0) {
                    if (removedGoals.contains(goalT))
                        continue;
                    test.getTestCase().addCoveredGoal(goalT);
                    toRemoveGoals.add(goalT);
                }
                if (Properties.TEST_ARCHIVE) {
                    Archive.getArchiveInstance().updateArchive(goalT, test, distanceT);
                }
            }
            for (CallContext context : falseMap.keySet()) {
                CBranchTestFitness goalF = getContextGoal(branchId, context, false);
                if (goalF == null)
                    continue;
                double distanceF = normalize(falseMap.get(context));
                if (distanceMap.get(goalF) == null || distanceMap.get(goalF) > distanceF) {
                    distanceMap.put(goalF, distanceF);
                }
                if (Double.compare(distanceF, 0.0) == 0) {
                    if (removedGoals.contains(goalF))
                        continue;
                    test.getTestCase().addCoveredGoal(goalF);
                    toRemoveGoals.add(goalF);
                }
                if (Properties.TEST_ARCHIVE) {
                    Archive.getArchiveInstance().updateArchive(goalF, test, distanceF);
                }
            }
        }
        for (Entry<Integer, Map<CallContext, Integer>> entry : result.getTrace().getPredicateContextExecutionCount().entrySet()) {
            for (Entry<CallContext, Integer> value : entry.getValue().entrySet()) {
                int count = value.getValue();
                CBranchTestFitness goalT = getContextGoal(entry.getKey(), value.getKey(), true);
                if (goalT != null) {
                    if (branchCounter.get(goalT.getGenericContextBranchIdentifier()) == null || branchCounter.get(goalT.getGenericContextBranchIdentifier()) < count) {
                        branchCounter.put(goalT.getGenericContextBranchIdentifier(), count);
                    }
                } else {
                    CBranchTestFitness goalF = getContextGoal(entry.getKey(), value.getKey(), false);
                    if (goalF != null) {
                        if (branchCounter.get(goalF.getGenericContextBranchIdentifier()) == null || branchCounter.get(goalF.getGenericContextBranchIdentifier()) < count) {
                            branchCounter.put(goalF.getGenericContextBranchIdentifier(), count);
                        }
                    } else
                        continue;
                }
            }
        }
        for (Entry<String, Map<CallContext, Integer>> entry : result.getTrace().getMethodContextCount().entrySet()) {
            for (Entry<CallContext, Integer> value : entry.getValue().entrySet()) {
                CBranchTestFitness goal = getContextGoal(entry.getKey(), value.getKey());
                if (goal == null)
                    continue;
                int count = value.getValue();
                if (callCounter.get(goal.hashCode()) == null || callCounter.get(goal.hashCode()) < count) {
                    callCounter.put(goal.hashCode(), count);
                }
                if (count > 0) {
                    if (removedGoals.contains(goal))
                        continue;
                    test.getTestCase().addCoveredGoal(goal);
                    toRemoveGoals.add(goal);
                }
                if (Properties.TEST_ARCHIVE) {
                    Archive.getArchiveInstance().updateArchive(goal, test, count == 0 ? 1.0 : 0.0);
                }
            }
        }
    }
    int numCoveredGoals = removedGoals.size();
    for (CBranchTestFitness goal : branchGoals) {
        if (removedGoals.contains(goal))
            continue;
        Double distance = distanceMap.get(goal);
        if (distance == null)
            distance = 1.0;
        if (goal.getBranch() == null) {
            Integer count = callCounter.get(goal.hashCode());
            if (count == null || count == 0) {
                fitness += 1;
            } else {
                numCoveredGoals++;
            }
        } else {
            Integer count = branchCounter.get(goal.getGenericContextBranchIdentifier());
            if (count == null || count == 0)
                fitness += 1;
            else if (count == 1)
                fitness += 0.5;
            else {
                if (Double.compare(distance, 0.0) == 0) {
                    numCoveredGoals++;
                }
                fitness += distance;
            }
        }
    }
    if (!branchGoals.isEmpty()) {
        suite.setCoverage(this, (double) numCoveredGoals / (double) branchGoals.size());
    } else {
        suite.setCoverage(this, 1);
    }
    suite.setNumOfCoveredGoals(this, numCoveredGoals);
    suite.setNumOfNotCoveredGoals(this, branchGoals.size() - numCoveredGoals);
    updateIndividual(this, suite, fitness);
    return fitness;
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) CallContext(org.evosuite.setup.CallContext) LinkedHashMap(java.util.LinkedHashMap) TestChromosome(org.evosuite.testcase.TestChromosome) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with CallContext

use of org.evosuite.setup.CallContext 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 CallContext

use of org.evosuite.setup.CallContext in project evosuite by EvoSuite.

the class IBranchSuiteFitness method getFitness.

public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> suite, boolean updateChromosome) {
    // branchFitness.getFitness(suite);
    double fitness = 0.0;
    List<ExecutionResult> results = runTestSuite(suite);
    Map<IBranchTestFitness, Double> distanceMap = new LinkedHashMap<>();
    Map<IBranchTestFitness, Integer> callCount = new LinkedHashMap<>();
    for (ExecutionResult result : results) {
        if (result.hasTimeout() || result.hasTestException()) {
            continue;
        }
        TestChromosome test = new TestChromosome();
        test.setTestCase(result.test);
        test.setLastExecutionResult(result);
        test.setChanged(false);
        for (Integer branchId : result.getTrace().getTrueDistancesContext().keySet()) {
            Map<CallContext, Double> trueMap = result.getTrace().getTrueDistancesContext().get(branchId);
            for (CallContext context : trueMap.keySet()) {
                IBranchTestFitness goalT = getContextGoal(branchId, context, true);
                if (goalT == null || removedBranchesT.contains(goalT))
                    continue;
                double distanceT = normalize(trueMap.get(context));
                if (distanceMap.get(goalT) == null || distanceMap.get(goalT) > distanceT) {
                    distanceMap.put(goalT, distanceT);
                }
                if (Double.compare(distanceT, 0.0) == 0) {
                    if (updateChromosome)
                        test.getTestCase().addCoveredGoal(goalT);
                    toRemoveBranchesT.add(goalT);
                }
                if (Properties.TEST_ARCHIVE) {
                    Archive.getArchiveInstance().updateArchive(goalT, test, distanceT);
                }
            }
        }
        for (Integer branchId : result.getTrace().getFalseDistancesContext().keySet()) {
            Map<CallContext, Double> falseMap = result.getTrace().getFalseDistancesContext().get(branchId);
            for (CallContext context : falseMap.keySet()) {
                IBranchTestFitness goalF = getContextGoal(branchId, context, false);
                if (goalF == null || removedBranchesF.contains(goalF))
                    continue;
                double distanceF = normalize(falseMap.get(context));
                if (distanceMap.get(goalF) == null || distanceMap.get(goalF) > distanceF) {
                    distanceMap.put(goalF, distanceF);
                }
                if (Double.compare(distanceF, 0.0) == 0) {
                    if (updateChromosome)
                        test.getTestCase().addCoveredGoal(goalF);
                    toRemoveBranchesF.add(goalF);
                }
                if (Properties.TEST_ARCHIVE) {
                    Archive.getArchiveInstance().updateArchive(goalF, test, distanceF);
                }
            }
        }
        for (Entry<String, Map<CallContext, Integer>> entry : result.getTrace().getMethodContextCount().entrySet()) {
            for (Entry<CallContext, Integer> value : entry.getValue().entrySet()) {
                IBranchTestFitness goal = getContextGoal(entry.getKey(), value.getKey());
                if (goal == null || removedRootBranches.contains(goal))
                    continue;
                int count = value.getValue();
                if (callCount.get(goal) == null || callCount.get(goal) < count) {
                    callCount.put(goal, count);
                }
                if (count > 0) {
                    if (updateChromosome)
                        result.test.addCoveredGoal(goal);
                    toRemoveRootBranches.add(goal);
                }
            }
        }
    }
    int numCoveredGoals = 0;
    for (IBranchTestFitness goal : branchGoals) {
        Double distance = distanceMap.get(goal);
        if (distance == null)
            distance = 1.0;
        if (goal.getBranch() == null) {
            Integer count = callCount.get(goal);
            if (count == null || count == 0) {
                fitness += 1;
            } else {
                numCoveredGoals++;
            }
        } else {
            if (distance == 0.0) {
                numCoveredGoals++;
            }
            fitness += distance;
        }
    }
    if (updateChromosome) {
        numCoveredGoals += removedBranchesF.size();
        numCoveredGoals += removedBranchesT.size();
        numCoveredGoals += removedRootBranches.size();
        if (totGoals > 0) {
            suite.setCoverage(this, (double) numCoveredGoals / (double) totGoals);
        }
        suite.setNumOfCoveredGoals(this, numCoveredGoals);
        suite.setNumOfNotCoveredGoals(this, totGoals - numCoveredGoals);
        updateIndividual(this, suite, fitness);
    }
    return fitness;
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) CallContext(org.evosuite.setup.CallContext) LinkedHashMap(java.util.LinkedHashMap) TestChromosome(org.evosuite.testcase.TestChromosome) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with CallContext

use of org.evosuite.setup.CallContext in project evosuite by EvoSuite.

the class ExecutionTraceImpl method updateMethodContextMaps.

/**
 * @param className
 * @param methodName
 * @param caller
 */
private void updateMethodContextMaps(String className, String methodName, Object caller) {
    String id = className + "." + methodName;
    if (!coveredMethodContext.containsKey(id)) {
        coveredMethodContext.put(id, new HashMap<>());
    }
    CallContext context = new CallContext(new Throwable().getStackTrace());
    if (!coveredMethodContext.get(id).containsKey(context)) {
        coveredMethodContext.get(id).put(context, 1);
    } else {
        coveredMethodContext.get(id).put(context, coveredMethodContext.get(id).get(context) + 1);
    }
}
Also used : CallContext(org.evosuite.setup.CallContext)

Aggregations

CallContext (org.evosuite.setup.CallContext)9 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)4 Call (org.evosuite.setup.Call)3 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 BranchCoverageFactory (org.evosuite.coverage.branch.BranchCoverageFactory)2 BranchCoverageTestFitness (org.evosuite.coverage.branch.BranchCoverageTestFitness)2 CallGraph (org.evosuite.setup.callgraph.CallGraph)2 TestChromosome (org.evosuite.testcase.TestChromosome)2 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)2