Search in sources :

Example 1 with RuntimeVariable

use of org.evosuite.statistics.RuntimeVariable 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)

Example 2 with RuntimeVariable

use of org.evosuite.statistics.RuntimeVariable in project evosuite by EvoSuite.

the class TestSuiteGeneratorHelper method getBytecodeStatistics.

static void getBytecodeStatistics() {
    if (Properties.TRACK_BOOLEAN_BRANCHES) {
        int gradientBranchCount = ExecutionTraceImpl.gradientBranches.size() * 2;
        ClientServices.track(RuntimeVariable.Gradient_Branches, gradientBranchCount);
    }
    if (Properties.TRACK_COVERED_GRADIENT_BRANCHES) {
        int coveredGradientBranchCount = ExecutionTraceImpl.gradientBranchesCoveredTrue.size() + ExecutionTraceImpl.gradientBranchesCoveredFalse.size();
        ClientServices.track(RuntimeVariable.Gradient_Branches_Covered, coveredGradientBranchCount);
    }
    if (Properties.BRANCH_COMPARISON_TYPES) {
        int cmp_intzero = 0, cmp_intint = 0, cmp_refref = 0, cmp_refnull = 0;
        int bc_lcmp = 0, bc_fcmpl = 0, bc_fcmpg = 0, bc_dcmpl = 0, bc_dcmpg = 0;
        for (Branch b : BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getAllBranches()) {
            int branchOpCode = b.getInstruction().getASMNode().getOpcode();
            int previousOpcode = -2;
            if (b.getInstruction().getASMNode().getPrevious() != null) {
                previousOpcode = b.getInstruction().getASMNode().getPrevious().getOpcode();
            }
            switch(previousOpcode) {
                case Opcodes.LCMP:
                    bc_lcmp++;
                    break;
                case Opcodes.FCMPL:
                    bc_fcmpl++;
                    break;
                case Opcodes.FCMPG:
                    bc_fcmpg++;
                    break;
                case Opcodes.DCMPL:
                    bc_dcmpl++;
                    break;
                case Opcodes.DCMPG:
                    bc_dcmpg++;
                    break;
            }
            switch(branchOpCode) {
                // copmpare int with zero
                case Opcodes.IFEQ:
                case Opcodes.IFNE:
                case Opcodes.IFLT:
                case Opcodes.IFGE:
                case Opcodes.IFGT:
                case Opcodes.IFLE:
                    cmp_intzero++;
                    break;
                // copmpare int with int
                case Opcodes.IF_ICMPEQ:
                case Opcodes.IF_ICMPNE:
                case Opcodes.IF_ICMPLT:
                case Opcodes.IF_ICMPGE:
                case Opcodes.IF_ICMPGT:
                case Opcodes.IF_ICMPLE:
                    cmp_intint++;
                    break;
                // copmpare reference with reference
                case Opcodes.IF_ACMPEQ:
                case Opcodes.IF_ACMPNE:
                    cmp_refref++;
                    break;
                // compare reference with null
                case Opcodes.IFNULL:
                case Opcodes.IFNONNULL:
                    cmp_refnull++;
                    break;
            }
        }
        ClientServices.track(RuntimeVariable.Cmp_IntZero, cmp_intzero);
        ClientServices.track(RuntimeVariable.Cmp_IntInt, cmp_intint);
        ClientServices.track(RuntimeVariable.Cmp_RefRef, cmp_refref);
        ClientServices.track(RuntimeVariable.Cmp_RefNull, cmp_refnull);
        ClientServices.track(RuntimeVariable.BC_lcmp, bc_lcmp);
        ClientServices.track(RuntimeVariable.BC_fcmpl, bc_fcmpl);
        ClientServices.track(RuntimeVariable.BC_fcmpg, bc_fcmpg);
        ClientServices.track(RuntimeVariable.BC_dcmpl, bc_dcmpl);
        ClientServices.track(RuntimeVariable.BC_dcmpg, bc_dcmpg);
        RuntimeVariable[] bytecodeVarsCovered = new RuntimeVariable[] { RuntimeVariable.Covered_lcmp, RuntimeVariable.Covered_fcmpl, RuntimeVariable.Covered_fcmpg, RuntimeVariable.Covered_dcmpl, RuntimeVariable.Covered_dcmpg, RuntimeVariable.Covered_IntInt, RuntimeVariable.Covered_IntInt, RuntimeVariable.Covered_IntZero, RuntimeVariable.Covered_RefRef, RuntimeVariable.Covered_RefNull };
        for (RuntimeVariable bcvar : bytecodeVarsCovered) {
            ClientServices.track(bcvar, getBytecodeCount(bcvar, ExecutionTraceImpl.bytecodeInstructionCoveredFalse) + getBytecodeCount(bcvar, ExecutionTraceImpl.bytecodeInstructionCoveredTrue));
        }
        RuntimeVariable[] bytecodeVarsReached = new RuntimeVariable[] { RuntimeVariable.Reached_lcmp, RuntimeVariable.Reached_fcmpl, RuntimeVariable.Reached_fcmpg, RuntimeVariable.Reached_dcmpl, RuntimeVariable.Reached_dcmpg, RuntimeVariable.Reached_IntInt, RuntimeVariable.Reached_IntInt, RuntimeVariable.Reached_IntZero, RuntimeVariable.Reached_RefRef, RuntimeVariable.Reached_RefNull };
        for (RuntimeVariable bcvar : bytecodeVarsReached) {
            ClientServices.track(bcvar, getBytecodeCount(bcvar, ExecutionTraceImpl.bytecodeInstructionReached) * 2);
        }
    }
}
Also used : Branch(org.evosuite.coverage.branch.Branch) RuntimeVariable(org.evosuite.statistics.RuntimeVariable)

Example 3 with RuntimeVariable

use of org.evosuite.statistics.RuntimeVariable in project evosuite by EvoSuite.

the class CoverageCriteriaAnalyzer method analyzeCoverage.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static void analyzeCoverage(TestSuiteChromosome testSuite, Properties.Criterion criterion, boolean recalculate) {
    TestSuiteChromosome testSuiteCopy = testSuite.clone();
    TestFitnessFactory factory = FitnessFunctions.getFitnessFactory(criterion);
    if (recalculate) {
        reinstrument(testSuiteCopy, criterion);
        for (TestChromosome test : testSuiteCopy.getTestChromosomes()) {
            test.getTestCase().clearCoveredGoals();
            test.clearCachedResults();
            // re-execute test cases and it will be able to find the covered goals
            if (isMutationCriterion(criterion)) {
                test.setChanged(true);
            }
        }
    }
    List<TestFitnessFunction> goals = factory.getCoverageGoals();
    Collections.sort(goals);
    StringBuffer buffer = new StringBuffer(goals.size());
    int covered = 0;
    for (TestFitnessFunction goal : goals) {
        if (goal.isCoveredBy(testSuiteCopy)) {
            logger.debug("Goal {} is covered", goal);
            covered++;
            buffer.append("1");
        } else {
            logger.debug("Goal {} is not covered", goal);
            buffer.append("0");
            if (Properties.PRINT_MISSED_GOALS)
                LoggingUtils.getEvoLogger().info(" - Missed goal {}", goal.toString());
        }
    }
    coverageBitString.put(criterion.name(), buffer);
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.CoverageBitString, coverageBitString.size() == 0 ? "0" : coverageBitString.values().toString().replace("[", "").replace("]", "").replace(", ", ""));
    RuntimeVariable bitStringVariable = getBitStringVariable(criterion);
    if (bitStringVariable != null) {
        String goalBitString = buffer.toString();
        ClientServices.getInstance().getClientNode().trackOutputVariable(bitStringVariable, goalBitString);
    }
    if (goals.isEmpty()) {
        if (criterion == Properties.Criterion.MUTATION || criterion == Properties.Criterion.STRONGMUTATION) {
            ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.MutationScore, 1.0);
        }
        LoggingUtils.getEvoLogger().info("* Coverage of criterion " + criterion + ": 100% (no goals)");
        ClientServices.getInstance().getClientNode().trackOutputVariable(getCoverageVariable(criterion), 1.0);
    } else {
        ClientServices.getInstance().getClientNode().trackOutputVariable(getCoverageVariable(criterion), (double) covered / (double) goals.size());
        if (criterion == Properties.Criterion.MUTATION || criterion == Properties.Criterion.STRONGMUTATION) {
            ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.MutationScore, (double) covered / (double) goals.size());
        }
        LoggingUtils.getEvoLogger().info("* Coverage of criterion " + criterion + ": " + NumberFormat.getPercentInstance().format((double) covered / (double) goals.size()));
        LoggingUtils.getEvoLogger().info("* Total number of goals: " + goals.size());
        LoggingUtils.getEvoLogger().info("* Number of covered goals: " + covered);
    }
    // FIXME it works, but needs a better way of handling this
    if (criterion == Properties.Criterion.RHO) {
        RhoCoverageSuiteFitness rho = new RhoCoverageSuiteFitness();
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.RhoScore, Math.abs(0.5 - rho.getFitness(testSuite)));
    } else if (criterion == Properties.Criterion.AMBIGUITY) {
        AmbiguityCoverageSuiteFitness ag = new AmbiguityCoverageSuiteFitness();
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.AmbiguityScore, ag.getFitness(testSuite));
    }
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) RuntimeVariable(org.evosuite.statistics.RuntimeVariable) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) RhoCoverageSuiteFitness(org.evosuite.coverage.rho.RhoCoverageSuiteFitness) TestChromosome(org.evosuite.testcase.TestChromosome) AmbiguityCoverageSuiteFitness(org.evosuite.coverage.ambiguity.AmbiguityCoverageSuiteFitness)

Aggregations

RuntimeVariable (org.evosuite.statistics.RuntimeVariable)3 TestChromosome (org.evosuite.testcase.TestChromosome)2 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)2 AmbiguityCoverageSuiteFitness (org.evosuite.coverage.ambiguity.AmbiguityCoverageSuiteFitness)1 Branch (org.evosuite.coverage.branch.Branch)1 Mutation (org.evosuite.coverage.mutation.Mutation)1 RhoCoverageSuiteFitness (org.evosuite.coverage.rho.RhoCoverageSuiteFitness)1 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)1 ExecutionTrace (org.evosuite.testcase.execution.ExecutionTrace)1 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)1