Search in sources :

Example 1 with Branch

use of org.evosuite.coverage.branch.Branch 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 Branch

use of org.evosuite.coverage.branch.Branch in project evosuite by EvoSuite.

the class TryCatchCoverageFactory method getCoverageGoals.

/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.evosuite.coverage.TestCoverageFactory#getCoverageGoals()
	 */
/**
 * {@inheritDoc}
 */
@Override
public List<TryCatchCoverageTestFitness> getCoverageGoals() {
    List<TryCatchCoverageTestFitness> goals = new ArrayList<>();
    // logger.info("Getting branches");
    for (String className : BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).knownClasses()) {
        final MethodNameMatcher matcher = new MethodNameMatcher();
        // Branches
        for (String methodName : BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).knownMethods(className)) {
            if (!matcher.methodMatches(methodName)) {
                logger.info("Method " + methodName + " does not match criteria. ");
                continue;
            }
            for (Branch b : BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).retrieveBranchesInMethod(className, methodName)) {
                if (b.isInstrumented()) {
                    goals.add(new TryCatchCoverageTestFitness(new BranchCoverageGoal(b, true, b.getClassName(), b.getMethodName())));
                    goals.add(new TryCatchCoverageTestFitness(new BranchCoverageGoal(b, false, b.getClassName(), b.getMethodName())));
                }
            }
        }
    }
    return goals;
}
Also used : MethodNameMatcher(org.evosuite.coverage.MethodNameMatcher) Branch(org.evosuite.coverage.branch.Branch) ArrayList(java.util.ArrayList) BranchCoverageGoal(org.evosuite.coverage.branch.BranchCoverageGoal)

Example 3 with Branch

use of org.evosuite.coverage.branch.Branch in project evosuite by EvoSuite.

the class BooleanTestabilityTransformation method getBranchID.

private int getBranchID(MethodNode mn, JumpInsnNode jumpNode) {
    assert (mn.instructions.contains(jumpNode));
    BytecodeInstruction insn = getBytecodeInstruction(mn, jumpNode);
    logger.info("Found instruction: " + insn);
    Branch branch = BranchPool.getInstance(classLoader).getBranchForInstruction(insn);
    return branch.getActualBranchId();
}
Also used : Branch(org.evosuite.coverage.branch.Branch) BytecodeInstruction(org.evosuite.graphs.cfg.BytecodeInstruction)

Example 4 with Branch

use of org.evosuite.coverage.branch.Branch in project evosuite by EvoSuite.

the class BranchInstrumentation method analyze.

/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.evosuite.cfg.MethodInstrumentation#analyze(org.objectweb
	 * .asm.tree.MethodNode, org.jgrapht.Graph, java.lang.String,
	 * java.lang.String, int)
	 */
/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public void analyze(ClassLoader classLoader, MethodNode mn, String className, String methodName, int access) {
    this.classLoader = classLoader;
    RawControlFlowGraph graph = GraphPool.getInstance(classLoader).getRawCFG(className, methodName);
    Iterator<AbstractInsnNode> j = mn.instructions.iterator();
    while (j.hasNext()) {
        AbstractInsnNode in = j.next();
        for (BytecodeInstruction v : graph.vertexSet()) {
            // If this is in the CFG and it's a branch...
            if (in.equals(v.getASMNode())) {
                if (v.isBranch()) {
                    if (in.getPrevious() instanceof LabelNode) {
                        LabelNode label = (LabelNode) in.getPrevious();
                        if (label.getLabel() instanceof AnnotatedLabel) {
                            AnnotatedLabel aLabel = (AnnotatedLabel) label.getLabel();
                            if (aLabel.isStartTag()) {
                                if (!aLabel.shouldIgnore()) {
                                    logger.debug("Found artificial branch: " + v);
                                    Branch b = BranchPool.getInstance(classLoader).getBranchForInstruction(v);
                                    b.setInstrumented(true);
                                } else {
                                    continue;
                                }
                            }
                        }
                    }
                    mn.instructions.insertBefore(v.getASMNode(), getInstrumentation(v));
                } else if (v.isSwitch()) {
                    mn.instructions.insertBefore(v.getASMNode(), getSwitchInstrumentation(v, mn, className, methodName));
                }
            }
        }
    }
    mn.maxStack += 4;
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) AnnotatedLabel(org.evosuite.runtime.instrumentation.AnnotatedLabel) Branch(org.evosuite.coverage.branch.Branch) BytecodeInstruction(org.evosuite.graphs.cfg.BytecodeInstruction) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) RawControlFlowGraph(org.evosuite.graphs.cfg.RawControlFlowGraph)

Example 5 with Branch

use of org.evosuite.coverage.branch.Branch in project evosuite by EvoSuite.

the class ControlDependenceGraph method getAlternativeBlocks.

/**
 * <p>getAlternativeBlocks</p>
 *
 * @param dependency a {@link org.evosuite.graphs.cfg.ControlDependency} object.
 * @return a {@link java.util.Set} object.
 */
public Set<BasicBlock> getAlternativeBlocks(ControlDependency dependency) {
    Set<BasicBlock> blocks = new LinkedHashSet<>();
    Branch branch = dependency.getBranch();
    BasicBlock block = branch.getInstruction().getBasicBlock();
    for (ControlFlowEdge e : outgoingEdgesOf(block)) {
        // TODO: Why can this be null?
        if (e.getControlDependency() == null || e.getControlDependency().equals(dependency))
            continue;
        BasicBlock next = getEdgeTarget(e);
        blocks.add(next);
        getReachableBasicBlocks(blocks, next);
    // blocks.addAll(getReachableBasicBlocks(next));
    }
    return blocks;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ControlFlowEdge(org.evosuite.graphs.cfg.ControlFlowEdge) Branch(org.evosuite.coverage.branch.Branch) BasicBlock(org.evosuite.graphs.cfg.BasicBlock)

Aggregations

Branch (org.evosuite.coverage.branch.Branch)19 ArrayList (java.util.ArrayList)4 BytecodeInstruction (org.evosuite.graphs.cfg.BytecodeInstruction)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 BranchCoverageGoal (org.evosuite.coverage.branch.BranchCoverageGoal)2 Mutation (org.evosuite.coverage.mutation.Mutation)2 ControlFlowEdge (org.evosuite.graphs.cfg.ControlFlowEdge)2 TestCase (org.evosuite.testcase.TestCase)2 TestChromosome (org.evosuite.testcase.TestChromosome)2 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)2 LabelNode (org.objectweb.asm.tree.LabelNode)2 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 Set (java.util.Set)1 Assertion (org.evosuite.assertion.Assertion)1 ContractViolation (org.evosuite.contracts.ContractViolation)1 MethodNameMatcher (org.evosuite.coverage.MethodNameMatcher)1