Search in sources :

Example 1 with LineCoverageTestFitness

use of org.evosuite.coverage.line.LineCoverageTestFitness in project evosuite by EvoSuite.

the class RhoAux method getLineGoals.

/**
 * Returns the list of lines goals of the CUT
 *
 * @return
 */
public static List<LineCoverageTestFitness> getLineGoals() {
    List<LineCoverageTestFitness> goals = new ArrayList<LineCoverageTestFitness>();
    for (String className : LinePool.getKnownClasses()) {
        // Only lines in CUT
        if (!isCUTorNot(className)) {
            continue;
        }
        Set<Integer> lines = new LinkedHashSet<Integer>();
        for (String methodName : LinePool.getKnownMethodsFor(className)) {
            lines.addAll(LinePool.getLines(className, methodName));
        }
        for (Integer line : lines) {
            logger.info("Adding line " + line + " for class '" + className + "'");
            // Properties.TARGET_METHOD as to be used instead of methodName, otherwise
            // an CFG exception would be thrown
            goals.add(new LineCoverageTestFitness(className, Properties.TARGET_METHOD, line));
        }
    }
    return goals;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) LineCoverageTestFitness(org.evosuite.coverage.line.LineCoverageTestFitness)

Example 2 with LineCoverageTestFitness

use of org.evosuite.coverage.line.LineCoverageTestFitness in project evosuite by EvoSuite.

the class TestCoverageGoalNameGeneration method testLineCoverageIsExcluded.

@Test
public void testLineCoverageIsExcluded() {
    TestCase test1 = new DefaultTestCase();
    MethodCoverageTestFitness methodGoal = new MethodCoverageTestFitness("FooClass", "toString()");
    test1.addCoveredGoal(methodGoal);
    LineCoverageTestFitness lineGoal1 = new LineCoverageTestFitness("FooClass", "toString()", 0);
    test1.addCoveredGoal(lineGoal1);
    TestCase test2 = new DefaultTestCase();
    test2.addCoveredGoal(methodGoal);
    // Need to add statements to change hashCode
    test2.addStatement(new IntPrimitiveStatement(test2, 0));
    LineCoverageTestFitness lineGoal2 = new LineCoverageTestFitness("FooClass", "toString()", 10);
    test2.addCoveredGoal(lineGoal2);
    List<TestCase> tests = new ArrayList<>();
    tests.add(test1);
    tests.add(test2);
    CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests);
    assertEquals("testToString0", naming.getName(test1));
    assertEquals("testToString1", naming.getName(test2));
}
Also used : TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) CoverageGoalTestNameGenerationStrategy(org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy) ArrayList(java.util.ArrayList) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) LineCoverageTestFitness(org.evosuite.coverage.line.LineCoverageTestFitness) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 3 with LineCoverageTestFitness

use of org.evosuite.coverage.line.LineCoverageTestFitness 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)

Aggregations

LineCoverageTestFitness (org.evosuite.coverage.line.LineCoverageTestFitness)3 ArrayList (java.util.ArrayList)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 LineCoverageFactory (org.evosuite.coverage.line.LineCoverageFactory)1 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)1 RawControlFlowGraph (org.evosuite.graphs.cfg.RawControlFlowGraph)1 CoverageGoalTestNameGenerationStrategy (org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy)1 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)1 TestCase (org.evosuite.testcase.TestCase)1 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)1 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)1 Test (org.junit.Test)1