Package: CoverageUtil

CoverageUtil

nameinstructionbranchcomplexitylinemethod
CoverageUtil(List, Map)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getHitList(File)
M: 42 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getHitLocations(File)
M: 63 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
getHitPercent(File)
M: 57 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
getMissList(File)
M: 42 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getMissLocations(File)
M: 63 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
getSpanCalls(ILexNameToken)
M: 50 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
getSpanNames(File)
M: 34 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getSpanPercent(ILexNameToken)
M: 72 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
lambda$removeDuplicates$0(Vector, Vector)
M: 70 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
lambda$removeDuplicates$1(ILexLocation)
M: 21 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
lambda$removeDuplicates$2(ILexLocation)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
removeDuplicates(List)
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package org.overture.ast.lex;
2:
3: import java.io.File;
4: import java.util.ArrayList;
5: import java.util.Arrays;
6: import java.util.HashMap;
7: import java.util.List;
8: import java.util.Map;
9: import java.util.Vector;
10: import java.util.function.BinaryOperator;
11: import java.util.stream.Collectors;
12:
13: import org.overture.ast.intf.lex.ILexLocation;
14: import org.overture.ast.intf.lex.ILexNameToken;
15:
16:
17: public class CoverageUtil {
18:         
19:         /** A collection of all ILexLocation objects. */
20:         private final List<ILexLocation> allLocations;
21:         
22:         /** A map of class names to their lexical span, for coverage. */
23:         private final Map<LexNameToken, ILexLocation> nameSpans;
24:
25:
26:         public CoverageUtil(List<ILexLocation> allLocations , Map<LexNameToken, ILexLocation> nameSpans) {
27:                 this.allLocations = removeDuplicates(allLocations);
28:                 this.nameSpans = nameSpans;
29:         }
30:
31:         
32:         // TODO: Comment
33:         public LexNameList getSpanNames(File filename)
34:         {
35:                 LexNameList list = new LexNameList();
36:
37:•                for (LexNameToken name : nameSpans.keySet())
38:                 {
39:                         ILexLocation span = nameSpans.get(name);
40:
41:•                        if (span.getFile().equals(filename))
42:                         {
43:                                 list.add(name);
44:                         }
45:                 }
46:
47:                 return list;
48:         }
49:
50:         // TODO: Comment
51:         public float getSpanPercent(ILexNameToken name)
52:         {
53:                 int hits = 0;
54:                 int misses = 0;
55:                 ILexLocation span = null;
56:
57:                 synchronized (nameSpans)
58:                 {
59:                         span = nameSpans.get(name);
60:                 }
61:
62:                 synchronized (allLocations)
63:                 {
64:•                        for (ILexLocation l : allLocations)
65:                         {
66:•                                if (l.getExecutable() && l.within(span))
67:                                 {
68:•                                        if (l.getHits() > 0)
69:                                         {
70:                                                 hits++;
71:                                         } else
72:                                         {
73:                                                 misses++;
74:                                         }
75:                                 }
76:                         }
77:                 }
78:
79:                 int sum = hits + misses;
80:•                return sum == 0 ? 0 : (float) (1000 * hits / sum) / 10; // NN.N%
81:         }
82:
83:         // TODO: Comment         
84:         public long getSpanCalls(ILexNameToken name)
85:         {
86:                 // The assumption is that the first executable location in
87:                 // the span for the name is hit as many time as the span is called.
88:
89:                 ILexLocation span = null;
90:
91:                 synchronized (nameSpans)
92:                 {
93:                         span = nameSpans.get(name);
94:                 }
95:
96:                 synchronized (allLocations)
97:                 {
98:•                        for (ILexLocation l : allLocations)
99:                         {
100:•                                if (l.getExecutable() && l.within(span))
101:                                 {
102:                                         return l.getHits();
103:                                 }
104:                         }
105:                 }
106:
107:                 return 0;
108:         }
109:
110:         // TODO: Comment
111:         public List<Integer> getHitList(File file)
112:         {
113:                 //FIXME skip lex location in other files
114:                 // idea: if !lextLocation.getFile().equals(file) then continue;
115:                 List<Integer> hits = new Vector<Integer>();
116:
117:                 synchronized (allLocations)
118:                 {
119:•                        for (ILexLocation l : allLocations)
120:                         {
121:•                                if (l.getHits() > 0 && l.getFile().equals(file))
122:                                 {
123:                                         hits.add(l.getStartLine());
124:                                 }
125:                         }
126:                 }
127:
128:                 return hits;
129:         }
130:
131:         // TODO: Comment
132:         public List<Integer> getMissList(File file)
133:         {
134:                 List<Integer> misses = new Vector<Integer>();
135:
136:                 synchronized (allLocations)
137:                 {
138:•                        for (ILexLocation l : allLocations)
139:                         {
140:•                                if (l.getHits() == 0 && l.getFile().equals(file))
141:                                 {
142:                                         misses.add(l.getStartLine());
143:                                 }
144:                         }
145:                 }
146:
147:                 return misses;
148:         }
149:         
150:         // TODO: Comment
151:         public Map<Integer, List<ILexLocation>> getHitLocations(File file)
152:         {
153:                 Map<Integer, List<ILexLocation>> map = new HashMap<Integer, List<ILexLocation>>();
154:
155:                 synchronized (allLocations)
156:                 {
157:•                        for (ILexLocation l : allLocations)
158:                         {
159:•                                if (l.getExecutable() && l.getHits() > 0 && l.getFile().equals(file))
160:                                 {
161:                                         List<ILexLocation> list = map.get(l.getStartLine());
162:
163:•                                        if (list == null)
164:                                         {
165:                                                 list = new Vector<ILexLocation>();
166:                                                 map.put(l.getStartLine(), list);
167:                                         }
168:
169:                                         list.add(l);
170:                                 }
171:                         }
172:                 }
173:
174:                 return map;
175:         }
176:
177:         // TODO: Comment
178:         public float getHitPercent(File file)
179:         {
180:                 int hits = 0;
181:                 int misses = 0;
182:
183:                 synchronized (allLocations)
184:                 {
185:•                        for (ILexLocation l : allLocations)
186:                         {
187:•                                if (l.getFile().equals(file) && l.getExecutable())
188:                                 {
189:•                                        if (l.getHits() > 0)
190:                                         {
191:                                                 hits++;
192:                                         } else
193:                                         {
194:                                                 misses++;
195:                                         }
196:                                 }
197:                         }
198:                 }
199:
200:                 int sum = hits + misses;
201:•                return sum == 0 ? 0 : (float) (1000 * hits / sum) / 10; // NN.N%
202:         }
203:
204:         // TODO: Comment
205:         public Map<Integer, List<ILexLocation>> getMissLocations(File file)
206:         {
207:                 Map<Integer, List<ILexLocation>> map = new HashMap<Integer, List<ILexLocation>>();
208:
209:                 synchronized (allLocations)
210:                 {
211:•                        for (ILexLocation l : allLocations)
212:                         {
213:•                                if (l.getExecutable() && l.getHits() == 0 && l.getFile().equals(file))
214:                                 {
215:                                         List<ILexLocation> list = map.get(l.getStartLine());
216:
217:•                                        if (list == null)
218:                                         {
219:                                                 list = new Vector<ILexLocation>();
220:                                                 map.put(l.getStartLine(), list);
221:                                         }
222:
223:                                         list.add(l);
224:                                 }
225:                         }
226:                 }
227:
228:                 return map;
229:         }
230:         
231:         /**
232:          * This method handles the case where a location exist both with hits>0 and hits==0. It will remove the location
233:          * with hits==0 when another location hits>0 exists.
234:          *
235:          *
236:          * The strategy is to build a map with the elements of ILexLocation that are used when test equality as keys
237:          * and the list of objects on the equivalence class as values.
238:          *
239:          * In the merging of the map (clash of keys) we deal with duplicates
240:          *
241:          * @param locations
242:          * @return the list of locations where the unwanted locations have been removed
243:          */
244:         private static List<ILexLocation> removeDuplicates(List<ILexLocation> locations)
245:         {
246:                 
247:                 // Map merging function
248:          BinaryOperator<Vector<ILexLocation>> merge = (old, latest)-> {
249:                  
250:•                 for (ILexLocation location : latest)
251:                         {
252:•                                if (location.getHits() > 0)
253:                                 {
254:                                         // Remove objects with 0 hits in the equivalence class
255:•                                        for(ILexLocation loc : old)
256:•                                                if(loc.getHits() == 0) old.remove(loc);
257:                                 
258:                                         old.add(location);
259:                                 }
260:                                 else
261:                                 {
262:                                         // According to previous function if all are 0 hits that is OK
263:                                         // TODO: Check if that is expected!
264:                                         boolean allZeroHits = true;
265:                                         
266:•                                        for(ILexLocation loc : old)
267:•                                                if(loc.getHits() > 0)
268:                                                 {
269:                                                         allZeroHits = false;
270:                                                         break;
271:                                                 }
272:                                                 
273:•                                        if(allZeroHits) old.add(location);        
274:                                 }
275:                         }
276:                         
277:          return old;
278:          };
279:         
280:          // Remove duplicate hits
281:                 Map<String, Vector<ILexLocation>> map = locations.stream()
282:                                 .collect(Collectors.toMap(loc -> ((ILexLocation)loc).getFile() +
283:                  ((ILexLocation)loc).getModule() +
284:                  "l" +
285:                  ((ILexLocation)loc).getStartLine() +
286:                  "p" +
287:                  ((ILexLocation)loc).getStartPos(),
288:                  loc -> new Vector<ILexLocation>(Arrays.asList(loc)),
289:                  merge)
290:                                  );
291:                 
292:                 return map.values()
293:                                 .parallelStream()
294:                                 .collect(ArrayList::new, ArrayList::addAll, ArrayList::addAll);                
295:         
296:         }
297: }