hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2014, STMicroelectronics International N.V.
 */
 
/*************************************************************************
* 1. Includes
*************************************************************************/
#include "adbg_int.h"
 
/*************************************************************************
* 2. Definition of external constants and variables
*************************************************************************/
 
/*************************************************************************
* 3. File scope types, constants and variables
*************************************************************************/
 
/*************************************************************************
* 4. Declaration of file local functions
*************************************************************************/
 
/*
 * Deletes a subcase. Don't call this function before the
 * subcase is removed from list.
 */
static void ADBG_SubCase_Delete(ADBG_SubCase_t *SubCase);
 
static ADBG_SubCase_t *ADBG_Case_CreateSubCase(ADBG_Case_t *Case_p,
                          const char *const Title_p);
 
static ADBG_SubCase_t *ADBG_Case_GetParentSubCase(ADBG_Case_t *Case_p,
                         ADBG_SubCase_t *SubCase_p);
 
static const char *ADBG_Case_GetTestID(ADBG_Case_t *Case_p);
 
/*************************************************************************
* 5. Definition of external functions
*************************************************************************/
ADBG_Case_t *ADBG_Case_New(const struct adbg_case_def *case_def)
{
   ADBG_Case_t *Case_p = NULL;
 
   Case_p = calloc(1, sizeof(*Case_p));
   if (Case_p)
       Case_p->case_def = case_def;
 
   return Case_p;
}
 
void ADBG_Case_Delete(ADBG_Case_t *Case_p)
{
   ADBG_SubCase_Delete(Case_p->FirstSubCase_p);
   free(Case_p);
}
 
bool ADBG_Case_SubCaseIsMain(
   const ADBG_Case_t *const Case_p,
   const ADBG_SubCase_t *const SubCase_p
   )
{
   IDENTIFIER_NOT_USED(Case_p)
   return SubCase_p->Parent_p == NULL;
}
 
 
void ADBG_Case_IterateSubCase(
   ADBG_Case_t *Case_p,
   ADBG_SubCase_Iterator_t *Iterator_p
   )
{
   Iterator_p->Case_p = Case_p;
   Iterator_p->CurrentSubCase_p = NULL;
}
 
ADBG_SubCase_t *ADBG_Case_NextSubCase(
   ADBG_SubCase_Iterator_t *Iterator_p
   )
{
   ADBG_Case_t *Case_p = Iterator_p->Case_p;
   ADBG_SubCase_t *SubCase_p = Iterator_p->CurrentSubCase_p;
 
 
   /*
    * Traverse the subcases depth first, that is:
    * 1.1.1.1
    * 1.1.1.2
    * 1.1.1
    * 1.1.2.1
    * 1.1.2
    * 1.1
    * 1.2.1
    * 1.2
    * 1
    */
   if (SubCase_p == NULL) {
       /* Find the first leaf */
       SubCase_p = Case_p->FirstSubCase_p;
       if (SubCase_p == NULL)
           goto CleanupReturn;
 
       while (!TAILQ_EMPTY(&SubCase_p->SubCasesList))
           SubCase_p = TAILQ_FIRST(&SubCase_p->SubCasesList);
       goto CleanupReturn;
   }
 
   /*
    * Look for the next leaf belonging to the parent
    */
 
   if (SubCase_p->Parent_p == NULL) {
       /* If parent is NULL this is the top
           subcase and we're done */
       SubCase_p = NULL;
       goto CleanupReturn;
   }
 
   if (TAILQ_NEXT(SubCase_p, Link) == NULL) {
       /* If this is the last subcase of the
           parent move up to parent */
       SubCase_p = SubCase_p->Parent_p;
       goto CleanupReturn;
   }
 
   /*
    * Find next leaf
    */
   SubCase_p = TAILQ_NEXT(SubCase_p, Link);
   while (!TAILQ_EMPTY(&SubCase_p->SubCasesList))
       SubCase_p = TAILQ_FIRST(&SubCase_p->SubCasesList);
 
CleanupReturn:
   Iterator_p->CurrentSubCase_p = SubCase_p;
   return SubCase_p;
}
 
void Do_ADBG_BeginSubCase(
   ADBG_Case_t *const Case_p,
   const char *const FormatTitle_p, ...
   )
{
   ADBG_SubCase_t *SubCase_p = NULL;
 
   if (Case_p == NULL) {
       Do_ADBG_Log("Do_ADBG_BeginSubCase: NULL Case_p!");
       return;
   }
 
   if (FormatTitle_p == NULL) {
       Do_ADBG_Log("Do_ADBG_BeginSubCase: NULL FormatTitle_p!");
       return;
   }
 
   va_list ArgList;
   char Title[80] = { };
 
   va_start(ArgList, FormatTitle_p);
   vsnprintf(Title, sizeof(Title), FormatTitle_p, ArgList);
   va_end(ArgList);
 
   SubCase_p = ADBG_Case_CreateSubCase(Case_p, Title);
 
   if (SubCase_p == NULL) {
       Do_ADBG_Log("Do_ADBG_BeginSubCase: HEAP_ALLOC failed");
       return;
   }
 
 
   if (ADBG_Case_SubCaseIsMain(Case_p, SubCase_p)) {
       /* Main SubCase */
       Do_ADBG_Log(" ");
       Do_ADBG_Log("* %s %s", SubCase_p->TestID_p, SubCase_p->Title_p);
   } else {
       Do_ADBG_Log("o %s %s", SubCase_p->TestID_p, SubCase_p->Title_p);
   }
}
 
void Do_ADBG_EndSubCase(
   ADBG_Case_t *const Case_p,
   const char *const FormatTitle_p, ...
   )
{
   va_list ArgList;
   char Title[80] = { };
   ADBG_SubCase_t *SubCase_p = NULL;
 
   if (Case_p == NULL) {
       Do_ADBG_Log("Do_ADBG_EndSubCase: NULL Case_p!");
       return;
   }
 
   if (FormatTitle_p == NULL) {
       strcpy(Title, "NULL");
   } else {
       va_start(ArgList, FormatTitle_p);
       vsnprintf(Title, sizeof(Title), FormatTitle_p, ArgList);
       va_end(ArgList);
   }
 
 
   SubCase_p = Case_p->CurrentSubCase_p;
 
   if (SubCase_p == NULL) {
       Do_ADBG_Log("Do_ADBG_EndSubCase: "
               "Have no active SubCase, bailing out for title \"%s\"",
               Title);
       return;
   }
 
   if (FormatTitle_p != NULL && strcmp(SubCase_p->Title_p, Title) != 0) {
       Do_ADBG_Log("Do_ADBG_EndSubCase: "
               "Active SubCase \"%s\" doesn't match supplied title \"%s\"",
               SubCase_p->Title_p, Title);
       return;
   }
 
   if (ADBG_Case_SubCaseIsMain(Case_p, SubCase_p)) {
       if (FormatTitle_p == NULL) {
           /* To end the main subcase we require
               a matching title */
           Do_ADBG_Log("Do_ADBG_EndSubCase: "
                   "The main SubCase \"%s\" doesn't match supplied title \"%s\"",
                   SubCase_p->Title_p, Title);
           return;
       }
       /*
        * Ending the main subcase
        * make a complete copy of the aggregated result.
        */
       Case_p->Result = SubCase_p->Result;
   } else {
       /*
        * Ending a subcase,
        * Aggregate results to parent.
        */
       ADBG_SubCase_t *Parent_p = SubCase_p->Parent_p;
 
       Parent_p->Result.NumSubTests += SubCase_p->Result.NumTests +
                       SubCase_p->Result.NumSubTests;
       Parent_p->Result.NumFailedSubTests +=
           SubCase_p->Result.NumFailedTests +
           SubCase_p->Result.
           NumFailedSubTests;
       Parent_p->Result.AbortTestSuite =
           SubCase_p->Result.AbortTestSuite;
       if (SubCase_p->Result.NumTests > 0 ||
           SubCase_p->Result.NumSubTests > 0)
           Parent_p->Result.NumFailedSubCases++;
   }
 
   /* Print a summary of the subcase result */
   if (SubCase_p->Result.NumFailedTests > 0 ||
       SubCase_p->Result.NumFailedSubTests > 0) {
       Do_ADBG_Log("  %s FAILED", SubCase_p->TestID_p);
   } else {
       Do_ADBG_Log("  %s OK", SubCase_p->TestID_p);
   }
 
   /* Update current subcase to be the parent of this subcase */
   Case_p->CurrentSubCase_p =
       ADBG_Case_GetParentSubCase(Case_p, SubCase_p);
}
 
/*************************************************************************
* 6. Definition of internal functions
*************************************************************************/
static ADBG_SubCase_t *ADBG_Case_CreateSubCase(
   ADBG_Case_t *Case_p,
   const char *const Title_p
   )
{
   ADBG_SubCase_t *SubCase_p = NULL;
 
   SubCase_p = calloc(1, sizeof(*SubCase_p));
   if (SubCase_p == NULL)
       goto ErrorReturn;
 
   TAILQ_INIT(&SubCase_p->SubCasesList);
 
   SubCase_p->Title_p = strdup(Title_p);
   if (SubCase_p->Title_p == NULL)
       goto ErrorReturn;
 
   /* Set parent pointer needed "early" below. */
   SubCase_p->Parent_p = Case_p->CurrentSubCase_p;
 
   if (SubCase_p->Parent_p == NULL) {
       /* Main SubCase */
       SubCase_p->TestID_p = strdup(ADBG_Case_GetTestID(Case_p));
       if (SubCase_p->TestID_p == NULL)
           goto ErrorReturn;
 
       Case_p->FirstSubCase_p = SubCase_p;
   } else {
       ADBG_SubCase_t *Parent_p = SubCase_p->Parent_p;
       char PrefixTitle[80] = { };
 
       /* Update parent SubCase */
       Parent_p->Result.NumSubCases++;
       snprintf(PrefixTitle, sizeof(PrefixTitle), "%s.%d",
            Parent_p->TestID_p, Parent_p->Result.NumSubCases);
       SubCase_p->TestID_p = strdup(PrefixTitle);
       if (SubCase_p->TestID_p == NULL)
           goto ErrorReturn;
 
       TAILQ_INSERT_TAIL(&Parent_p->SubCasesList, SubCase_p, Link);
   }
 
   Case_p->CurrentSubCase_p = SubCase_p;
   return SubCase_p;
 
ErrorReturn:
   ADBG_SubCase_Delete(SubCase_p);
   return NULL;
}
 
static void ADBG_SubCase_Delete(
   ADBG_SubCase_t *SubCase_p
   )
{
   if (SubCase_p != NULL) {
       /*
        * Note that Util_ListDestroy() checks
        * if SubCase_p->SubCasesList_p
        * is NULL.
        */
       while (true) {
           ADBG_SubCase_t *s =
               TAILQ_FIRST(&SubCase_p->SubCasesList);
 
           if (s == NULL)
               break;
 
           TAILQ_REMOVE(&SubCase_p->SubCasesList, s, Link);
           ADBG_SubCase_Delete(s);
       }
       free(SubCase_p->TestID_p);
       free(SubCase_p->Title_p);
       free(SubCase_p);
   }
}
 
ADBG_SubCase_t *ADBG_Case_GetParentSubCase(
   ADBG_Case_t *Case_p,
   ADBG_SubCase_t *SubCase_p
   )
{
   IDENTIFIER_NOT_USED(Case_p)
   IDENTIFIER_NOT_USED(SubCase_p)
   return SubCase_p->Parent_p;
}
 
static const char *ADBG_Case_GetTestID(ADBG_Case_t *Case_p)
{
   IDENTIFIER_NOT_USED(Case_p)
 
   return Case_p->case_def->TestID_p;
}