Search in sources :

Example 31 with TestChromosome

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

the class MOSA method updateArchive.

/**
 * This method update the archive by adding test cases that cover new test goals, or replacing the
 * old tests if the new ones are smaller (at the same level of coverage).
 *
 * @param solutionSet is the list of Chromosomes (population)
 */
private void updateArchive(T solution, FitnessFunction<T> covered) {
    // the next two lines are needed since that coverage information are used
    // during EvoSuite post-processing
    TestChromosome tch = (TestChromosome) solution;
    tch.getTestCase().getCoveredGoals().add((TestFitnessFunction) covered);
    // archive
    if (archive.containsKey(covered)) {
        TestChromosome existingSolution = (TestChromosome) this.archive.get(covered);
        // if the new solution is better (based on secondary criterion), then the archive must be updated
        if (solution.compareSecondaryObjective(existingSolution) < 0) {
            this.archive.put(covered, solution);
        }
    } else {
        archive.put(covered, solution);
        this.uncoveredGoals.remove(covered);
    }
}
Also used : TestChromosome(org.evosuite.testcase.TestChromosome)

Example 32 with TestChromosome

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

the class AbstractMOSA method areEqual.

/**
 * This method verifies whether two TestCromosome contain
 * the same test case. Here the equality is computed looking at
 * the strings composing the tests. This method is strongly needed
 * in {@link AbstractMOSA#breedNextGeneration()}.
 * @param test1 first test
 * @param test2 second test
 * @return true if the test1 and test 2 (meant as String) are equal
 * to each other; false otherwise.
 */
protected boolean areEqual(T test1, T test2) {
    TestChromosome tch1 = (TestChromosome) test1;
    TestChromosome tch2 = (TestChromosome) test2;
    if (tch1.size() != tch2.size())
        return false;
    if (tch1.size() == 0)
        return false;
    if (tch2.size() == 0)
        return false;
    return tch1.getTestCase().toCode().equals(tch2.getTestCase().toCode());
}
Also used : TestChromosome(org.evosuite.testcase.TestChromosome)

Example 33 with TestChromosome

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

the class BranchesManager method calculateFitness.

public void calculateFitness(T c) {
    // run the test
    TestCase test = ((TestChromosome) c).getTestCase();
    ExecutionResult result = TestCaseExecutor.runTest(test);
    ((TestChromosome) c).setLastExecutionResult(result);
    c.setChanged(false);
    if (result.hasTimeout() || result.hasTestException()) {
        for (FitnessFunction<T> f : currentGoals) c.setFitness(f, Double.MAX_VALUE);
        return;
    }
    // 1) we update the set of currents goals
    Set<FitnessFunction<T>> visitedStatements = new HashSet<FitnessFunction<T>>(uncoveredGoals.size() * 2);
    LinkedList<FitnessFunction<T>> targets = new LinkedList<FitnessFunction<T>>();
    targets.addAll(this.currentGoals);
    while (targets.size() > 0) {
        FitnessFunction<T> fitnessFunction = targets.poll();
        int past_size = visitedStatements.size();
        visitedStatements.add(fitnessFunction);
        if (past_size == visitedStatements.size())
            continue;
        double value = fitnessFunction.getFitness(c);
        if (value == 0.0) {
            updateCoveredGoals(fitnessFunction, c);
            for (FitnessFunction<T> child : graph.getStructuralChildren(fitnessFunction)) {
                targets.addLast(child);
            }
        } else {
            currentGoals.add(fitnessFunction);
        }
    }
    currentGoals.removeAll(coveredGoals.keySet());
    // 2) we update the archive
    for (Integer branchid : result.getTrace().getCoveredFalseBranches()) {
        FitnessFunction<T> branch = this.branchCoverageFalseMap.get(branchid);
        if (branch == null)
            continue;
        updateCoveredGoals((FitnessFunction<T>) branch, c);
    }
    for (Integer branchid : result.getTrace().getCoveredTrueBranches()) {
        FitnessFunction<T> branch = this.branchCoverageTrueMap.get(branchid);
        if (branch == null)
            continue;
        updateCoveredGoals((FitnessFunction<T>) branch, c);
    }
    for (String method : result.getTrace().getCoveredBranchlessMethods()) {
        FitnessFunction<T> branch = this.branchlessMethodCoverageMap.get(method);
        if (branch == null)
            continue;
        updateCoveredGoals((FitnessFunction<T>) branch, c);
    }
// debugStructuralDependencies(c);
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) FitnessFunction(org.evosuite.ga.FitnessFunction) LinkedList(java.util.LinkedList) TestCase(org.evosuite.testcase.TestCase) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet)

Example 34 with TestChromosome

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

the class CoverageArchive method mergeArchiveAndSolution.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public TestSuiteChromosome mergeArchiveAndSolution(Chromosome solution) {
    // Deactivate in case a test is executed and would access the archive as this might cause a
    // concurrent access
    Properties.TEST_ARCHIVE = false;
    TestSuiteChromosome mergedSolution = (TestSuiteChromosome) solution.clone();
    // skip solutions that have been modified as those might not have been evaluated yet, or have
    // timeout or throw some exception and therefore they may slow down future analysis on the final
    // test suite
    mergedSolution.getTestChromosomes().removeIf(t -> t.isChanged() || (t.getLastExecutionResult() != null && (t.getLastExecutionResult().hasTimeout() || t.getLastExecutionResult().hasTestException())));
    // to avoid adding the same solution to 'mergedSolution' suite
    Set<T> solutionsSampledFromArchive = new LinkedHashSet<T>();
    for (F target : this.getTargets()) {
        // has target been covered? to answer it, we perform a local check rather than calling method
        // {@link TestFitnessFunction.isCoveredBy} as it may perform a fitness evaluation to access
        // whether that 'target' is covered or not (and therefore, it could be more expensive)
        boolean isGoalCovered = false;
        for (TestChromosome test : mergedSolution.getTestChromosomes()) {
            if (test.getTestCase().isGoalCovered(target)) {
                isGoalCovered = true;
                break;
            }
        }
        if (!isGoalCovered) {
            T chromosome = this.covered.get(target);
            // considered yet?
            if (chromosome != null && !solutionsSampledFromArchive.contains(chromosome)) {
                solutionsSampledFromArchive.add(chromosome);
                mergedSolution.addTest(chromosome);
            }
        }
    }
    // re-evaluate merged solution
    for (FitnessFunction fitnessFunction : solution.getFitnessValues().keySet()) {
        fitnessFunction.getFitness(mergedSolution);
    }
    // re-active it
    Properties.TEST_ARCHIVE = true;
    return mergedSolution;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) FitnessFunction(org.evosuite.ga.FitnessFunction) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 35 with TestChromosome

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

the class MIO method evolve.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
protected void evolve() {
    if (this.solution == null || (this.solution.getNumberOfMutations() >= Properties.MAX_NUM_MUTATIONS_BEFORE_GIVING_UP || this.solution.getNumberOfEvaluations() >= Properties.MAX_NUM_FITNESS_EVALUATIONS_BEFORE_GIVING_UP)) {
        this.solution = new TestSuiteChromosome(this.randomFactory);
        TestChromosome test = null;
        if (Randomness.nextDouble() < this.pr) {
            test = this.randomFactory.getChromosome();
            if (test.size() == 0) {
                // in case EvoSuite fails to generate a new random test
                // case, get one from the archive
                test = Archive.getArchiveInstance().getSolution();
            }
        } else {
            test = Archive.getArchiveInstance().getSolution();
            if (test == null || test.size() == 0) {
                test = this.randomFactory.getChromosome();
            }
        }
        assert test != null && test.size() != 0;
        this.solution.addTest(test);
    }
    assert this.solution != null;
    // mutate it
    notifyMutation(this.solution);
    this.solution.mutate();
    // evaluate it
    for (FitnessFunction<T> fitnessFunction : this.fitnessFunctions) {
        fitnessFunction.getFitness((T) this.solution);
    }
    double usedBudget = this.progress();
    if (Double.compare(usedBudget, Properties.EXPLOITATION_STARTS_AT_PERCENT) >= 0) {
        // focused search has started
        this.pr = 0.0;
        this.n = 1;
    } else {
        double scale = usedBudget / Properties.EXPLOITATION_STARTS_AT_PERCENT;
        this.pr = Properties.P_RANDOM_TEST_OR_FROM_ARCHIVE - (scale * Properties.P_RANDOM_TEST_OR_FROM_ARCHIVE);
        this.n = (int) Math.ceil(Properties.NUMBER_OF_TESTS_PER_TARGET - (scale * Properties.NUMBER_OF_TESTS_PER_TARGET));
        logger.debug("usedBudget: " + usedBudget + " | scale: " + scale + " | Pr: " + this.pr + " | N: " + this.n);
    }
    assert this.pr >= 0.0;
    assert this.n >= 1;
    Archive.getArchiveInstance().shrinkSolutions(this.n);
    this.currentIteration++;
}
Also used : 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