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;
}
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));
}
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());
}
}
}
Aggregations