Search in sources :

Example 51 with TestFitnessFunction

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

the class StructuralGoalManager method updateCoveredGoals.

protected void updateCoveredGoals(FitnessFunction<T> f, T tc) {
    // the next two lines are needed since that coverage information are used
    // during EvoSuite post-processing
    TestChromosome tch = (TestChromosome) tc;
    tch.getTestCase().getCoveredGoals().add((TestFitnessFunction) f);
    // update covered targets
    boolean toArchive = false;
    T best = coveredGoals.get(f);
    if (best == null) {
        toArchive = true;
        coveredGoals.put(f, tc);
        uncoveredGoals.remove(f);
        currentGoals.remove(f);
    } else {
        double bestSize = best.size();
        double size = tc.size();
        if (size < bestSize && size > 1) {
            toArchive = true;
            coveredGoals.put(f, tc);
            archive.get(best).remove(f);
            if (archive.get(best).size() == 0)
                archive.remove(best);
        }
    }
    // update archive
    if (toArchive) {
        List<FitnessFunction<T>> coveredTargets = archive.get(tc);
        if (coveredTargets == null) {
            List<FitnessFunction<T>> list = new ArrayList<FitnessFunction<T>>();
            list.add(f);
            archive.put(tc, list);
        } else {
            coveredTargets.add(f);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) FitnessFunction(org.evosuite.ga.FitnessFunction) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 52 with TestFitnessFunction

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

the class OnlyMutationSuiteFitness 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);
    double fitness = 0.0;
    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 = this.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 : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) Entry(java.util.Map.Entry) AbstractTestSuiteChromosome(org.evosuite.testsuite.AbstractTestSuiteChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 53 with TestFitnessFunction

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

the class MutationSuiteFitness method updateCoveredGoals.

@Override
public boolean updateCoveredGoals() {
    if (!Properties.TEST_ARCHIVE) {
        return false;
    }
    for (Integer mutant : this.toRemoveMutants) {
        TestFitnessFunction ff = this.mutantMap.remove(mutant);
        if (ff != null) {
            this.removedMutants.add(mutant);
        } else {
            throw new IllegalStateException("goal to remove not found");
        }
    }
    this.toRemoveMutants.clear();
    logger.info("Current state of archive: " + Archive.getArchiveInstance().toString());
    return true;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction)

Example 54 with TestFitnessFunction

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

the class CoverageCrossOver method crossOver.

/* (non-Javadoc)
	 * @see org.evosuite.ga.CrossOverFunction#crossOver(org.evosuite.ga.Chromosome, org.evosuite.ga.Chromosome)
	 */
/**
 * {@inheritDoc}
 */
@Override
public void crossOver(Chromosome parent1, Chromosome parent2) throws ConstructionFailedException {
    assert (parent1 instanceof TestSuiteChromosome);
    assert (parent2 instanceof TestSuiteChromosome);
    TestSuiteChromosome suite1 = (TestSuiteChromosome) parent1;
    TestSuiteChromosome suite2 = (TestSuiteChromosome) parent2;
    // Determine coverage information
    Map<TestFitnessFunction, Set<TestChromosome>> goalMap = new HashMap<TestFitnessFunction, Set<TestChromosome>>();
    populateCoverageMap(goalMap, suite1);
    populateCoverageMap(goalMap, suite2);
    // Extract set of tests that have unique coverage
    // We need all of these tests in both offspring
    Set<TestChromosome> unique = removeUniqueCoveringTests(goalMap);
    logger.debug("Uniquely covering tests: " + unique.size());
    Set<TestChromosome> offspring1 = new HashSet<TestChromosome>();
    Set<TestChromosome> offspring2 = new HashSet<TestChromosome>();
    Set<TestChromosome> workingSet = new HashSet<TestChromosome>();
    for (TestFitnessFunction goal : goalMap.keySet()) {
        workingSet.addAll(goalMap.get(goal));
    }
    int targetSize = workingSet.size() / 2;
    while (offspring2.size() < targetSize) {
        logger.debug("Sizes: " + workingSet.size() + ", " + offspring1.size() + ", " + offspring2.size());
        // Move a randomly selected redundant test case t from workingset to offspring2
        TestChromosome choice = Randomness.choice(workingSet);
        workingSet.remove(choice);
        offspring2.add(choice);
        // Move all tests with unique coverage to offspring 1?
        offspring1.addAll(removeUniqueCoveringTests(goalMap));
    }
    offspring1.addAll(workingSet);
    suite1.clearTests();
    suite2.clearTests();
    // Add unique tests
    for (TestChromosome test : unique) {
        suite1.addTest((TestChromosome) test.clone());
        suite2.addTest((TestChromosome) test.clone());
    }
    // Add redundancy tests
    suite1.addTests(offspring1);
    suite2.addTests(offspring2);
    logger.debug("Final sizes: " + suite1.size() + ", " + suite2.size());
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet)

Example 55 with TestFitnessFunction

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

the class RegressionSuiteStrategy method generateRandomRegressionTests.

private TestSuiteChromosome generateRandomRegressionTests() {
    LoggingUtils.getEvoLogger().info("* Using RANDOM regression test generation");
    if (Properties.KEEP_REGRESSION_ARCHIVE) {
        Properties.TEST_ARCHIVE = true;
    }
    RegressionTestSuiteChromosome suite = new RegressionTestSuiteChromosome();
    PropertiesSuiteGAFactory algorithmFactory = new PropertiesSuiteGAFactory();
    GeneticAlgorithm<?> suiteGA = algorithmFactory.getSearchAlgorithm();
    // statistics.searchStarted(suiteGA);
    BranchCoverageSuiteFitness branchCoverageSuiteFitness = new BranchCoverageSuiteFitness(TestGenerationContext.getInstance().getClassLoaderForSUT());
    // regressionMonitor.searchStarted(suiteGA);
    RegressionTestChromosomeFactory factory = new RegressionTestChromosomeFactory();
    LoggingUtils.getEvoLogger().warn("*** generating RANDOM regression tests");
    // TODO: Shutdown hook?
    List<TestFitnessFunction> goals = getGoals(true);
    track(RuntimeVariable.Total_Goals, goals.size());
    StoppingCondition stoppingCondition = getStoppingCondition();
    // fitnessFunction.getFitness(suite);
    int totalTestCount = 0;
    int usefulTestCount = 0;
    int simulatedAge = 0;
    int numAssertions = 0;
    int executedStatemets = 0;
    boolean firstTry = true;
    // Properties.REGRESSION_RANDOM_STRATEGY:
    // 0: skip evaluation after first find, dont keep tests
    // 1: dont skip evaluation after first find, dont keep tests
    // 2: dont skip evaluation after first find, keep tests
    // 3: skip evaluation after first find, keep tests [default]
    long startTime = System.currentTimeMillis();
    while (!stoppingCondition.isFinished() || (numAssertions != 0)) {
        if (numAssertions == 0 || Properties.REGRESSION_RANDOM_STRATEGY == 1 || Properties.REGRESSION_RANDOM_STRATEGY == 2) {
            RegressionTestChromosome test = factory.getChromosome();
            RegressionTestSuiteChromosome clone = new RegressionTestSuiteChromosome();
            clone.addTest(test);
            List<TestCase> testCases = clone.getTests();
            // fitnessFunction.getFitness(clone);
            /*
         * logger.debug("Old fitness: {}, new fitness: {}",
         * suite.getFitness(), clone.getFitness());
         */
            executedStatemets += test.size();
            numAssertions = RegressionAssertionCounter.getNumAssertions(clone);
            if (Properties.KEEP_REGRESSION_ARCHIVE) {
                branchCoverageSuiteFitness.getFitness(clone.getTestSuite());
            }
            if (numAssertions > 0) {
                LoggingUtils.getEvoLogger().warn("Generated test with {} assertions.", numAssertions);
            }
            totalTestCount++;
            if (numAssertions > 0) {
                numAssertions = 0;
                // boolean compilable = JUnitAnalyzer.verifyCompilationAndExecution(testCases);
                JUnitAnalyzer.removeTestsThatDoNotCompile(testCases);
                JUnitAnalyzer.handleTestsThatAreUnstable(testCases);
                if (testCases.size() > 0) {
                    clone = new RegressionTestSuiteChromosome();
                    for (TestCase t : testCases) {
                        RegressionTestChromosome rtc = new RegressionTestChromosome();
                        if (t.isUnstable()) {
                            continue;
                        }
                        TestChromosome tc = new TestChromosome();
                        tc.setTestCase(t);
                        rtc.setTest(tc);
                        clone.addTest(rtc);
                    }
                    // test.set
                    // clone.addTest(testCases);
                    numAssertions = RegressionAssertionCounter.getNumAssertions(clone, false, false);
                    LoggingUtils.getEvoLogger().warn("Keeping {} assertions.", numAssertions);
                    if (numAssertions > 0) {
                        usefulTestCount++;
                        suite.addTest(test);
                    }
                } else {
                    LoggingUtils.getEvoLogger().warn("ignored assertions. tests were removed.");
                }
            }
        } else {
            if (numAssertions > 0) {
                break;
            }
        /*
        try {
                Thread.sleep(1000);
        } catch (InterruptedException e) {
                e.printStackTrace();
        }
        */
        }
        // regressionMonitor.iteration(suiteGA);
        if (firstTry || (System.currentTimeMillis() - startTime) >= 4000) {
            startTime = System.currentTimeMillis();
            simulatedAge++;
            firstTry = false;
        }
    }
    // regressionMonitor.searchFinished(suiteGA);
    LoggingUtils.getEvoLogger().warn("*** Random test generation finished.");
    LoggingUtils.getEvoLogger().warn("*=*=*=* Total tests: {} | Tests with assertion: {}", totalTestCount, usefulTestCount);
    // statistics.searchFinished(suiteGA);
    zero_fitness.setFinished();
    LoggingUtils.getEvoLogger().info("* Generated " + suite.size() + " tests with total length " + suite.totalLengthOfTestCases());
    goals = getGoals(false);
    track(RuntimeVariable.Total_Goals, goals.size());
    suiteGA.printBudget();
    if (!(Properties.REGRESSION_RANDOM_STRATEGY == 2 || Properties.REGRESSION_RANDOM_STRATEGY == 3)) {
        suite = new RegressionTestSuiteChromosome();
    }
    TestSuiteChromosome bestSuites = new TestSuiteChromosome();
    for (TestCase t : suite.getTests()) {
        bestSuites.addTest(t);
    }
    return bestSuites;
}
Also used : RegressionTestChromosomeFactory(org.evosuite.regression.RegressionTestChromosomeFactory) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) RegressionTestSuiteChromosome(org.evosuite.regression.RegressionTestSuiteChromosome) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) MaxStatementsStoppingCondition(org.evosuite.ga.stoppingconditions.MaxStatementsStoppingCondition) ZeroFitnessStoppingCondition(org.evosuite.ga.stoppingconditions.ZeroFitnessStoppingCondition) StoppingCondition(org.evosuite.ga.stoppingconditions.StoppingCondition) TestCase(org.evosuite.testcase.TestCase) RegressionTestChromosome(org.evosuite.regression.RegressionTestChromosome) RegressionTestSuiteChromosome(org.evosuite.regression.RegressionTestSuiteChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome) RegressionTestChromosome(org.evosuite.regression.RegressionTestChromosome)

Aggregations

TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)73 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)26 TestCase (org.evosuite.testcase.TestCase)24 Test (org.junit.Test)22 TestChromosome (org.evosuite.testcase.TestChromosome)21 ArrayList (java.util.ArrayList)20 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)15 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)12 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)11 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)11 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)9 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)9 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)8 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)8 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)8 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)6 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)4 ExceptionCoverageTestFitness (org.evosuite.coverage.exception.ExceptionCoverageTestFitness)4 Properties (org.evosuite.Properties)3