Search in sources :

Example 16 with TestChromosome

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

the class PropertiesTestGAFactory method getSearchAlgorithm.

@Override
public GeneticAlgorithm<TestChromosome> getSearchAlgorithm() {
    ChromosomeFactory<TestChromosome> factory = getChromosomeFactory();
    // FIXXME
    GeneticAlgorithm<TestChromosome> ga = getGeneticAlgorithm(factory);
    if (Properties.NEW_STATISTICS)
        ga.addListener(new org.evosuite.statistics.StatisticsListener());
    // How to select candidates for reproduction
    SelectionFunction<TestChromosome> selection_function = getSelectionFunction();
    selection_function.setMaximize(false);
    ga.setSelectionFunction(selection_function);
    // When to stop the search
    StoppingCondition stopping_condition = getStoppingCondition();
    ga.setStoppingCondition(stopping_condition);
    // ga.addListener(stopping_condition);
    if (Properties.STOP_ZERO) {
        ga.addStoppingCondition(new ZeroFitnessStoppingCondition());
    }
    if (!(stopping_condition instanceof MaxTimeStoppingCondition)) {
        ga.addStoppingCondition(new GlobalTimeStoppingCondition());
    }
    if (ArrayUtil.contains(Properties.CRITERION, Criterion.MUTATION) || ArrayUtil.contains(Properties.CRITERION, Criterion.STRONGMUTATION)) {
        ga.addStoppingCondition(new MutationTimeoutStoppingCondition());
    }
    ga.resetStoppingConditions();
    ga.setPopulationLimit(getPopulationLimit());
    // How to cross over
    CrossOverFunction crossover_function = getCrossoverFunction();
    ga.setCrossOverFunction(crossover_function);
    if (Properties.CHECK_BEST_LENGTH) {
        org.evosuite.testcase.RelativeTestLengthBloatControl bloat_control = new org.evosuite.testcase.RelativeTestLengthBloatControl();
        ga.addBloatControl(bloat_control);
        ga.addListener(bloat_control);
    }
    TestCaseSecondaryObjective.setSecondaryObjectives();
    if (Properties.DYNAMIC_LIMIT) {
        // max_s = GAProperties.generations * getBranches().size();
        // TODO: might want to make this dependent on the selected coverage
        // criterion
        // TODO also, question: is branchMap.size() really intended here?
        // I think BranchPool.getBranchCount() was intended
        Properties.SEARCH_BUDGET = Properties.SEARCH_BUDGET * (BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getNumBranchlessMethods(Properties.TARGET_CLASS) + BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranchCountForClass(Properties.TARGET_CLASS) * 2);
        stopping_condition.setLimit(Properties.SEARCH_BUDGET);
        logger.info("Setting dynamic length limit to " + Properties.SEARCH_BUDGET);
    }
    if (Properties.RECYCLE_CHROMOSOMES) {
        if (Properties.STRATEGY == Strategy.ONEBRANCH)
            ga.addListener(TestCaseRecycler.getInstance());
    }
    // ga.addListener(new ResourceController<TestChromosome>());
    return ga;
}
Also used : ZeroFitnessStoppingCondition(org.evosuite.ga.stoppingconditions.ZeroFitnessStoppingCondition) MaxTimeStoppingCondition(org.evosuite.ga.stoppingconditions.MaxTimeStoppingCondition) CrossOverFunction(org.evosuite.ga.operators.crossover.CrossOverFunction) MutationTimeoutStoppingCondition(org.evosuite.coverage.mutation.MutationTimeoutStoppingCondition) ZeroFitnessStoppingCondition(org.evosuite.ga.stoppingconditions.ZeroFitnessStoppingCondition) StoppingCondition(org.evosuite.ga.stoppingconditions.StoppingCondition) MaxTimeStoppingCondition(org.evosuite.ga.stoppingconditions.MaxTimeStoppingCondition) GlobalTimeStoppingCondition(org.evosuite.ga.stoppingconditions.GlobalTimeStoppingCondition) MutationTimeoutStoppingCondition(org.evosuite.coverage.mutation.MutationTimeoutStoppingCondition) TestChromosome(org.evosuite.testcase.TestChromosome) GlobalTimeStoppingCondition(org.evosuite.ga.stoppingconditions.GlobalTimeStoppingCondition)

Example 17 with TestChromosome

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

the class ConcolicExecution method getSymbolicPath.

/**
 * Retrieve the path condition for a given test case
 *
 * @param test
 *            a {@link org.evosuite.testcase.TestChromosome} object.
 * @return a {@link java.util.List} object.
 */
public static List<BranchCondition> getSymbolicPath(TestChromosome test) {
    TestChromosome dscCopy = (TestChromosome) test.clone();
    DefaultTestCase defaultTestCase = (DefaultTestCase) dscCopy.getTestCase();
    return executeConcolic(defaultTestCase);
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 18 with TestChromosome

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

the class StatisticsSender method sendCoveredInfo.

private static void sendCoveredInfo(TestSuiteChromosome testSuite) {
    Set<String> coveredMethods = new HashSet<String>();
    Set<Integer> coveredTrueBranches = new HashSet<Integer>();
    Set<Integer> coveredFalseBranches = new HashSet<Integer>();
    Set<String> coveredBranchlessMethods = new HashSet<String>();
    Set<Integer> coveredLines = new HashSet<Integer>();
    Set<Integer> coveredRealBranches = new HashSet<Integer>();
    Set<Integer> coveredInstrumentedBranches = new HashSet<Integer>();
    for (TestChromosome test : testSuite.getTestChromosomes()) {
        ExecutionTrace trace = test.getLastExecutionResult().getTrace();
        coveredMethods.addAll(trace.getCoveredMethods());
        coveredTrueBranches.addAll(trace.getCoveredTrueBranches());
        coveredFalseBranches.addAll(trace.getCoveredFalseBranches());
        coveredBranchlessMethods.addAll(trace.getCoveredBranchlessMethods());
        coveredLines.addAll(trace.getCoveredLines());
    }
    int coveredBranchesInstrumented = 0;
    int coveredBranchesReal = 0;
    if (Properties.ERROR_BRANCHES || Properties.EXCEPTION_BRANCHES) {
        BranchPool branchPool = BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT());
        for (Integer branchId : coveredTrueBranches) {
            Branch b = branchPool.getBranch(branchId);
            if (b.isInstrumented())
                coveredBranchesInstrumented++;
            else {
                coveredBranchesReal++;
            }
        }
        for (Integer branchId : coveredFalseBranches) {
            Branch b = branchPool.getBranch(branchId);
            if (b.isInstrumented())
                coveredBranchesInstrumented++;
            else {
                coveredBranchesReal++;
            }
        }
    } else {
        coveredBranchesReal = coveredTrueBranches.size() + coveredFalseBranches.size();
    }
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Covered_Goals, testSuite.getCoveredGoals().size());
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Covered_Methods, coveredMethods.size());
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Covered_Branches, coveredTrueBranches.size() + coveredFalseBranches.size());
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Covered_Branchless_Methods, coveredBranchlessMethods.size());
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Covered_Branches_Real, coveredBranchesReal);
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Covered_Branches_Instrumented, coveredBranchesInstrumented);
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Covered_Lines, coveredLines.size());
}
Also used : Branch(org.evosuite.coverage.branch.Branch) ExecutionTrace(org.evosuite.testcase.execution.ExecutionTrace) BranchPool(org.evosuite.coverage.branch.BranchPool) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet)

Example 19 with TestChromosome

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

the class FixedNumRandomTestStrategy method generateTests.

@Override
public TestSuiteChromosome generateTests() {
    LoggingUtils.getEvoLogger().info("* Generating fixed number of random tests");
    RandomLengthTestFactory factory = new org.evosuite.testcase.factories.RandomLengthTestFactory();
    TestSuiteChromosome suite = new TestSuiteChromosome();
    if (!canGenerateTestsForSUT()) {
        LoggingUtils.getEvoLogger().info("* Found no testable methods in the target class " + Properties.TARGET_CLASS);
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, 0);
        return suite;
    }
    for (int i = 0; i < Properties.NUM_RANDOM_TESTS; i++) {
        logger.info("Current test: " + i + "/" + Properties.NUM_RANDOM_TESTS);
        TestChromosome test = factory.getChromosome();
        ExecutionResult result = TestCaseExecutor.runTest(test.getTestCase());
        Integer pos = result.getFirstPositionOfThrownException();
        if (pos != null) {
            if (result.getExceptionThrownAtPosition(pos) instanceof CodeUnderTestException || result.getExceptionThrownAtPosition(pos) instanceof UncompilableCodeException || result.getExceptionThrownAtPosition(pos) instanceof TestCaseExecutor.TimeoutExceeded) {
                // Filter invalid tests
                continue;
            } else {
                // Remove anything that follows an exception
                test.getTestCase().chop(pos + 1);
            }
            test.setChanged(true);
        } else {
            test.setLastExecutionResult(result);
        }
        suite.addTest(test);
    }
    // Search is finished, send statistics
    sendExecutionStatistics();
    return suite;
}
Also used : TestCaseExecutor(org.evosuite.testcase.execution.TestCaseExecutor) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) RandomLengthTestFactory(org.evosuite.testcase.factories.RandomLengthTestFactory) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) TestChromosome(org.evosuite.testcase.TestChromosome) UncompilableCodeException(org.evosuite.testcase.execution.UncompilableCodeException)

Example 20 with TestChromosome

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

the class TestCaseRecycler method searchFinished.

@Override
public void searchFinished(GeneticAlgorithm<?> algorithm) {
    Chromosome individual = algorithm.getBestIndividual();
    if (individual instanceof TestChromosome) {
        TestChromosome testChromosome = (TestChromosome) individual;
        testPool.add(testChromosome.getTestCase());
    } else if (individual instanceof TestSuiteChromosome) {
        TestSuiteChromosome testSuiteChromosome = (TestSuiteChromosome) individual;
        testPool.addAll(testSuiteChromosome.getTests());
    }
}
Also used : TestChromosome(org.evosuite.testcase.TestChromosome) Chromosome(org.evosuite.ga.Chromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) 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