Search in sources :

Example 11 with TestChromosome

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

the class InputCoverageSuiteFitness method computeDistance.

private double computeDistance(List<ExecutionResult> results, Set<TestFitnessFunction> setOfCoveredGoals) {
    Map<InputCoverageTestFitness, Double> mapDistances = new LinkedHashMap<InputCoverageTestFitness, Double>();
    for (InputCoverageTestFitness testFitness : this.inputCoverageMap) {
        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<InputCoverageTestFitness> it = this.inputCoverageMap.iterator();
        while (it.hasNext()) {
            InputCoverageTestFitness 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 12 with TestChromosome

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

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

the class WeakMutationSuiteFitness method getFitness.

/* (non-Javadoc)
	 * @see org.evosuite.ga.FitnessFunction#getFitness(org.evosuite.ga.Chromosome)
	 */
/**
 * {@inheritDoc}
 */
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> individual) {
    /**
     * e.g. classes with only static constructors
     */
    if (this.numMutants == 0) {
        updateIndividual(this, individual, 0.0);
        ((TestSuiteChromosome) individual).setCoverage(this, 1.0);
        ((TestSuiteChromosome) individual).setNumOfCoveredGoals(this, 0);
        return 0.0;
    }
    List<ExecutionResult> results = runTestSuite(individual);
    // First objective: achieve branch coverage
    logger.debug("Calculating branch fitness: ");
    /*
		 * Note: results are cached, so the test suite is not executed again when we
		 * calculated the branch fitness
		 */
    double fitness = branchFitness.getFitness(individual);
    Map<Integer, Double> mutant_distance = new LinkedHashMap<Integer, Double>();
    Set<Integer> touchedMutants = new LinkedHashSet<Integer>();
    for (ExecutionResult result : results) {
        // use reflection for basic criteria, not for mutation
        if (result.hasTimeout() || result.hasTestException() || result.calledReflection()) {
            continue;
        }
        touchedMutants.addAll(result.getTrace().getTouchedMutants());
        Map<Integer, Double> touchedMutantsDistances = result.getTrace().getMutationDistances();
        if (touchedMutantsDistances.isEmpty()) {
            // if 'result' does not touch any mutant, no need to continue
            continue;
        }
        TestChromosome test = new TestChromosome();
        test.setTestCase(result.test);
        test.setLastExecutionResult(result);
        test.setChanged(false);
        Iterator<Entry<Integer, MutationTestFitness>> it = this.mutantMap.entrySet().iterator();
        while (it.hasNext()) {
            Entry<Integer, MutationTestFitness> entry = it.next();
            int mutantID = entry.getKey();
            TestFitnessFunction goal = entry.getValue();
            double fit = 0.0;
            if (touchedMutantsDistances.containsKey(mutantID)) {
                fit = touchedMutantsDistances.get(mutantID);
                if (!mutant_distance.containsKey(mutantID)) {
                    mutant_distance.put(mutantID, fit);
                } else {
                    mutant_distance.put(mutantID, Math.min(mutant_distance.get(mutantID), fit));
                }
            } else {
                // archive is updated by the TestFitnessFunction class
                fit = goal.getFitness(test, result);
            }
            if (fit == 0.0) {
                // update list of covered goals
                test.getTestCase().addCoveredGoal(goal);
                // goal to not be considered by the next iteration of the evolutionary algorithm
                this.toRemoveMutants.add(mutantID);
            }
            if (Properties.TEST_ARCHIVE) {
                Archive.getArchiveInstance().updateArchive(goal, test, fit);
            }
        }
    }
    // Second objective: touch all mutants?
    fitness += MutationPool.getMutantCounter() - touchedMutants.size();
    int covered = removedMutants.size();
    for (Double distance : mutant_distance.values()) {
        if (distance < 0) {
            logger.warn("Distance is " + distance + " / " + Integer.MAX_VALUE + " / " + Integer.MIN_VALUE);
            // FIXXME
            distance = 0.0;
        }
        fitness += normalize(distance);
        if (distance == 0.0) {
            covered++;
        }
    }
    updateIndividual(this, individual, fitness);
    ((TestSuiteChromosome) individual).setCoverage(this, (double) covered / (double) this.numMutants);
    ((TestSuiteChromosome) individual).setNumOfCoveredGoals(this, covered);
    return fitness;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) AbstractTestSuiteChromosome(org.evosuite.testsuite.AbstractTestSuiteChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 14 with TestChromosome

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

the class StatementCoverageSuiteFitness method getFitness.

/**
 * {@inheritDoc}
 */
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> suite) {
    List<ExecutionResult> results = runTestSuite(suite);
    double fitness = 0.0;
    // first simple and naive idea:
    // just take each goal, calculate the minimal fitness over all results in the suite
    // once a goal is covered don't check for it again
    // in the end sum up all those fitness and it's the resulting suite-fitness
    // guess this is horribly inefficient but it's a start
    List<? extends TestFitnessFunction> totalGoals = StatementCoverageFactory.retrieveCoverageGoals();
    Set<TestFitnessFunction> coveredGoals = new HashSet<TestFitnessFunction>();
    for (TestFitnessFunction goal : totalGoals) {
        double goalFitness = Double.MAX_VALUE;
        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);
                coveredGoals.add(goal);
                break;
            }
        }
        fitness += goalFitness;
    }
    if (totalGoals.size() > 0)
        suite.setCoverage(this, coveredGoals.size() / (double) totalGoals.size());
    else
        suite.setCoverage(this, 1.0);
    suite.setNumOfCoveredGoals(this, coveredGoals.size());
    updateIndividual(this, suite, fitness);
    return fitness;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet)

Example 15 with TestChromosome

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

the class StrongMutationSuiteFitness method getFitness.

/* (non-Javadoc)
	 * @see org.evosuite.ga.FitnessFunction#getFitness(org.evosuite.ga.Chromosome)
	 */
/**
 * {@inheritDoc}
 */
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> individual) {
    runTestSuite(individual);
    // Set<MutationTestFitness> uncoveredMutants = MutationTestPool.getUncoveredFitnessFunctions();
    TestSuiteChromosome suite = (TestSuiteChromosome) individual;
    for (TestChromosome test : suite.getTestChromosomes()) {
        ExecutionResult result = test.getLastExecutionResult();
        if (result.hasTimeout() || result.hasTestException()) {
            logger.debug("Skipping test with timeout");
            double fitness = branchFitness.totalGoals * 2 + branchFitness.totalMethods + 3 * this.numMutants;
            updateIndividual(this, individual, fitness);
            suite.setCoverage(this, 0.0);
            logger.info("Test case has timed out, setting fitness to max value " + fitness);
            return fitness;
        }
    }
    // First objective: achieve branch coverage
    logger.debug("Calculating branch fitness: ");
    double fitness = branchFitness.getFitness(individual);
    Set<Integer> touchedMutants = new LinkedHashSet<Integer>();
    Map<Mutation, Double> minMutantFitness = new LinkedHashMap<Mutation, Double>();
    // 0..1 -> propagation distance
    for (Integer mutantId : this.mutantMap.keySet()) {
        MutationTestFitness mutantFitness = mutantMap.get(mutantId);
        minMutantFitness.put(mutantFitness.getMutation(), 3.0);
    }
    int mutantsChecked = 0;
    int numKilled = removedMutants.size();
    Set<Integer> newKilled = new LinkedHashSet<Integer>();
    // Quicker tests first
    List<TestChromosome> executionOrder = prioritizeTests(suite);
    for (TestChromosome test : executionOrder) {
        ExecutionResult result = test.getLastExecutionResult();
        // use reflection for basic criteria, not for mutation
        if (result.calledReflection())
            continue;
        ExecutionTrace trace = result.getTrace();
        touchedMutants.addAll(trace.getTouchedMutants());
        logger.debug("Tests touched " + touchedMutants.size() + " mutants");
        Map<Integer, Double> touchedMutantsDistances = trace.getMutationDistances();
        if (touchedMutantsDistances.isEmpty()) {
            // if 'result' does not touch any mutant, no need to continue
            continue;
        }
        Iterator<Entry<Integer, MutationTestFitness>> it = this.mutantMap.entrySet().iterator();
        while (it.hasNext()) {
            Entry<Integer, MutationTestFitness> entry = it.next();
            int mutantID = entry.getKey();
            if (newKilled.contains(mutantID)) {
                continue;
            }
            MutationTestFitness goal = entry.getValue();
            if (MutationTimeoutStoppingCondition.isDisabled(goal.getMutation())) {
                logger.debug("Skipping timed out mutation " + goal.getMutation().getId());
                continue;
            }
            mutantsChecked++;
            double mutantInfectionDistance = 3.0;
            boolean hasBeenTouched = touchedMutantsDistances.containsKey(mutantID);
            if (hasBeenTouched) {
                // Infection happened, so we need to check propagation
                if (touchedMutantsDistances.get(mutantID) == 0.0) {
                    logger.debug("Executing test against mutant " + goal.getMutation());
                    // archive is updated by the TestFitnessFunction class
                    mutantInfectionDistance = goal.getFitness(test, result);
                } else {
                    // We can skip calling the test fitness function since we already know
                    // fitness is 1.0 (for propagation) + infection distance
                    mutantInfectionDistance = 1.0 + normalize(touchedMutantsDistances.get(mutantID));
                }
            } else {
                // archive is updated by the TestFitnessFunction class
                mutantInfectionDistance = goal.getFitness(test, result);
            }
            if (mutantInfectionDistance == 0.0) {
                numKilled++;
                newKilled.add(mutantID);
                // update list of covered goals
                result.test.addCoveredGoal(goal);
                // goal to not be considered by the next iteration of the evolutionary algorithm
                this.toRemoveMutants.add(mutantID);
            } else {
                minMutantFitness.put(goal.getMutation(), Math.min(mutantInfectionDistance, minMutantFitness.get(goal.getMutation())));
            }
        }
    }
    // logger.info("Fitness values for " + minMutantFitness.size() + " mutants");
    for (Double fit : minMutantFitness.values()) {
        fitness += fit;
    }
    logger.debug("Mutants killed: {}, Checked: {}, Goals: {})", numKilled, mutantsChecked, this.numMutants);
    updateIndividual(this, individual, fitness);
    assert numKilled == newKilled.size() + removedMutants.size();
    assert numKilled <= this.numMutants;
    double coverage = (double) numKilled / (double) this.numMutants;
    assert coverage >= 0.0 && coverage <= 1.0;
    suite.setCoverage(this, coverage);
    suite.setNumOfCoveredGoals(this, numKilled);
    return fitness;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ExecutionTrace(org.evosuite.testcase.execution.ExecutionTrace) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) AbstractTestSuiteChromosome(org.evosuite.testsuite.AbstractTestSuiteChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) 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