]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/coverage/CoverageUtils.java
Enhancements to modelled STS-tests
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / module / coverage / CoverageUtils.java
index 59eb2abea2596d17bf0472121da80a59e3d8170d..a9e08af884cc3df303f6040f436b5bbd7662199a 100644 (file)
-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.getCodeSize();\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.getVisitCounter() == 0)\r
-            return branchPoint.getCodeSize();\r
-        else {\r
-            int sum = 0;\r
-            for(BranchPoint child : branchPoint.getChildren())\r
-                sum += uncoveredCodeSize(child);\r
-            return sum;\r
-        }\r
-    }\r
-}\r
+package org.simantics.scl.compiler.module.coverage;
+
+import java.util.Collection;
+import java.util.Map;
+
+import org.simantics.scl.compiler.module.Module;
+import org.simantics.scl.runtime.profiling.BranchPoint;
+
+import gnu.trove.map.hash.THashMap;
+
+public class CoverageUtils {
+
+    public static ModuleCoverage getCoverage(String moduleName, THashMap<String, BranchPoint[]> branchPoints) {
+        THashMap<String, FunctionCoverage> methodCoverages = new THashMap<String, FunctionCoverage>();
+        int totalCodeSize = 0;
+        int coveredCodeSize = 0;
+        int totalFunctionCount = 0;
+        int coveredFunctionCount = 0;
+        for(Map.Entry<String, BranchPoint[]> entry : branchPoints.entrySet()) {
+            int totalFunctionCodeSize = 0;
+            int uncoveredFunctionCodeSize = 0;
+            for(BranchPoint branchPoint : entry.getValue()) {
+                totalFunctionCodeSize += branchPoint.getCodeSize();
+                uncoveredFunctionCodeSize += uncoveredCodeSize(branchPoint);
+            }
+            int coveredFunctionCodeSize = totalFunctionCodeSize - uncoveredFunctionCodeSize;
+            String functionName = entry.getKey();
+            methodCoverages.put(functionName,
+                    new FunctionCoverage(functionName, totalFunctionCodeSize, coveredFunctionCodeSize));
+            totalCodeSize += totalFunctionCodeSize;
+            coveredCodeSize += coveredFunctionCodeSize;
+            ++totalFunctionCount;
+            if(coveredFunctionCodeSize > 0)
+                ++coveredFunctionCount;
+        }
+        
+        return new ModuleCoverage(moduleName, methodCoverages,
+                totalCodeSize, coveredCodeSize,
+                totalFunctionCount, coveredFunctionCount);
+    }
+    
+    public static ModuleCoverage getCoverage(Module module) {
+        THashMap<String, BranchPoint[]> branchPoints = module.getBranchPoints();
+        if(branchPoints == null)
+            return null;
+        else
+            return getCoverage(module.getName(), branchPoints);
+    }
+    
+    public static CombinedCoverage combineCoverages(THashMap<String,ModuleCoverage> moduleCoverages) {
+        int totalCodeSize = 0;
+        int coveredCodeSize = 0;
+        int totalFunctionCount = 0;
+        int coveredFunctionCount = 0;
+        for(ModuleCoverage mCov : moduleCoverages.values()) {
+            totalCodeSize += mCov.getTotalCodeSize();
+            coveredCodeSize += mCov.getCoveredCodeSize();
+            totalFunctionCount += mCov.totalFunctionCount;
+            coveredFunctionCount += mCov.coveredFunctionCount;
+        }
+        return new CombinedCoverage(moduleCoverages,
+                totalCodeSize, coveredCodeSize,
+                totalFunctionCount, coveredFunctionCount);
+    }
+    
+    public static CombinedCoverage getCoverage(Collection<Module> modules) {
+        THashMap<String,ModuleCoverage> moduleCoverages =
+                new THashMap<String,ModuleCoverage>();
+        for(Module module : modules) {
+            ModuleCoverage coverage = getCoverage(module);
+            if(coverage != null)
+                moduleCoverages.put(module.getName(), coverage);
+        }
+        return combineCoverages(moduleCoverages);
+    }
+
+    public static void resetCoverage(Collection<Module> modules) {
+        modules.forEach(CoverageUtils::resetCoverage);
+    }
+
+    public static void resetCoverage(Module module) {
+        THashMap<String, BranchPoint[]> branches = module.getBranchPoints();
+        if (branches != null) {
+            for (BranchPoint[] points : branches.values()) {
+                if (points != null) {
+                    for (BranchPoint point : points) {
+                        point.resetVisitCountersRecursively();
+                    }
+                }
+            }
+        }
+    }
+    
+    static double safeDiv(int a, int b) {
+        if(b == 0)
+            return 1.0;
+        else
+            return ((double)a) / b;
+    }
+
+    private static int uncoveredCodeSize(BranchPoint branchPoint) {
+        if(branchPoint.getVisitCounter() == 0)
+            return branchPoint.getCodeSize();
+        else {
+            int sum = 0;
+            for(BranchPoint child : branchPoint.getChildren())
+                sum += uncoveredCodeSize(child);
+            return sum;
+        }
+    }
+}