Search in sources :

Example 26 with FitnessFunction

use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.

the class CoverageAnalysis method analyzeCoverage.

/**
 * Identify all JUnit tests starting with the given name prefix, instrument
 * and run tests
 */
public static void analyzeCoverage() {
    Sandbox.goingToExecuteSUTCode();
    TestGenerationContext.getInstance().goingToExecuteSUTCode();
    Sandbox.goingToExecuteUnsafeCodeOnSameThread();
    ExecutionTracer.setCheckCallerThread(false);
    try {
        String cp = ClassPathHandler.getInstance().getTargetProjectClasspath();
        if (Properties.TARGET_CLASS.endsWith(".jar") || Properties.TARGET_CLASS.contains(File.separator)) {
            targetClasses = DependencyAnalysis.analyzeTarget(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator)));
        } else {
            targetClasses.add(Properties.TARGET_CLASS);
            DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator)));
        }
        LoggingUtils.getEvoLogger().info("* Finished analyzing classpath");
    } catch (Throwable e) {
        LoggingUtils.getEvoLogger().error("* Error while initializing target class: " + (e.getMessage() != null ? e.getMessage() : e.toString()));
        logger.error("Problem for " + Properties.TARGET_CLASS + ". Full stack:", e);
        return;
    } finally {
        Sandbox.doneWithExecutingUnsafeCodeOnSameThread();
        Sandbox.doneWithExecutingSUTCode();
        TestGenerationContext.getInstance().doneWithExecutingSUTCode();
    }
    // TestCluster.getInstance();
    List<Class<?>> testClasses = getTestClasses();
    LoggingUtils.getEvoLogger().info("* Found " + testClasses.size() + " test class(es)");
    if (testClasses.isEmpty())
        return;
    /*
         * sort them in a deterministic way, in case there are 
         * static state dependencies
         */
    sortTestClasses(testClasses);
    Class<?>[] tests = testClasses.toArray(new Class<?>[testClasses.size()]);
    LoggingUtils.getEvoLogger().info("* Executing test(s)");
    if (Properties.SELECTED_JUNIT == null) {
        boolean origUseAgent = EvoRunner.useAgent;
        boolean origUseClassLoader = EvoRunner.useClassLoader;
        try {
            // avoid double instrumentation
            EvoRunner.useAgent = false;
            // avoid double instrumentation
            EvoRunner.useClassLoader = false;
            List<JUnitResult> results = executeTests(tests);
            printReport(results);
        } finally {
            EvoRunner.useAgent = origUseAgent;
            EvoRunner.useClassLoader = origUseClassLoader;
        }
    } else {
        // instead of just running junit tests, carve them
        JUnitTestCarvedChromosomeFactory carvedFactory = new JUnitTestCarvedChromosomeFactory(null);
        TestSuiteChromosome testSuite = carvedFactory.getCarvedTestSuite();
        int goals = 0;
        for (Properties.Criterion pc : Properties.CRITERION) {
            LoggingUtils.getEvoLogger().info("* Coverage analysis for criterion " + pc);
            TestFitnessFactory ffactory = FitnessFunctions.getFitnessFactory(pc);
            goals += ffactory.getCoverageGoals().size();
            FitnessFunction ffunction = FitnessFunctions.getFitnessFunction(pc);
            ffunction.getFitness(testSuite);
            CoverageCriteriaAnalyzer.analyzeCoverage(testSuite, pc);
        }
        // Generate test suite
        TestSuiteGenerator.writeJUnitTestsAndCreateResult(testSuite);
        StatisticsSender.executedAndThenSendIndividualToMaster(testSuite);
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals);
        if (Properties.COVERAGE_MATRIX)
            throw new IllegalArgumentException("Coverage matrix not yet available when measuring coverage of a carved test suite");
    }
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) FitnessFunction(org.evosuite.ga.FitnessFunction) Properties(org.evosuite.Properties) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestClass(org.junit.runners.model.TestClass) Criterion(org.evosuite.Properties.Criterion)

Example 27 with FitnessFunction

use of org.evosuite.ga.FitnessFunction 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 28 with FitnessFunction

use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.

the class MIOArchive 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();
    // to avoid adding the same solution to 'mergedSolution' suite
    Set<T> solutionsSampledFromArchive = new LinkedHashSet<T>();
    for (F target : this.archive.keySet()) {
        // does solution cover target?
        if (!target.isCoveredBy(mergedSolution)) {
            Population population = this.archive.get(target);
            // is there any solution in the archive that covers it?
            T t = population.getBestSolutionIfAny();
            if (t != null) {
                // has t been considered?
                if (!solutionsSampledFromArchive.contains(t)) {
                    solutionsSampledFromArchive.add(t);
                    T tClone = (T) t.clone();
                    mergedSolution.addTest(tClone);
                }
            }
        }
    }
    // re-evaluate merged solution
    for (FitnessFunction fitnessFunction : solution.getFitnessValues().keySet()) {
        fitnessFunction.getFitness(mergedSolution);
    }
    // re-active it
    Properties.TEST_ARCHIVE = true;
    logger.info("Final test suite size from archive: " + mergedSolution);
    return mergedSolution;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) FitnessFunction(org.evosuite.ga.FitnessFunction)

Example 29 with FitnessFunction

use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.

the class TestDominanceComparator method testDominanceComparatorOneFitness.

@Test
public void testDominanceComparatorOneFitness() {
    Problem p = new Booths<NSGAChromosome>();
    List<FitnessFunction<NSGAChromosome>> fitnessFunctions = p.getFitnessFunctions();
    FitnessFunction<NSGAChromosome> ff = fitnessFunctions.get(0);
    NSGAChromosome c1 = new NSGAChromosome();
    NSGAChromosome c2 = new NSGAChromosome();
    // Set Fitness
    c1.setFitness(ff, 0.7);
    c2.setFitness(ff, 0.3);
    List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
    population.add(c1);
    population.add(c2);
    DominanceComparator dc = new DominanceComparator();
    Collections.sort(population, dc);
    Assert.assertEquals(population.get(0).getFitness(ff), 0.3, 0.0);
    Assert.assertEquals(population.get(1).getFitness(ff), 0.7, 0.0);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) ArrayList(java.util.ArrayList) Problem(org.evosuite.ga.problems.Problem) FitnessFunction(org.evosuite.ga.FitnessFunction) Booths(org.evosuite.ga.problems.singleobjective.Booths) FONIntTest(org.evosuite.ga.problems.multiobjective.FONIntTest) Test(org.junit.Test)

Example 30 with FitnessFunction

use of org.evosuite.ga.FitnessFunction in project evosuite by EvoSuite.

the class TestDominanceComparator method testDominanceComparatorSeveralFitnessesDomination.

@Test
public void testDominanceComparatorSeveralFitnessesDomination() {
    Problem p = new FON();
    List<FitnessFunction<NSGAChromosome>> fitnessFunctions = p.getFitnessFunctions();
    FitnessFunction<NSGAChromosome> ff_1 = fitnessFunctions.get(0);
    FitnessFunction<NSGAChromosome> ff_2 = fitnessFunctions.get(1);
    NSGAChromosome c1 = new NSGAChromosome();
    NSGAChromosome c2 = new NSGAChromosome();
    // Set Fitness
    c1.setFitness(ff_1, 0.7);
    c1.setFitness(ff_2, 0.6);
    c2.setFitness(ff_1, 0.3);
    c2.setFitness(ff_2, 0.5);
    List<NSGAChromosome> population = new ArrayList<NSGAChromosome>();
    population.add(c1);
    population.add(c2);
    DominanceComparator dc = new DominanceComparator();
    Collections.sort(population, dc);
    Assert.assertEquals(population.get(0).getFitness(ff_1), 0.3, 0.0);
    Assert.assertEquals(population.get(0).getFitness(ff_2), 0.5, 0.0);
    Assert.assertEquals(population.get(1).getFitness(ff_1), 0.7, 0.0);
    Assert.assertEquals(population.get(1).getFitness(ff_2), 0.6, 0.0);
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) FON(org.evosuite.ga.problems.multiobjective.FON) ArrayList(java.util.ArrayList) Problem(org.evosuite.ga.problems.Problem) FitnessFunction(org.evosuite.ga.FitnessFunction) FONIntTest(org.evosuite.ga.problems.multiobjective.FONIntTest) Test(org.junit.Test)

Aggregations

FitnessFunction (org.evosuite.ga.FitnessFunction)47 Test (org.junit.Test)38 Problem (org.evosuite.ga.problems.Problem)33 NSGAChromosome (org.evosuite.ga.NSGAChromosome)32 List (java.util.List)15 Chromosome (org.evosuite.ga.Chromosome)15 ArrayList (java.util.ArrayList)13 SBXCrossover (org.evosuite.ga.operators.crossover.SBXCrossover)13 BinaryTournamentSelectionCrowdedComparison (org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison)13 NSGAII (org.evosuite.ga.metaheuristics.NSGAII)12 RandomFactory (org.evosuite.ga.metaheuristics.RandomFactory)12 GenerationalDistance (org.evosuite.ga.problems.metrics.GenerationalDistance)9 Spacing (org.evosuite.ga.problems.metrics.Spacing)9 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)9 Booths (org.evosuite.ga.problems.singleobjective.Booths)5 TestChromosome (org.evosuite.testcase.TestChromosome)5 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)5 EvoSuite (org.evosuite.EvoSuite)4 LinkedHashSet (java.util.LinkedHashSet)3 Properties (org.evosuite.Properties)3