1 /* ==========================================
2 Unity Project - A Test Framework for C
3 Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
4 [Released under MIT License. Please refer to license.txt for details]
5 ========================================== */
6
7 #include "unity.h"
8 #include <string.h>
9 #include <stdarg.h>
10
11 #ifdef GCOV_DO_COVERAGE
12 #include "gcov_support.h"
13 #endif
14
15 /* This is SquishCoco code coverage tool related code */
16 #ifdef __COVERAGESCANNER__
17
18 extern void __coveragescanner_set_custom_io(char *(*csfgets)(char *s, int size, void *stream),
19 int (*csfputs)(const char *s, void *stream),
20 void *(*csfopenappend)(const char *path),
21 void *(*csfopenread)(const char *path),
22 void *(*csfopenwrite)(const char *path),
23 int (*csfclose)(void *fp),
24 int (*csremove)(const char *filename));
25 extern void __coveragescanner_save(void);
26 extern void __coveragescanner_testname(const char *name);
27 extern void __coveragescanner_teststate(const char *state);
28
29 /* Use preprocessor macro from the following list to chose the way results are stored:
30 SQUISHCOCO_RESULT_DATA_SAVE_TO_FILE
31 SQUISHCOCO_RESULT_DATA_SAVE_TO_MEMORY
32 SQUISHCOCO_RESULT_DATA_SAVE_TO_CONSOLE
33 */
34 #if ((!defined(SQUISHCOCO_RESULT_DATA_SAVE_TO_FILE)) && (!defined(SQUISHCOCO_RESULT_DATA_SAVE_TO_MEMORY)) && \
35 (!defined(SQUISHCOCO_RESULT_DATA_SAVE_TO_CONSOLE)) && (!defined(SQUISHCOCO_RESULT_DATA_SAVE_CUSTOM)))
36 #define SQUISHCOCO_RESULT_DATA_SAVE_TO_CONSOLE
37
38 /* Squish Coco I/O functions set implementation */
39 int csfputs(const char *s, void *stream);
40 void *csfopenappend(const char *path);
41 int csfclose(void *fp);
42 void *csfopenwrite(const char *path);
43
44 #else
45
46 /* Squish Coco I/O functions implemented externally */
47 extern int csfputs(const char *s, void *stream);
48 extern void *csfopenappend(const char *path);
49 extern int csfclose(void *fp);
50 extern void *csfopenwrite(const char *path);
51
52 #endif
53
54 #if defined(SQUISHCOCO_RESULT_DATA_SAVE_TO_FILE)
csfopenwrite(const char * path)55 void *csfopenwrite(const char *path)
56 {
57 return (void *)fopen("../measurements.csexe", "w");
58 }
59
csfputs(const char * s,void * stream)60 int csfputs(const char *s, void *stream)
61 {
62 return fputs(s, (FILE *)stream);
63 }
64
csfopenappend(const char * path)65 void *csfopenappend(const char *path)
66 {
67 return (void *)fopen(path, "a+");
68 }
69
csfclose(void * fp)70 int csfclose(void *fp)
71 {
72 return fclose((FILE *)fp);
73 }
74 #elif defined(SQUISHCOCO_RESULT_DATA_SAVE_TO_MEMORY)
75 uint32_t mem_offset = 0x10U;
76 #define SH_MEM_TOTAL_SIZE (6144U)
77 #if defined(__ICCARM__) /* IAR Workbench */
78 #pragma location = "rpmsg_sh_mem_section"
79 static uint8_t mem_to_store_coverage_results[SH_MEM_TOTAL_SIZE];
80 #elif defined(__CC_ARM) || defined(__ARMCC_VERSION) /* Keil MDK */
81 static char mem_to_store_coverage_results[SH_MEM_TOTAL_SIZE] __attribute__((section("rpmsg_sh_mem_section")));
82 #elif defined(__GNUC__)
83 static char mem_to_store_coverage_results[SH_MEM_TOTAL_SIZE] __attribute__((section(".noinit.$rpmsg_sh_mem")));
84 #endif
csfputs(const char * s,void * stream)85 int csfputs(const char *s, void *stream)
86 {
87 int return_value = 0;
88 uint32_t len = strlen(s);
89 memcpy((void *)(char *)(mem_to_store_coverage_results + mem_offset), s, len);
90 mem_offset += len;
91 return return_value;
92 }
93
94 static uint32_t stream = 0;
csfopenappend(const char * path)95 void *csfopenappend(const char *path)
96 {
97 return &stream;
98 }
99
csfclose(void * fp)100 int csfclose(void *fp)
101 {
102 return 0;
103 }
104 #elif defined(SQUISHCOCO_RESULT_DATA_SAVE_TO_CONSOLE)
csfputs(const char * s,void * stream)105 int csfputs(const char *s, void *stream)
106 {
107 int return_value = 0;
108 uint16_t len = strlen(s);
109 uint16_t status = PRINTF("%s", (char *)s);
110 return return_value;
111 }
112
113 static uint32_t stream = 0;
csfopenappend(const char * path)114 void *csfopenappend(const char *path)
115 {
116 return &stream;
117 }
118
csfclose(void * fp)119 int csfclose(void *fp)
120 {
121 return 0;
122 }
123 #endif /* SQUISHCOCO_RESULT_DATA_SAVE_TO_x */
124 #endif /*__COVERAGESCANNER__*/
125
126 #ifdef UNITY_DUMP_RESULT
127 #if defined(UNITY_DUMP_RESULT_ARRAY_SMALL)
128 #define DUMP_RESULT_ARRAY_SIZE 20U
129 #elif defined(UNITY_DUMP_RESULT_ARRAY_LARGE)
130 #define DUMP_RESULT_ARRAY_SIZE 200U
131 #else
132 #define DUMP_RESULT_ARRAY_SIZE 50U // default result array size
133 #endif
134
135 #if defined(UNITY_DUMP_CASE_RESULT_ONLY)
136 #define CASE_NUM_IN_BYTE 4U
137 _UU8 g_dumpResultArray[DUMP_RESULT_ARRAY_SIZE] = {0};
138 #elif defined(UNITY_DUMP_COMPATIBLE_WITH_EU)
139 #define EU_COUNT_RESETS 4U
140 _UU8 g_dumpResultArray[DUMP_RESULT_ARRAY_SIZE] = {0};
141 static UNITY_LINE_TYPE s_IndepAssertId = 0;
142 #else
143 typedef struct DumpTestCaseResult
144 {
145 _UU32 testResultType : 2;
146 _UU32 testID : 14;
147 _UU32 testFailIgnoreLine : 16;
148 } DumpTestCaseResultT;
149
150 typedef struct DumpTestSummary
151 {
152 _UU32 testResultType : 2;
153 _UU32 testTotalNum : 10;
154 _UU32 testFailNum : 10;
155 _UU32 testIgnoreNum : 10;
156 } DumpTestSummaryT;
157
158 typedef union DumpTestData
159 {
160 DumpTestCaseResultT testCaseResult;
161 DumpTestSummaryT testSummary;
162 _UU32 testResultData;
163 } DumpTestDataT;
164
165 DumpTestDataT g_dumpResultArray[DUMP_RESULT_ARRAY_SIZE] = {0};
166 _UU16 g_dumpResultArrayIndex = 0;
167 #endif
168 #endif
169 /* add do {} while(0) because of MISRA C 2014 rule 14.3 issue*/
170 #define UNITY_FAIL_AND_BAIL \
171 do \
172 { \
173 UNITY_OUTPUT_CHAR('\r'); \
174 UNITY_OUTPUT_CHAR('\n'); \
175 if (!Unity.ContinueCurTestIfAssertFail) \
176 { \
177 longjmp(Unity.AbortFrame, 1); \
178 } \
179 } while (0)
180
181 /* add do {} while(0) because of MISRA C 2014 rule 14.3 issue*/
182 #define UNITY_IGNORE_AND_BAIL \
183 do \
184 { \
185 UNITY_OUTPUT_CHAR('\r'); \
186 UNITY_OUTPUT_CHAR('\n'); \
187 longjmp(Unity.AbortFrame, 1); \
188 } while (0)
189
190 // return prematurely if we are already in failure or ignore state
191 /* add do {} while(0) because of MISRA C 2014 rule 14.3 issue*/
192 #define UNITY_SKIP_EXECUTION \
193 do \
194 { \
195 if ((!Unity.ContinueCurTestIfAssertFail) && \
196 ((Unity.CurrentTestFailed != 0) || (Unity.CurrentTestIgnored != 0))) \
197 { \
198 return; \
199 } \
200 } while (0)
201 #define UNITY_PRINT_EOL \
202 do \
203 { \
204 UNITY_OUTPUT_CHAR('\r'); \
205 UNITY_OUTPUT_CHAR('\n'); \
206 } while (0)
207
208 /* static struct _Unity Unity = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, {{ 0 }} }; */
209 /* The "jmp_buf AbortFrame" is defined as array[] for armgcc and keil, while array[][] for iar.
210 */
211 static struct _Unity Unity = {0};
212
213 const char *UnityStrNull = "NULL";
214 const char *UnityStrSpacer = ". ";
215 const char *UnityStrExpected = " Expected ";
216 const char *UnityStrWas = " Was ";
217 const char *UnityStrTo = " To ";
218 const char *UnityStrElement = " Element ";
219 const char *UnityStrByte = " Byte ";
220 const char *UnityStrMemory = " Memory Mismatch.";
221 const char *UnityStrDelta = " Values Not Within Delta ";
222 const char *UnityStrPointless = " You Asked Me To Compare Nothing, Which Was Pointless.";
223 const char *UnityStrNullPointerForExpected = " Expected pointer to be NULL";
224 const char *UnityStrNullPointerForActual = " Actual pointer was NULL";
225 const char *UnityStrInf = "Infinity";
226 const char *UnityStrNegInf = "Negative Infinity";
227 const char *UnityStrNaN = "NaN";
228
229 /* f_zero and d_zero are no longer used in modified Unity. */
230 /*
231 #ifndef UNITY_EXCLUDE_FLOAT
232 // Dividing by these constants produces +/- infinity.
233 // The rationale is given in UnityAssertFloatIsInf's body.
234 static const _UF f_zero = 0.0f;
235 #ifndef UNITY_EXCLUDE_DOUBLE
236 static const _UD d_zero = 0.0;
237 #endif
238 #endif
239 */
240
241 // compiler-generic print formatting masks
242 const _U_UINT UnitySizeMask[] = {255u, // 0xFF
243 65535u, // 0xFFFF
244 65535u,
245 4294967295u, // 0xFFFFFFFF
246 4294967295u,
247 4294967295u,
248 4294967295u
249 #ifdef UNITY_SUPPORT_64
250 ,
251 0xFFFFFFFFFFFFFFFF
252 #endif
253 };
254
255 #ifdef UNITY_DUMP_RESULT
256 #if defined(UNITY_DUMP_COMPATIBLE_WITH_EU) || defined(UNITY_DUMP_CASE_RESULT_ONLY)
257 static void UnityDumpFillResult(const _UU8 result, _U_UINT id, _U_UINT numInByte);
258 #endif
259 #endif
260
261 #ifdef UNITY_NOT_PRINT_LOG
262 /* add do {} while(0) because of MISRA C 2014 rule 14.3 issue*/
263 #define UnityPrintFail() \
264 do \
265 { \
266 } while (0)
267 #define UnityPrintOk() \
268 do \
269 { \
270 } while (0)
271 #define UnityAddMsgIfSpecified(a) \
272 do \
273 { \
274 } while (0)
275 #define UnityPrintExpectedAndActualStrings(a, b) \
276 do \
277 { \
278 } while (0)
279 #else
280 void UnityPrintFail(void);
281 void UnityPrintOk(void);
282 /* Static function need be defined in C file */
283 static void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style);
284
285 //-----------------------------------------------
286 // Pretty Printers & Test Result Output Handlers
287 //-----------------------------------------------
288 // not conver "\r", "\n", just print all strings
UnityPrintf(const char * format,...)289 void UnityPrintf(const char *format, ...)
290 {
291 if (NULL == format)
292 {
293 return;
294 }
295
296 int charNum;
297 va_list ap;
298 char printfBuffer[MAX_CMD_CHAR_CNT] = {0};
299 va_start(ap, format);
300 charNum = vsprintf(printfBuffer, format, ap);
301
302 char *pBuffer = printfBuffer;
303 int printNum = 0;
304 while (*pBuffer)
305 {
306 UNITY_OUTPUT_CHAR(*pBuffer);
307 pBuffer++;
308 printNum++;
309 if ((printNum > charNum) || (printNum >= MAX_CMD_CHAR_CNT))
310 {
311 break;
312 }
313 }
314
315 va_end(ap);
316 }
317
318 // conver "\r", "\n" to show its char
UnityPrint(const char * string)319 void UnityPrint(const char *string)
320 {
321 const char *pch = string;
322
323 if (pch != NULL)
324 {
325 while (*pch)
326 {
327 // printable characters plus CR & LF are printed
328 if ((*pch <= 126) && (*pch >= 10))
329 {
330 UNITY_OUTPUT_CHAR(*pch);
331 }
332 /*
333 //write escaped carriage returns
334 else if (*pch == 13)
335 {
336 UNITY_OUTPUT_CHAR('\\');
337 UNITY_OUTPUT_CHAR('r');
338 }
339 //write escaped line feeds
340 else if (*pch == 10)
341 {
342 UNITY_OUTPUT_CHAR('\\');
343 UNITY_OUTPUT_CHAR('n');
344 }*/
345 // unprintable characters are shown as codes
346 else
347 {
348 UNITY_OUTPUT_CHAR('\\');
349 UnityPrintNumberHex((_U_SINT)*pch, 2);
350 }
351 pch++;
352 }
353 }
354 }
355
356 //-----------------------------------------------
UnityPrintNumberByStyle(const _U_SINT number,const UNITY_DISPLAY_STYLE_T style)357 static void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style)
358 {
359 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
360 {
361 UnityPrintNumber(number);
362 }
363 else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
364 {
365 UnityPrintNumberUnsigned((_U_UINT)number & UnitySizeMask[((_U_UINT)style & (_U_UINT)0x0F) - 1]);
366 }
367 else
368 {
369 UnityPrintNumberHex((_U_UINT)number, (style & 0x000FU) << 1);
370 }
371 }
372
373 //-----------------------------------------------
374 /// basically do an itoa using as little ram as possible
UnityPrintNumber(const _U_SINT number_to_print)375 void UnityPrintNumber(const _U_SINT number_to_print)
376 {
377 _U_SINT divisor = 1;
378 _U_SINT next_divisor;
379 _U_SINT number = number_to_print;
380
381 if (number < 0)
382 {
383 UNITY_OUTPUT_CHAR('-');
384 number = -number;
385 }
386
387 // figure out initial divisor
388 while (number / divisor > 9)
389 {
390 next_divisor = divisor * 10;
391 if (next_divisor > divisor)
392 {
393 divisor = next_divisor;
394 }
395 else
396 {
397 break;
398 }
399 }
400
401 // now mod and print, then divide divisor
402 do
403 {
404 UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
405 divisor /= 10;
406 } while (divisor > 0);
407 }
408
409 //-----------------------------------------------
410 /// basically do an itoa using as little ram as possible
UnityPrintNumberUnsigned(const _U_UINT number)411 void UnityPrintNumberUnsigned(const _U_UINT number)
412 {
413 _U_UINT divisor = 1;
414 _U_UINT next_divisor;
415
416 // figure out initial divisor
417 while (number / divisor > 9)
418 {
419 next_divisor = divisor * 10;
420 if (next_divisor > divisor)
421 {
422 divisor = next_divisor;
423 }
424 else
425 {
426 break;
427 }
428 }
429
430 // now mod and print, then divide divisor
431 do
432 {
433 UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
434 divisor /= 10;
435 } while (divisor > 0);
436 }
437
438 //-----------------------------------------------
UnityPrintNumberHex(const _U_UINT number,const char nibbles_to_print)439 void UnityPrintNumberHex(const _U_UINT number, const char nibbles_to_print)
440 {
441 _U_UINT nibble;
442 char nibbles = nibbles_to_print;
443 UNITY_OUTPUT_CHAR('0');
444 UNITY_OUTPUT_CHAR('x');
445
446 while (nibbles > 0)
447 {
448 nibble = (number >> (--nibbles << 2)) & 0x0000000F;
449 if (nibble <= 9)
450 {
451 UNITY_OUTPUT_CHAR((char)('0' + nibble));
452 }
453 else
454 {
455 UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble));
456 }
457 }
458 }
459
460 //-----------------------------------------------
UnityPrintMask(const _U_UINT mask,const _U_UINT number)461 void UnityPrintMask(const _U_UINT mask, const _U_UINT number)
462 {
463 _U_UINT current_bit = (_U_UINT)1 << (UNITY_INT_WIDTH - 1);
464 _US32 i;
465
466 for (i = 0; i < UNITY_INT_WIDTH; i++)
467 {
468 if (current_bit & mask)
469 {
470 if (current_bit & number)
471 {
472 UNITY_OUTPUT_CHAR('1');
473 }
474 else
475 {
476 UNITY_OUTPUT_CHAR('0');
477 }
478 }
479 else
480 {
481 UNITY_OUTPUT_CHAR('X');
482 }
483 current_bit = current_bit >> 1;
484 }
485 }
486
487 //-----------------------------------------------
488 #ifdef UNITY_FLOAT_VERBOSE
UnityPrintFloat(_UF number)489 void UnityPrintFloat(_UF number)
490 {
491 char TempBuffer[32];
492 sprintf(TempBuffer, "%.6f", number);
493 UnityPrint(TempBuffer);
494 }
495 #endif
496
497 //-----------------------------------------------
498
UnityPrintFail(void)499 void UnityPrintFail(void)
500 {
501 UnityPrint("FAIL");
502 }
503
UnityPrintOk(void)504 void UnityPrintOk(void)
505 {
506 UnityPrint("OK");
507 }
508 #endif //#ifdef UNITY_NOT_PRINT_LOG
509
510 //-----------------------------------------------
UnityTestResultsBegin(const char * file,const UNITY_LINE_TYPE line)511 void UnityTestResultsBegin(const char *file, const UNITY_LINE_TYPE line)
512 {
513 Unity.CurrentTestLineNumber = line;
514
515 UnityPrintNumber(Unity.CurrentTestId);
516 UNITY_OUTPUT_CHAR(':');
517 UnityPrint(Unity.CurrentTestName);
518 UNITY_OUTPUT_CHAR(':');
519 if ((Unity.CurrentTestFailed) || (Unity.CurrentTestIgnored))
520 {
521 #ifdef UNITY_SHOW_TEST_FILE_PATH
522 UnityPrint(file);
523 #endif
524 UNITY_OUTPUT_CHAR(':');
525 UnityPrintNumber(line);
526 UNITY_OUTPUT_CHAR(':');
527 }
528 }
529
530 //-----------------------------------------------
UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)531 void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)
532 {
533 Unity.CurrentTestFailed = 1;
534 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_FAIL, line);
535 UnityDumpCaseResult(DUMP_RESULT_TEST_RESULT_FAIL, line);
536 UnityTestResultsBegin(Unity.TestFile, line);
537 UnityPrint("FAIL:");
538 }
539
540 //-----------------------------------------------
UnityConcludeTest(void)541 void UnityConcludeTest(void)
542 {
543 if (Unity.CurrentTestIgnored)
544 {
545 Unity.TestIgnores++;
546 }
547 else if (!Unity.CurrentTestFailed)
548 {
549 UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);
550 UnityPrint("PASS");
551 UNITY_PRINT_EOL;
552 UnityDumpCaseResult(DUMP_RESULT_TEST_RESULT_PASS, Unity.CurrentTestLineNumber);
553 }
554 else
555 {
556 Unity.TestFailures++;
557 }
558
559 Unity.CurrentTestFailed = 0;
560 Unity.CurrentTestIgnored = 0;
561 }
562
563 #ifndef UNITY_NOT_PRINT_LOG
564 //-----------------------------------------------
UnityAddMsgIfSpecified(const char * msg)565 void UnityAddMsgIfSpecified(const char *msg)
566 {
567 if (msg)
568 {
569 UnityPrint(UnityStrSpacer);
570 UnityPrint(msg);
571 }
572 }
573
574 //-----------------------------------------------
UnityPrintExpectedAndActualStrings(const char * expected,const char * actual)575 void UnityPrintExpectedAndActualStrings(const char *expected, const char *actual)
576 {
577 UnityPrint(UnityStrExpected);
578 if (expected != NULL)
579 {
580 UNITY_OUTPUT_CHAR('\'');
581 UnityPrint(expected);
582 UNITY_OUTPUT_CHAR('\'');
583 }
584 else
585 {
586 UnityPrint(UnityStrNull);
587 }
588 UnityPrint(UnityStrWas);
589 if (actual != NULL)
590 {
591 UNITY_OUTPUT_CHAR('\'');
592 UnityPrint(actual);
593 UNITY_OUTPUT_CHAR('\'');
594 }
595 else
596 {
597 UnityPrint(UnityStrNull);
598 }
599 }
600 #endif //#ifndef UNITY_NOT_PRINT_LOG
601
602 //-----------------------------------------------
603 // Assertion & Control Helpers
604 //-----------------------------------------------
605
UnityCheckArraysForNull(const void * expected,const void * actual,const UNITY_LINE_TYPE lineNumber,const char * msg)606 int UnityCheckArraysForNull(const void *expected, const void *actual, const UNITY_LINE_TYPE lineNumber, const char *msg)
607 {
608 // return true if they are both NULL
609 if ((expected == NULL) && (actual == NULL))
610 {
611 return 1;
612 }
613 // throw error if just expected is NULL
614 if (expected == NULL)
615 {
616 UnityTestResultsFailBegin(lineNumber);
617 UnityPrint(UnityStrNullPointerForExpected);
618 UnityAddMsgIfSpecified(msg);
619 UNITY_FAIL_AND_BAIL;
620 return 1;
621 }
622
623 // throw error if just actual is NULL
624 if (actual == NULL)
625 {
626 UnityTestResultsFailBegin(lineNumber);
627 UnityPrint(UnityStrNullPointerForActual);
628 UnityAddMsgIfSpecified(msg);
629 UNITY_FAIL_AND_BAIL;
630 return 1;
631 }
632 return 0;
633 }
634
635 //-----------------------------------------------
636 // Assertion Functions
637 //-----------------------------------------------
638
UnityAssertBits(const _U_UINT mask,const _U_UINT expected,const _U_UINT actual,const char * msg,const UNITY_LINE_TYPE lineNumber)639 void UnityAssertBits(
640 const _U_UINT mask, const _U_UINT expected, const _U_UINT actual, const char *msg, const UNITY_LINE_TYPE lineNumber)
641 {
642 UNITY_SKIP_EXECUTION;
643
644 if ((mask & expected) != (mask & actual))
645 {
646 UnityTestResultsFailBegin(lineNumber);
647 UnityPrint(UnityStrExpected);
648 UnityPrintMask(mask, expected);
649 UnityPrint(UnityStrWas);
650 UnityPrintMask(mask, actual);
651 UnityAddMsgIfSpecified(msg);
652 UNITY_FAIL_AND_BAIL;
653 }
654 else
655 {
656 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_PASS, lineNumber);
657 }
658 }
659
660 //-----------------------------------------------
UnityAssertEqualNumber(const _U_SINT expected,const _U_SINT actual,const char * msg,const UNITY_LINE_TYPE lineNumber,const UNITY_DISPLAY_STYLE_T style)661 void UnityAssertEqualNumber(const _U_SINT expected,
662 const _U_SINT actual,
663 const char *msg,
664 const UNITY_LINE_TYPE lineNumber,
665 const UNITY_DISPLAY_STYLE_T style)
666 {
667 UNITY_SKIP_EXECUTION;
668
669 if (expected != actual)
670 {
671 UnityTestResultsFailBegin(lineNumber);
672 UnityPrint(UnityStrExpected);
673 UnityPrintNumberByStyle(expected, style);
674 UnityPrint(UnityStrWas);
675 UnityPrintNumberByStyle(actual, style);
676 UnityAddMsgIfSpecified(msg);
677 UNITY_FAIL_AND_BAIL;
678 }
679 else
680 {
681 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_PASS, lineNumber);
682 }
683 }
684
685 //-----------------------------------------------
UnityAssertEqualIntArray(const _U_SINT * expected,const _U_SINT * actual,const _UU32 num_elements,const char * msg,const UNITY_LINE_TYPE lineNumber,const UNITY_DISPLAY_STYLE_T style)686 void UnityAssertEqualIntArray(const _U_SINT *expected,
687 const _U_SINT *actual,
688 const _UU32 num_elements,
689 const char *msg,
690 const UNITY_LINE_TYPE lineNumber,
691 const UNITY_DISPLAY_STYLE_T style)
692 {
693 _UU32 elements = num_elements;
694 const _US8 *ptr_exp = (const _US8 *)expected;
695 const _US8 *ptr_act = (const _US8 *)actual;
696
697 UNITY_SKIP_EXECUTION;
698
699 if (elements == 0)
700 {
701 UnityTestResultsFailBegin(lineNumber);
702 UnityPrint(UnityStrPointless);
703 UnityAddMsgIfSpecified(msg);
704 UNITY_FAIL_AND_BAIL;
705 }
706
707 /* type "_US32 const *" to "void const *" (MISRA C 2004 rule 11.2)
708 if (UnityCheckArraysForNull((const void*)expected, (const void*)actual, lineNumber, msg) == 1)
709 */
710 if (expected == NULL)
711 {
712 UnityTestResultsFailBegin(lineNumber);
713 UnityPrint(UnityStrNullPointerForExpected);
714 UnityAddMsgIfSpecified(msg);
715 UNITY_FAIL_AND_BAIL;
716 return;
717 }
718
719 if (actual == NULL)
720 {
721 UnityTestResultsFailBegin(lineNumber);
722 UnityPrint(UnityStrNullPointerForActual);
723 UnityAddMsgIfSpecified(msg);
724 UNITY_FAIL_AND_BAIL;
725 return;
726 }
727
728 // If style is UNITY_DISPLAY_STYLE_INT, we'll fall into the default case rather than the INT16 or INT32 (etc) case
729 // as UNITY_DISPLAY_STYLE_INT includes a flag for UNITY_DISPLAY_RANGE_AUTO, which the width-specific
730 // variants do not. Therefore remove this flag.
731 switch (style & ~UNITY_DISPLAY_RANGE_AUTO)
732 {
733 case UNITY_DISPLAY_STYLE_HEX8:
734 case UNITY_DISPLAY_STYLE_INT8:
735 case UNITY_DISPLAY_STYLE_UINT8:
736 while (elements--)
737 {
738 if (*ptr_exp != *ptr_act)
739 {
740 UnityTestResultsFailBegin(lineNumber);
741 UnityPrint(UnityStrElement);
742 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
743 UnityPrint(UnityStrExpected);
744 UnityPrintNumberByStyle(*ptr_exp, style);
745 UnityPrint(UnityStrWas);
746 UnityPrintNumberByStyle(*ptr_act, style);
747 UnityAddMsgIfSpecified(msg);
748 UNITY_FAIL_AND_BAIL;
749 }
750 ptr_exp += 1;
751 ptr_act += 1;
752 }
753 break;
754 case UNITY_DISPLAY_STYLE_HEX16:
755 case UNITY_DISPLAY_STYLE_INT16:
756 case UNITY_DISPLAY_STYLE_UINT16:
757 while (elements--)
758 {
759 if (*(const _US16 *)ptr_exp != *(const _US16 *)ptr_act)
760 {
761 UnityTestResultsFailBegin(lineNumber);
762 UnityPrint(UnityStrElement);
763 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
764 UnityPrint(UnityStrExpected);
765 UnityPrintNumberByStyle(*(const _US16 *)ptr_exp, style);
766 UnityPrint(UnityStrWas);
767 UnityPrintNumberByStyle(*(const _US16 *)ptr_act, style);
768 UnityAddMsgIfSpecified(msg);
769 UNITY_FAIL_AND_BAIL;
770 }
771 ptr_exp += 2;
772 ptr_act += 2;
773 }
774 break;
775 #ifdef UNITY_SUPPORT_64
776 case UNITY_DISPLAY_STYLE_HEX64:
777 case UNITY_DISPLAY_STYLE_INT64:
778 case UNITY_DISPLAY_STYLE_UINT64:
779 while (elements--)
780 {
781 if (*(_US64 *)ptr_exp != *(_US64 *)ptr_act)
782 {
783 UnityTestResultsFailBegin(lineNumber);
784 UnityPrint(UnityStrElement);
785 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
786 UnityPrint(UnityStrExpected);
787 UnityPrintNumberByStyle(*(_US64 *)ptr_exp, style);
788 UnityPrint(UnityStrWas);
789 UnityPrintNumberByStyle(*(_US64 *)ptr_act, style);
790 UnityAddMsgIfSpecified(msg);
791 UNITY_FAIL_AND_BAIL;
792 }
793 ptr_exp += 8;
794 ptr_act += 8;
795 }
796 break;
797 #endif
798 default:
799 while (elements--)
800 {
801 if (*(const _US32 *)ptr_exp != *(const _US32 *)ptr_act)
802 {
803 UnityTestResultsFailBegin(lineNumber);
804 UnityPrint(UnityStrElement);
805 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
806 UnityPrint(UnityStrExpected);
807 UnityPrintNumberByStyle(*(const _US32 *)ptr_exp, style);
808 UnityPrint(UnityStrWas);
809 UnityPrintNumberByStyle(*(const _US32 *)ptr_act, style);
810 UnityAddMsgIfSpecified(msg);
811 UNITY_FAIL_AND_BAIL;
812 }
813 ptr_exp += 4;
814 ptr_act += 4;
815 }
816 break;
817 }
818
819 if (!Unity.CurrentTestFailed)
820 {
821 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_PASS, lineNumber);
822 }
823 }
824
825 //-----------------------------------------------
826 #ifndef UNITY_EXCLUDE_FLOAT
UnityAssertEqualFloatArray(const _UF * expected,const _UF * actual,const _UU32 num_elements,const char * msg,const UNITY_LINE_TYPE lineNumber)827 void UnityAssertEqualFloatArray(
828 const _UF *expected, const _UF *actual, const _UU32 num_elements, const char *msg, const UNITY_LINE_TYPE lineNumber)
829 {
830 _UU32 elements = num_elements;
831 const _UF *ptr_expected = expected;
832 const _UF *ptr_actual = actual;
833 _UF diff, tol;
834
835 UNITY_SKIP_EXECUTION;
836
837 if (elements == 0)
838 {
839 UnityTestResultsFailBegin(lineNumber);
840 UnityPrint(UnityStrPointless);
841 UnityAddMsgIfSpecified(msg);
842 UNITY_FAIL_AND_BAIL;
843 }
844
845 /* type "_US32 const *" to "void const *" (MISRA C 2004 rule 11.2)
846 if (UnityCheckArraysForNull((const void*)expected, (const void*)actual, lineNumber, msg) == 1)
847 */
848 if (expected == NULL)
849 {
850 UnityTestResultsFailBegin(lineNumber);
851 UnityPrint(UnityStrNullPointerForExpected);
852 UnityAddMsgIfSpecified(msg);
853 UNITY_FAIL_AND_BAIL;
854 return;
855 }
856
857 if (actual == NULL)
858 {
859 UnityTestResultsFailBegin(lineNumber);
860 UnityPrint(UnityStrNullPointerForActual);
861 UnityAddMsgIfSpecified(msg);
862 UNITY_FAIL_AND_BAIL;
863 return;
864 }
865
866 while (elements--)
867 {
868 diff = *ptr_expected - *ptr_actual;
869 if (diff < 0.0f)
870 {
871 diff = 0.0f - diff;
872 }
873 tol = UNITY_FLOAT_PRECISION * *ptr_expected;
874 if (tol < 0.0f)
875 {
876 tol = 0.0f - tol;
877 }
878
879 /*
880 Error[Pm046]: floating point values shall not be tested for exact equality or inequality (MISRA C 2004 rule
881 13.3)
882 //This first part of this condition will catch any NaN or Infinite values, but it almost doesn't happen, disable
883 it for MIRSA issue
884 */
885 if (/*(diff * 0.0f != 0.0f) || */ (diff > tol))
886 {
887 UnityTestResultsFailBegin(lineNumber);
888 UnityPrint(UnityStrElement);
889 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
890 #ifdef UNITY_FLOAT_VERBOSE
891 UnityPrint(UnityStrExpected);
892 UnityPrintFloat(*ptr_expected);
893 UnityPrint(UnityStrWas);
894 UnityPrintFloat(*ptr_actual);
895 #else
896 UnityPrint(UnityStrDelta);
897 #endif
898 UnityAddMsgIfSpecified(msg);
899 UNITY_FAIL_AND_BAIL;
900 }
901 ptr_expected++;
902 ptr_actual++;
903 }
904
905 if (!Unity.CurrentTestFailed)
906 {
907 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_PASS, lineNumber);
908 }
909 }
910
911 //-----------------------------------------------
UnityAssertFloatsWithin(const _UF delta,const _UF expected,const _UF actual,const char * msg,const UNITY_LINE_TYPE lineNumber)912 void UnityAssertFloatsWithin(
913 const _UF delta, const _UF expected, const _UF actual, const char *msg, const UNITY_LINE_TYPE lineNumber)
914 {
915 _UF diff = actual - expected;
916 _UF pos_delta = delta;
917
918 UNITY_SKIP_EXECUTION;
919
920 if (diff < 0.0f)
921 {
922 diff = 0.0f - diff;
923 }
924 if (pos_delta < 0.0f)
925 {
926 pos_delta = 0.0f - pos_delta;
927 }
928
929 /*
930 Error[Pm046]: floating point values shall not be tested for exact equality or inequality (MISRA C 2004 rule 13.3)
931 //This first part of this condition will catch any NaN or Infinite values, but it almost doesn't happen, disable it
932 for MIRSA issue
933 */
934 if (/*(diff * 0.0f != 0.0f) || */ (pos_delta < diff))
935 {
936 UnityTestResultsFailBegin(lineNumber);
937 #ifdef UNITY_FLOAT_VERBOSE
938 UnityPrint(UnityStrExpected);
939 UnityPrintFloat(expected);
940 UnityPrint(UnityStrWas);
941 UnityPrintFloat(actual);
942 #else
943 UnityPrint(UnityStrDelta);
944 #endif
945 UnityAddMsgIfSpecified(msg);
946 UNITY_FAIL_AND_BAIL;
947 }
948 else
949 {
950 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_PASS, lineNumber);
951 }
952 }
953
954 /*
955 Error[Pm046]: floating point values shall not be tested for exact equality or inequality (MISRA C 2004 rule 13.3)
956 //-----------------------------------------------
957 void UnityAssertFloatIsInf(const _UF actual,
958 const char* msg,
959 const UNITY_LINE_TYPE lineNumber)
960 {
961 UNITY_SKIP_EXECUTION;
962
963 // In Microsoft Visual C++ Express Edition 2008,
964 // if ((1.0f / f_zero) != actual)
965 // produces
966 // error C2124: divide or mod by zero
967 // As a workaround, place 0 into a variable.
968 if ((1.0f / f_zero) != actual)
969 {
970 UnityTestResultsFailBegin(lineNumber);
971 #ifdef UNITY_FLOAT_VERBOSE
972 UnityPrint(UnityStrExpected);
973 UnityPrint(UnityStrInf);
974 UnityPrint(UnityStrWas);
975 UnityPrintFloat(actual);
976 #else
977 UnityPrint(UnityStrDelta);
978 #endif
979 UnityAddMsgIfSpecified(msg);
980 UNITY_FAIL_AND_BAIL;
981 } else
982 {
983 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_PASS, lineNumber);
984 }
985 }
986
987 //-----------------------------------------------
988 void UnityAssertFloatIsNegInf(const _UF actual,
989 const char* msg,
990 const UNITY_LINE_TYPE lineNumber)
991 {
992 UNITY_SKIP_EXECUTION;
993
994 // The rationale for not using 1.0f/0.0f is given in UnityAssertFloatIsInf's body.
995 if ((-1.0f / f_zero) != actual)
996 {
997 UnityTestResultsFailBegin(lineNumber);
998 #ifdef UNITY_FLOAT_VERBOSE
999 UnityPrint(UnityStrExpected);
1000 UnityPrint(UnityStrNegInf);
1001 UnityPrint(UnityStrWas);
1002 UnityPrintFloat(actual);
1003 #else
1004 UnityPrint(UnityStrDelta);
1005 #endif
1006 UnityAddMsgIfSpecified(msg);
1007 UNITY_FAIL_AND_BAIL;
1008 } else
1009 {
1010 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_PASS, lineNumber);
1011 }
1012 }
1013
1014 //-----------------------------------------------
1015 void UnityAssertFloatIsNaN(const _UF actual,
1016 const char* msg,
1017 const UNITY_LINE_TYPE lineNumber)
1018 {
1019 UNITY_SKIP_EXECUTION;
1020
1021 if (actual == actual)
1022 {
1023 UnityTestResultsFailBegin(lineNumber);
1024 #ifdef UNITY_FLOAT_VERBOSE
1025 UnityPrint(UnityStrExpected);
1026 UnityPrint(UnityStrNaN);
1027 UnityPrint(UnityStrWas);
1028 UnityPrintFloat(actual);
1029 #else
1030 UnityPrint(UnityStrDelta);
1031 #endif
1032 UnityAddMsgIfSpecified(msg);
1033 UNITY_FAIL_AND_BAIL;
1034 } else
1035 {
1036 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_PASS, lineNumber);
1037 }
1038 }
1039 Error[Pm046]: floating point values shall not be tested for exact equality or inequality (MISRA C 2004 rule 13.3)*/
1040
1041 #endif // not UNITY_EXCLUDE_FLOAT
1042
1043 //-----------------------------------------------
1044 #ifndef UNITY_EXCLUDE_DOUBLE
UnityAssertEqualDoubleArray(const _UD * expected,const _UD * actual,const _UU32 num_elements,const char * msg,const UNITY_LINE_TYPE lineNumber)1045 void UnityAssertEqualDoubleArray(
1046 const _UD *expected, const _UD *actual, const _UU32 num_elements, const char *msg, const UNITY_LINE_TYPE lineNumber)
1047 {
1048 _UU32 elements = num_elements;
1049 const _UD *ptr_expected = expected;
1050 const _UD *ptr_actual = actual;
1051 _UD diff, tol;
1052
1053 UNITY_SKIP_EXECUTION;
1054
1055 if (elements == 0)
1056 {
1057 UnityTestResultsFailBegin(lineNumber);
1058 UnityPrint(UnityStrPointless);
1059 UnityAddMsgIfSpecified(msg);
1060 UNITY_FAIL_AND_BAIL;
1061 }
1062
1063 if (UnityCheckArraysForNull((void *)expected, (void *)actual, lineNumber, msg) == 1)
1064 return;
1065
1066 while (elements--)
1067 {
1068 diff = *ptr_expected - *ptr_actual;
1069 if (diff < 0.0)
1070 diff = 0.0 - diff;
1071 tol = UNITY_DOUBLE_PRECISION * *ptr_expected;
1072 if (tol < 0.0)
1073 tol = 0.0 - tol;
1074
1075 // This first part of this condition will catch any NaN or Infinite values
1076 if ((diff * 0.0 != 0.0) || (diff > tol))
1077 {
1078 UnityTestResultsFailBegin(lineNumber);
1079 UnityPrint(UnityStrElement);
1080 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
1081 #ifdef UNITY_DOUBLE_VERBOSE
1082 UnityPrint(UnityStrExpected);
1083 UnityPrintFloat((float)(*ptr_expected));
1084 UnityPrint(UnityStrWas);
1085 UnityPrintFloat((float)(*ptr_actual));
1086 #else
1087 UnityPrint(UnityStrDelta);
1088 #endif
1089 UnityAddMsgIfSpecified(msg);
1090 UNITY_FAIL_AND_BAIL;
1091 }
1092 ptr_expected++;
1093 ptr_actual++;
1094 }
1095
1096 if (!Unity.CurrentTestFailed)
1097 {
1098 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_PASS, lineNumber);
1099 }
1100 }
1101
1102 //-----------------------------------------------
UnityAssertDoublesWithin(const _UD delta,const _UD expected,const _UD actual,const char * msg,const UNITY_LINE_TYPE lineNumber)1103 void UnityAssertDoublesWithin(
1104 const _UD delta, const _UD expected, const _UD actual, const char *msg, const UNITY_LINE_TYPE lineNumber)
1105 {
1106 _UD diff = actual - expected;
1107 _UD pos_delta = delta;
1108
1109 UNITY_SKIP_EXECUTION;
1110
1111 if (diff < 0.0)
1112 {
1113 diff = 0.0 - diff;
1114 }
1115 if (pos_delta < 0.0)
1116 {
1117 pos_delta = 0.0 - pos_delta;
1118 }
1119
1120 // This first part of this condition will catch any NaN or Infinite values
1121 if ((diff * 0.0 != 0.0) || (pos_delta < diff))
1122 {
1123 UnityTestResultsFailBegin(lineNumber);
1124 #ifdef UNITY_DOUBLE_VERBOSE
1125 UnityPrint(UnityStrExpected);
1126 UnityPrintFloat((float)expected);
1127 UnityPrint(UnityStrWas);
1128 UnityPrintFloat((float)actual);
1129 #else
1130 UnityPrint(UnityStrDelta);
1131 #endif
1132 UnityAddMsgIfSpecified(msg);
1133 UNITY_FAIL_AND_BAIL;
1134 }
1135 else
1136 {
1137 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_PASS, lineNumber);
1138 }
1139 }
1140 /*
1141 Error[Pm046]: floating point values shall not be tested for exact equality or inequality (MISRA C 2004 rule 13.3)
1142 //-----------------------------------------------
1143 void UnityAssertDoubleIsInf(const _UD actual,
1144 const char* msg,
1145 const UNITY_LINE_TYPE lineNumber)
1146 {
1147 UNITY_SKIP_EXECUTION;
1148
1149 // The rationale for not using 1.0/0.0 is given in UnityAssertFloatIsInf's body.
1150 if ((1.0 / d_zero) != actual)
1151 {
1152 UnityTestResultsFailBegin(lineNumber);
1153 #ifdef UNITY_DOUBLE_VERBOSE
1154 UnityPrint(UnityStrExpected);
1155 UnityPrint(UnityStrInf);
1156 UnityPrint(UnityStrWas);
1157 UnityPrintFloat((float)actual);
1158 #else
1159 UnityPrint(UnityStrDelta);
1160 #endif
1161 UnityAddMsgIfSpecified(msg);
1162 UNITY_FAIL_AND_BAIL;
1163 }
1164 }
1165
1166 //-----------------------------------------------
1167 void UnityAssertDoubleIsNegInf(const _UD actual,
1168 const char* msg,
1169 const UNITY_LINE_TYPE lineNumber)
1170 {
1171 UNITY_SKIP_EXECUTION;
1172
1173 // The rationale for not using 1.0/0.0 is given in UnityAssertFloatIsInf's body.
1174 if ((-1.0 / d_zero) != actual)
1175 {
1176 UnityTestResultsFailBegin(lineNumber);
1177 #ifdef UNITY_DOUBLE_VERBOSE
1178 UnityPrint(UnityStrExpected);
1179 UnityPrint(UnityStrNegInf);
1180 UnityPrint(UnityStrWas);
1181 UnityPrintFloat((float)actual);
1182 #else
1183 UnityPrint(UnityStrDelta);
1184 #endif
1185 UnityAddMsgIfSpecified(msg);
1186 UNITY_FAIL_AND_BAIL;
1187 }
1188 }
1189
1190 //-----------------------------------------------
1191 void UnityAssertDoubleIsNaN(const _UD actual,
1192 const char* msg,
1193 const UNITY_LINE_TYPE lineNumber)
1194 {
1195 UNITY_SKIP_EXECUTION;
1196
1197 if (actual == actual)
1198 {
1199 UnityTestResultsFailBegin(lineNumber);
1200 #ifdef UNITY_DOUBLE_VERBOSE
1201 UnityPrint(UnityStrExpected);
1202 UnityPrint(UnityStrNaN);
1203 UnityPrint(UnityStrWas);
1204 UnityPrintFloat((float)actual);
1205 #else
1206 UnityPrint(UnityStrDelta);
1207 #endif
1208 UnityAddMsgIfSpecified(msg);
1209 UNITY_FAIL_AND_BAIL;
1210 }
1211 }
1212 */
1213 #endif // not UNITY_EXCLUDE_DOUBLE
1214
1215 //-----------------------------------------------
UnityAssertNumbersWithin(const _U_SINT delta,const _U_SINT expected,const _U_SINT actual,const char * msg,const UNITY_LINE_TYPE lineNumber,const UNITY_DISPLAY_STYLE_T style)1216 void UnityAssertNumbersWithin(const _U_SINT delta,
1217 const _U_SINT expected,
1218 const _U_SINT actual,
1219 const char *msg,
1220 const UNITY_LINE_TYPE lineNumber,
1221 const UNITY_DISPLAY_STYLE_T style)
1222 {
1223 UNITY_SKIP_EXECUTION;
1224
1225 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
1226 {
1227 if (actual > expected)
1228 {
1229 Unity.CurrentTestFailed = ((actual - expected) > delta);
1230 }
1231 else
1232 {
1233 Unity.CurrentTestFailed = ((expected - actual) > delta);
1234 }
1235 }
1236 else
1237 {
1238 if ((_U_UINT)actual > (_U_UINT)expected)
1239 {
1240 Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > (_U_UINT)delta);
1241 }
1242 else
1243 {
1244 Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > (_U_UINT)delta);
1245 }
1246 }
1247
1248 if (Unity.CurrentTestFailed)
1249 {
1250 UnityTestResultsFailBegin(lineNumber);
1251 UnityPrint(UnityStrDelta);
1252 UnityPrintNumberByStyle(delta, style);
1253 UnityPrint(UnityStrExpected);
1254 UnityPrintNumberByStyle(expected, style);
1255 UnityPrint(UnityStrWas);
1256 UnityPrintNumberByStyle(actual, style);
1257 UnityAddMsgIfSpecified(msg);
1258 UNITY_FAIL_AND_BAIL;
1259 }
1260 else
1261 {
1262 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_PASS, lineNumber);
1263 }
1264 }
1265
1266 //-----------------------------------------------
UnityAssertEqualString(const char * expected,const char * actual,const char * msg,const UNITY_LINE_TYPE lineNumber)1267 void UnityAssertEqualString(const char *expected, const char *actual, const char *msg, const UNITY_LINE_TYPE lineNumber)
1268 {
1269 _UU32 i;
1270
1271 UNITY_SKIP_EXECUTION;
1272
1273 // if both pointers not null compare the strings
1274 if (expected && actual)
1275 {
1276 for (i = 0; (expected[i]) || (actual[i]); i++)
1277 {
1278 if (expected[i] != actual[i])
1279 {
1280 Unity.CurrentTestFailed = 1;
1281 break;
1282 }
1283 }
1284 }
1285 else
1286 { // handle case of one pointers being null (if both null, test should pass)
1287 if (expected != actual)
1288 {
1289 Unity.CurrentTestFailed = 1;
1290 }
1291 }
1292
1293 if (Unity.CurrentTestFailed)
1294 {
1295 UnityTestResultsFailBegin(lineNumber);
1296 UnityPrintExpectedAndActualStrings(expected, actual);
1297 UnityAddMsgIfSpecified(msg);
1298 UNITY_FAIL_AND_BAIL;
1299 }
1300 else
1301 {
1302 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_PASS, lineNumber);
1303 }
1304 }
1305
1306 //-----------------------------------------------
UnityAssertEqualStringArray(const char ** expected,const char ** actual,const _UU32 num_elements,const char * msg,const UNITY_LINE_TYPE lineNumber)1307 void UnityAssertEqualStringArray(const char **expected,
1308 const char **actual,
1309 const _UU32 num_elements,
1310 const char *msg,
1311 const UNITY_LINE_TYPE lineNumber)
1312 {
1313 _UU32 i, j = 0;
1314
1315 UNITY_SKIP_EXECUTION;
1316
1317 // if no elements, it's an error
1318 if (num_elements == 0)
1319 {
1320 UnityTestResultsFailBegin(lineNumber);
1321 UnityPrint(UnityStrPointless);
1322 UnityAddMsgIfSpecified(msg);
1323 UNITY_FAIL_AND_BAIL;
1324 }
1325
1326 if (UnityCheckArraysForNull((void *)expected, (void *)actual, lineNumber, msg) == 1)
1327 {
1328 return;
1329 }
1330 do
1331 {
1332 // if both pointers not null compare the strings
1333 if ((expected[j]) && (actual[j]))
1334 {
1335 for (i = 0; (expected[j][i]) || (actual[j][i]); i++)
1336 {
1337 if (expected[j][i] != actual[j][i])
1338 {
1339 Unity.CurrentTestFailed = 1;
1340 break;
1341 }
1342 }
1343 }
1344 else
1345 { // handle case of one pointers being null (if both null, test should pass)
1346 if (expected[j] != actual[j])
1347 {
1348 Unity.CurrentTestFailed = 1;
1349 }
1350 }
1351
1352 if (Unity.CurrentTestFailed)
1353 {
1354 UnityTestResultsFailBegin(lineNumber);
1355 if (num_elements > 1)
1356 {
1357 UnityPrint(UnityStrElement);
1358 UnityPrintNumberByStyle((j), UNITY_DISPLAY_STYLE_UINT);
1359 }
1360 UnityPrintExpectedAndActualStrings((const char *)(expected[j]), (const char *)(actual[j]));
1361 UnityAddMsgIfSpecified(msg);
1362 UNITY_FAIL_AND_BAIL;
1363 }
1364 } while (++j < num_elements);
1365
1366 if (!Unity.CurrentTestFailed)
1367 {
1368 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_PASS, lineNumber);
1369 }
1370 }
1371
1372 //-----------------------------------------------
UnityAssertEqualMemory(unsigned char * expected,unsigned char * actual,const _UU32 length,const _UU32 num_elements,const char * msg,const UNITY_LINE_TYPE lineNumber)1373 void UnityAssertEqualMemory(unsigned char *expected,
1374 unsigned char *actual,
1375 const _UU32 length,
1376 const _UU32 num_elements,
1377 const char *msg,
1378 const UNITY_LINE_TYPE lineNumber)
1379 {
1380 /* type "void const *" to "unsigned char *" (MISRA C 2004 rule 11.2)*/
1381 unsigned char *ptr_exp = expected;
1382 unsigned char *ptr_act = actual;
1383 _UU32 elements = num_elements;
1384 _UU32 bytes;
1385
1386 UNITY_SKIP_EXECUTION;
1387
1388 if ((elements == 0) || (length == 0))
1389 {
1390 UnityTestResultsFailBegin(lineNumber);
1391 UnityPrint(UnityStrPointless);
1392 UnityAddMsgIfSpecified(msg);
1393 UNITY_FAIL_AND_BAIL;
1394 }
1395
1396 /* type "_US32 const *" to "void const *" (MISRA C 2004 rule 11.2)
1397 if (UnityCheckArraysForNull((const void*)expected, (const void*)actual, lineNumber, msg) == 1)
1398 */
1399 if (expected == NULL)
1400 {
1401 UnityTestResultsFailBegin(lineNumber);
1402 UnityPrint(UnityStrNullPointerForExpected);
1403 UnityAddMsgIfSpecified(msg);
1404 UNITY_FAIL_AND_BAIL;
1405 return;
1406 }
1407
1408 if (actual == NULL)
1409 {
1410 UnityTestResultsFailBegin(lineNumber);
1411 UnityPrint(UnityStrNullPointerForActual);
1412 UnityAddMsgIfSpecified(msg);
1413 UNITY_FAIL_AND_BAIL;
1414 return;
1415 }
1416
1417 while (elements--)
1418 {
1419 /////////////////////////////////////
1420 bytes = length;
1421 while (bytes--)
1422 {
1423 if (*ptr_exp != *ptr_act)
1424 {
1425 UnityTestResultsFailBegin(lineNumber);
1426 UnityPrint(UnityStrMemory);
1427 if (num_elements > 1)
1428 {
1429 UnityPrint(UnityStrElement);
1430 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
1431 }
1432 UnityPrint(UnityStrByte);
1433 UnityPrintNumberByStyle((length - bytes - 1), UNITY_DISPLAY_STYLE_UINT);
1434 UnityPrint(UnityStrExpected);
1435 UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
1436 UnityPrint(UnityStrWas);
1437 UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
1438 UnityAddMsgIfSpecified(msg);
1439 UNITY_FAIL_AND_BAIL;
1440 }
1441 ptr_exp += 1;
1442 ptr_act += 1;
1443 }
1444 /////////////////////////////////////
1445 }
1446
1447 if (!Unity.CurrentTestFailed)
1448 {
1449 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_PASS, lineNumber);
1450 }
1451 }
1452
1453 //-----------------------------------------------
1454 // Control Functions
1455 //-----------------------------------------------
1456
UnityFail(const char * msg,const UNITY_LINE_TYPE line)1457 void UnityFail(const char *msg, const UNITY_LINE_TYPE line)
1458 {
1459 UNITY_SKIP_EXECUTION;
1460 Unity.CurrentTestFailed = 1;
1461 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_FAIL, line);
1462 UnityDumpCaseResult(DUMP_RESULT_TEST_RESULT_FAIL, line);
1463 UnityTestResultsBegin(Unity.TestFile, line);
1464 UnityPrintFail();
1465 if (msg != NULL)
1466 {
1467 UNITY_OUTPUT_CHAR(':');
1468 if (msg[0] != ' ')
1469 {
1470 UNITY_OUTPUT_CHAR(' ');
1471 }
1472 UnityPrint(msg);
1473 }
1474 UNITY_FAIL_AND_BAIL;
1475 }
1476
1477 //-----------------------------------------------
UnityIgnore(const char * msg,const UNITY_LINE_TYPE line)1478 void UnityIgnore(const char *msg, const UNITY_LINE_TYPE line)
1479 {
1480 UNITY_SKIP_EXECUTION;
1481 Unity.CurrentTestIgnored = 1;
1482 UnityDumpAssertResult(DUMP_RESULT_TEST_RESULT_IGNORE, line);
1483 UnityDumpCaseResult(DUMP_RESULT_TEST_RESULT_IGNORE, line);
1484 UnityTestResultsBegin(Unity.TestFile, line);
1485 UnityPrint("IGNORE");
1486 if (msg != NULL)
1487 {
1488 UNITY_OUTPUT_CHAR(':');
1489 UNITY_OUTPUT_CHAR(' ');
1490 UnityPrint(msg);
1491 }
1492 UNITY_IGNORE_AND_BAIL;
1493 }
1494
1495 //-----------------------------------------------
1496 #ifdef UNITY_SHOW_TEST_FILE_PATH
UnityDefaultTestRun(UnityTestSetUpFunction SetUpFunc,UnityTestFunction Func,UnityTestTearDownFunction TearDownFunc,const char * FuncName,const int CaseId,const char * TestFile)1497 void UnityDefaultTestRun(UnityTestSetUpFunction SetUpFunc,
1498 UnityTestFunction Func,
1499 UnityTestTearDownFunction TearDownFunc,
1500 const char *FuncName,
1501 const int CaseId,
1502 const char *TestFile)
1503 {
1504 Unity.TestFile = TestFile;
1505 #else
1506 void UnityDefaultTestRun(UnityTestSetUpFunction SetUpFunc,
1507 UnityTestFunction Func,
1508 UnityTestTearDownFunction TearDownFunc,
1509 const char *FuncName,
1510 const int CaseId)
1511 {
1512 #endif
1513 Unity.CurrentTestName = FuncName;
1514 Unity.CurrentTestId = CaseId;
1515 Unity.CurrentTestLineNumber = 0;
1516
1517 Unity.NumberOfTests++;
1518 Unity.ContinueCurTestIfAssertFail = IS_CONTINUE_CUR_TEST_IF_ASSERT_FAIL;
1519 if (TEST_PROTECT())
1520 {
1521 if (SetUpFunc != NULL)
1522 {
1523 SetUpFunc();
1524 }
1525 Func();
1526 }
1527 if (TEST_PROTECT())
1528 {
1529 if (TearDownFunc != NULL)
1530 {
1531 TearDownFunc();
1532 }
1533 }
1534 UnityConcludeTest();
1535 }
1536
1537 //-----------------------------------------------
1538 void UnityBegin(void)
1539 {
1540 Unity.NumberOfTests = 0;
1541 Unity.TestFailures = 0;
1542 Unity.TestIgnores = 0;
1543 Unity.CurrentTestFailed = 0;
1544 Unity.CurrentTestIgnored = 0;
1545
1546 #ifdef __COVERAGESCANNER__
1547 /* Define the Squish Coco set of I/O functions */
1548 #ifdef SQUISHCOCO_RESULT_DATA_SAVE_TO_FILE
1549 __coveragescanner_set_custom_io(NULL, csfputs, csfopenappend, NULL, csfopenwrite, csfclose, NULL);
1550 #else
1551 __coveragescanner_set_custom_io(NULL, csfputs, csfopenappend, NULL, NULL, csfclose, NULL);
1552 #endif /* SQUISHCOCO_RESULT_DATA_SAVE_TO_FILE */
1553 #endif /*__COVERAGESCANNER__*/
1554
1555 #if GCOV_DO_COVERAGE
1556 /* Currently, only MCUXPresso support this function. */
1557 #if (defined(__GNUC__) && defined(__MCUXPRESSO))
1558 #if defined(__REDLIB__)
1559 #error "gcov not supported with RedLib"
1560 #else
1561 gcov_init(); /* do *not* call this for redlib as it does not implement constructors! */
1562 #endif
1563 if (!gcov_check())
1564 {
1565 printf("WARNING: writing coverage does not work! Wrong library used?\n");
1566 }
1567 #endif /* MCUXPresso IDE used. */
1568 #endif
1569 }
1570
1571 //-----------------------------------------------
1572 int UnityEnd(void)
1573 {
1574 UnityPrint("-----------------------");
1575 UNITY_PRINT_EOL;
1576 UnityPrintNumber(Unity.NumberOfTests);
1577 UnityPrint(" Tests ");
1578 UnityPrintNumber(Unity.TestFailures);
1579 UnityPrint(" Failures ");
1580 UnityPrintNumber(Unity.TestIgnores);
1581 UnityPrint(" Ignored");
1582 UNITY_PRINT_EOL;
1583 if (Unity.TestFailures == 0U)
1584 {
1585 UnityPrintOk();
1586 #ifdef __COVERAGESCANNER__
1587 /* Set the state of the current test */
1588 __coveragescanner_teststate("PASSED");
1589 #endif /*__COVERAGESCANNER__*/
1590 }
1591 else
1592 {
1593 UnityPrintFail();
1594 #ifdef __COVERAGESCANNER__
1595 /* Set the state of the current test */
1596 __coveragescanner_teststate("FAILED");
1597 #endif /*__COVERAGESCANNER__*/
1598 }
1599 UNITY_PRINT_EOL;
1600
1601 #ifdef UNITY_DUMP_RESULT
1602 #if !defined(UNITY_DUMP_COMPATIBLE_WITH_EU)
1603 #if defined UNITY_DUMP_CASE_RESULT_ONLY
1604 UnityDumpFillResult(DUMP_RESULT_TEST_RESULT_SUMMARY, Unity.NumberOfTests, CASE_NUM_IN_BYTE);
1605 #else
1606 if (g_dumpResultArrayIndex < DUMP_RESULT_ARRAY_SIZE)
1607 {
1608 /*Last index to record the test summary, how many cases failed totally*/
1609 DumpTestDataT *pData = g_dumpResultArray + g_dumpResultArrayIndex;
1610 pData->testSummary.testResultType = DUMP_RESULT_TEST_RESULT_SUMMARY;
1611 pData->testSummary.testTotalNum = Unity.NumberOfTests;
1612 pData->testSummary.testFailNum = Unity.TestFailures;
1613 pData->testSummary.testIgnoreNum = Unity.TestIgnores;
1614 g_dumpResultArrayIndex++;
1615 }
1616 #endif
1617 #endif
1618 #endif
1619
1620 #if defined(GCOV_DO_COVERAGE) && defined(__GNUC__)
1621 gcov_write(); /* write coverage files */
1622 #endif
1623
1624 #ifdef UNITY_DUMP_RESULT
1625 UnityMemDumpEntry();
1626 #endif
1627
1628 #ifdef __COVERAGESCANNER__
1629 //__coveragescanner_testname(Unity.CurrentTestName);
1630 /* Save the execution report and reset the status of all instrumentations */
1631 __coveragescanner_save();
1632 #if defined(SQUISHCOCO_RESULT_DATA_SAVE_TO_MEMORY)
1633 ((uint32_t *)mem_to_store_coverage_results)[0] = mem_offset - 0x10;
1634 #endif
1635 #endif /*__COVERAGESCANNER__*/
1636
1637 /*do not return for MCU*/
1638 // while(1);
1639 return Unity.TestFailures;
1640 }
1641
1642 #ifdef UNITY_DUMP_RESULT
1643 void UnityMemDumpEntry(void)
1644 {
1645 UnityPrint("-----------------------");
1646 UNITY_PRINT_EOL;
1647 UnityPrint("Success to dump test result!\r\n");
1648
1649 #ifdef UNITY_DUMP_RESULT_LOG
1650 #if defined(UNITY_DUMP_CASE_RESULT_ONLY) || defined(UNITY_DUMP_COMPATIBLE_WITH_EU)
1651 for (int i = 0; i < DUMP_RESULT_ARRAY_SIZE; i++)
1652 {
1653 UnityPrintNumberByStyle(g_dumpResultArray[i], UNITY_DISPLAY_STYLE_HEX8);
1654 UnityPrint("\r\n");
1655 }
1656 #else
1657 for (int i = 0; i < g_dumpResultArrayIndex; i++)
1658 {
1659 if ((g_dumpResultArrayIndex - 1) == i)
1660 {
1661 UnityPrint("\r\nsummary data: ");
1662 UnityPrintNumberByStyle(g_dumpResultArray[i].testResultData, UNITY_DISPLAY_STYLE_HEX32);
1663
1664 UnityPrint(", total: ");
1665 UnityPrintNumberUnsigned(g_dumpResultArray[i].testSummary.testTotalNum);
1666
1667 UnityPrint(", fail: ");
1668 UnityPrintNumberUnsigned(g_dumpResultArray[i].testSummary.testFailNum);
1669
1670 UnityPrint(", ignore: ");
1671 UnityPrintNumberUnsigned(g_dumpResultArray[i].testSummary.testIgnoreNum);
1672 }
1673 else
1674 {
1675 UnityPrint("\r\ntest case data: ");
1676 UnityPrintNumberByStyle(g_dumpResultArray[i].testResultData, UNITY_DISPLAY_STYLE_HEX32);
1677
1678 UnityPrint(", resultType: ");
1679 UnityPrintNumberUnsigned(g_dumpResultArray[i].testCaseResult.testResultType);
1680
1681 UnityPrint(", testId: ");
1682 UnityPrintNumberUnsigned(g_dumpResultArray[i].testCaseResult.testID);
1683
1684 UnityPrint(", testFailIgnoreLine: ");
1685 UnityPrintNumberUnsigned(g_dumpResultArray[i].testCaseResult.testFailIgnoreLine);
1686 }
1687 }
1688 #endif
1689 #endif
1690 }
1691 #endif
1692
1693 void UnityContinueCurTestIfAssertFail(int enabled)
1694 {
1695 Unity.ContinueCurTestIfAssertFail = enabled;
1696 }
1697
1698 #ifdef UNITY_DUMP_RESULT
1699 #if defined(UNITY_DUMP_COMPATIBLE_WITH_EU) || defined(UNITY_DUMP_CASE_RESULT_ONLY)
1700 // common function for fill the test result in bytes
1701 static void UnityDumpFillResult(const _UU8 result, _U_UINT id, _U_UINT numInByte)
1702 {
1703 _UU16 dumpResultIndex = id / numInByte;
1704 if (dumpResultIndex < DUMP_RESULT_ARRAY_SIZE)
1705 {
1706 // 8 bit _UU8 variable stores 4 asserts/cases result, from high to low
1707 // assert/case id -- 0, stored in bits 8~7, margintRight is 6
1708 // assert/case id -- 1, stored in bits 6~5, margintRight is 4
1709 // assert/case id -- 2, stored in bits 4~3, marginRight is 2
1710 // assert/case id -- 3, stored in bits 2~1, marginRight is 0
1711
1712 _UU8 marginRight = DUMP_RESULT_BIT_NUM * (numInByte - 1) - ((id % numInByte) << 1);
1713
1714 // get the old result, check whether it is failed, don't set pass/ignore if it has failed.
1715 if (((g_dumpResultArray[dumpResultIndex] >> marginRight) & DUMP_RESULT_TEST_RESULT_MASK) !=
1716 DUMP_RESULT_TEST_RESULT_FAIL)
1717 {
1718 // clear the old result
1719 g_dumpResultArray[dumpResultIndex] &= ((_UU8)(~((_UU8)(DUMP_RESULT_TEST_RESULT_MASK << marginRight))));
1720 // set result
1721 g_dumpResultArray[dumpResultIndex] |= ((_UU8)(result << marginRight));
1722 }
1723 }
1724 }
1725 #endif
1726 #endif
1727
1728 #ifdef UNITY_DUMP_RESULT
1729 #if defined(UNITY_DUMP_COMPATIBLE_WITH_EU)
1730 void UnityDumpAssertResult(const _UU8 result, const UNITY_LINE_TYPE lineNumber)
1731 {
1732 // record assert result
1733 UnityDumpFillResult(resut, s_IndepAssertId, EU_COUNT_RESETS);
1734 }
1735
1736 void setIndepAssertId(const UNITY_LINE_TYPE indepAssertId)
1737 {
1738 s_IndepAssertId = indepAssertId;
1739 }
1740 #elif defined(UNITY_DUMP_CASE_RESULT_ONLY)
1741 void UnityDumpCaseResult(const _UU8 result, const UNITY_LINE_TYPE lineNumber)
1742 {
1743 // record case result
1744 UnityDumpFillResult(result, (Unity.NumberOfTests - 1), CASE_NUM_IN_BYTE);
1745 }
1746 #else
1747 void UnityDumpCaseResult(const _UU8 result, const UNITY_LINE_TYPE lineNumber)
1748 {
1749 // record all test case result
1750 #ifdef UNMITY_NOT_DUMP_PASS_RESULT
1751 if (DUMP_RESULT_TEST_RESULT_PASS == result)
1752 {
1753 return;
1754 }
1755 #endif
1756
1757 // the last one store the test summary
1758 if (g_dumpResultArrayIndex < (DUMP_RESULT_ARRAY_SIZE - 1))
1759 {
1760 DumpTestDataT *pData = g_dumpResultArray + g_dumpResultArrayIndex;
1761 pData->testCaseResult.testResultType = result;
1762 pData->testCaseResult.testID = Unity.CurrentTestId;
1763 pData->testCaseResult.testFailIgnoreLine = lineNumber;
1764 g_dumpResultArrayIndex++;
1765 }
1766 }
1767 #endif
1768 #endif
1769