Search in sources :

Example 11 with ExecutionTrace

use of org.evosuite.testcase.execution.ExecutionTrace 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)

Example 12 with ExecutionTrace

use of org.evosuite.testcase.execution.ExecutionTrace in project evosuite by EvoSuite.

the class JUnitTestSuite method runSuite.

/**
 * <p>runSuite</p>
 *
 * @param name a {@link java.lang.String} object.
 */
public void runSuite(String name) {
    try {
        Class<?> forName = null;
        forName = Class.forName(name);
        logger.info("Running against JUnit test suite " + name);
        JUnitCore.runClasses(forName);
        ExecutionTrace trace = ExecutionTracer.getExecutionTracer().getTrace();
        coveredMethods = new HashSet<String>();
        coveredBranchesTrue = trace.getCoveredTrueBranches();
        coveredBranchesFalse = trace.getCoveredFalseBranches();
        for (String methodName : trace.getCoveredMethods()) {
            if (!methodName.contains("$"))
                coveredMethods.add(methodName);
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}
Also used : ExecutionTrace(org.evosuite.testcase.execution.ExecutionTrace)

Example 13 with ExecutionTrace

use of org.evosuite.testcase.execution.ExecutionTrace in project evosuite by EvoSuite.

the class DefUseFitnessCalculator method callTestFitnessFunctionForTrace.

// other core methods
/**
 * Executes the TestFitnessFunction.getFitness() function on the given
 * ExecutionResult but using the given targetTrace
 *
 * The ExecutionResult is left in it's original state after execution
 */
private double callTestFitnessFunctionForTrace(ExecutionTrace targetTrace, TestFitnessFunction targetFitness) {
    ExecutionTrace originalTrace = result.getTrace();
    result.setTrace(targetTrace);
    double fitness = targetFitness.getFitness(individual, result);
    result.setTrace(originalTrace);
    return fitness;
}
Also used : ExecutionTrace(org.evosuite.testcase.execution.ExecutionTrace)

Example 14 with ExecutionTrace

use of org.evosuite.testcase.execution.ExecutionTrace in project evosuite by EvoSuite.

the class DefUseFitnessCalculator method calculateFitnessForObject.

/**
 * Calculates the DefUseCoverage fitness for this DefUsePair considering
 * only the objectId'th CUT-Object in the ExecutionResult
 *
 * Gets called for all CUT-objects in the ExecutionResult whenever the
 * goalDefinition and the goalUse were passed at least once on that object.
 */
private double calculateFitnessForObject(Integer objectId) {
    // filter out trace information from other objects
    ExecutionTrace objectTrace = result.getTrace().getTraceForObject(objectId);
    double fitness = 1;
    // handle special definition case TODO already handled!?
    if (isSpecialDefinition(goalDefinition)) {
        double useFitness = callTestFitnessFunctionForTrace(objectTrace, goalUseFitness);
        fitness = normalize(useFitness);
        if (ArrayUtil.contains(Properties.CRITERION, Criterion.DEFUSE) && fitness == 0.0)
            goal.setCovered(individual, objectTrace, objectId);
        return fitness;
    }
    // check if goalDefinition is active at any goalUsePosition
    List<Integer> usePositions = DefUseExecutionTraceAnalyzer.getUsePositions(goalUse, objectTrace, objectId);
    List<Integer> goalDefinitionPositions = DefUseExecutionTraceAnalyzer.getDefinitionPositions(goalDefinition, objectTrace, objectId);
    if (!defVariable.equals(useVariable)) {
        logger.debug("Checking an aliasing case: " + goalDefinition + "\n" + goalUse);
    }
    for (Integer usePos : usePositions) {
        int activeDefId = DefUseExecutionTraceAnalyzer.getActiveDefinitionIdAt(defVariable, objectTrace, usePos, objectId);
        logger.debug("Activedef at position " + usePos + " is: " + DefUsePool.getDefinitionByDefId(activeDefId));
        if (activeDefId == goalDefinition.getDefId()) {
            // Case 3.1.
            if (ArrayUtil.contains(Properties.CRITERION, Criterion.DEFUSE))
                goal.setCovered(individual, objectTrace, objectId);
            {
                if (!defVariable.equals(useVariable)) {
                    // Check if object is equal
                    Object definedObject = DefUseExecutionTraceAnalyzer.getActiveObjectAtDefinition(result.getTrace(), defVariable, objectId, usePos);
                    Object usedObject = DefUseExecutionTraceAnalyzer.getActiveObjectAtUse(result.getTrace(), useVariable, objectId, usePos);
                    logger.info(definedObject + ", " + usedObject);
                    if (definedObject == usedObject) {
                        logger.debug("That's the target def we're looking for, and objects are equal!");
                        return 0.0;
                    } else {
                        logger.debug("That's the target def we're looking for, but objects are not equal!");
                        continue;
                    }
                }
                return 0.0;
            }
        }
    }
    // not goalDefinitionId
    if (!goalUse.isRootBranchDependent())
        // always be 1.0
        for (Integer goalDefinitionPos : goalDefinitionPositions) {
            double useFitness;
            try {
                useFitness = calculateUseFitnessForDefinitionPos(objectTrace, objectId, goalDefinitionPos);
            } catch (UnexpectedFitnessException e) {
                continue;
            }
            // if(useFitness == 0.0)
            // throw new
            // IllegalStateException("unexpected: should have been detected earlier");
            double newFitness = normalize(useFitness);
            if (newFitness < fitness)
                fitness = newFitness;
        }
    return fitness;
}
Also used : ExecutionTrace(org.evosuite.testcase.execution.ExecutionTrace)

Example 15 with ExecutionTrace

use of org.evosuite.testcase.execution.ExecutionTrace in project evosuite by EvoSuite.

the class DefUseFitnessCalculator method calculateFitnessForDURange.

/**
 * Used to calculate a BranchCoverageTestFitness considering only trace
 * information in a given range of duCounter positions
 *
 * Filters the ExecutionTrace using
 * ExecutionTrace.getTraceInDUCounterRange() to only contain information
 * made between duCounterStart and duCounterEnd
 *
 * Additionally, if wantToCoverTargetDU is set all points in the trace where
 * the given targetDUBranch would be passed is filtered out in order to
 * prevent miscalculations. Again ExecutionTrace.getTraceInDUCounterRange()
 * holds more information
 *
 * This method gets called for alternative fitness calculation -
 * calculateAlternativeFitness() - and in order to determine the
 * goalUseBranch fitness between a goalUsePos and its next overwriting
 * definition - calculateUseFitnessForDefinitionPos()
 *
 * @throws UnexpectedFitnessException
 */
private double calculateFitnessForDURange(ExecutionTrace targetTrace, Integer objectId, TestFitnessFunction targetFitness, DefUse targetDU, boolean wantToCoverTargetDU, int duCounterStart, int duCounterEnd, boolean expectNotToBeZero) throws UnexpectedFitnessException {
    // filter trace
    ExecutionTrace cutTrace = targetTrace.getTraceInDUCounterRange(targetDU, wantToCoverTargetDU, duCounterStart, duCounterEnd);
    // calculate fitness
    double fitness = callTestFitnessFunctionForTrace(cutTrace, targetFitness);
    // }
    if (expectNotToBeZero && fitness == 0.0)
        throw new UnexpectedFitnessException();
    return fitness;
}
Also used : ExecutionTrace(org.evosuite.testcase.execution.ExecutionTrace)

Aggregations

ExecutionTrace (org.evosuite.testcase.execution.ExecutionTrace)20 Ignore (org.junit.Ignore)6 Test (org.junit.Test)6 TestChromosome (org.evosuite.testcase.TestChromosome)4 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)4 HashSet (java.util.HashSet)3 Set (java.util.Set)2 TestabilityTransformationClassLoader (org.evosuite.instrumentation.testability.TestabilityTransformationClassLoader)2 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)2 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 Entry (java.util.Map.Entry)1 Branch (org.evosuite.coverage.branch.Branch)1 BranchPool (org.evosuite.coverage.branch.BranchPool)1 Mutation (org.evosuite.coverage.mutation.Mutation)1 RhoCoverageFactory (org.evosuite.coverage.rho.RhoCoverageFactory)1