Search in sources :

Example 6 with TestSuiteFitnessFunction

use of org.evosuite.testsuite.TestSuiteFitnessFunction in project evosuite by EvoSuite.

the class TestSuiteLocalSearch method ensureDoubleExecution.

/**
 * Ensure that all branches are executed twice For each branch such that
 * exists only one test case in the suite that covers that branch, it
 * creates a duplicate of that test case.
 *
 * By doing this, we avoid to incorrectly mark a new test case produced by
 * the local search as an improving test case because it simply executes
 * again a predicate.
 */
protected static void ensureDoubleExecution(TestSuiteChromosome individual, LocalSearchObjective<TestSuiteChromosome> objective) {
    logger.debug("Ensuring double execution");
    Set<TestChromosome> duplicates = new HashSet<TestChromosome>();
    TestSuiteFitnessFunction defaultFitness = (TestSuiteFitnessFunction) objective.getFitnessFunctions().get(0);
    Map<Integer, Integer> covered = new HashMap<Integer, Integer>();
    Map<Integer, TestChromosome> testMap = new HashMap<Integer, TestChromosome>();
    for (TestChromosome test : individual.getTestChromosomes()) {
        // Make sure we have an execution result
        if (test.getLastExecutionResult() == null || test.isChanged()) {
            ExecutionResult result = test.executeForFitnessFunction(defaultFitness);
            // .clone();
            test.setLastExecutionResult(result);
            test.setChanged(false);
        }
        for (Entry<Integer, Integer> entry : test.getLastExecutionResult().getTrace().getPredicateExecutionCount().entrySet()) {
            if (!covered.containsKey(entry.getKey())) {
                covered.put(entry.getKey(), 0);
            }
            covered.put(entry.getKey(), covered.get(entry.getKey()) + entry.getValue());
            testMap.put(entry.getKey(), test);
        }
    }
    for (Entry<Integer, Integer> entry : covered.entrySet()) {
        int branchId = entry.getKey();
        int count = entry.getValue();
        if (count == 1) {
            TestChromosome duplicate = (TestChromosome) testMap.get(branchId).clone();
            ExecutionResult result = duplicate.executeForFitnessFunction(defaultFitness);
            // .clone();
            duplicate.setLastExecutionResult(result);
            duplicate.setChanged(false);
            duplicates.add(duplicate);
        }
    }
    if (!duplicates.isEmpty()) {
        logger.info("Adding " + duplicates.size() + " tests to cover branches sufficiently");
        for (TestChromosome test : duplicates) {
            individual.addTest(test);
        }
        individual.setChanged(true);
        for (FitnessFunction<? extends Chromosome> ff : objective.getFitnessFunctions()) {
            ((TestSuiteFitnessFunction) ff).getFitness(individual);
        }
    }
}
Also used : HashMap(java.util.HashMap) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 7 with TestSuiteFitnessFunction

use of org.evosuite.testsuite.TestSuiteFitnessFunction in project evosuite by EvoSuite.

the class TestSuiteLocalSearch method expandTestSuite.

/**
 * Before applying DSE we expand test cases, such that each primitive value
 * is used at only exactly one position as a parameter
 *
 * For example, given the following test case:
 *
 * <code>
 * foo0.bar(1);
 * foo1.bar(1);
 * </code>
 *
 * is rewritten as:
 *
 * <code>
 * int int0 = 1;
 * int int1 = 1;
 * foo0.bar(int0);
 * foo1.bar(int1);
 * </code>
 *
 * @param suite
 * @return
 */
private static void expandTestSuite(TestSuiteChromosome suite, LocalSearchObjective<TestSuiteChromosome> objective) {
    logger.debug("Expanding tests for local search");
    TestSuiteChromosome newTestSuite = new TestSuiteChromosome();
    for (TestChromosome test : suite.getTestChromosomes()) {
        // First make sure we are up to date with the execution
        if (test.getLastExecutionResult() == null || test.isChanged()) {
            test.setLastExecutionResult(TestCaseExecutor.runTest(test.getTestCase()));
            test.setChanged(false);
        }
        // We skip tests that have problems
        if (test.getLastExecutionResult().hasTimeout() || test.getLastExecutionResult().hasTestException()) {
            logger.info("Skipping test with timeout or exception");
            continue;
        }
        // If local search has already been applied on the original test
        // then we also set that flag on the expanded test
        boolean hasLocalSearchBeenApplied = test.hasLocalSearchBeenApplied();
        TestCase newTest = test.getTestCase().clone();
        TestCase expandedTest = expandTestCase(newTest);
        TestChromosome expandedTestChromosome = newTestSuite.addTest(expandedTest);
        expandedTestChromosome.setLocalSearchApplied(hasLocalSearchBeenApplied);
    }
    List<TestChromosome> oldTests = suite.getTestChromosomes();
    oldTests.clear();
    oldTests.addAll(newTestSuite.getTestChromosomes());
    suite.setChanged(true);
    for (FitnessFunction<? extends Chromosome> ff : objective.getFitnessFunctions()) {
        ((TestSuiteFitnessFunction) ff).getFitness(suite);
    }
}
Also used : TestCase(org.evosuite.testcase.TestCase) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 8 with TestSuiteFitnessFunction

use of org.evosuite.testsuite.TestSuiteFitnessFunction in project evosuite by EvoSuite.

the class DeprecatedTestSuiteDSE method getFitness.

private double getFitness(TestSuiteChromosome suite) {
    for (FitnessFunction<? extends Chromosome> ff : objective.getFitnessFunctions()) {
        TestSuiteFitnessFunction tff = (TestSuiteFitnessFunction) ff;
        tff.getFitness(suite);
    }
    return suite.getFitness();
}
Also used : TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction)

Example 9 with TestSuiteFitnessFunction

use of org.evosuite.testsuite.TestSuiteFitnessFunction in project evosuite by EvoSuite.

the class TestSuiteLocalSearchObjective method buildNewTestSuiteLocalSearchObjective.

/**
 * Creates a new <code>TestSuiteLocalSearchObjective</code> for a given list
 * of fitness functions, a test suite and a <code>testIndex</code> for
 * replacing an optimised test case (i.e. a test case over which was applied
 * local search)
 *
 * @param fitness
 *            the list of fitness functions to be used on the modified test
 *            suite
 * @param suite
 *            the original test suite
 * @param index
 *            the index (between 0 and the suite length) that will be
 *            replaced with a new test case
 * @return
 */
public static TestSuiteLocalSearchObjective buildNewTestSuiteLocalSearchObjective(List<FitnessFunction<? extends Chromosome>> fitness, TestSuiteChromosome suite, int index) {
    List<TestSuiteFitnessFunction> ffs = new ArrayList<>();
    for (FitnessFunction<? extends Chromosome> ff : fitness) {
        TestSuiteFitnessFunction tff = (TestSuiteFitnessFunction) ff;
        ffs.add(tff);
    }
    return new TestSuiteLocalSearchObjective(ffs, suite, index);
}
Also used : ArrayList(java.util.ArrayList) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction)

Example 10 with TestSuiteFitnessFunction

use of org.evosuite.testsuite.TestSuiteFitnessFunction in project evosuite by EvoSuite.

the class TestSuiteLocalSearchObjective method hasChanged.

/**
 * Overrides the test case at position <code>testIndex</code> with the
 * individual. It returns <code>-1</code> if the new fitness has improved,
 * <code>1</code> if the fitness has worsened or <code>0</code> if the
 * fitness has not changed.
 */
@Override
public int hasChanged(TestChromosome testCase) {
    testCase.setChanged(true);
    suite.setTestChromosome(testIndex, testCase);
    LocalSearchBudget.getInstance().countFitnessEvaluation();
    for (TestSuiteFitnessFunction fitnessFunction : fitnessFunctions) fitnessFunction.getFitness(suite);
    double newFitness = suite.getFitness();
    if (isFitnessBetter(newFitness, lastFitnessSum)) {
        logger.info("Local search improved fitness from " + lastFitnessSum + " to " + newFitness);
        updateLastFitness();
        updateLastCoverage();
        return -1;
    } else if (isFitnessWorse(newFitness, lastFitnessSum)) {
        logger.info("Local search worsened fitness from " + lastFitnessSum + " to " + newFitness);
        suite.setFitnessValues(lastFitness);
        suite.setCoverageValues(lastCoverage);
        return 1;
    } else {
        logger.info("Local search did not change fitness of " + lastFitnessSum);
        updateLastCoverage();
        return 0;
    }
}
Also used : TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction)

Aggregations

TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)17 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)11 ArrayList (java.util.ArrayList)6 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)6 TestChromosome (org.evosuite.testcase.TestChromosome)5 TestCase (org.evosuite.testcase.TestCase)3 HashSet (java.util.HashSet)2 Properties (org.evosuite.Properties)2 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)2 StoppingCondition (org.evosuite.ga.stoppingconditions.StoppingCondition)2 DiversityObserver (org.evosuite.testsuite.similarity.DiversityObserver)2 MIMEType (com.examples.with.different.packagename.concolic.MIMEType)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Criterion (org.evosuite.Properties.Criterion)1 DefaultLocalSearchObjective (org.evosuite.ga.localsearch.DefaultLocalSearchObjective)1 MaxStatementsStoppingCondition (org.evosuite.ga.stoppingconditions.MaxStatementsStoppingCondition)1 MaxTestsStoppingCondition (org.evosuite.ga.stoppingconditions.MaxTestsStoppingCondition)1 BranchNoveltyFunction (org.evosuite.novelty.BranchNoveltyFunction)1 NoveltyFitnessEvaluationListener (org.evosuite.novelty.NoveltyFitnessEvaluationListener)1