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