]> begriffs open source - cmsis-driver-validation/blob - Source/DV_Report.c
Set release date for release v3.0.0
[cmsis-driver-validation] / Source / DV_Report.c
1 /*
2  * Copyright (c) 2015-2021 Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the License); you may
7  * not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * -----------------------------------------------------------------------------
19  *
20  * Project:     CMSIS-Driver Validation
21  * Title:       Report statistics and layout implementation
22  *
23  * -----------------------------------------------------------------------------
24  */
25
26
27 #include "DV_Report.h"
28
29
30 /* Local macros */
31 #define PRINT(x) MsgPrint x
32 #define FLUSH()  MsgFlush()
33
34 /* Local functions */
35 static void tr_Init    (void);
36 static void tr_Uninit  (void);
37 static void tg_Init    (const char *title, const char *date, const char *time, const char *fn);
38 static void tg_Info    (const char *info);
39 static void tg_InfoDone(void);
40 static void tg_Uninit  (void);
41 static void tc_Init    (uint32_t num, const char *fn);
42 static void tc_Detail  (const char *module, uint32_t line, const char *message);
43 static void tc_Uninit  (void);
44 static void as_Result  (TC_RES res);
45
46 static void MsgPrint (const char *msg, ...);
47 static void MsgFlush (void);
48
49 /* Global variables */
50 REPORT_ITF ritf = {                     /* Structure for report interface     */
51   tr_Init,
52   tr_Uninit,
53   tg_Init,
54   tg_Info,
55   tg_InfoDone,
56   tg_Uninit,
57   tc_Init,
58   tc_Detail,
59   tc_Uninit,
60   as_Result,
61 };
62
63 /* Local variables */
64 static TEST_GROUP_RESULTS test_group_result;    /* Test group results         */
65
66 static uint32_t   as_passed = 0U;       /* Assertions passed                  */
67 static uint32_t   as_failed = 0U;       /* Assertions failed                  */
68 static uint32_t   as_detail = 0U;       /* Assertions details available       */
69 static const char Passed[] = "PASSED";
70 static const char Failed[] = "FAILED";
71 static const char NotExe[] = "NOT EXECUTED";
72
73
74 /*-----------------------------------------------------------------------------
75  * No path - helper function
76  *----------------------------------------------------------------------------*/
77 static const char *no_path (const char *fn) {
78   const char *cp;
79 #if defined(__CC_ARM)
80   cp = strrchr(fn, (int32_t)'\\');
81 #else
82   cp = strrchr(fn, (int32_t)'/');
83 #endif
84   if (cp != NULL) {
85     cp = &cp[1];
86   } else {
87     cp = fn;
88   }
89   return (cp);
90 }
91
92 /*-----------------------------------------------------------------------------
93  * Init test report
94  *----------------------------------------------------------------------------*/
95 static void tr_Init (void) {
96
97   test_group_result.idx = 0;
98
99 #if (PRINT_XML_REPORT==1)
100   PRINT(("<?xml version=\"1.0\"?>\n"));
101   PRINT(("<?xml-stylesheet href=\"TR_Style.xsl\" type=\"text/xsl\" ?>\n"));
102   PRINT(("<report>\n"));
103 #else
104   PRINT(("                                \n\n"));
105 #endif  
106 }
107
108 /*-----------------------------------------------------------------------------
109  * Uninit test report
110  *----------------------------------------------------------------------------*/
111 static void tr_Uninit (void) {
112 #if (PRINT_XML_REPORT==1)
113   PRINT(("</report>\n"));
114 #endif
115 }
116
117 /*-----------------------------------------------------------------------------
118  * Init test group
119  *----------------------------------------------------------------------------*/
120 static void tg_Init (const char *title, const char *date, const char *time, const char *fn) {
121
122   test_group_result.idx++;
123   test_group_result.tests  = 0U;
124   test_group_result.passed = 0U;
125   test_group_result.failed = 0U;
126
127 #if (PRINT_XML_REPORT==1)
128   PRINT(("<test>\n"));
129   PRINT(("<title>%s</title>\n", title));
130   PRINT(("<date>%s</date>\n",   date));
131   PRINT(("<time>%s</time>\n",   time));
132   PRINT(("<file>%s</file>\n",   fn));
133   PRINT(("<group>%i</group>\n", test_group_result.idx));
134   PRINT(("<info>\n"));
135 #else
136   (void) fn;
137   PRINT(("%s   %s   %s \n\n", title, date, time));
138 #endif  
139 }
140
141 /*-----------------------------------------------------------------------------
142  * Write test group info
143  *----------------------------------------------------------------------------*/
144 static void tg_Info (const char *info) {
145
146   PRINT(("%s", info));
147   PRINT(("\n"));
148 #if (PRINT_XML_REPORT==0)
149   PRINT(("\n"));
150 #endif
151 }
152
153 /*-----------------------------------------------------------------------------
154  * Test group info done
155  *----------------------------------------------------------------------------*/
156 static void tg_InfoDone (void) {
157
158 #if (PRINT_XML_REPORT==1)
159   PRINT(("</info>\n"));
160   PRINT(("<test_cases>\n"));
161 #endif  
162 }
163
164 /*-----------------------------------------------------------------------------
165  * Uninit test group
166  *----------------------------------------------------------------------------*/
167 static void tg_Uninit (void) {
168   const char *tres;
169
170   if (test_group_result.failed > 0U) {  /* If any test failed => Failed       */
171     tres = Failed;
172   } else if (test_group_result.passed > 0U) {   /* If 1 passed => Passed      */
173     tres = Passed;
174   } else {                              /* If no tests exec => Not-executed   */
175     tres = NotExe;
176   }
177
178 #if (PRINT_XML_REPORT==1) 
179   PRINT(("</test_cases>\n"));
180   PRINT(("<summary>\n"));
181   PRINT(("<tcnt>%d</tcnt>\n", test_group_result.tests));
182   PRINT(("<pass>%d</pass>\n", test_group_result.passed));
183   PRINT(("<fail>%d</fail>\n", test_group_result.failed));
184   PRINT(("<tres>%s</tres>\n", tres));
185   PRINT(("</summary>\n"));
186   PRINT(("</test>\n"));
187 #else
188   PRINT(("\nTest Summary: %d Tests, %d Passed, %d Failed.\n", 
189          test_group_result.tests, 
190          test_group_result.passed, 
191          test_group_result.failed));
192   PRINT(("Test Result: %s\n\n\n", tres));
193 #endif 
194   FLUSH();
195 }
196
197 /*-----------------------------------------------------------------------------
198  * Init test
199  *----------------------------------------------------------------------------*/
200 static void tc_Init (uint32_t num, const char *fn) {
201
202   as_passed = 0U;
203   as_failed = 0U;
204   as_detail = 0U;
205
206 #if (PRINT_XML_REPORT==1)   
207   PRINT(("<tc>\n"));
208   PRINT(("<no>%d</no>\n",     num));
209   PRINT(("<func>%s</func>\n", fn));
210   PRINT(("<dbgi>\n"));
211 #else
212   PRINT(("TEST %02d: %-32s ", num, fn));
213 #endif 
214 }
215
216 /*-----------------------------------------------------------------------------
217  * Write test detail
218  *----------------------------------------------------------------------------*/
219 static void tc_Detail (const char *module, uint32_t line, const char *message) {
220   const char *module_no_path;
221
222   module_no_path = no_path (module);
223
224   as_detail = 1U;
225
226 #if (PRINT_XML_REPORT==1) 
227   PRINT(("<detail>\n"));
228   PRINT(("<module>%s</module>\n", module_no_path));
229   PRINT(("<line>%d</line>\n",     line));
230   if (message != NULL) {
231     PRINT(("<message>%s</message>\n", message));
232   }
233   PRINT(("</detail>\n"));
234 #else
235   PRINT(("\n  %s (%d)", module_no_path, line));
236   if (message != NULL) {
237     PRINT((": %s", message));
238   }
239 #endif
240 }
241
242 /*-----------------------------------------------------------------------------
243  * Uninit test
244  *----------------------------------------------------------------------------*/
245 static void tc_Uninit (void) {
246   const char *res;
247
248   test_group_result.tests++;
249
250   if (as_failed > 0U) {                 /* If any assertion failed => Failed  */
251     test_group_result.failed++;
252     res = Failed;
253   } else if (as_passed > 0U) {          /* If 1 assertion passed => Passed    */
254     test_group_result.passed++;
255     res = Passed;
256   } else {                              /* If no assertions => Not-executed   */
257     res = NotExe;
258   }
259
260 #if (PRINT_XML_REPORT==1) 
261   PRINT(("</dbgi>\n"));
262   PRINT(("<res>%s</res>\n", res));
263   PRINT(("</tc>\n"));
264   (void)as_detail;
265 #else
266   if (as_detail != 0U) {
267     PRINT(("\n                                          "));
268   }
269   PRINT(("%s\n", res));
270 #endif 
271 }
272
273 /*-----------------------------------------------------------------------------
274  * Assertion result registering
275  *----------------------------------------------------------------------------*/
276 static void as_Result (TC_RES res) {
277
278   if (res == PASSED) {
279     as_passed++;
280   } else if (res == FAILED) {
281     as_failed++;
282   } else {
283     // Do nothing
284   }
285 }
286
287 /*-----------------------------------------------------------------------------
288  * Add info line to group info
289  *----------------------------------------------------------------------------*/
290 void __tg_info (const char *info) {
291
292   if (info != NULL) {
293     tg_Info(info);
294   }
295 }
296
297 /*-----------------------------------------------------------------------------
298  * Set result
299  *----------------------------------------------------------------------------*/
300 void __set_result (const char *module, uint32_t line, const char *message, TC_RES res) {
301
302   // Set debug info
303   if (message != NULL) {
304     tc_Detail(module, line, message);
305   }
306
307   // Set result
308   as_Result(res);
309 }
310
311 /*-----------------------------------------------------------------------------
312  * Set message
313  *----------------------------------------------------------------------------*/
314 void __set_message (const char *module, uint32_t line, const char *message) {
315   if (message != NULL) {
316     tc_Detail(module, line, message);
317   }
318 }
319
320
321 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
322 #pragma clang diagnostic push
323 #pragma clang diagnostic ignored "-Wformat-nonliteral"
324 #endif
325 /*-----------------------------------------------------------------------------
326  *       MsgPrint:  Print a message to the standard output
327  *----------------------------------------------------------------------------*/
328 static void MsgPrint (const char *msg, ...) {
329   va_list args;
330   va_start(args, msg);
331   (void)vprintf(msg, args);
332   va_end(args);
333 }
334 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
335 #pragma clang diagnostic pop
336 #endif
337
338 /*-----------------------------------------------------------------------------
339  *       SER_MsgFlush:  Flush the standard output
340  *----------------------------------------------------------------------------*/
341 static void MsgFlush(void) {
342   (void)fflush(stdout);
343 }
344
345 /*-----------------------------------------------------------------------------
346  * End of file
347  *----------------------------------------------------------------------------*/