1 package org.simantics.scl.compiler.module.coverage;
\r
3 import org.simantics.scl.compiler.module.Module;
\r
4 import org.simantics.scl.runtime.profiling.BranchPoint;
\r
6 import gnu.trove.map.hash.THashMap;
\r
7 import gnu.trove.procedure.TObjectObjectProcedure;
\r
9 public class CoverageBuilder {
\r
10 THashMap<String, THashMap<String, BranchPoint[]>> combined =
\r
11 new THashMap<String, THashMap<String, BranchPoint[]>>();
\r
13 public void addCoverage(Module module) {
\r
14 THashMap<String, BranchPoint[]> branchPointMap = module.getBranchPoints();
\r
15 if(branchPointMap == null)
\r
17 THashMap<String, BranchPoint[]> oldBranchPointMap = combined.get(module.getName());
\r
18 if(oldBranchPointMap == null) {
\r
19 oldBranchPointMap = new THashMap<String, BranchPoint[]>();
\r
20 combined.put(module.getName(), oldBranchPointMap);
\r
22 THashMap<String, BranchPoint[]> oldBranchPointMap_ = oldBranchPointMap;
\r
23 branchPointMap.forEachEntry(new TObjectObjectProcedure<String, BranchPoint[]>() {
\r
25 public boolean execute(String name, BranchPoint[] branchPoints) {
\r
26 BranchPoint[] oldBranchPoints = oldBranchPointMap_.get(name);
\r
27 if(oldBranchPoints == null)
\r
28 oldBranchPointMap_.put(name, branchPoints);
\r
30 combineCounters(oldBranchPoints, branchPoints);
\r
36 private static void combineCounters(BranchPoint[] oldBranchPoints, BranchPoint[] branchPoints) {
\r
37 if(oldBranchPoints.length != branchPoints.length)
\r
38 throw new IllegalArgumentException("Incompatible branch points.");
\r
39 for(int i=0;i<branchPoints.length;++i) {
\r
40 BranchPoint oldBP = oldBranchPoints[i];
\r
41 BranchPoint newBP = branchPoints[i];
\r
42 if(oldBP.location != newBP.location)
\r
43 throw new IllegalArgumentException("Incompatible branch points.");
\r
44 oldBP.visitCounter += newBP.visitCounter;
\r
45 combineCounters(oldBP.children, newBP.children);
\r
49 public CombinedCoverage getCoverage() {
\r
50 THashMap<String,ModuleCoverage> moduleCoverages =
\r
51 new THashMap<String,ModuleCoverage>();
\r
52 combined.forEachEntry(new TObjectObjectProcedure<String, THashMap<String, BranchPoint[]>>() {
\r
54 public boolean execute(String name, THashMap<String, BranchPoint[]> branchPoints) {
\r
55 moduleCoverages.put(name, CoverageUtils.getCoverage(name, branchPoints));
\r
59 return CoverageUtils.combineCoverages(moduleCoverages);
\r