Search in sources :

Example 1 with RhoCoverageFactory

use of org.evosuite.coverage.rho.RhoCoverageFactory in project evosuite by EvoSuite.

the class EntBugTestStrategy method generateTests.

@Override
public TestSuiteChromosome generateTests() {
    // Set up search algorithm
    LoggingUtils.getEvoLogger().info("* Setting up search algorithm for individual test generation (ASE'13)");
    ExecutionTracer.enableTraceCalls();
    // Set up genetic algorithm
    PropertiesTestGAFactory factory = new PropertiesTestGAFactory();
    GeneticAlgorithm<TestChromosome> ga = factory.getSearchAlgorithm();
    if (Properties.SERIALIZE_GA || Properties.CLIENT_ON_THREAD) {
        TestGenerationResultBuilder.getInstance().setGeneticAlgorithm(ga);
    }
    // What's the search target
    RhoCoverageFactory rhoFactory = (RhoCoverageFactory) FitnessFunctions.getFitnessFactory(Properties.Criterion.RHO);
    RhoCoverageTestFitness rhoTestFitnessFunction = new RhoCoverageTestFitness();
    ga.addFitnessFunction(rhoTestFitnessFunction);
    // Goals
    List<TestFitnessFunction> goals = new ArrayList<TestFitnessFunction>(rhoFactory.getCoverageGoals());
    LoggingUtils.getEvoLogger().info("* Total number of test goals: ");
    LoggingUtils.getEvoLogger().info("  - Rho " + goals.size());
    double previous_fitness = RhoCoverageFactory.getRho();
    double best_fitness = 0.0;
    int number_of_generations = (int) (Properties.SEARCH_BUDGET / 10);
    // Properties.SEARCH_BUDGET = 10; // 10 seconds for each generation
    TestSuiteChromosome bests = new TestSuiteChromosome();
    while (number_of_generations > 0) {
        LoggingUtils.getEvoLogger().info("  * iteration(" + number_of_generations + ")");
        // 10 seconds for each generation
        // FIXME: should be a parameter?
        ga.setStoppingConditionLimit(10);
        ga.resetStoppingConditions();
        ga.clearPopulation();
        // ga.setChromosomeFactory(getDefaultChromosomeFactory()); // not in the original version
        ga.generateSolution();
        number_of_generations--;
        TestChromosome best = (TestChromosome) ga.getBestIndividual();
        if (best.getLastExecutionResult() == null) {
            // some timeout?
            continue;
        }
        best_fitness = best.getFitness(rhoTestFitnessFunction);
        if (// we've found a better test case
        (best_fitness < previous_fitness) || // or this new test case is not so bad (i.e., < Properties.EPSON)
        (best_fitness <= Properties.EPSON)) {
            // GOOD
            LoggingUtils.getEvoLogger().info("  * new best (previous fitness: " + previous_fitness + " | best_fitness: " + best_fitness + ")");
            ExecutionResult exec = best.getLastExecutionResult();
            ExecutionTrace trace = exec.getTrace();
            Set<Integer> testCoverage = trace.getCoveredLines();
            LoggingUtils.getEvoLogger().info("  * new test case added " + testCoverage.toString());
            rhoTestFitnessFunction.incrementNumber_of_Ones(testCoverage.size());
            rhoTestFitnessFunction.incrementNumber_of_Test_Cases();
            rhoTestFitnessFunction.addTestCoverage(testCoverage);
            bests.addTest(best);
            // update global fitness
            previous_fitness = best_fitness;
        } else {
            // BAD
            LoggingUtils.getEvoLogger().info("  * new test case ignored (previous fitness: " + previous_fitness + " | best_fitness: " + best_fitness + ")");
        }
    }
    LoggingUtils.getEvoLogger().info("* Search finished after, best individual has fitness " + best_fitness);
    LoggingUtils.getEvoLogger().info("Resulting test suite: " + bests.size() + " tests, length " + bests.totalLengthOfTestCases());
    return bests;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ArrayList(java.util.ArrayList) ExecutionTrace(org.evosuite.testcase.execution.ExecutionTrace) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) RhoCoverageTestFitness(org.evosuite.coverage.rho.RhoCoverageTestFitness) RhoCoverageFactory(org.evosuite.coverage.rho.RhoCoverageFactory) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Aggregations

ArrayList (java.util.ArrayList)1 RhoCoverageFactory (org.evosuite.coverage.rho.RhoCoverageFactory)1 RhoCoverageTestFitness (org.evosuite.coverage.rho.RhoCoverageTestFitness)1 TestChromosome (org.evosuite.testcase.TestChromosome)1 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)1 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)1 ExecutionTrace (org.evosuite.testcase.execution.ExecutionTrace)1 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)1