use of org.kanonizo.framework.instrumentation.Instrumenter in project kanonizo by kanonizo.
the class CoverageWriter method prepareCsv.
@Override
public void prepareCsv() {
String[] headers = new String[] { "Class", "ClassId", "NumLinesCovered", "NumLinesMissed", "LinesCovered", "LinesMissed", "PercentageLineCoverage", "Total Branches", "BranchesCovered", "BranchesMissed", "PercentageBranchCoverage" };
setHeaders(headers);
List<ClassUnderTest> cuts = system.getClassesUnderTest();
List<TestCase> testCases = system.getTestSuite().getTestCases();
Instrumenter inst = Framework.getInstance().getInstrumenter();
for (ClassUnderTest cut : cuts) {
if (!cut.getCUT().isInterface()) {
Set<Line> linesCovered = new HashSet<>();
Set<Branch> branchesCovered = new HashSet<>();
for (TestCase tc : testCases) {
Set<Line> lines = inst.getLinesCovered(tc).stream().filter(line -> line.getParent().equals(cut)).collect(Collectors.toSet());
Set<Branch> branches = inst.getBranchesCovered(tc);
linesCovered.addAll(lines);
branchesCovered.addAll(branches);
}
int totalLines = inst.getTotalLines(cut);
int totalBranches = inst.getTotalBranches(cut);
Set<Line> linesMissed = cut.getLines().stream().filter(line -> !linesCovered.contains(line)).collect(HashSet::new, HashSet::add, HashSet::addAll);
Set<Branch> branchesMissed = cut.getBranches().stream().filter(branch -> !branchesCovered.contains(branch)).collect(HashSet::new, HashSet::add, HashSet::addAll);
List<Line> orderedLinesCovered = new ArrayList<>(linesCovered);
Collections.sort(orderedLinesCovered);
List<Line> orderedLinesMissed = new ArrayList<Line>(linesMissed);
Collections.sort(orderedLinesMissed);
double percentageCoverage = totalLines > 0 ? (double) linesCovered.size() / totalLines : 0;
double percentageBranch = totalBranches > 0 ? (double) branchesCovered.size() / totalBranches : 0;
String[] csv = new String[] { cut.getCUT().getName(), Integer.toString(cut.getId()), Integer.toString(linesCovered.size()), Integer.toString(linesMissed.size()), linesCovered.size() > 0 ? orderedLinesCovered.stream().map(line -> Integer.toString(line.getLineNumber())).reduce((lineNumber, lineNumber2) -> lineNumber + ":" + lineNumber2).get() : "", linesMissed.size() > 0 ? orderedLinesMissed.stream().map(line -> Integer.toString(line.getLineNumber())).reduce((lineNumber, lineNumber2) -> lineNumber + ":" + lineNumber2).get() : "", Double.toString(percentageCoverage), Integer.toString(totalBranches), Double.toString(branchesCovered.size()), Double.toString(branchesMissed.size()), Double.toString(percentageBranch) };
addRow(csv);
}
}
}
use of org.kanonizo.framework.instrumentation.Instrumenter in project kanonizo by kanonizo.
the class InstrumentedFitnessFunction method instrument.
public void instrument(TestSuite chrom) {
Instrumenter inst = Framework.getInstance().getInstrumenter();
inst.setTestSuite(chrom);
inst.collectCoverage();
calculateTotalGoalsCovered();
}
use of org.kanonizo.framework.instrumentation.Instrumenter in project kanonizo by kanonizo.
the class Main method setInstrumenter.
private static void setInstrumenter(Framework fw, String instrumenter) {
Reflections r = Util.getReflections();
Set<?> instrumenters = r.getTypesAnnotatedWith(org.kanonizo.annotations.Instrumenter.class).stream().map(cl -> {
try {
return cl.newInstance();
} catch (IllegalAccessException | InstantiationException e) {
logger.error(e);
}
return null;
}).collect(Collectors.toSet());
for (Object inst : instrumenters) {
if (instrumenter.equals(((Instrumenter) inst).readableName())) {
fw.setInstrumenter((org.kanonizo.framework.instrumentation.Instrumenter) inst);
}
}
}
use of org.kanonizo.framework.instrumentation.Instrumenter in project kanonizo by kanonizo.
the class Framework method getAvailableInstrumenters.
public static List<Instrumenter> getAvailableInstrumenters() throws InstantiationException, IllegalAccessException {
Reflections r = Util.getReflections();
Set<Class<?>> algorithms = r.getTypesAnnotatedWith(org.kanonizo.annotations.Instrumenter.class);
List<Instrumenter> algorithmsInst = algorithms.stream().map(cl -> {
try {
return (Instrumenter) cl.newInstance();
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
throw new RuntimeException("Could not instantiate one of more instrumenters");
}).collect(Collectors.toList());
return algorithmsInst;
}
use of org.kanonizo.framework.instrumentation.Instrumenter in project kanonizo by kanonizo.
the class TestSuite method toString.
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("\n-------------------------------------------\nMAXIMUM FITNESS: " + String.format("%.4f", getFitness()) + "\n-------------------------------------------\n");
Set<Line> covered = new HashSet<>();
Instrumenter inst = Framework.getInstance().getInstrumenter();
testCases.stream().forEach(tc -> {
Set<Line> branches = inst.getLinesCovered(tc);
covered.addAll(branches);
});
int coveredBranches = covered.size();
int totalBranches = parent.getClassesUnderTest().stream().mapToInt(cut -> inst.getTotalLines(cut)).sum();
sb.append("Line Coverage: " + (double) coveredBranches / (double) totalBranches);
sb.append("\n-------------------------------------------\nMaximum fitness found by " + getFitnessFunction().getClass().getSimpleName() + "\n-------------------------------------------\n");
return sb.toString();
}
Aggregations