winIDEA SDK
test_coverage_data.py
1# This script is licensed under BSD License, see file LICENSE.txt.
2#
3# (c) TASKING Germany GmbH, 2023
4
5"""
6This script reads data recorded by winIDEA coverage and prints it to stdout.
7It demonstrates usage of class CCoverageData2 and related classes.
8"""
9
10from __future__ import print_function
11
12import sys
13import isystem.connect as ic
14
15
16def printFuncAsmNodes(stat, indent):
17 numAsmLines = stat.getNumAsmLines()
18 for idx in range(numAsmLines):
19 asmLine = stat.getAsmLine(idx)
20 print(indent, 'Asm:')
21 print(indent, ' lineType:', asmLine.getLineType())
22 print(indent, ' coverageMarker:', asmLine.getCoverageMarker())
23 print(indent, ' address:', asmLine.getAddress())
24 print(indent, ' lineText:', asmLine.getLineText())
25 print(indent, ' lineNumber:', asmLine.getLineNumber())
26 print(indent, ' moduleName:', asmLine.getModuleName())
27
28
29def printStatisticForArea(stat, isPrintSourceLinesInModule=False, indent=2):
30
31 if stat is None:
32 print('ERROR: no statistic to print!')
33 return
34
35 sp = ' ' * indent
36 print(sp, 'Statistic:')
37 print(sp, ' areaType: ', ic.CCoverageStatistic2.areaType2Str(stat.getAreaType()))
38 print(sp, ' areaName: ', stat.getAreaName())
39
40 if stat.getParent() is not None:
41 print(sp, ' parentName: ', stat.getParent().getAreaName())
42
43 print(sp, ' linesInArea: ', stat.getLinesAll())
44 print(sp, ' lines executed/not exec.: ', stat.getLinesExecuted(), ' / ',
45 stat.getLinesAll() - stat.getLinesExecuted())
46 print(sp, ' bytesInArea: ', stat.getBytesAll())
47 print(sp, ' bytes executed/not exec.: ', stat.getBytesExecuted(), ' / ',
48 stat.getBytesAll() - stat.getBytesExecuted())
49 print(sp, ' conditionsInArea: ', stat.getConditionsAll())
50 print(sp, ' conditions true: ', stat.getConditionsTrue())
51 print(sp, ' conditions false: ', stat.getConditionsFalse())
52 print(sp, ' conditions both: ', stat.getConditionsBoth())
53 print(sp, ' execution count: ', stat.getExecutionCount())
54 printFuncAsmNodes(stat, sp + ' ')
55
56 print(sp, ' number of children: ', stat.getNumChildren())
57
58 if (stat.getAreaType() == ic.CCoverageStatistic2.EFolder or
59 stat.getAreaType() == ic.CCoverageStatistic2.EModule):
60
61 print(sp, ' abs. path: ', stat.getTextOrAbsPath())
62 print(sp, ' rel. path: ', stat.getRelPath())
63
64 if stat.getAreaType() == ic.CCoverageStatistic2.ESrcLine:
65 print(sp, ' source line number: ', stat.getLineNumber())
66 print(sp, ' source text: ', stat.getTextOrAbsPath())
67
68 if (stat.getAreaType() == ic.CCoverageStatistic2.EModule and
69 isPrintSourceLinesInModule):
70
71 numSrcLines = stat.getNumSourceLines()
72 print(sp, ' No. of source lines: ', numSrcLines, ':')
73 for i in range(numSrcLines):
74 srcLineInfo = stat.getSourceLineInfo(i)
75 print(sp, ' - source text: ', srcLineInfo.getSourceLineText())
76 print(sp, ' source line number: ', srcLineInfo.getSourceLineNumber())
77 print(sp, ' coverage marker: ', srcLineInfo.getCoverageMarker())
78
79 numAsmOpCodes = srcLineInfo.getNumAsmOpCodes()
80 print(sp, ' Assembler Op Codes: ', numAsmOpCodes, ':')
81 for j in range(numAsmOpCodes):
82 asmOpCodeInfo = srcLineInfo.getAsmOpCodeInfo(j)
83 print(sp, ' - source text: ', asmOpCodeInfo.getSourceLineText())
84 print(sp, ' coverage marker: ', asmOpCodeInfo.getCoverageMarker())
85 print(sp, ' address: ', asmOpCodeInfo.getAddress())
86
87
88def printCoverageInfo2(info):
89 print(' Coverage meta information:')
90 print(' tester name: ', info.getName())
91 print(' id: ', info.getId())
92 print(' date: ', info.getDate())
93 print(' time: ', info.getTime())
94 print(' software: ', info.getSoftware())
95 print(' hardware: ', info.getHardware())
96 print(' description: ', info.getDescription())
97 print(' comment: ', info.getComment())
98
99
100def iterateStatistic(coverageData, areaType):
101 """
102 This method demonstrates iteration on tree of statistic nodes.
103 """
104 it = coverageData.getIterator(areaType)
105 while it.hasNext():
106 printStatisticForArea(it.next())
107
108
109def iterateStatisticChildren(statistic):
110 """
111 This method demonstrates walking tree of statistic nodes. It can be used
112 only when complete file has been parsed into memory.
113 """
114
115 printStatisticForArea(statistic)
116
117 numChildren = statistic.getNumChildren()
118 for idx in range(numChildren):
119 printStatisticForArea(statistic.getChild(idx))
120
121
122def iterateStatisticRecursively(statistic, indent=2):
123 """
124 This method demonstrates walking tree of statistic nodes. It can be used
125 only when complete file has been parsed into memory.
126 """
127
128 printStatisticForArea(statistic, indent = indent)
129
130 numChildren = statistic.getNumChildren()
131 for idx in range(numChildren):
132 childStat = statistic.getChild(idx)
133 # printStatisticForArea(childStat)
134 iterateStatisticRecursively(childStat, indent + 2)
135
136
137def printCoverageInfo():
138 print('In-memory coverage data parser')
139 print('==============================\n')
140 coverageData = ic.CCoverageData2.createInstance('../../targetProjects/coverageSample-1.xml',
141 True)
142
143 warnings = coverageData.getParserWarnings()
144 if warnings:
145 print('WARNING(S): ', warnings)
146 else:
147 print('OK, 0 warnings')
148
149 coverageData.closeParser()
150
151 printCoverageInfo2(coverageData.getCoverageMetaInfo())
152
153 print('\n\n ---\nCoverage statistic for complete recording: ')
154 rootStatistic = coverageData.getRoot()
155 printStatisticForArea(rootStatistic)
156
157 funcName = 'test_fibonacci'
158 print('\n\n ---\nCoverage statistic for ' + funcName)
159 func1Stat = coverageData.getStatistic(ic.CCoverageStatistic2.EFunction,
160 funcName)
161 printStatisticForArea(func1Stat)
162
163 print("\n\n ---\nCoverage statistic for '" + funcName +
164 "' and it's immediate children.")
165
166 # iterateStatisticChildren(func1Stat)
167 iterateStatisticRecursively(func1Stat)
168
169 print("\n\n ---\nCoverage statistic for all folders.")
170 iterateStatistic(coverageData, ic.CCoverageStatistic2.EFolder)
171
172 print("\n\n ---\nList of all modules in coverage file.")
173 iter = coverageData.getIterator(ic.CCoverageStatistic2.EModule)
174 while iter.hasNext():
175 stat = iter.next()
176 # print only module name, call printStatisticForArea() to see all info
177 print(stat.getAreaName())
178
179
180def printCoverageInfoWithIterarors():
181 print('\n\nFile parser')
182 print('===========\n')
183
184 coverageData = ic.CCoverageData2.createInstance('../../targetProjects/coverageSample-1.xml',
185 False)
186
187 printCoverageInfo2(coverageData.getCoverageMetaInfo())
188
189 moduleName = 'main.cpp'
190 print("\n\n ---\nCoverage statistic for '" + moduleName + "' including source lines:")
191 mainStat = coverageData.getStatistic(ic.CCoverageStatistic2.EModule,
192 moduleName)
193 warnings = coverageData.getParserWarnings()
194 if warnings:
195 print('WARNING(S): ', warnings)
196
197 printStatisticForArea(mainStat, True)
198
199 coverageData.closeParser()
200
201 # since going back in file is not possible, we have to reopen it
202 coverageData = ic.CCoverageData2.createInstance('../../targetProjects/coverageSample-1.xml',
203 False)
204
205 print('\n\n ---\nCoverage statistic for all functions (percentage of object code executed):')
206 iter = coverageData.getIterator(ic.CCoverageStatistic2.EFunction)
207 while iter.hasNext():
208 stat = iter.next()
209 # print(only function name, call printStatisticForArea() to see all info
210 print(' ', stat.getAreaName() + ': ' +
211 str(stat.getBytesExecuted() * 100 / stat.getBytesAll()) + '%')
212
213
214printCoverageInfo()
215printCoverageInfoWithIterarors()