]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/coverage/CoverageUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / module / coverage / CoverageUtils.java
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/coverage/CoverageUtils.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/coverage/CoverageUtils.java
new file mode 100644 (file)
index 0000000..a70651c
--- /dev/null
@@ -0,0 +1,111 @@
+package org.simantics.scl.compiler.module.coverage;\r
+\r
+import java.util.Collection;\r
+import java.util.Map;\r
+\r
+import org.simantics.scl.compiler.module.Module;\r
+import org.simantics.scl.runtime.profiling.BranchPoint;\r
+\r
+import gnu.trove.map.hash.THashMap;\r
+\r
+public class CoverageUtils {\r
+\r
+    public static ModuleCoverage getCoverage(String moduleName, THashMap<String, BranchPoint[]> branchPoints) {\r
+        THashMap<String, FunctionCoverage> methodCoverages = new THashMap<String, FunctionCoverage>();\r
+        int totalCodeSize = 0;\r
+        int coveredCodeSize = 0;\r
+        int totalFunctionCount = 0;\r
+        int coveredFunctionCount = 0;\r
+        for(Map.Entry<String, BranchPoint[]> entry : branchPoints.entrySet()) {\r
+            int totalFunctionCodeSize = 0;\r
+            int uncoveredFunctionCodeSize = 0;\r
+            for(BranchPoint branchPoint : entry.getValue()) {\r
+                totalFunctionCodeSize += branchPoint.codeSize;\r
+                uncoveredFunctionCodeSize += uncoveredCodeSize(branchPoint);\r
+            }\r
+            int coveredFunctionCodeSize = totalFunctionCodeSize - uncoveredFunctionCodeSize;\r
+            String functionName = entry.getKey();\r
+            methodCoverages.put(functionName,\r
+                    new FunctionCoverage(functionName, totalFunctionCodeSize, coveredFunctionCodeSize));\r
+            totalCodeSize += totalFunctionCodeSize;\r
+            coveredCodeSize += coveredFunctionCodeSize;\r
+            ++totalFunctionCount;\r
+            if(coveredFunctionCodeSize > 0)\r
+                ++coveredFunctionCount;\r
+        }\r
+        \r
+        return new ModuleCoverage(moduleName, methodCoverages,\r
+                totalCodeSize, coveredCodeSize,\r
+                totalFunctionCount, coveredFunctionCount);\r
+    }\r
+    \r
+    public static ModuleCoverage getCoverage(Module module) {\r
+        THashMap<String, BranchPoint[]> branchPoints = module.getBranchPoints();\r
+        if(branchPoints == null)\r
+            return null;\r
+        else\r
+            return getCoverage(module.getName(), branchPoints);\r
+    }\r
+    \r
+    public static CombinedCoverage combineCoverages(THashMap<String,ModuleCoverage> moduleCoverages) {\r
+        int totalCodeSize = 0;\r
+        int coveredCodeSize = 0;\r
+        int totalFunctionCount = 0;\r
+        int coveredFunctionCount = 0;\r
+        for(ModuleCoverage mCov : moduleCoverages.values()) {\r
+            totalCodeSize += mCov.getTotalCodeSize();\r
+            coveredCodeSize += mCov.getCoveredCodeSize();\r
+            totalFunctionCount += mCov.totalFunctionCount;\r
+            coveredFunctionCount += mCov.coveredFunctionCount;\r
+        }\r
+        return new CombinedCoverage(moduleCoverages,\r
+                totalCodeSize, coveredCodeSize,\r
+                totalFunctionCount, coveredFunctionCount);\r
+    }\r
+    \r
+    public static CombinedCoverage getCoverage(Collection<Module> modules) {\r
+        THashMap<String,ModuleCoverage> moduleCoverages =\r
+                new THashMap<String,ModuleCoverage>();\r
+        for(Module module : modules) {\r
+            ModuleCoverage coverage = getCoverage(module);\r
+            if(coverage != null)\r
+                moduleCoverages.put(module.getName(), coverage);\r
+        }\r
+        return combineCoverages(moduleCoverages);\r
+    }\r
+\r
+    public static void resetCoverage(Collection<Module> modules) {\r
+        modules.forEach(module -> resetCoverage(module));\r
+    }\r
+\r
+    public static void resetCoverage(Module module) {\r
+        THashMap<String, BranchPoint[]> branches = module.getBranchPoints();\r
+        if (branches != null) {\r
+            for (BranchPoint[] points : branches.values()) {\r
+                if (points != null) {\r
+                    for (BranchPoint point : points) {\r
+                        point.resetVisitCountersRecursively();\r
+                    }\r
+                }\r
+            }\r
+        }\r
+    }\r
+    \r
+    static double safeDiv(int a, int b) {\r
+        if(b == 0)\r
+            return 1.0;\r
+        else\r
+            return ((double)a) / b;\r
+    }\r
+\r
+    private static int uncoveredCodeSize(BranchPoint branchPoint) {\r
+        if(branchPoint.visitCounter == 0)\r
+            return branchPoint.codeSize;\r
+        else {\r
+            int sum = 0;\r
+            for(BranchPoint child : branchPoint.children)\r
+                sum += uncoveredCodeSize(child);\r
+            return sum;\r
+        }\r
+    }\r
+}\r