Search in sources :

Example 66 with TestFitnessFunction

use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.

the class BranchCoverageSuiteFitness method handleBranchlessMethods.

protected void handleBranchlessMethods(TestChromosome test, ExecutionResult result, Map<String, Integer> callCount) {
    for (Entry<String, Integer> entry : result.getTrace().getMethodExecutionCount().entrySet()) {
        if (entry.getKey() == null || !methods.contains(entry.getKey()) || removedRootBranches.contains(entry.getKey()))
            continue;
        if (!callCount.containsKey(entry.getKey()))
            callCount.put(entry.getKey(), entry.getValue());
        else {
            callCount.put(entry.getKey(), callCount.get(entry.getKey()) + entry.getValue());
        }
        // if this is a target branch or not
        if (branchlessMethodCoverageMap.containsKey(entry.getKey())) {
            TestFitnessFunction goal = branchlessMethodCoverageMap.get(entry.getKey());
            test.getTestCase().addCoveredGoal(goal);
            toRemoveRootBranches.add(entry.getKey());
            if (Properties.TEST_ARCHIVE) {
                Archive.getArchiveInstance().updateArchive(goal, test, 0.0);
            }
        }
    }
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction)

Example 67 with TestFitnessFunction

use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.

the class BranchCoverageSuiteFitness method handleConstructorExceptions.

/**
 * If there is an exception in a superconstructor, then the corresponding
 * constructor might not be included in the execution trace
 *
 * @param results
 * @param callCount
 */
private void handleConstructorExceptions(TestChromosome test, ExecutionResult result, Map<String, Integer> callCount) {
    if (result.hasTimeout() || result.hasTestException() || result.noThrownExceptions()) {
        return;
    }
    Integer exceptionPosition = result.getFirstPositionOfThrownException();
    // TODO: Not sure why that can happen
    if (exceptionPosition >= result.test.size()) {
        return;
    }
    Statement statement = null;
    if (result.test.hasStatement(exceptionPosition)) {
        statement = result.test.getStatement(exceptionPosition);
    }
    if (statement instanceof ConstructorStatement) {
        ConstructorStatement c = (ConstructorStatement) statement;
        String className = c.getConstructor().getName();
        String methodName = "<init>" + Type.getConstructorDescriptor(c.getConstructor().getConstructor());
        String name = className + "." + methodName;
        if (!callCount.containsKey(name)) {
            callCount.put(name, 1);
            if (branchlessMethodCoverageMap.containsKey(name)) {
                TestFitnessFunction goal = branchlessMethodCoverageMap.get(name);
                test.getTestCase().addCoveredGoal(goal);
                toRemoveRootBranches.add(name);
                if (Properties.TEST_ARCHIVE) {
                    Archive.getArchiveInstance().updateArchive(goal, test, 0.0);
                }
            }
        }
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction)

Example 68 with TestFitnessFunction

use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.

the class BranchCoverageSuiteFitness method updateCoveredGoals.

@Override
public boolean updateCoveredGoals() {
    if (!Properties.TEST_ARCHIVE) {
        return false;
    }
    for (String method : toRemoveRootBranches) {
        boolean removed = branchlessMethods.remove(method);
        TestFitnessFunction f = branchlessMethodCoverageMap.remove(method);
        if (removed && f != null) {
            totalMethods--;
            methods.remove(method);
            removedRootBranches.add(method);
        // removeTestCall(f.getTargetClass(), f.getTargetMethod());
        } else {
            throw new IllegalStateException("goal to remove not found");
        }
    }
    for (Integer branch : toRemoveBranchesT) {
        TestFitnessFunction f = branchCoverageTrueMap.remove(branch);
        if (f != null) {
            removedBranchesT.add(branch);
            if (removedBranchesF.contains(branch)) {
                totalBranches--;
            // if(isFullyCovered(f.getTargetClass(), f.getTargetMethod())) {
            // removeTestCall(f.getTargetClass(), f.getTargetMethod());
            // }
            }
        } else {
            throw new IllegalStateException("goal to remove not found");
        }
    }
    for (Integer branch : toRemoveBranchesF) {
        TestFitnessFunction f = branchCoverageFalseMap.remove(branch);
        if (f != null) {
            removedBranchesF.add(branch);
            if (removedBranchesT.contains(branch)) {
                totalBranches--;
            // if(isFullyCovered(f.getTargetClass(), f.getTargetMethod())) {
            // removeTestCall(f.getTargetClass(), f.getTargetMethod());
            // }
            }
        } else {
            throw new IllegalStateException("goal to remove not found");
        }
    }
    toRemoveRootBranches.clear();
    toRemoveBranchesF.clear();
    toRemoveBranchesT.clear();
    logger.info("Current state of archive: " + Archive.getArchiveInstance().toString());
    return true;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction)

Example 69 with TestFitnessFunction

use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.

the class ClassStatisticsPrinter method printClassStatistics.

/**
 * Identify all JUnit tests starting with the given name prefix, instrument
 * and run tests
 */
public static void printClassStatistics() {
    ExecutionTracer.disable();
    ExecutionTracer.setCheckCallerThread(false);
    try {
        DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(ClassPathHandler.getInstance().getClassPathElementsForTargetProject()));
        Sandbox.goingToExecuteSUTCode();
        TestGenerationContext.getInstance().goingToExecuteSUTCode();
        Sandbox.goingToExecuteUnsafeCodeOnSameThread();
        // Load SUT without initialising it
        Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
        if (targetClass != null) {
            LoggingUtils.getEvoLogger().info("* Finished analyzing classpath");
        } else {
            LoggingUtils.getEvoLogger().info("* Error while initializing target class, not continuing");
            return;
        }
        int publicMethods = 0;
        int nonpublicMethods = 0;
        int staticMethods = 0;
        int staticFields = 0;
        for (Method method : targetClass.getDeclaredMethods()) {
            if (method.getName().equals(ClassResetter.STATIC_RESET))
                continue;
            if (Modifier.isPublic(method.getModifiers())) {
                publicMethods++;
            } else {
                nonpublicMethods++;
            }
            if (Modifier.isStatic(method.getModifiers())) {
                LoggingUtils.getEvoLogger().info("Static: " + method);
                staticMethods++;
            }
        }
        for (Constructor<?> constructor : targetClass.getDeclaredConstructors()) {
            if (Modifier.isPublic(constructor.getModifiers())) {
                publicMethods++;
            } else {
                nonpublicMethods++;
            }
        }
        for (Field field : targetClass.getDeclaredFields()) {
            if (Modifier.isStatic(field.getModifiers())) {
                staticFields++;
            }
        }
        LoggingUtils.getEvoLogger().info("* Abstract: " + Modifier.isAbstract(targetClass.getModifiers()));
        LoggingUtils.getEvoLogger().info("* Public methods/constructors: " + publicMethods);
        LoggingUtils.getEvoLogger().info("* Non-Public methods/constructors: " + nonpublicMethods);
        LoggingUtils.getEvoLogger().info("* Static methods: " + staticMethods);
        LoggingUtils.getEvoLogger().info("* Inner classes: " + targetClass.getDeclaredClasses().length);
        LoggingUtils.getEvoLogger().info("* Total fields: " + targetClass.getDeclaredFields().length);
        LoggingUtils.getEvoLogger().info("* Static fields: " + staticFields);
        LoggingUtils.getEvoLogger().info("* Type parameters: " + targetClass.getTypeParameters().length);
    } catch (Throwable e) {
        LoggingUtils.getEvoLogger().error("* Error while initializing target class: " + (e.getMessage() != null ? e.getMessage() : e.toString()));
        return;
    } finally {
        Sandbox.doneWithExecutingUnsafeCodeOnSameThread();
        Sandbox.doneWithExecutingSUTCode();
        TestGenerationContext.getInstance().doneWithExecutingSUTCode();
    }
    LoggingUtils.getEvoLogger().info("* Subclasses: " + (TestCluster.getInheritanceTree().getSubclasses(Properties.TARGET_CLASS).size() - 1));
    LoggingUtils.getEvoLogger().info("* Superclasses/interfaces: " + (TestCluster.getInheritanceTree().getSuperclasses(Properties.TARGET_CLASS).size() - 1));
    LoggingUtils.getEvoLogger().info("* Lines of code: " + LinePool.getNumLines());
    LoggingUtils.getEvoLogger().info("* Methods without branches: " + BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getNumBranchlessMethods());
    LoggingUtils.getEvoLogger().info("* Total branch predicates: " + BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranchCounter());
    double complexity = 0.0;
    int maxComplexity = 0;
    for (Entry<String, RawControlFlowGraph> entry : GraphPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getRawCFGs(Properties.TARGET_CLASS).entrySet()) {
        int c = entry.getValue().getCyclomaticComplexity();
        if (c > maxComplexity)
            maxComplexity = c;
        complexity += c;
    // LoggingUtils.getEvoLogger().info("* Complexity of method "+entry.getKey()+": "+entry.getValue().getCyclomaticComplexity());
    }
    LoggingUtils.getEvoLogger().info("* Average cyclomatic complexity: " + (complexity / CFGMethodAdapter.getNumMethods(TestGenerationContext.getInstance().getClassLoaderForSUT())));
    LoggingUtils.getEvoLogger().info("* Maximum cyclomatic complexity: " + maxComplexity);
    StringBuilder allGoals = new StringBuilder();
    List<TestFitnessFactory<?>> factories = TestGenerationStrategy.getFitnessFactories();
    int numCriterion = 0;
    for (TestFitnessFactory<?> factory : factories) {
        List<TestFitnessFunction> goals = (List<TestFitnessFunction>) factory.getCoverageGoals();
        LoggingUtils.getEvoLogger().info("* Criterion " + Properties.CRITERION[numCriterion++] + ": " + goals.size());
        if (Properties.PRINT_GOALS) {
            if (factory instanceof LineCoverageFactory) {
                Collections.sort(goals, new Comparator<TestFitnessFunction>() {

                    @Override
                    public int compare(TestFitnessFunction l1, TestFitnessFunction l2) {
                        return Integer.compare(((LineCoverageTestFitness) l1).getLine(), ((LineCoverageTestFitness) l2).getLine());
                    }
                });
            }
            for (TestFitnessFunction goal : goals) {
                allGoals.append(goal.toString() + java.lang.System.getProperty("line.separator"));
            }
        }
    }
    if (allGoals.length() > 0 && Properties.PRINT_GOALS) {
        if (Properties.WRITE_ALL_GOALS_FILE) {
            FileIOUtils.writeFile(allGoals.toString(), Properties.ALL_GOALS_FILE);
        } else {
            LoggingUtils.getEvoLogger().info(allGoals.toString());
        }
    }
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) Method(java.lang.reflect.Method) RawControlFlowGraph(org.evosuite.graphs.cfg.RawControlFlowGraph) Field(java.lang.reflect.Field) List(java.util.List) LineCoverageFactory(org.evosuite.coverage.line.LineCoverageFactory) LineCoverageTestFitness(org.evosuite.coverage.line.LineCoverageTestFitness)

Example 70 with TestFitnessFunction

use of org.evosuite.testcase.TestFitnessFunction 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

TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)73 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)26 TestCase (org.evosuite.testcase.TestCase)24 Test (org.junit.Test)22 TestChromosome (org.evosuite.testcase.TestChromosome)21 ArrayList (java.util.ArrayList)20 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)15 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)12 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)11 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)11 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)9 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)9 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)8 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)8 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)8 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)6 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)4 ExceptionCoverageTestFitness (org.evosuite.coverage.exception.ExceptionCoverageTestFitness)4 Properties (org.evosuite.Properties)3