Search in sources :

Example 6 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class CoverageCriteriaAnalyzer method reinstrument.

private static void reinstrument(TestSuiteChromosome testSuite, Properties.Criterion criterion) {
    if (ArrayUtil.contains(Properties.SECONDARY_OBJECTIVE, Properties.SecondaryObjective.IBRANCH)) {
        ExecutionTracer.enableContext();
    }
    if (!ExecutionTracer.isTraceCallsEnabled()) {
        ExecutionTracer.enableTraceCalls();
    }
    testSuite.setChanged(true);
    for (TestChromosome test : testSuite.getTestChromosomes()) {
        test.setChanged(true);
        // clears last execution result and last mutation result
        test.clearCachedResults();
    }
    Properties.Criterion[] oldCriterion = Arrays.copyOf(Properties.CRITERION, Properties.CRITERION.length);
    Properties.CRITERION = new Properties.Criterion[] { criterion };
    logger.info("Re-instrumenting for criterion: " + criterion);
    TestGenerationContext.getInstance().resetContext();
    // Need to load class explicitly in case there are no test cases.
    // If there are tests, then this is redundant
    Properties.getInitializedTargetClass();
    // TODO: Now all existing test cases have reflection objects pointing to the wrong classloader
    logger.info("Changing classloader of test suite for criterion: " + criterion);
    for (TestChromosome test : testSuite.getTestChromosomes()) {
        DefaultTestCase dtest = (DefaultTestCase) test.getTestCase();
        dtest.changeClassLoader(TestGenerationContext.getInstance().getClassLoaderForSUT());
    }
    Properties.CRITERION = oldCriterion;
}
Also used : Criterion(org.evosuite.Properties.Criterion) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Properties(org.evosuite.Properties) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 7 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class AllDefsCoverageSuiteFitness method getFitness.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.evosuite.ga.FitnessFunction#getFitness(org.
	 * evosuite.ga.Chromosome)
	 */
/**
 * {@inheritDoc}
 */
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> individual) {
    logger.trace("Calculating defuse fitness");
    TestSuiteChromosome suite = (TestSuiteChromosome) individual;
    List<ExecutionResult> results = runTestSuite(suite);
    double fitness = 0.0;
    Set<TestFitnessFunction> coveredGoals = new HashSet<TestFitnessFunction>();
    for (TestFitnessFunction goal : goals) {
        if (coveredGoals.contains(goal))
            continue;
        double goalFitness = 2.0;
        for (ExecutionResult result : results) {
            TestChromosome tc = new TestChromosome();
            tc.setTestCase(result.test);
            double resultFitness = goal.getFitness(tc, result);
            if (resultFitness < goalFitness)
                goalFitness = resultFitness;
            if (goalFitness == 0.0) {
                result.test.addCoveredGoal(goal);
                // System.out.println(goal.toString());
                // System.out.println(result.test.toCode());
                // System.out.println(resultFitness);
                coveredGoals.add(goal);
                break;
            }
        }
        fitness += goalFitness;
    }
    updateIndividual(this, individual, fitness);
    setSuiteCoverage(suite, coveredGoals);
    return fitness;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) AbstractTestSuiteChromosome(org.evosuite.testsuite.AbstractTestSuiteChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet)

Example 8 with TestChromosome

use of org.evosuite.testcase.TestChromosome 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 9 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class OutputCoverageSuiteFitness method computeDistance.

public double computeDistance(List<ExecutionResult> results, Set<TestFitnessFunction> setOfCoveredGoals) {
    Map<OutputCoverageTestFitness, Double> mapDistances = new LinkedHashMap<OutputCoverageTestFitness, Double>();
    for (OutputCoverageTestFitness testFitness : this.outputCoverageGoals) {
        mapDistances.put(testFitness, 1.0);
    }
    for (ExecutionResult result : results) {
        if (result.hasTimeout() || result.hasTestException()) {
            continue;
        }
        TestChromosome test = new TestChromosome();
        test.setTestCase(result.test);
        test.setLastExecutionResult(result);
        test.setChanged(false);
        Iterator<OutputCoverageTestFitness> it = this.outputCoverageGoals.iterator();
        while (it.hasNext()) {
            OutputCoverageTestFitness testFitness = it.next();
            if (!mapDistances.containsKey(testFitness)) {
                continue;
            }
            // archive is updated by the TestFitnessFunction class
            double distance = testFitness.getFitness(test, result);
            mapDistances.put(testFitness, Math.min(distance, mapDistances.get(testFitness)));
            if (distance == 0.0) {
                mapDistances.remove(testFitness);
                // helper to count the number of covered goals
                setOfCoveredGoals.add(testFitness);
                // goal to not be considered by the next iteration of the evolutionary algorithm
                this.toRemoveGoals.add(testFitness);
            }
        }
    }
    double distance = 0.0;
    if (!mapDistances.isEmpty()) {
        distance = mapDistances.values().stream().reduce(Double::sum).get().doubleValue();
    }
    return distance;
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome) LinkedHashMap(java.util.LinkedHashMap)

Example 10 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class OnlyLineCoverageSuiteFitness method analyzeTraces.

/**
 * Iterate over all execution results and summarize statistics
 *
 * @param results
 * @param coveredLines
 * @return
 */
private boolean analyzeTraces(List<ExecutionResult> results, Set<Integer> coveredLines) {
    boolean hasTimeoutOrTestException = false;
    for (ExecutionResult result : results) {
        if (result.hasTimeout() || result.hasTestException()) {
            hasTimeoutOrTestException = true;
            continue;
        }
        TestChromosome test = new TestChromosome();
        test.setTestCase(result.test);
        test.setLastExecutionResult(result);
        test.setChanged(false);
        for (Integer goalID : this.lineGoals.keySet()) {
            TestFitnessFunction goal = this.lineGoals.get(goalID);
            // archive is updated by the TestFitnessFunction class
            double fit = goal.getFitness(test, result);
            if (fit == 0.0) {
                // helper to count the number of covered goals
                coveredLines.add(goalID);
                // goal to not be considered by the next iteration of the evolutionary algorithm
                this.toRemoveLines.add(goalID);
            }
        }
    }
    return hasTimeoutOrTestException;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome)

Aggregations

TestChromosome (org.evosuite.testcase.TestChromosome)128 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)47 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)33 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)22 ArrayList (java.util.ArrayList)17 TestCase (org.evosuite.testcase.TestCase)17 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)16 HashSet (java.util.HashSet)15 Properties (org.evosuite.Properties)15 Test (org.junit.Test)15 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)14 HashMap (java.util.HashMap)11 DefaultLocalSearchObjective (org.evosuite.ga.localsearch.DefaultLocalSearchObjective)10 AbstractTestSuiteChromosome (org.evosuite.testsuite.AbstractTestSuiteChromosome)8 LinkedHashMap (java.util.LinkedHashMap)7 Foo (com.examples.with.different.packagename.symbolic.Foo)6 LinkedHashSet (java.util.LinkedHashSet)6 Set (java.util.Set)6 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)6 Map (java.util.Map)5