Package: ComparisonIR

ComparisonIR

nameinstructionbranchcomplexitylinemethod
ComparisonIR(File)
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
compare(Object, Object)
M: 240 C: 0
0%
M: 62 C: 0
0%
M: 32 C: 0
0%
M: 63 C: 0
0%
M: 1 C: 0
0%
handleBoolean(Object, Value)
M: 22 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
handleCharacter(Object, Value)
M: 22 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
handleMap(Object, Value)
M: 74 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%
handleNumber(Object, Value)
M: 21 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
handleObject(Object, Value)
M: 78 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 22 C: 0
0%
M: 1 C: 0
0%
handleQuote(Object, Value)
M: 13 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
handleRecord(Object, Value)
M: 72 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%
handleSeq(Object, Value)
M: 46 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
handleSet(Object, Value)
M: 71 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
handleString(Object, Value)
M: 42 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
handleToken(Object, Value)
M: 35 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
handleTuple(Object, Value)
M: 47 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * VDM Code Generator
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.codegen.tests.exec.util;
23:
24: import java.io.File;
25: import java.lang.reflect.Field;
26: import java.util.LinkedList;
27: import java.util.List;
28: import java.util.Set;
29:
30: import org.overture.ast.intf.lex.ILexNameToken;
31: import org.overture.codegen.runtime.Record;
32: import org.overture.codegen.runtime.Token;
33: import org.overture.codegen.runtime.Tuple;
34: import org.overture.codegen.runtime.VDMMap;
35: import org.overture.codegen.runtime.VDMSeq;
36: import org.overture.codegen.runtime.VDMSet;
37: import org.overture.ct.ctruntime.utils.TraceTest;
38: import org.overture.interpreter.traces.Verdict;
39: import org.overture.interpreter.values.BooleanValue;
40: import org.overture.interpreter.values.CharacterValue;
41: import org.overture.interpreter.values.FieldMap;
42: import org.overture.interpreter.values.FieldValue;
43: import org.overture.interpreter.values.InvariantValue;
44: import org.overture.interpreter.values.MapValue;
45: import org.overture.interpreter.values.NameValuePairMap;
46: import org.overture.interpreter.values.NilValue;
47: import org.overture.interpreter.values.NumericValue;
48: import org.overture.interpreter.values.ObjectValue;
49: import org.overture.interpreter.values.QuoteValue;
50: import org.overture.interpreter.values.RecordValue;
51: import org.overture.interpreter.values.SeqValue;
52: import org.overture.interpreter.values.SetValue;
53: import org.overture.interpreter.values.TokenValue;
54: import org.overture.interpreter.values.TupleValue;
55: import org.overture.interpreter.values.UpdatableValue;
56: import org.overture.interpreter.values.Value;
57:
58: public class ComparisonIR
59: {
60:         public ComparisonIR(File testInputFile)
61:         {
62:•                if (testInputFile == null)
63:                 {
64:                         throw new IllegalArgumentException("Test file cannot be null");
65:                 }
66:         }
67:
68:         public boolean compare(Object cgResult, Object vdmResult)
69:         {
70:                 // If the VDM result is a String then it must be an error message
71:•                if (vdmResult instanceof String)
72:                 {
73:                         String vdmError = vdmResult.toString();
74:                         String cgError = cgResult.toString();
75:
76:•                        return vdmError.toLowerCase().contains("error")
77:•                                        && vdmError.contains(cgError);
78:                 }
79:
80:•                if (!(vdmResult instanceof Value))
81:                 {
82:•                        if (vdmResult instanceof List && cgResult instanceof List)
83:                         {
84:                                 @SuppressWarnings("rawtypes")
85:                                 List vdmList = (List) vdmResult;
86:                                 @SuppressWarnings("rawtypes")
87:                                 List cgList = (List) cgResult;
88:
89:•                                if (vdmList.size() != cgList.size())
90:                                 {
91:                                         return false;
92:                                 }
93:
94:•                                for (int i = 0; i < vdmList.size(); i++)
95:                                 {
96:                                         Object vdmElem = vdmList.get(i);
97:                                         Object cgElem = cgList.get(i);
98:
99:•                                        if (vdmElem instanceof TraceTest
100:                                                         && cgElem instanceof org.overture.codegen.runtime.traces.TraceTest)
101:                                         {
102:                                                 TraceTest vdmTest = (TraceTest) vdmElem;
103:                                                 org.overture.codegen.runtime.traces.TraceTest cgTest = (org.overture.codegen.runtime.traces.TraceTest) cgElem;
104:
105:•                                                if (vdmTest.getVerdict().toString().equals(cgTest.getVerdict().toString()))
106:                                                 {
107:•                                                        if (!(vdmTest.getNo().equals(cgTest.getNo())
108:•                                                                        && vdmTest.getTest().equals(cgTest.getTest())))
109:                                                         {
110:                                                                 return false;
111:                                                         }
112:
113:•                                                        if (vdmTest.getVerdict() == Verdict.PASSED)
114:                                                         {
115:•                                                                if (!vdmTest.getResult().equals(cgTest.getResult()))
116:                                                                 {
117:                                                                         return false;
118:                                                                 }
119:                                                         }
120:                                                 } else
121:                                                 {
122:                                                         return false;
123:                                                 }
124:                                         } else
125:                                         {
126:                                                 return false;
127:                                         }
128:                                 }
129:                                 return true;
130:                         } else
131:                         {
132:                                 return false;
133:                         }
134:                 }
135:
136:                 Value vdmValue = (Value) vdmResult;
137:
138:•                while (vdmValue instanceof UpdatableValue)
139:                 {
140:                         UpdatableValue upValue = (UpdatableValue) vdmValue;
141:                         vdmValue = upValue.getConstant();
142:                 }
143:
144:•                while (vdmValue instanceof InvariantValue)
145:                 {
146:                         vdmValue = vdmValue.deref();
147:                 }
148:
149:•                if (vdmValue instanceof BooleanValue)
150:                 {
151:                         return handleBoolean(cgResult, vdmValue);
152:•                } else if (vdmValue instanceof CharacterValue)
153:                 {
154:                         return handleCharacter(cgResult, vdmValue);
155:•                } else if (vdmValue instanceof MapValue)
156:                 {
157:                         return handleMap(cgResult, vdmValue);
158:•                } else if (vdmValue instanceof QuoteValue)
159:                 {
160:                         return handleQuote(cgResult, vdmValue);
161:•                } else if (vdmValue instanceof NumericValue)
162:                 {
163:                         return handleNumber(cgResult, vdmValue);
164:•                } else if (vdmValue instanceof SeqValue)
165:                 {
166:•                        if (cgResult instanceof String)
167:                         {
168:                                 return handleString(cgResult, vdmValue);
169:                         } else
170:                         {
171:                                 return handleSeq(cgResult, vdmValue);
172:                         }
173:•                } else if (vdmValue instanceof SetValue)
174:                 {
175:                         return handleSet(cgResult, vdmValue);
176:•                } else if (vdmValue instanceof TupleValue)
177:                 {
178:                         return handleTuple(cgResult, vdmValue);
179:•                } else if (vdmValue instanceof TokenValue)
180:                 {
181:                         return handleToken(cgResult, vdmValue);
182:•                } else if (vdmValue instanceof NilValue)
183:                 {
184:•                        return cgResult == null;
185:•                } else if (vdmValue instanceof ObjectValue)
186:                 {
187:                         return handleObject(cgResult, vdmValue);
188:•                } else if (vdmValue instanceof RecordValue)
189:                 {
190:                         return handleRecord(cgResult, vdmValue);
191:                 }
192:
193:                 return false;
194:         }
195:
196:         private boolean handleRecord(Object cgValue, Value vdmValue)
197:         {
198:•                if (!(cgValue instanceof Record))
199:                 {
200:                         return false;
201:                 }
202:
203:                 RecordValue vdmRecord = (RecordValue) vdmValue;
204:                 Record cgRecord = (Record) cgValue;
205:
206:•                if (!cgRecord.getClass().getName().endsWith(vdmRecord.type.getName().getName()))
207:                 {
208:                         return false;
209:                 }
210:
211:                 Field[] cgRecFields = cgRecord.getClass().getFields();
212:
213:                 FieldMap vdmRecFields = vdmRecord.fieldmap;
214:
215:•                for (int i = 0; i < vdmRecFields.size(); i++)
216:                 {
217:                         FieldValue vdmField = vdmRecFields.get(i);
218:                         Field cgField = cgRecFields[i];
219:
220:                         try
221:                         {
222:•                                if (!cgField.getName().equals(vdmField.name))
223:                                 {
224:                                         return false;
225:                                 }
226:
227:•                                if (!compare(cgField.get(cgRecord), vdmField.value))
228:                                 {
229:                                         return false;
230:                                 }
231:                         } catch (Exception e)
232:                         {
233:                                 e.printStackTrace();
234:                                 return false;
235:                         }
236:                 }
237:
238:                 return true;
239:         }
240:
241:         private boolean handleObject(Object cgValue, Value vdmValue)
242:         {
243:                 ObjectValue vdmObject = (ObjectValue) vdmValue;
244:
245:                 Field[] fields = cgValue.getClass().getFields();
246:                 NameValuePairMap memberValues = vdmObject.getMemberValues();
247:                 Set<ILexNameToken> keySet = memberValues.keySet();
248:
249:•                for (Field field : fields)
250:                 {
251:                         boolean foundMatchingField = false;
252:
253:•                        for (ILexNameToken tok : keySet)
254:                         {
255:•                                if (field.getName().equals(tok.getName()))
256:                                 {
257:                                         Value vdmFieldValue = memberValues.get(tok);
258:
259:•                                        if (vdmFieldValue != null)
260:                                         {
261:                                                 try
262:                                                 {
263:                                                         Object cgFieldValue = field.get(cgValue);
264:
265:•                                                        if (compare(cgFieldValue, vdmFieldValue))
266:                                                         {
267:                                                                 foundMatchingField = true;
268:                                                                 break;
269:                                                         }
270:
271:                                                 } catch (Exception e)
272:                                                 {
273:                                                         e.printStackTrace();
274:                                                         return false;
275:                                                 }
276:                                         }
277:                                 }
278:                         }
279:
280:•                        if (!foundMatchingField)
281:                         {
282:                                 return false;
283:                         }
284:                 }
285:
286:                 return true;
287:         }
288:
289:         private boolean handleQuote(Object cgValue, Value vdmValue)
290:         {
291:•                if (cgValue == null)
292:                 {
293:                         return false;
294:                 }
295:
296:                 // For example, the replacement constructs <A> from <AQuote>
297:                 return cgValue.toString().replace("Quote>", ">").equals(vdmValue.toString());
298:         }
299:
300:         private boolean handleToken(Object cgValue, Value vdmValue)
301:         {
302:•                if (!(cgValue instanceof Token))
303:                 {
304:                         return false;
305:                 }
306:
307:                 Token cgToken = (Token) cgValue;
308:                 TokenValue vdmToken = (TokenValue) vdmValue;
309:
310:                 try
311:                 {
312:                         Field f = vdmToken.getClass().getDeclaredField("value");
313:                         f.setAccessible(true);
314:                         Value value = (Value) f.get(vdmToken);
315:
316:                         return compare(cgToken.getValue(), value);
317:
318:                 } catch (Exception e)
319:                 {
320:                         e.printStackTrace();
321:                         return false;
322:                 }
323:         }
324:
325:         private static boolean handleCharacter(Object cgValue, Value vdmValue)
326:         {
327:•                if (!(cgValue instanceof Character))
328:                 {
329:                         return false;
330:                 }
331:
332:                 Character cgChar = (Character) cgValue;
333:                 CharacterValue vdmChar = (CharacterValue) vdmValue;
334:
335:•                return cgChar != null && cgChar == vdmChar.unicode;
336:         }
337:
338:         private static boolean handleNumber(Object cgValue, Value vdmValue)
339:         {
340:•                if (!(cgValue instanceof Number))
341:                 {
342:                         return false;
343:                 }
344:
345:                 Number number = (Number) cgValue;
346:                 NumericValue vdmNumeric = (NumericValue) vdmValue;
347:
348:•                return number.doubleValue() == vdmNumeric.value;
349:         }
350:
351:         private boolean handleSeq(Object cgValue, Value vdmValue)
352:         {
353:•                if (!(cgValue instanceof VDMSeq))
354:                 {
355:                         return false;
356:                 }
357:
358:                 VDMSeq cgSeq = (VDMSeq) cgValue;
359:                 SeqValue vdmSeq = (SeqValue) vdmValue;
360:
361:•                if (cgSeq.size() != vdmSeq.values.size())
362:                 {
363:                         return false;
364:                 }
365:
366:•                for (int i = 0; i < cgSeq.size(); i++)
367:                 {
368:                         Object cgElement = cgSeq.get(i);
369:                         Value vdmElement = vdmSeq.values.get(i);
370:
371:•                        if (!compare(cgElement, vdmElement))
372:                         {
373:                                 return false;
374:                         }
375:                 }
376:
377:                 return true;
378:         }
379:
380:         private boolean handleSet(Object cgValue, Value vdmValue)
381:         {
382:•                if (!(cgValue instanceof VDMSet))
383:                 {
384:                         return false;
385:                 }
386:
387:                 VDMSet cgSet = (VDMSet) cgValue;
388:                 SetValue vdmSet = (SetValue) vdmValue;
389:
390:•                if (cgSet.size() != vdmSet.values.size())
391:                 {
392:                         return false;
393:                 }
394:
395:                 @SuppressWarnings("unchecked")
396:                 List<Object> cgValuesList = new LinkedList<Object>(cgSet);
397:                 List<Value> vdmValuesList = new LinkedList<Value>(vdmSet.values);
398:
399:•                for (int i = 0; i < cgValuesList.size(); i++)
400:                 {
401:                         Object cgElement = cgValuesList.get(i);
402:
403:                         boolean match = false;
404:•                        for (int k = 0; k < vdmValuesList.size(); k++)
405:                         {
406:                                 Value vdmElement = vdmValuesList.get(k);
407:
408:•                                if (compare(cgElement, vdmElement))
409:                                 {
410:                                         match = true;
411:                                         break;
412:                                 }
413:                         }
414:
415:•                        if (!match)
416:                         {
417:                                 return false;
418:                         }
419:                 }
420:
421:                 return true;
422:         }
423:
424:         private boolean handleTuple(Object cgValue, Value vdmValue)
425:         {
426:•                if (!(cgValue instanceof Tuple))
427:                 {
428:                         return false;
429:                 }
430:
431:                 Tuple javaTuple = (Tuple) cgValue;
432:                 TupleValue vdmTuple = (TupleValue) vdmValue;
433:
434:•                if (javaTuple.size() != vdmTuple.values.size())
435:                 {
436:                         return false;
437:                 }
438:
439:•                for (int i = 0; i < javaTuple.size(); i++)
440:                 {
441:•                        if (!compare(javaTuple.get(i), vdmTuple.values.get(i)))
442:                         {
443:                                 return false;
444:                         }
445:                 }
446:
447:                 return true;
448:         }
449:
450:         private boolean handleString(Object cgValue, Value vdmValue)
451:         {
452:•                if (!(cgValue instanceof String))
453:                 {
454:                         return false;
455:                 }
456:
457:                 String cgString = (String) cgValue;
458:                 SeqValue vdmSeq = (SeqValue) vdmValue;
459:
460:•                if (cgString.length() != vdmSeq.values.size())
461:                 {
462:                         return false;
463:                 }
464:
465:•                for (int i = 0; i < cgString.length(); i++)
466:                 {
467:•                        if (!compare(cgString.charAt(i), vdmSeq.values.get(i)))
468:                         {
469:                                 return false;
470:                         }
471:                 }
472:
473:                 return true;
474:         }
475:
476:         private boolean handleMap(Object cgValue, Value vdmValue)
477:         {
478:•                if (!(cgValue instanceof VDMMap))
479:                 {
480:                         return false;
481:                 }
482:
483:                 VDMMap cgMap = (VDMMap) cgValue;
484:                 MapValue vdmMap = (MapValue) vdmValue;
485:
486:•                if (cgMap.size() != vdmMap.values.size())
487:                 {
488:                         return false;
489:                 }
490:
491:•                for (Object cgKey : cgMap.keySet())
492:                 {
493:                         boolean match = false;
494:•                        for (Value vdmKey : vdmMap.values.keySet())
495:                         {
496:•                                if (compare(cgKey, vdmKey))
497:                                 {
498:                                         Object cgVal = cgMap.get(cgKey);
499:                                         Value vdmVal = vdmMap.values.get(vdmKey);
500:
501:•                                        if (compare(cgVal, vdmVal))
502:                                         {
503:                                                 match = true;
504:                                                 break;
505:                                         }
506:                                 }
507:                         }
508:
509:•                        if (!match)
510:                         {
511:                                 return false;
512:                         }
513:                 }
514:
515:                 return true;
516:         }
517:
518:         private boolean handleBoolean(Object cgValue, Value vdmValue)
519:         {
520:•                if (!(cgValue instanceof Boolean))
521:                 {
522:                         return false;
523:                 }
524:
525:                 Boolean cgBool = (Boolean) cgValue;
526:                 BooleanValue vdmBool = (BooleanValue) vdmValue;
527:
528:•                return cgBool != null && cgBool == vdmBool.value;
529:         }
530: }