Search in sources :

Example 1 with ExecutionTrace

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

the class TestSuiteGenerator method configureClassReInitializer.

/**
 * Reports the initialized classes during class initialization to the
 * ClassReInitializater and configures the ClassReInitializer accordingly
 */
private void configureClassReInitializer() {
    // add loaded classes during building of dependency graph
    ExecutionTrace execTrace = ExecutionTracer.getExecutionTracer().getTrace();
    final List<String> initializedClasses = execTrace.getInitializedClasses();
    ClassReInitializer.getInstance().addInitializedClasses(initializedClasses);
    // set the behaviour of the ClassReInitializer
    final boolean reset_all_classes = Properties.RESET_ALL_CLASSES_DURING_TEST_GENERATION;
    ClassReInitializer.getInstance().setReInitializeAllClasses(reset_all_classes);
}
Also used : ExecutionTrace(org.evosuite.testcase.execution.ExecutionTrace)

Example 2 with ExecutionTrace

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

the class StrongMutationSuiteFitness method getFitness.

/* (non-Javadoc)
	 * @see org.evosuite.ga.FitnessFunction#getFitness(org.evosuite.ga.Chromosome)
	 */
/**
 * {@inheritDoc}
 */
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> individual) {
    runTestSuite(individual);
    // Set<MutationTestFitness> uncoveredMutants = MutationTestPool.getUncoveredFitnessFunctions();
    TestSuiteChromosome suite = (TestSuiteChromosome) individual;
    for (TestChromosome test : suite.getTestChromosomes()) {
        ExecutionResult result = test.getLastExecutionResult();
        if (result.hasTimeout() || result.hasTestException()) {
            logger.debug("Skipping test with timeout");
            double fitness = branchFitness.totalGoals * 2 + branchFitness.totalMethods + 3 * this.numMutants;
            updateIndividual(this, individual, fitness);
            suite.setCoverage(this, 0.0);
            logger.info("Test case has timed out, setting fitness to max value " + fitness);
            return fitness;
        }
    }
    // First objective: achieve branch coverage
    logger.debug("Calculating branch fitness: ");
    double fitness = branchFitness.getFitness(individual);
    Set<Integer> touchedMutants = new LinkedHashSet<Integer>();
    Map<Mutation, Double> minMutantFitness = new LinkedHashMap<Mutation, Double>();
    // 0..1 -> propagation distance
    for (Integer mutantId : this.mutantMap.keySet()) {
        MutationTestFitness mutantFitness = mutantMap.get(mutantId);
        minMutantFitness.put(mutantFitness.getMutation(), 3.0);
    }
    int mutantsChecked = 0;
    int numKilled = removedMutants.size();
    Set<Integer> newKilled = new LinkedHashSet<Integer>();
    // Quicker tests first
    List<TestChromosome> executionOrder = prioritizeTests(suite);
    for (TestChromosome test : executionOrder) {
        ExecutionResult result = test.getLastExecutionResult();
        // use reflection for basic criteria, not for mutation
        if (result.calledReflection())
            continue;
        ExecutionTrace trace = result.getTrace();
        touchedMutants.addAll(trace.getTouchedMutants());
        logger.debug("Tests touched " + touchedMutants.size() + " mutants");
        Map<Integer, Double> touchedMutantsDistances = trace.getMutationDistances();
        if (touchedMutantsDistances.isEmpty()) {
            // if 'result' does not touch any mutant, no need to continue
            continue;
        }
        Iterator<Entry<Integer, MutationTestFitness>> it = this.mutantMap.entrySet().iterator();
        while (it.hasNext()) {
            Entry<Integer, MutationTestFitness> entry = it.next();
            int mutantID = entry.getKey();
            if (newKilled.contains(mutantID)) {
                continue;
            }
            MutationTestFitness goal = entry.getValue();
            if (MutationTimeoutStoppingCondition.isDisabled(goal.getMutation())) {
                logger.debug("Skipping timed out mutation " + goal.getMutation().getId());
                continue;
            }
            mutantsChecked++;
            double mutantInfectionDistance = 3.0;
            boolean hasBeenTouched = touchedMutantsDistances.containsKey(mutantID);
            if (hasBeenTouched) {
                // Infection happened, so we need to check propagation
                if (touchedMutantsDistances.get(mutantID) == 0.0) {
                    logger.debug("Executing test against mutant " + goal.getMutation());
                    // archive is updated by the TestFitnessFunction class
                    mutantInfectionDistance = goal.getFitness(test, result);
                } else {
                    // We can skip calling the test fitness function since we already know
                    // fitness is 1.0 (for propagation) + infection distance
                    mutantInfectionDistance = 1.0 + normalize(touchedMutantsDistances.get(mutantID));
                }
            } else {
                // archive is updated by the TestFitnessFunction class
                mutantInfectionDistance = goal.getFitness(test, result);
            }
            if (mutantInfectionDistance == 0.0) {
                numKilled++;
                newKilled.add(mutantID);
                // update list of covered goals
                result.test.addCoveredGoal(goal);
                // goal to not be considered by the next iteration of the evolutionary algorithm
                this.toRemoveMutants.add(mutantID);
            } else {
                minMutantFitness.put(goal.getMutation(), Math.min(mutantInfectionDistance, minMutantFitness.get(goal.getMutation())));
            }
        }
    }
    // logger.info("Fitness values for " + minMutantFitness.size() + " mutants");
    for (Double fit : minMutantFitness.values()) {
        fitness += fit;
    }
    logger.debug("Mutants killed: {}, Checked: {}, Goals: {})", numKilled, mutantsChecked, this.numMutants);
    updateIndividual(this, individual, fitness);
    assert numKilled == newKilled.size() + removedMutants.size();
    assert numKilled <= this.numMutants;
    double coverage = (double) numKilled / (double) this.numMutants;
    assert coverage >= 0.0 && coverage <= 1.0;
    suite.setCoverage(this, coverage);
    suite.setNumOfCoveredGoals(this, numKilled);
    return fitness;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ExecutionTrace(org.evosuite.testcase.execution.ExecutionTrace) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) AbstractTestSuiteChromosome(org.evosuite.testsuite.AbstractTestSuiteChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 3 with ExecutionTrace

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

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

the class TestExecutionTracer method testLesserEqual.

@Ignore
@Test
public void testLesserEqual() {
    final Integer branchId = 3;
    final String methodName = "lesserEqual_IF_CMPGT";
    ExecutionTrace execTrace = execute(methodName, 5, 5);
    Assert.assertEquals(methodName + signature, BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranch(branchId).getMethodName());
    Assert.assertEquals(0.0, execTrace.getTrueDistance(branchId), DELTA);
    Assert.assertEquals(1.0, execTrace.getFalseDistance(branchId), DELTA);
    execTrace = execute(methodName, 6, 5);
    Assert.assertEquals(1.0, execTrace.getTrueDistance(branchId), DELTA);
    Assert.assertEquals(0.0, execTrace.getFalseDistance(branchId), DELTA);
    execTrace = execute(methodName, 5, 6);
    Assert.assertEquals(0.0, execTrace.getTrueDistance(branchId), DELTA);
    Assert.assertEquals(2.0, execTrace.getFalseDistance(branchId), DELTA);
}
Also used : ExecutionTrace(org.evosuite.testcase.execution.ExecutionTrace) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with ExecutionTrace

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

the class CoverageAnalysis method analyzeCoverageCriterion.

private static void analyzeCoverageCriterion(List<JUnitResult> results, Properties.Criterion criterion) {
    logger.info("analysing coverage of " + criterion);
    // Factory
    TestFitnessFactory<? extends TestFitnessFunction> factory = FitnessFunctions.getFitnessFactory(criterion);
    // Goals
    List<?> goals = null;
    if (criterion == Criterion.MUTATION || criterion == Criterion.STRONGMUTATION) {
        goals = MutationPool.getMutants();
    } else {
        goals = factory.getCoverageGoals();
    }
    totalGoals += goals.size();
    // A dummy Chromosome
    TestChromosome dummy = new TestChromosome();
    dummy.setChanged(false);
    // Execution result of a dummy Test Case
    ExecutionResult executionResult = new ExecutionResult(dummy.getTestCase());
    // coverage matrix (each row represents the coverage of each test case
    // and each column represents the coverage of each component (e.g., line)
    // this coverage matrix is useful for Rho fitness
    // +1 because we also want to include the test result
    boolean[][] coverage_matrix = new boolean[results.size()][goals.size() + 1];
    BitSet covered = new BitSet(goals.size());
    for (int index_test = 0; index_test < results.size(); index_test++) {
        JUnitResult tR = results.get(index_test);
        ExecutionTrace trace = tR.getExecutionTrace();
        executionResult.setTrace(trace);
        dummy.getTestCase().clearCoveredGoals();
        dummy.setLastExecutionResult(executionResult);
        if (criterion == Criterion.MUTATION || criterion == Criterion.STRONGMUTATION) {
            for (Integer mutationID : trace.getTouchedMutants()) {
                Mutation mutation = MutationPool.getMutant(mutationID);
                if (goals.contains(mutation)) {
                    MutationObserver.activateMutation(mutationID);
                    List<JUnitResult> mutationResults = executeTests(tR.getJUnitClass());
                    MutationObserver.deactivateMutation();
                    for (JUnitResult mR : mutationResults) {
                        if (mR.getFailureCount() != tR.getFailureCount()) {
                            logger.info("Mutation killed: " + mutationID);
                            covered.set(mutation.getId());
                            coverage_matrix[index_test][mutationID.intValue()] = true;
                            break;
                        }
                    }
                }
            }
        } else {
            for (int index_component = 0; index_component < goals.size(); index_component++) {
                TestFitnessFunction goal = (TestFitnessFunction) goals.get(index_component);
                if (goal.isCovered(dummy)) {
                    covered.set(index_component);
                    coverage_matrix[index_test][index_component] = true;
                } else {
                    coverage_matrix[index_test][index_component] = false;
                }
            }
        }
        coverage_matrix[index_test][goals.size()] = tR.wasSuccessful();
    }
    totalCoveredGoals += covered.cardinality();
    if (Properties.COVERAGE_MATRIX) {
        CoverageReportGenerator.writeCoverage(coverage_matrix, criterion);
    }
    StringBuilder str = new StringBuilder();
    for (int index_component = 0; index_component < goals.size(); index_component++) {
        str.append(covered.get(index_component) ? "1" : "0");
    }
    logger.info("* CoverageBitString " + str.toString());
    RuntimeVariable bitStringVariable = CoverageCriteriaAnalyzer.getBitStringVariable(criterion);
    if (goals.isEmpty()) {
        LoggingUtils.getEvoLogger().info("* Coverage of criterion " + criterion + ": 100% (no goals)");
        ClientServices.getInstance().getClientNode().trackOutputVariable(CoverageCriteriaAnalyzer.getCoverageVariable(criterion), 1.0);
        if (bitStringVariable != null) {
            ClientServices.getInstance().getClientNode().trackOutputVariable(bitStringVariable, "1");
        }
    } else {
        double coverage = ((double) covered.cardinality()) / ((double) goals.size());
        LoggingUtils.getEvoLogger().info("* Coverage of criterion " + criterion + ": " + NumberFormat.getPercentInstance().format(coverage));
        LoggingUtils.getEvoLogger().info("* Number of covered goals: " + covered.cardinality() + " / " + goals.size());
        ClientServices.getInstance().getClientNode().trackOutputVariable(CoverageCriteriaAnalyzer.getCoverageVariable(criterion), coverage);
        if (bitStringVariable != null) {
            ClientServices.getInstance().getClientNode().trackOutputVariable(bitStringVariable, str.toString());
        }
    }
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionTrace(org.evosuite.testcase.execution.ExecutionTrace) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) RuntimeVariable(org.evosuite.statistics.RuntimeVariable) Mutation(org.evosuite.coverage.mutation.Mutation) TestChromosome(org.evosuite.testcase.TestChromosome)

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