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