]> gerrit.simantics Code Review - simantics/platform.git/blob
269e8f6af9f9e49692951110d9484acf5b290f2f
[simantics/platform.git] /
1 package org.simantics.scl.compiler.module.coverage;
2
3 import java.util.Collection;
4 import java.util.Map;
5
6 import org.simantics.scl.compiler.module.Module;
7 import org.simantics.scl.runtime.profiling.BranchPoint;
8
9 import gnu.trove.map.hash.THashMap;
10
11 public class CoverageUtils {
12
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);
25             }
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;
32             ++totalFunctionCount;
33             if(coveredFunctionCodeSize > 0)
34                 ++coveredFunctionCount;
35         }
36         
37         return new ModuleCoverage(moduleName, methodCoverages,
38                 totalCodeSize, coveredCodeSize,
39                 totalFunctionCount, coveredFunctionCount);
40     }
41     
42     public static ModuleCoverage getCoverage(Module module) {
43         THashMap<String, BranchPoint[]> branchPoints = module.getBranchPoints();
44         if(branchPoints == null)
45             return null;
46         else
47             return getCoverage(module.getName(), branchPoints);
48     }
49     
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;
60         }
61         return new CombinedCoverage(moduleCoverages,
62                 totalCodeSize, coveredCodeSize,
63                 totalFunctionCount, coveredFunctionCount);
64     }
65     
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);
71             if(coverage != null)
72                 moduleCoverages.put(module.getName(), coverage);
73         }
74         return combineCoverages(moduleCoverages);
75     }
76
77     public static void resetCoverage(Collection<Module> modules) {
78         modules.forEach(module -> resetCoverage(module));
79     }
80
81     public static void resetCoverage(Module module) {
82         THashMap<String, BranchPoint[]> branches = module.getBranchPoints();
83         if (branches != null) {
84             for (BranchPoint[] points : branches.values()) {
85                 if (points != null) {
86                     for (BranchPoint point : points) {
87                         point.resetVisitCountersRecursively();
88                     }
89                 }
90             }
91         }
92     }
93     
94     static double safeDiv(int a, int b) {
95         if(b == 0)
96             return 1.0;
97         else
98             return ((double)a) / b;
99     }
100
101     private static int uncoveredCodeSize(BranchPoint branchPoint) {
102         if(branchPoint.getVisitCounter() == 0)
103             return branchPoint.getCodeSize();
104         else {
105             int sum = 0;
106             for(BranchPoint child : branchPoint.getChildren())
107                 sum += uncoveredCodeSize(child);
108             return sum;
109         }
110     }
111 }