-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathutassert.c
588 lines (516 loc) · 17.7 KB
/
utassert.c
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
* NASA Docket No. GSC-18,370-1, and identified as "Operating System Abstraction Layer"
*
* Copyright (c) 2019 United States Government as represented by
* the Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* File: utassert.c
*
* Purpose: This code implements a standard set of asserts for use in unit tests.
*/
/*
* Includes
*/
#include <stdlib.h>
#include <stdarg.h>
#include "common_types.h"
#include "utassert.h"
#include "uttools.h"
#include "utbsp.h"
/*
* Local Data
*/
UtAssert_CaseType_t DefaultContext = UTASSERT_CASETYPE_FAILURE;
UtAssert_TestCounter_t UT_SegmentCounters = {0};
UtAssert_TestCounter_t UT_TotalCounters = {0};
static char CurrentSegment[64];
/*
* Function Definitions
*/
void UtAssert_DoReport(const char *File, uint32 LineNum, uint32 SegmentNum, uint32 TestSeq, uint8 MessageType,
const char *SubsysName, const char *ShortDesc)
{
uint32 FileLen;
const char *BasePtr;
char ReportBuffer[320];
FileLen = strlen(File);
BasePtr = File + FileLen;
while (FileLen > 0)
{
--BasePtr;
--FileLen;
if (*BasePtr == '/' || *BasePtr == '\\')
{
++BasePtr;
break;
}
}
snprintf(ReportBuffer, sizeof(ReportBuffer), "%02u.%03u %s:%u - %s", (unsigned int)SegmentNum,
(unsigned int)TestSeq, BasePtr, (unsigned int)LineNum, ShortDesc);
UT_BSP_DoText(MessageType, ReportBuffer);
}
void UtAssert_DoTestSegmentReport(const char *SegmentName, const UtAssert_TestCounter_t *TestCounters)
{
char ReportBuffer[144];
snprintf(ReportBuffer, sizeof(ReportBuffer),
"%02u %-20s TOTAL::%-4u PASS::%-4u FAIL::%-4u MIR::%-4u TSF::%-4u TTF::%-4u WARN::%-4u\n",
(unsigned int)TestCounters->TestSegmentCount, SegmentName, (unsigned int)TestCounters->TotalTestCases,
(unsigned int)TestCounters->CaseCount[UTASSERT_CASETYPE_PASS],
(unsigned int)TestCounters->CaseCount[UTASSERT_CASETYPE_FAILURE],
(unsigned int)TestCounters->CaseCount[UTASSERT_CASETYPE_MIR],
(unsigned int)TestCounters->CaseCount[UTASSERT_CASETYPE_TSF],
(unsigned int)TestCounters->CaseCount[UTASSERT_CASETYPE_TTF],
(unsigned int)TestCounters->CaseCount[UTASSERT_CASETYPE_WARN]);
UT_BSP_DoText(UTASSERT_CASETYPE_END, ReportBuffer);
}
uint32 UtAssert_GetPassCount(void)
{
return (UT_TotalCounters.CaseCount[UTASSERT_CASETYPE_PASS]);
}
uint32 UtAssert_GetFailCount(void)
{
return (UT_TotalCounters.CaseCount[UTASSERT_CASETYPE_FAILURE]);
}
const UtAssert_TestCounter_t *UtAssert_GetCounters(void)
{
return &UT_TotalCounters;
}
void UtAssert_BeginTest(const char *SegmentName)
{
uint32 TestSegmentCount;
UT_BSP_Lock();
memset(&UT_SegmentCounters, 0, sizeof(UT_SegmentCounters));
strncpy(CurrentSegment, SegmentName, sizeof(CurrentSegment) - 1);
CurrentSegment[sizeof(CurrentSegment) - 1] = 0;
TestSegmentCount = 1 + UT_TotalCounters.TestSegmentCount;
UT_BSP_Unlock();
UT_BSP_StartTestSegment(TestSegmentCount, SegmentName);
}
const char *UtAssert_GetSegmentName(void)
{
return CurrentSegment;
}
void UtAssert_EndTest(void)
{
uint32 Ct;
bool SegmentValid;
UtAssert_TestCounter_t Local_SegmentCounters;
char Local_SegmentName[sizeof(CurrentSegment)];
UT_BSP_Lock();
SegmentValid = (UT_SegmentCounters.TotalTestCases > 0);
if (SegmentValid)
{
++UT_TotalCounters.TestSegmentCount;
UT_SegmentCounters.TestSegmentCount = UT_TotalCounters.TestSegmentCount;
UT_TotalCounters.TotalTestCases += UT_SegmentCounters.TotalTestCases;
for (Ct = 0; Ct < UTASSERT_CASETYPE_MAX; ++Ct)
{
UT_TotalCounters.CaseCount[Ct] += UT_SegmentCounters.CaseCount[Ct];
}
memcpy(&Local_SegmentCounters, &UT_SegmentCounters, sizeof(Local_SegmentCounters));
/*
* note, strcpy is OK because both are fixed size buffers of the same size,
* and the null termination on CurrentSegment was locally enforced already
*/
strcpy(Local_SegmentName, CurrentSegment);
}
memset(&UT_SegmentCounters, 0, sizeof(UT_SegmentCounters));
UT_BSP_Unlock();
if (SegmentValid)
{
UtAssert_DoTestSegmentReport(Local_SegmentName, &Local_SegmentCounters);
}
else
{
UT_BSP_DoText(UTASSERT_CASETYPE_END, "No test cases\n");
}
}
void UtAssert_SetContext(UtAssert_CaseType_t Context)
{
DefaultContext = Context;
}
UtAssert_CaseType_t UtAssert_GetContext(void)
{
return DefaultContext;
}
bool UtAssert(bool Expression, const char *Description, const char *File, uint32 Line)
{
return UtAssertEx(Expression, UtAssert_GetContext(), File, Line, "%s", Description);
}
bool UtAssertEx(bool Expression, UtAssert_CaseType_t CaseType, const char *File, uint32 Line, const char *MessageFormat,
...)
{
va_list va;
char FinalMessage[256];
uint32 TestSegmentCount;
uint32 TotalTestCases;
UT_BSP_Lock();
++UT_SegmentCounters.TotalTestCases;
if (Expression)
{
CaseType = UTASSERT_CASETYPE_PASS;
}
if ((uint32)CaseType < UTASSERT_CASETYPE_MAX)
{
++UT_SegmentCounters.CaseCount[(uint32)CaseType];
}
TestSegmentCount = 1 + UT_TotalCounters.TestSegmentCount;
TotalTestCases = UT_SegmentCounters.TotalTestCases;
UT_BSP_Unlock();
va_start(va, MessageFormat);
vsnprintf(FinalMessage, sizeof(FinalMessage), MessageFormat, va);
va_end(va);
UtAssert_DoReport(File, Line, TestSegmentCount, TotalTestCases, CaseType, CurrentSegment, FinalMessage);
return Expression;
}
void UtAssert_Abort(const char *Message)
{
UT_BSP_DoText(UTASSERT_CASETYPE_ABORT, Message);
}
const char *UtAssert_GetCaseTypeAbbrev(UtAssert_CaseType_t CaseType)
{
const char *AbbrevStr;
switch (CaseType)
{
case UTASSERT_CASETYPE_ABORT:
AbbrevStr = "ABORT";
break;
case UTASSERT_CASETYPE_FAILURE:
AbbrevStr = "FAIL";
break;
case UTASSERT_CASETYPE_MIR:
AbbrevStr = "MIR";
break;
case UTASSERT_CASETYPE_TSF:
AbbrevStr = "TSF";
break;
case UTASSERT_CASETYPE_TTF:
AbbrevStr = "TTF";
break;
case UTASSERT_CASETYPE_WARN:
AbbrevStr = "WARN";
break;
case UTASSERT_CASETYPE_NA:
AbbrevStr = "N/A";
break;
case UTASSERT_CASETYPE_BEGIN:
AbbrevStr = "BEGIN";
break;
case UTASSERT_CASETYPE_END:
AbbrevStr = "END";
break;
case UTASSERT_CASETYPE_PASS:
AbbrevStr = "PASS";
break;
case UTASSERT_CASETYPE_INFO:
AbbrevStr = "INFO";
break;
case UTASSERT_CASETYPE_FLOW:
AbbrevStr = "FLOW";
break;
case UTASSERT_CASETYPE_DEBUG:
AbbrevStr = "DEBUG";
break;
default:
/* do not return NULL, as the result may be directly passed to C library functions */
AbbrevStr = "OTHER";
break;
}
return AbbrevStr;
}
void UtAssert_Message(uint8 MessageType, const char *File, uint32 Line, const char *Spec, ...)
{
va_list va;
char FinalMessage[256];
const char *BaseName;
size_t MsgLen;
if (File != NULL)
{
BaseName = strrchr(File, '/');
if (BaseName == NULL)
{
BaseName = File;
}
else
{
++BaseName;
}
snprintf(FinalMessage, sizeof(FinalMessage), "%s:%u:", BaseName, (unsigned int)Line);
MsgLen = strlen(FinalMessage);
}
else
{
MsgLen = 0;
}
va_start(va, Spec);
vsnprintf(&FinalMessage[MsgLen], sizeof(FinalMessage) - MsgLen, Spec, va);
va_end(va);
UT_BSP_DoText(MessageType, FinalMessage);
}
const char *UtAssert_GetOpText(UtAssert_Compare_t CompareType)
{
const char *OpText;
switch (CompareType)
{
case UtAssert_Compare_EQ: /* actual equals reference value */
OpText = "==";
break;
case UtAssert_Compare_NEQ: /* actual does not non equal reference value */
OpText = "!=";
break;
case UtAssert_Compare_LT: /* actual less than reference (exclusive) */
OpText = "<";
break;
case UtAssert_Compare_GT: /* actual greater than reference (exclusive) */
OpText = ">";
break;
case UtAssert_Compare_LTEQ: /* actual less than or equal to reference (inclusive) */
OpText = "<=";
break;
case UtAssert_Compare_GTEQ: /* actual greater than reference (inclusive) */
OpText = ">=";
break;
case UtAssert_Compare_BITMASK_SET: /* bit(s) in reference are set in actual */
OpText = "&";
break;
case UtAssert_Compare_BITMASK_UNSET: /* bit(s) in reference are not set in actual */
OpText = "&~";
break;
default: /* should never happen */
OpText = "??";
break;
}
return OpText;
}
bool UtAssert_GenericUnsignedCompare(unsigned long ActualValue, UtAssert_Compare_t CompareType,
unsigned long ReferenceValue, UtAssert_Radix_t RadixType, const char *File,
uint32 Line, const char *Desc, const char *ActualText, const char *ReferenceText)
{
bool Result;
const char *FormatStr;
switch (CompareType)
{
case UtAssert_Compare_EQ: /* actual equals reference value */
Result = (ActualValue == ReferenceValue);
break;
case UtAssert_Compare_NEQ: /* actual does not non equal reference value */
Result = (ActualValue != ReferenceValue);
break;
case UtAssert_Compare_LT: /* actual less than reference (exclusive) */
Result = (ActualValue < ReferenceValue);
break;
case UtAssert_Compare_GT: /* actual greater than reference (exclusive) */
Result = (ActualValue > ReferenceValue);
break;
case UtAssert_Compare_LTEQ: /* actual less than or equal to reference (inclusive) */
Result = (ActualValue <= ReferenceValue);
break;
case UtAssert_Compare_GTEQ: /* actual greater than reference (inclusive) */
Result = (ActualValue >= ReferenceValue);
break;
case UtAssert_Compare_BITMASK_SET: /* bit(s) in reference are set in actual */
Result = (ActualValue & ReferenceValue) == ReferenceValue;
break;
case UtAssert_Compare_BITMASK_UNSET: /* bit(s) in reference are not set in actual */
Result = (ActualValue & ReferenceValue) == 0;
break;
default: /* should never happen */
Result = false;
break;
}
switch (RadixType)
{
case UtAssert_Radix_OCTAL:
FormatStr = "%s%s (0%lo) %s %s (0%lo)";
break;
case UtAssert_Radix_DECIMAL:
FormatStr = "%s%s (%lu) %s %s (%lu)";
break;
default:
/* for unsigned, default is hex */
FormatStr = "%s%s (0x%lx) %s %s (0x%lx)";
break;
}
return UtAssertEx(Result, UTASSERT_CASETYPE_FAILURE, File, Line, FormatStr, Desc, ActualText, ActualValue,
UtAssert_GetOpText(CompareType), ReferenceText, ReferenceValue);
}
bool UtAssert_GenericSignedCompare(long ActualValue, UtAssert_Compare_t CompareType, long ReferenceValue,
UtAssert_Radix_t RadixType, const char *File, uint32 Line, const char *Desc,
const char *ActualText, const char *ReferenceText)
{
bool Result;
const char *FormatStr;
switch (CompareType)
{
case UtAssert_Compare_EQ: /* actual equals reference value */
Result = (ActualValue == ReferenceValue);
break;
case UtAssert_Compare_NEQ: /* actual does not non equal reference value */
Result = (ActualValue != ReferenceValue);
break;
case UtAssert_Compare_LT: /* actual less than reference (exclusive) */
Result = (ActualValue < ReferenceValue);
break;
case UtAssert_Compare_GT: /* actual greater than reference (exclusive) */
Result = (ActualValue > ReferenceValue);
break;
case UtAssert_Compare_LTEQ: /* actual less than or equal to reference (inclusive) */
Result = (ActualValue <= ReferenceValue);
break;
case UtAssert_Compare_GTEQ: /* actual greater than reference (inclusive) */
Result = (ActualValue >= ReferenceValue);
break;
default: /* should never happen */
Result = false;
break;
}
switch (RadixType)
{
case UtAssert_Radix_OCTAL:
FormatStr = "%s%s (0%lo) %s %s (0%lo)";
break;
case UtAssert_Radix_HEX:
FormatStr = "%s%s (0x%lx) %s %s (0x%lx)";
break;
default:
/* for signed, default is decimal */
FormatStr = "%s%s (%ld) %s %s (%ld)";
break;
}
return UtAssertEx(Result, UTASSERT_CASETYPE_FAILURE, File, Line, FormatStr, Desc, ActualText, ActualValue,
UtAssert_GetOpText(CompareType), ReferenceText, ReferenceValue);
}
bool UtAssert_StringBufCompare(const char *String1, size_t String1Max, const char *String2, size_t String2Max,
UtAssert_Compare_t CompareType, const char *File, uint32 Line)
{
char ScrubbedString1[256];
char ScrubbedString2[256];
const char *EndPtr1;
const char *EndPtr2;
size_t FormatLen1;
size_t FormatLen2;
bool Result;
int Compare;
/* Locate the actual end of both strings */
if (String1 == NULL)
{
EndPtr1 = NULL;
}
else
{
EndPtr1 = memchr(String1, 0, String1Max);
}
if (EndPtr1 != NULL)
{
FormatLen1 = EndPtr1 - String1;
}
else
{
FormatLen1 = String1Max;
}
if (String2 == NULL)
{
EndPtr2 = NULL;
}
else
{
EndPtr2 = memchr(String2, 0, String2Max);
}
if (EndPtr2 != NULL)
{
FormatLen2 = EndPtr2 - String2;
}
else
{
FormatLen2 = String2Max;
}
if (FormatLen1 == 0 && FormatLen2 == 0)
{
/* Two empty strings are considered equal */
Compare = 0;
}
else
{
/* Compare actual content based on the shorter of the two strings */
if (FormatLen1 < FormatLen2)
{
Compare = memcmp(String1, String2, FormatLen1);
}
else
{
Compare = memcmp(String1, String2, FormatLen2);
}
/* If initial content was the same, go by whichever is longer */
if (Compare == 0)
{
/*
* If String1 is longer, compare should be positive (String1 > String2)
* If String2 is longer, compare should be negative (String1 < String2)
* If strings are the same length, compare should be 0.
*/
Compare = FormatLen1 - FormatLen2;
}
}
switch (CompareType)
{
case UtAssert_Compare_EQ: /* actual equals reference value */
Result = (Compare == 0);
break;
case UtAssert_Compare_NEQ: /* actual does not non equal reference value */
Result = (Compare != 0);
break;
case UtAssert_Compare_LT: /* actual less than reference (exclusive) */
Result = (Compare < 0);
break;
case UtAssert_Compare_GT: /* actual greater than reference (exclusive) */
Result = (Compare > 0);
break;
case UtAssert_Compare_LTEQ: /* actual less than or equal to reference (inclusive) */
Result = (Compare <= 0);
break;
case UtAssert_Compare_GTEQ: /* actual greater than reference (inclusive) */
Result = (Compare >= 0);
break;
default: /* should never happen */
Result = false;
break;
}
/* Now make "safe" copies of the strings */
/* Check for a newline within the string, and if present, end the string there instead */
if (FormatLen1 > 0)
{
EndPtr1 = memchr(String1, '\n', FormatLen1);
if (EndPtr1 != NULL)
{
FormatLen1 = EndPtr1 - String1;
}
memcpy(ScrubbedString1, String1, FormatLen1);
}
ScrubbedString1[FormatLen1] = 0;
if (FormatLen2 > 0)
{
EndPtr2 = memchr(String2, '\n', FormatLen2);
if (EndPtr2 != NULL)
{
FormatLen2 = EndPtr2 - String2;
}
memcpy(ScrubbedString2, String2, FormatLen2);
}
ScrubbedString2[FormatLen2] = 0;
return UtAssertEx(Result, UTASSERT_CASETYPE_FAILURE, File, Line, "String: \'%s\' == \'%s\'", ScrubbedString1,
ScrubbedString2);
}