Search in sources :

Example 1 with DefUseCoverageTestFitness

use of org.evosuite.coverage.dataflow.DefUseCoverageTestFitness in project evosuite by EvoSuite.

the class SearchStatistics method minimized.

/**
 * {@inheritDoc}
 */
@Override
public void minimized(Chromosome chromosome) {
    TestSuiteChromosome best = (TestSuiteChromosome) chromosome;
    StatisticEntry entry = statistics.get(statistics.size() - 1);
    entry.tests = best.getTests();
    // TODO: Remember which lines were covered
    // This information is in ExecutionTrace.coverage
    entry.size_minimized = best.size();
    entry.length_minimized = best.totalLengthOfTestCases();
    entry.minimized_time = System.currentTimeMillis();
    entry.coverage = new HashSet<Integer>();
    entry.coveredIntraMethodPairs = 0;
    entry.coveredInterMethodPairs = 0;
    entry.coveredIntraClassPairs = 0;
    entry.coveredParameterPairs = 0;
    entry.aliasingIntraMethodPairs = 0;
    entry.aliasingInterMethodPairs = 0;
    entry.aliasingIntraClassPairs = 0;
    entry.aliasingParameterPairs = 0;
    entry.coveredAliasIntraMethodPairs = 0;
    entry.coveredAliasInterMethodPairs = 0;
    entry.coveredAliasIntraClassPairs = 0;
    entry.coveredAliasParameterPairs = 0;
    // TODO isn't this more or less copy-paste of
    // BranchCoverageSuiteFitness.getFitness()?
    // DONE To make this work for other criteria too, it would be perfect if
    // one
    // could ask every suite fitness how many goals were covered
    logger.debug("Calculating coverage of best individual with fitness " + chromosome.getFitness());
    Map<Integer, Double> true_distance = new HashMap<Integer, Double>();
    Map<Integer, Double> false_distance = new HashMap<Integer, Double>();
    Map<Integer, Integer> predicate_count = new HashMap<Integer, Integer>();
    Set<String> covered_methods = new HashSet<String>();
    Map<String, Set<Class<?>>> implicitTypesOfExceptions = new HashMap<String, Set<Class<?>>>();
    Map<String, Set<Class<?>>> explicitTypesOfExceptions = new HashMap<String, Set<Class<?>>>();
    Map<TestCase, Map<Integer, Boolean>> isExceptionExplicit = new HashMap<TestCase, Map<Integer, Boolean>>();
    Set<DefUseCoverageTestFitness> coveredDUGoals = new HashSet<DefUseCoverageTestFitness>();
    if (ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.DEFUSE) || Properties.ANALYSIS_CRITERIA.toUpperCase().contains("DEFUSE")) {
        for (DefUseCoverageTestFitness goal : DefUseCoverageFactory.getDUGoals()) {
            if (goal.isInterMethodPair())
                entry.numInterMethodPairs++;
            else if (goal.isIntraClassPair())
                entry.numIntraClassPairs++;
            else if (goal.isParameterGoal())
                entry.numParameterPairs++;
            else
                entry.numIntraMethodPairs++;
            if (goal.isAlias()) {
                if (goal.isInterMethodPair())
                    entry.aliasingInterMethodPairs++;
                else if (goal.isIntraClassPair())
                    entry.aliasingIntraClassPairs++;
                else if (goal.isParameterGoal())
                    entry.aliasingParameterPairs++;
                else
                    entry.aliasingIntraMethodPairs++;
            }
        }
        entry.numDefinitions = DefUsePool.getDefCounter();
        entry.numUses = DefUsePool.getUseCounter();
        entry.numDefUsePairs = DefUseCoverageFactory.getDUGoals().size();
    }
    logger.debug("Calculating line coverage");
    for (TestChromosome test : best.tests) {
        ExecutionResult result = executeTest(test, entry.className);
        ExecutionTrace trace = result.getTrace();
        entry.coverage.addAll(getCoveredLines(trace, entry.className));
        isExceptionExplicit.put(test.getTestCase(), result.explicitExceptions);
        if (ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.DEFUSE) || Properties.ANALYSIS_CRITERIA.toUpperCase().contains("DEFUSE")) {
            for (DefUseCoverageTestFitness goal : DefUseCoverageFactory.getDUGoals()) {
                if (coveredDUGoals.contains(goal))
                    continue;
                if (goal.isCovered(result)) {
                    coveredDUGoals.add(goal);
                    if (goal.isInterMethodPair()) {
                        entry.coveredInterMethodPairs++;
                        if (goal.isAlias()) {
                            entry.coveredAliasInterMethodPairs++;
                        }
                    } else if (goal.isIntraClassPair()) {
                        entry.coveredIntraClassPairs++;
                        if (goal.isAlias()) {
                            entry.coveredAliasIntraClassPairs++;
                        }
                    } else if (goal.isParameterGoal()) {
                        entry.coveredParameterPairs++;
                        if (goal.isAlias()) {
                            entry.coveredAliasParameterPairs++;
                        }
                    } else {
                        entry.coveredIntraMethodPairs++;
                        if (goal.isAlias()) {
                            entry.coveredAliasIntraMethodPairs++;
                        }
                    }
                }
            }
        }
        for (String method : trace.getCoveredMethods()) {
            if (method.startsWith(Properties.TARGET_CLASS) || method.startsWith(Properties.TARGET_CLASS + '$'))
                covered_methods.add(method);
        }
        for (Entry<Integer, Double> e : trace.getTrueDistances().entrySet()) {
            if (!predicate_count.containsKey(e.getKey()))
                predicate_count.put(e.getKey(), 1);
            else
                predicate_count.put(e.getKey(), predicate_count.get(e.getKey()) + 1);
            if (!true_distance.containsKey(e.getKey()) || true_distance.get(e.getKey()) > e.getValue()) {
                true_distance.put(e.getKey(), e.getValue());
            }
        }
        for (Entry<Integer, Double> e : trace.getFalseDistances().entrySet()) {
            if (!predicate_count.containsKey(e.getKey()))
                predicate_count.put(e.getKey(), 1);
            else
                predicate_count.put(e.getKey(), predicate_count.get(e.getKey()) + 1);
            if (!false_distance.containsKey(e.getKey()) || false_distance.get(e.getKey()) > e.getValue()) {
                false_distance.put(e.getKey(), e.getValue());
            }
        }
    }
    for (TestCase test : entry.results.keySet()) {
        Map<Integer, Throwable> exceptions = entry.results.get(test);
        // iterate on the indexes of the statements that resulted in an exception
        for (Integer i : exceptions.keySet()) {
            Throwable t = exceptions.get(i);
            if (t instanceof SecurityException && Properties.SANDBOX)
                continue;
            if (i >= test.size()) {
                // Timeouts are put after the last statement if the process was forcefully killed
                continue;
            }
            String methodName = "";
            boolean sutException = false;
            if (test.getStatement(i) instanceof MethodStatement) {
                MethodStatement ms = (MethodStatement) test.getStatement(i);
                Method method = ms.getMethod().getMethod();
                methodName = method.getName() + Type.getMethodDescriptor(method);
                if (method.getDeclaringClass().equals(Properties.getTargetClass()))
                    sutException = true;
            } else if (test.getStatement(i) instanceof ConstructorStatement) {
                ConstructorStatement cs = (ConstructorStatement) test.getStatement(i);
                Constructor<?> constructor = cs.getConstructor().getConstructor();
                methodName = "<init>" + Type.getConstructorDescriptor(constructor);
                if (constructor.getDeclaringClass().equals(Properties.getTargetClass()))
                    sutException = true;
            }
            boolean notDeclared = !test.getStatement(i).getDeclaredExceptions().contains(t.getClass());
            if (notDeclared && sutException) {
                /*
					 * we need to distinguish whether it is explicit (ie "throw" in the code, eg for validating
					 * input for pre-condition) or implicit ("likely" a real fault).
					 */
                /*
					 * FIXME: need to find a way to calculate it
					 */
                boolean isExplicit = isExceptionExplicit.get(test).containsKey(i) && isExceptionExplicit.get(test).get(i);
                if (isExplicit) {
                    if (!explicitTypesOfExceptions.containsKey(methodName))
                        explicitTypesOfExceptions.put(methodName, new HashSet<Class<?>>());
                    explicitTypesOfExceptions.get(methodName).add(t.getClass());
                } else {
                    if (!implicitTypesOfExceptions.containsKey(methodName))
                        implicitTypesOfExceptions.put(methodName, new HashSet<Class<?>>());
                    implicitTypesOfExceptions.get(methodName).add(t.getClass());
                }
            }
        }
    }
    int num_covered = 0;
    entry.error_branches = BranchPool.getNumArtificialBranches();
    for (Integer key : predicate_count.keySet()) {
        // logger.info("Key: "+key);
        double df = true_distance.get(key);
        double dt = false_distance.get(key);
        Branch b = BranchPool.getBranch(key);
        if (!b.getClassName().startsWith(Properties.TARGET_CLASS) && !b.getClassName().startsWith(Properties.TARGET_CLASS + '$'))
            continue;
        // if (!b.isInstrumented()) {
        if (df == 0.0)
            num_covered++;
        if (dt == 0.0)
            num_covered++;
        // }
        if (b.isInstrumented()) {
            // entry.error_branches++;
            if (df == 0.0)
                entry.error_branches_covered++;
            if (dt == 0.0)
                entry.error_branches_covered++;
        }
    }
    for (String methodName : CFGMethodAdapter.getMethodsPrefix(Properties.TARGET_CLASS)) {
        boolean allArtificial = true;
        int splitPoint = methodName.lastIndexOf(".");
        String cName = methodName.substring(0, splitPoint);
        String mName = methodName.substring(splitPoint + 1);
        boolean hasBranches = false;
        for (Branch b : BranchPool.retrieveBranchesInMethod(cName, mName)) {
            hasBranches = true;
            if (!b.isInstrumented()) {
                allArtificial = false;
                break;
            }
        }
        if (hasBranches && allArtificial) {
            entry.error_branchless_methods++;
            if (covered_methods.contains(methodName)) {
                entry.error_branchless_methods_covered++;
            }
        }
    }
    int coveredBranchlessMethods = 0;
    for (String branchlessMethod : BranchPool.getBranchlessMethodsMemberClasses(Properties.TARGET_CLASS)) {
        if (covered_methods.contains(branchlessMethod))
            coveredBranchlessMethods++;
    }
    // + covered branchless methods?
    entry.covered_branches = num_covered;
    entry.covered_methods = covered_methods.size();
    entry.covered_branchless_methods = coveredBranchlessMethods;
    // BranchCoverageSuiteFitness f = new BranchCoverageSuiteFitness();
    /*
		if (Properties.CRITERION == Properties.Criterion.DEFUSE
		        || Properties.ANALYSIS_CRITERIA.contains("DefUse")) {
			entry.coveredIntraMethodPairs = DefUseCoverageSuiteFitness.mostCoveredGoals.get(DefUsePairType.INTRA_METHOD);
			entry.coveredInterMethodPairs = DefUseCoverageSuiteFitness.mostCoveredGoals.get(DefUsePairType.INTER_METHOD);
			entry.coveredIntraClassPairs = DefUseCoverageSuiteFitness.mostCoveredGoals.get(DefUsePairType.INTRA_CLASS);
			entry.coveredParameterPairs = DefUseCoverageSuiteFitness.mostCoveredGoals.get(DefUsePairType.PARAMETER);
		}
			*/
    // System.out.println(covered_methods);
    // DONE make this work for other criteria too. this will only work for
    // branch coverage - see searchStarted()/Finished()
    // entry.total_goals = 2 * entry.total_branches +
    // entry.branchless_methods; - moved to searchStarted()
    // entry.covered_goals = num_covered; - moved to searchFinished()
    // for(String e : CFGMethodAdapter.branchless_methods) {
    // for (String e : CFGMethodAdapter.methods) {
    // for (String e : BranchPool.getBranchlessMethods()) {
    // if (covered_methods.contains(e)) {
    // logger.info("Covered method: " + e);
    // entry.covered_goals++;
    // } else {
    // logger.info("Method is not covered: " + e);
    // }
    /*
		 * logger.debug("Covered methods: " + covered_methods.size() + "/" +
		 * entry.total_methods); for (String method : covered_methods) {
		 * logger.debug("Covered method: " + method); }
		 */
    // }
    String s = calculateCoveredBranchesBitString(best);
    entry.goalCoverage = s;
    entry.explicitMethodExceptions = getNumExceptions(explicitTypesOfExceptions);
    entry.explicitTypeExceptions = getNumClassExceptions(explicitTypesOfExceptions);
    entry.implicitMethodExceptions = getNumExceptions(implicitTypesOfExceptions);
    entry.implicitTypeExceptions = getNumClassExceptions(implicitTypesOfExceptions);
    entry.implicitExceptions = implicitTypesOfExceptions;
    entry.explicitExceptions = explicitTypesOfExceptions;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) DefUseCoverageTestFitness(org.evosuite.coverage.dataflow.DefUseCoverageTestFitness) Branch(org.evosuite.coverage.branch.Branch) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet) ConstructorStatement(org.evosuite.testcase.ConstructorStatement) MethodStatement(org.evosuite.testcase.MethodStatement) Constructor(java.lang.reflect.Constructor) ExecutionTrace(org.evosuite.testcase.ExecutionTrace) ExecutionResult(org.evosuite.testcase.ExecutionResult) Method(java.lang.reflect.Method) TestCase(org.evosuite.testcase.TestCase) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with DefUseCoverageTestFitness

use of org.evosuite.coverage.dataflow.DefUseCoverageTestFitness in project evosuite by EvoSuite.

the class AllUsesAnalysis method preAnalyzeMethods.

/**
 * Checks if there are methods in the CCG that dont call any other methods
 * except for maybe itself. For these we can predetermine free uses and
 * activeDefs prior to looking for inter_method_pairs. After that we can
 * even repeat this process for methods we now have determined free uses and
 * activeDefs! that way you can save a lot of computation. Map activeDefs
 * and freeUses according to the variable so you can easily determine which
 * defs will be active and which uses are free once you encounter a
 * methodCall to that method without looking at its part of the CCFG
 */
private Set<DefUseCoverageTestFitness> preAnalyzeMethods() {
    // TODO after preanalyze, order the remaining methods as follows:
    // first order each method by the number of instructions within them in
    // ascending order. after that for each method check if there is a
    // method that calls this one and also has to be analyzed still. if you
    // find such a pair move the calling method in front of the called one
    Set<DefUseCoverageTestFitness> r = new HashSet<DefUseCoverageTestFitness>();
    LinkedList<ClassCallNode> toAnalyze = new LinkedList<ClassCallNode>();
    toAnalyze.addAll(getInitialPreAnalyzeableMethods());
    while (!toAnalyze.isEmpty()) {
        ClassCallNode currentMethod = toAnalyze.poll();
        CCFGMethodEntryNode analyzeableEntry = ccfg.getMethodEntryNodeForClassCallNode(currentMethod);
        if (analyzedMethods.contains(analyzeableEntry))
            continue;
        r.addAll(determineIntraInterMethodPairs(analyzeableEntry));
        // check if we can pre-analyze further methods now
        Set<ClassCallNode> parents = ccfg.getCcg().getParents(currentMethod);
        for (ClassCallNode parent : parents) {
            if (toAnalyze.contains(parent))
                // will be analyzed anyway
                continue;
            if (analyzedMethods.contains(ccfg.getMethodEntryNodeForClassCallNode(parent)))
                // was already analyzed
                continue;
            Set<ClassCallNode> parentsChildren = ccfg.getCcg().getChildren(parent);
            boolean canAnalyzeNow = true;
            for (ClassCallNode parentsChild : parentsChildren) {
                if (parentsChild == null)
                    continue;
                if (!parentsChild.equals(parent) && !(toAnalyze.contains(parentsChild) || analyzedMethods.contains(ccfg.getMethodEntryNodeForClassCallNode(parentsChild)))) {
                    // found child of parent that will not be pre-analyzed
                    canAnalyzeNow = false;
                    break;
                }
            }
            if (canAnalyzeNow) {
                toAnalyze.offer(parent);
            }
        }
    }
    return r;
}
Also used : CCFGFieldClassCallNode(org.evosuite.graphs.ccfg.CCFGFieldClassCallNode) ClassCallNode(org.evosuite.graphs.ccg.ClassCallNode) DefUseCoverageTestFitness(org.evosuite.coverage.dataflow.DefUseCoverageTestFitness) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet) CCFGMethodEntryNode(org.evosuite.graphs.ccfg.CCFGMethodEntryNode)

Example 3 with DefUseCoverageTestFitness

use of org.evosuite.coverage.dataflow.DefUseCoverageTestFitness in project evosuite by EvoSuite.

the class AllUsesAnalysis method handleUseInstruction.

private void handleUseInstruction(CCFGMethodEntryNode investigatedMethod, BytecodeInstruction code, Stack<MethodCall> callStack, Set<Map<String, VariableDefinition>> activeDefMaps, Set<BytecodeInstruction> freeUses, Set<DefUseCoverageTestFitness> foundPairs) {
    String varName = code.getVariableName();
    for (Map<String, VariableDefinition> activeDefs : activeDefMaps) {
        VariableDefinition activeDef = activeDefs.get(varName);
        if (activeDef != null) {
            // we have an intraMethodPair iff use and definition are in
            // the
            // same method and executed during a single invocation of
            // that method
            boolean isIntraPair = activeDef.getMethodCall().equals(callStack.peek());
            DefUseCoverageTestFitness.DefUsePairType type;
            if (isIntraPair)
                type = DefUseCoverageTestFitness.DefUsePairType.INTRA_METHOD;
            else {
                type = DefUseCoverageTestFitness.DefUsePairType.INTER_METHOD;
            }
            if (!activeDef.getDefinition().isLocalDU() || type.equals(DefUsePairType.INTRA_METHOD))
                addNewGoalToFoundPairs(investigatedMethod, activeDef, code, type, foundPairs);
        } else {
            // use has a definition-free path from method start
            if (code.isFieldUse()) {
                freeUses.add(code);
                // code.toString());
                ;
            }
        }
    }
}
Also used : DefUsePairType(org.evosuite.coverage.dataflow.DefUseCoverageTestFitness.DefUsePairType) DefUseCoverageTestFitness(org.evosuite.coverage.dataflow.DefUseCoverageTestFitness)

Example 4 with DefUseCoverageTestFitness

use of org.evosuite.coverage.dataflow.DefUseCoverageTestFitness in project evosuite by EvoSuite.

the class TestSuiteWriter method getInformation.

/**
 * When writing out the JUnit test file, each test can have a text comment
 *
 * @param num Index of test case
 * @return Comment for test case
 */
protected String getInformation(int num) {
    if (testComment.containsKey(num)) {
        String comment = testComment.get(num);
        if (!comment.endsWith("\n"))
            comment = comment + NEWLINE;
        return comment;
    }
    TestCase test = testCases.get(num);
    Set<TestFitnessFunction> coveredGoals = test.getCoveredGoals();
    StringBuilder builder = new StringBuilder();
    builder.append("Test case number: " + num);
    if (!coveredGoals.isEmpty()) {
        builder.append(NEWLINE);
        builder.append("  /*");
        builder.append(NEWLINE);
        builder.append("   * ");
        builder.append(coveredGoals.size() + " covered goal");
        if (coveredGoals.size() != 1)
            builder.append("s");
        builder.append(":");
        int nr = 1;
        for (TestFitnessFunction goal : coveredGoals) {
            builder.append(NEWLINE);
            builder.append("   * Goal " + nr + ". " + goal.toString());
            // TODO only for debugging purposes
            if (ArrayUtil.contains(Properties.CRITERION, Criterion.DEFUSE) && (goal instanceof DefUseCoverageTestFitness)) {
                DefUseCoverageTestFitness duGoal = (DefUseCoverageTestFitness) goal;
                if (duGoal.getCoveringTrace() != null) {
                    String traceInformation = duGoal.getCoveringTrace().toDefUseTraceInformation(duGoal.getGoalVariable(), duGoal.getCoveringObjectId());
                    traceInformation = traceInformation.replaceAll("\n", "");
                    builder.append(NEWLINE);
                    builder.append("     * DUTrace: " + traceInformation);
                }
            }
            nr++;
        }
        builder.append(NEWLINE);
        builder.append("   */");
        builder.append(NEWLINE);
    }
    return builder.toString();
}
Also used : DefUseCoverageTestFitness(org.evosuite.coverage.dataflow.DefUseCoverageTestFitness)

Example 5 with DefUseCoverageTestFitness

use of org.evosuite.coverage.dataflow.DefUseCoverageTestFitness in project evosuite by EvoSuite.

the class AllUsesAnalysis method determineDefUsePairs.

// Definition-Use Pair computation
/**
 * Makes a run of determineInterMethodPairs() for each public method. If you
 * reach a use for which you have no def yet, remember that also remember
 * activeDefs after each run then create intra-class pairs from these uses
 * and defs and during each single run we detect intra and inter method
 * pairs
 *
 * @return a {@link java.util.Set} object.
 */
public Set<DefUseCoverageTestFitness> determineDefUsePairs() {
    // TODO clinit? id say uses dont count, defs do
    Set<DefUseCoverageTestFitness> r = preAnalyzeMethods();
    // create inter-method-pairs
    for (CCFGMethodEntryNode publicMethodEntry : ccfg.publicMethods) {
        if (analyzedMethods.contains(publicMethodEntry)) {
            continue;
        }
        if (publicMethodEntry.getEntryInstruction() == null)
            throw new IllegalStateException("expect each CCFGMethodEntryNode to have its entryInstruction set");
        r.addAll(determineIntraInterMethodPairs(publicMethodEntry));
    }
    // create intra-method pairs
    r.addAll(createIntraClassPairs());
    freeMemory();
    return r;
}
Also used : DefUseCoverageTestFitness(org.evosuite.coverage.dataflow.DefUseCoverageTestFitness) CCFGMethodEntryNode(org.evosuite.graphs.ccfg.CCFGMethodEntryNode)

Aggregations

DefUseCoverageTestFitness (org.evosuite.coverage.dataflow.DefUseCoverageTestFitness)7 HashSet (java.util.HashSet)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CCFGMethodEntryNode (org.evosuite.graphs.ccfg.CCFGMethodEntryNode)2 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 LinkedList (java.util.LinkedList)1 Set (java.util.Set)1 Branch (org.evosuite.coverage.branch.Branch)1 DefUsePairType (org.evosuite.coverage.dataflow.DefUseCoverageTestFitness.DefUsePairType)1 CCFGFieldClassCallNode (org.evosuite.graphs.ccfg.CCFGFieldClassCallNode)1 ClassCallNode (org.evosuite.graphs.ccg.ClassCallNode)1 BytecodeInstruction (org.evosuite.graphs.cfg.BytecodeInstruction)1 ConstructorStatement (org.evosuite.testcase.ConstructorStatement)1 ExecutionResult (org.evosuite.testcase.ExecutionResult)1 ExecutionTrace (org.evosuite.testcase.ExecutionTrace)1 MethodStatement (org.evosuite.testcase.MethodStatement)1 TestCase (org.evosuite.testcase.TestCase)1 TestChromosome (org.evosuite.testcase.TestChromosome)1