1 package org.simantics.scl.compiler.module.coverage;
3 import java.util.Collection;
6 import org.simantics.scl.compiler.module.Module;
7 import org.simantics.scl.runtime.profiling.BranchPoint;
9 import gnu.trove.map.hash.THashMap;
11 public class CoverageUtils {
13 public static ModuleCoverage getCoverage(String moduleName, THashMap<String, BranchPoint[]> branchPoints) {
14 THashMap<String, FunctionCoverage> methodCoverages = new THashMap<String, FunctionCoverage>();
15 int totalCodeSize = 0;
16 int coveredCodeSize = 0;
17 int totalFunctionCount = 0;
18 int coveredFunctionCount = 0;
19 for(Map.Entry<String, BranchPoint[]> entry : branchPoints.entrySet()) {
20 int totalFunctionCodeSize = 0;
21 int uncoveredFunctionCodeSize = 0;
22 for(BranchPoint branchPoint : entry.getValue()) {
23 totalFunctionCodeSize += branchPoint.getCodeSize();
24 uncoveredFunctionCodeSize += uncoveredCodeSize(branchPoint);
26 int coveredFunctionCodeSize = totalFunctionCodeSize - uncoveredFunctionCodeSize;
27 String functionName = entry.getKey();
28 methodCoverages.put(functionName,
29 new FunctionCoverage(functionName, totalFunctionCodeSize, coveredFunctionCodeSize));
30 totalCodeSize += totalFunctionCodeSize;
31 coveredCodeSize += coveredFunctionCodeSize;
33 if(coveredFunctionCodeSize > 0)
34 ++coveredFunctionCount;
37 return new ModuleCoverage(moduleName, methodCoverages,
38 totalCodeSize, coveredCodeSize,
39 totalFunctionCount, coveredFunctionCount);
42 public static ModuleCoverage getCoverage(Module module) {
43 THashMap<String, BranchPoint[]> branchPoints = module.getBranchPoints();
44 if(branchPoints == null)
47 return getCoverage(module.getName(), branchPoints);
50 public static CombinedCoverage combineCoverages(THashMap<String,ModuleCoverage> moduleCoverages) {
51 int totalCodeSize = 0;
52 int coveredCodeSize = 0;
53 int totalFunctionCount = 0;
54 int coveredFunctionCount = 0;
55 for(ModuleCoverage mCov : moduleCoverages.values()) {
56 totalCodeSize += mCov.getTotalCodeSize();
57 coveredCodeSize += mCov.getCoveredCodeSize();
58 totalFunctionCount += mCov.totalFunctionCount;
59 coveredFunctionCount += mCov.coveredFunctionCount;
61 return new CombinedCoverage(moduleCoverages,
62 totalCodeSize, coveredCodeSize,
63 totalFunctionCount, coveredFunctionCount);
66 public static CombinedCoverage getCoverage(Collection<Module> modules) {
67 THashMap<String,ModuleCoverage> moduleCoverages =
68 new THashMap<String,ModuleCoverage>();
69 for(Module module : modules) {
70 ModuleCoverage coverage = getCoverage(module);
72 moduleCoverages.put(module.getName(), coverage);
74 return combineCoverages(moduleCoverages);
77 public static void resetCoverage(Collection<Module> modules) {
78 modules.forEach(module -> resetCoverage(module));
81 public static void resetCoverage(Module module) {
82 THashMap<String, BranchPoint[]> branches = module.getBranchPoints();
83 if (branches != null) {
84 for (BranchPoint[] points : branches.values()) {
86 for (BranchPoint point : points) {
87 point.resetVisitCountersRecursively();
94 static double safeDiv(int a, int b) {
98 return ((double)a) / b;
101 private static int uncoveredCodeSize(BranchPoint branchPoint) {
102 if(branchPoint.getVisitCounter() == 0)
103 return branchPoint.getCodeSize();
106 for(BranchPoint child : branchPoint.getChildren())
107 sum += uncoveredCodeSize(child);