1 /* =========================================================================
2 Unity Project - A Test Framework for C
3 Copyright (c) 2007-21 Mike Karlesky, Mark VanderVoord, Greg Williams
4 [Released under MIT License. Please refer to license.txt for details]
5 ============================================================================ */
6 #if LV_BUILD_TEST
7 #define UNITY_INCLUDE_PRINT_FORMATTED 1
8 #include "unity.h"
9 #include <stddef.h>
10
11 #ifdef AVR
12 #include <avr/pgmspace.h>
13 #else
14 #define PROGMEM
15 #endif
16
17 /* If omitted from header, declare overrideable prototypes here so they're ready for use */
18 #ifdef UNITY_OMIT_OUTPUT_CHAR_HEADER_DECLARATION
19 void UNITY_OUTPUT_CHAR(int);
20 #endif
21
22 /* Helpful macros for us to use here in Assert functions */
23 #define UNITY_FAIL_AND_BAIL { Unity.CurrentTestFailed = 1; UNITY_OUTPUT_FLUSH(); TEST_ABORT(); }
24 #define UNITY_IGNORE_AND_BAIL { Unity.CurrentTestIgnored = 1; UNITY_OUTPUT_FLUSH(); TEST_ABORT(); }
25 #define RETURN_IF_FAIL_OR_IGNORE if (Unity.CurrentTestFailed || Unity.CurrentTestIgnored) TEST_ABORT()
26
27 struct UNITY_STORAGE_T Unity;
28
29 #ifdef UNITY_OUTPUT_COLOR
30 const char PROGMEM UnityStrOk[] = "\033[42mOK\033[00m";
31 const char PROGMEM UnityStrPass[] = "\033[42mPASS\033[00m";
32 const char PROGMEM UnityStrFail[] = "\033[41mFAIL\033[00m";
33 const char PROGMEM UnityStrIgnore[] = "\033[43mIGNORE\033[00m";
34 #else
35 const char PROGMEM UnityStrOk[] = "OK";
36 const char PROGMEM UnityStrPass[] = "PASS";
37 const char PROGMEM UnityStrFail[] = "FAIL";
38 const char PROGMEM UnityStrIgnore[] = "IGNORE";
39 #endif
40 static const char PROGMEM UnityStrNull[] = "NULL";
41 static const char PROGMEM UnityStrSpacer[] = ". ";
42 static const char PROGMEM UnityStrExpected[] = " Expected ";
43 static const char PROGMEM UnityStrWas[] = " Was ";
44 static const char PROGMEM UnityStrGt[] = " to be greater than ";
45 static const char PROGMEM UnityStrLt[] = " to be less than ";
46 static const char PROGMEM UnityStrOrEqual[] = "or equal to ";
47 static const char PROGMEM UnityStrNotEqual[] = " to be not equal to ";
48 static const char PROGMEM UnityStrElement[] = " Element ";
49 static const char PROGMEM UnityStrByte[] = " Byte ";
50 static const char PROGMEM UnityStrMemory[] = " Memory Mismatch.";
51 static const char PROGMEM UnityStrDelta[] = " Values Not Within Delta ";
52 static const char PROGMEM UnityStrPointless[] = " You Asked Me To Compare Nothing, Which Was Pointless.";
53 static const char PROGMEM UnityStrNullPointerForExpected[] = " Expected pointer to be NULL";
54 static const char PROGMEM UnityStrNullPointerForActual[] = " Actual pointer was NULL";
55 #ifndef UNITY_EXCLUDE_FLOAT
56 static const char PROGMEM UnityStrNot[] = "Not ";
57 static const char PROGMEM UnityStrInf[] = "Infinity";
58 static const char PROGMEM UnityStrNegInf[] = "Negative Infinity";
59 static const char PROGMEM UnityStrNaN[] = "NaN";
60 static const char PROGMEM UnityStrDet[] = "Determinate";
61 static const char PROGMEM UnityStrInvalidFloatTrait[] = "Invalid Float Trait";
62 #endif
63 const char PROGMEM UnityStrErrShorthand[] = "Unity Shorthand Support Disabled";
64 const char PROGMEM UnityStrErrFloat[] = "Unity Floating Point Disabled";
65 const char PROGMEM UnityStrErrDouble[] = "Unity Double Precision Disabled";
66 const char PROGMEM UnityStrErr64[] = "Unity 64-bit Support Disabled";
67 static const char PROGMEM UnityStrBreaker[] = "-----------------------";
68 static const char PROGMEM UnityStrResultsTests[] = " Tests ";
69 static const char PROGMEM UnityStrResultsFailures[] = " Failures ";
70 static const char PROGMEM UnityStrResultsIgnored[] = " Ignored ";
71 #ifndef UNITY_EXCLUDE_DETAILS
72 static const char PROGMEM UnityStrDetail1Name[] = UNITY_DETAIL1_NAME " ";
73 static const char PROGMEM UnityStrDetail2Name[] = " " UNITY_DETAIL2_NAME " ";
74 #endif
75 /*-----------------------------------------------
76 * Pretty Printers & Test Result Output Handlers
77 *-----------------------------------------------*/
78
79 /*-----------------------------------------------*/
80 /* Local helper function to print characters. */
UnityPrintChar(const char * pch)81 static void UnityPrintChar(const char* pch)
82 {
83 /* printable characters plus CR & LF are printed */
84 if ((*pch <= 126) && (*pch >= 32))
85 {
86 UNITY_OUTPUT_CHAR(*pch);
87 }
88 /* write escaped carriage returns */
89 else if (*pch == 13)
90 {
91 UNITY_OUTPUT_CHAR('\\');
92 UNITY_OUTPUT_CHAR('r');
93 }
94 /* write escaped line feeds */
95 else if (*pch == 10)
96 {
97 UNITY_OUTPUT_CHAR('\\');
98 UNITY_OUTPUT_CHAR('n');
99 }
100 /* unprintable characters are shown as codes */
101 else
102 {
103 UNITY_OUTPUT_CHAR('\\');
104 UNITY_OUTPUT_CHAR('x');
105 UnityPrintNumberHex((UNITY_UINT)*pch, 2);
106 }
107 }
108
109 /*-----------------------------------------------*/
110 /* Local helper function to print ANSI escape strings e.g. "\033[42m". */
111 #ifdef UNITY_OUTPUT_COLOR
UnityPrintAnsiEscapeString(const char * string)112 static UNITY_UINT UnityPrintAnsiEscapeString(const char* string)
113 {
114 const char* pch = string;
115 UNITY_UINT count = 0;
116
117 while (*pch && (*pch != 'm'))
118 {
119 UNITY_OUTPUT_CHAR(*pch);
120 pch++;
121 count++;
122 }
123 UNITY_OUTPUT_CHAR('m');
124 count++;
125
126 return count;
127 }
128 #endif
129
130 /*-----------------------------------------------*/
UnityPrint(const char * string)131 void UnityPrint(const char* string)
132 {
133 const char* pch = string;
134
135 if (pch != NULL)
136 {
137 while (*pch)
138 {
139 #ifdef UNITY_OUTPUT_COLOR
140 /* print ANSI escape code */
141 if ((*pch == 27) && (*(pch + 1) == '['))
142 {
143 pch += UnityPrintAnsiEscapeString(pch);
144 continue;
145 }
146 #endif
147 UnityPrintChar(pch);
148 pch++;
149 }
150 }
151 }
152 /*-----------------------------------------------*/
UnityPrintLen(const char * string,const UNITY_UINT32 length)153 void UnityPrintLen(const char* string, const UNITY_UINT32 length)
154 {
155 const char* pch = string;
156
157 if (pch != NULL)
158 {
159 while (*pch && ((UNITY_UINT32)(pch - string) < length))
160 {
161 /* printable characters plus CR & LF are printed */
162 if ((*pch <= 126) && (*pch >= 32))
163 {
164 UNITY_OUTPUT_CHAR(*pch);
165 }
166 /* write escaped carriage returns */
167 else if (*pch == 13)
168 {
169 UNITY_OUTPUT_CHAR('\\');
170 UNITY_OUTPUT_CHAR('r');
171 }
172 /* write escaped line feeds */
173 else if (*pch == 10)
174 {
175 UNITY_OUTPUT_CHAR('\\');
176 UNITY_OUTPUT_CHAR('n');
177 }
178 /* unprintable characters are shown as codes */
179 else
180 {
181 UNITY_OUTPUT_CHAR('\\');
182 UNITY_OUTPUT_CHAR('x');
183 UnityPrintNumberHex((UNITY_UINT)*pch, 2);
184 }
185 pch++;
186 }
187 }
188 }
189
190 /*-----------------------------------------------*/
UnityPrintNumberByStyle(const UNITY_INT number,const UNITY_DISPLAY_STYLE_T style)191 void UnityPrintNumberByStyle(const UNITY_INT number, const UNITY_DISPLAY_STYLE_T style)
192 {
193 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
194 {
195 if (style == UNITY_DISPLAY_STYLE_CHAR)
196 {
197 /* printable characters plus CR & LF are printed */
198 UNITY_OUTPUT_CHAR('\'');
199 if ((number <= 126) && (number >= 32))
200 {
201 UNITY_OUTPUT_CHAR((int)number);
202 }
203 /* write escaped carriage returns */
204 else if (number == 13)
205 {
206 UNITY_OUTPUT_CHAR('\\');
207 UNITY_OUTPUT_CHAR('r');
208 }
209 /* write escaped line feeds */
210 else if (number == 10)
211 {
212 UNITY_OUTPUT_CHAR('\\');
213 UNITY_OUTPUT_CHAR('n');
214 }
215 /* unprintable characters are shown as codes */
216 else
217 {
218 UNITY_OUTPUT_CHAR('\\');
219 UNITY_OUTPUT_CHAR('x');
220 UnityPrintNumberHex((UNITY_UINT)number, 2);
221 }
222 UNITY_OUTPUT_CHAR('\'');
223 }
224 else
225 {
226 UnityPrintNumber(number);
227 }
228 }
229 else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
230 {
231 UnityPrintNumberUnsigned((UNITY_UINT)number);
232 }
233 else
234 {
235 UNITY_OUTPUT_CHAR('0');
236 UNITY_OUTPUT_CHAR('x');
237 UnityPrintNumberHex((UNITY_UINT)number, (char)((style & 0xF) * 2));
238 }
239 }
240
241 /*-----------------------------------------------*/
UnityPrintNumber(const UNITY_INT number_to_print)242 void UnityPrintNumber(const UNITY_INT number_to_print)
243 {
244 UNITY_UINT number = (UNITY_UINT)number_to_print;
245
246 if (number_to_print < 0)
247 {
248 /* A negative number, including MIN negative */
249 UNITY_OUTPUT_CHAR('-');
250 number = (~number) + 1;
251 }
252 UnityPrintNumberUnsigned(number);
253 }
254
255 /*-----------------------------------------------
256 * basically do an itoa using as little ram as possible */
UnityPrintNumberUnsigned(const UNITY_UINT number)257 void UnityPrintNumberUnsigned(const UNITY_UINT number)
258 {
259 UNITY_UINT divisor = 1;
260
261 /* figure out initial divisor */
262 while (number / divisor > 9)
263 {
264 divisor *= 10;
265 }
266
267 /* now mod and print, then divide divisor */
268 do
269 {
270 UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
271 divisor /= 10;
272 } while (divisor > 0);
273 }
274
275 /*-----------------------------------------------*/
UnityPrintNumberHex(const UNITY_UINT number,const char nibbles_to_print)276 void UnityPrintNumberHex(const UNITY_UINT number, const char nibbles_to_print)
277 {
278 int nibble;
279 char nibbles = nibbles_to_print;
280
281 if ((unsigned)nibbles > UNITY_MAX_NIBBLES)
282 {
283 nibbles = UNITY_MAX_NIBBLES;
284 }
285
286 while (nibbles > 0)
287 {
288 nibbles--;
289 nibble = (int)(number >> (nibbles * 4)) & 0x0F;
290 if (nibble <= 9)
291 {
292 UNITY_OUTPUT_CHAR((char)('0' + nibble));
293 }
294 else
295 {
296 UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble));
297 }
298 }
299 }
300
301 /*-----------------------------------------------*/
UnityPrintMask(const UNITY_UINT mask,const UNITY_UINT number)302 void UnityPrintMask(const UNITY_UINT mask, const UNITY_UINT number)
303 {
304 UNITY_UINT current_bit = (UNITY_UINT)1 << (UNITY_INT_WIDTH - 1);
305 UNITY_INT32 i;
306
307 for (i = 0; i < UNITY_INT_WIDTH; i++)
308 {
309 if (current_bit & mask)
310 {
311 if (current_bit & number)
312 {
313 UNITY_OUTPUT_CHAR('1');
314 }
315 else
316 {
317 UNITY_OUTPUT_CHAR('0');
318 }
319 }
320 else
321 {
322 UNITY_OUTPUT_CHAR('X');
323 }
324 current_bit = current_bit >> 1;
325 }
326 }
327
328 /*-----------------------------------------------*/
329 #ifndef UNITY_EXCLUDE_FLOAT_PRINT
330 /*
331 * This function prints a floating-point value in a format similar to
332 * printf("%.7g") on a single-precision machine or printf("%.9g") on a
333 * double-precision machine. The 7th digit won't always be totally correct
334 * in single-precision operation (for that level of accuracy, a more
335 * complicated algorithm would be needed).
336 */
UnityPrintFloat(const UNITY_DOUBLE input_number)337 void UnityPrintFloat(const UNITY_DOUBLE input_number)
338 {
339 #ifdef UNITY_INCLUDE_DOUBLE
340 static const int sig_digits = 9;
341 static const UNITY_INT32 min_scaled = 100000000;
342 static const UNITY_INT32 max_scaled = 1000000000;
343 #else
344 static const int sig_digits = 7;
345 static const UNITY_INT32 min_scaled = 1000000;
346 static const UNITY_INT32 max_scaled = 10000000;
347 #endif
348
349 UNITY_DOUBLE number = input_number;
350
351 /* print minus sign (does not handle negative zero) */
352 if (number < 0.0f)
353 {
354 UNITY_OUTPUT_CHAR('-');
355 number = -number;
356 }
357
358 /* handle zero, NaN, and +/- infinity */
359 if (number == 0.0f)
360 {
361 UnityPrint("0");
362 }
363 else if (isnan(number))
364 {
365 UnityPrint("nan");
366 }
367 else if (isinf(number))
368 {
369 UnityPrint("inf");
370 }
371 else
372 {
373 UNITY_INT32 n_int = 0, n;
374 int exponent = 0;
375 int decimals, digits;
376 char buf[16] = {0};
377
378 /*
379 * Scale up or down by powers of 10. To minimize rounding error,
380 * start with a factor/divisor of 10^10, which is the largest
381 * power of 10 that can be represented exactly. Finally, compute
382 * (exactly) the remaining power of 10 and perform one more
383 * multiplication or division.
384 */
385 if (number < 1.0f)
386 {
387 UNITY_DOUBLE factor = 1.0f;
388
389 while (number < (UNITY_DOUBLE)max_scaled / 1e10f) { number *= 1e10f; exponent -= 10; }
390 while (number * factor < (UNITY_DOUBLE)min_scaled) { factor *= 10.0f; exponent--; }
391
392 number *= factor;
393 }
394 else if (number > (UNITY_DOUBLE)max_scaled)
395 {
396 UNITY_DOUBLE divisor = 1.0f;
397
398 while (number > (UNITY_DOUBLE)min_scaled * 1e10f) { number /= 1e10f; exponent += 10; }
399 while (number / divisor > (UNITY_DOUBLE)max_scaled) { divisor *= 10.0f; exponent++; }
400
401 number /= divisor;
402 }
403 else
404 {
405 /*
406 * In this range, we can split off the integer part before
407 * doing any multiplications. This reduces rounding error by
408 * freeing up significant bits in the fractional part.
409 */
410 UNITY_DOUBLE factor = 1.0f;
411 n_int = (UNITY_INT32)number;
412 number -= (UNITY_DOUBLE)n_int;
413
414 while (n_int < min_scaled) { n_int *= 10; factor *= 10.0f; exponent--; }
415
416 number *= factor;
417 }
418
419 /* round to nearest integer */
420 n = ((UNITY_INT32)(number + number) + 1) / 2;
421
422 #ifndef UNITY_ROUND_TIES_AWAY_FROM_ZERO
423 /* round to even if exactly between two integers */
424 if ((n & 1) && (((UNITY_DOUBLE)n - number) == 0.5f))
425 n--;
426 #endif
427
428 n += n_int;
429
430 if (n >= max_scaled)
431 {
432 n = min_scaled;
433 exponent++;
434 }
435
436 /* determine where to place decimal point */
437 decimals = ((exponent <= 0) && (exponent >= -(sig_digits + 3))) ? (-exponent) : (sig_digits - 1);
438 exponent += decimals;
439
440 /* truncate trailing zeroes after decimal point */
441 while ((decimals > 0) && ((n % 10) == 0))
442 {
443 n /= 10;
444 decimals--;
445 }
446
447 /* build up buffer in reverse order */
448 digits = 0;
449 while ((n != 0) || (digits < (decimals + 1)))
450 {
451 buf[digits++] = (char)('0' + n % 10);
452 n /= 10;
453 }
454 while (digits > 0)
455 {
456 if (digits == decimals) { UNITY_OUTPUT_CHAR('.'); }
457 UNITY_OUTPUT_CHAR(buf[--digits]);
458 }
459
460 /* print exponent if needed */
461 if (exponent != 0)
462 {
463 UNITY_OUTPUT_CHAR('e');
464
465 if (exponent < 0)
466 {
467 UNITY_OUTPUT_CHAR('-');
468 exponent = -exponent;
469 }
470 else
471 {
472 UNITY_OUTPUT_CHAR('+');
473 }
474
475 digits = 0;
476 while ((exponent != 0) || (digits < 2))
477 {
478 buf[digits++] = (char)('0' + exponent % 10);
479 exponent /= 10;
480 }
481 while (digits > 0)
482 {
483 UNITY_OUTPUT_CHAR(buf[--digits]);
484 }
485 }
486 }
487 }
488 #endif /* ! UNITY_EXCLUDE_FLOAT_PRINT */
489
490 /*-----------------------------------------------*/
UnityTestResultsBegin(const char * file,const UNITY_LINE_TYPE line)491 static void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line)
492 {
493 #ifdef UNITY_OUTPUT_FOR_ECLIPSE
494 UNITY_OUTPUT_CHAR('(');
495 UnityPrint(file);
496 UNITY_OUTPUT_CHAR(':');
497 UnityPrintNumber((UNITY_INT)line);
498 UNITY_OUTPUT_CHAR(')');
499 UNITY_OUTPUT_CHAR(' ');
500 UnityPrint(Unity.CurrentTestName);
501 UNITY_OUTPUT_CHAR(':');
502 #else
503 #ifdef UNITY_OUTPUT_FOR_IAR_WORKBENCH
504 UnityPrint("<SRCREF line=");
505 UnityPrintNumber((UNITY_INT)line);
506 UnityPrint(" file=\"");
507 UnityPrint(file);
508 UNITY_OUTPUT_CHAR('"');
509 UNITY_OUTPUT_CHAR('>');
510 UnityPrint(Unity.CurrentTestName);
511 UnityPrint("</SRCREF> ");
512 #else
513 #ifdef UNITY_OUTPUT_FOR_QT_CREATOR
514 UnityPrint("file://");
515 UnityPrint(file);
516 UNITY_OUTPUT_CHAR(':');
517 UnityPrintNumber((UNITY_INT)line);
518 UNITY_OUTPUT_CHAR(' ');
519 UnityPrint(Unity.CurrentTestName);
520 UNITY_OUTPUT_CHAR(':');
521 #else
522 UnityPrint(file);
523 UNITY_OUTPUT_CHAR(':');
524 UnityPrintNumber((UNITY_INT)line);
525 UNITY_OUTPUT_CHAR(':');
526 UnityPrint(Unity.CurrentTestName);
527 UNITY_OUTPUT_CHAR(':');
528 #endif
529 #endif
530 #endif
531 }
532
533 /*-----------------------------------------------*/
UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)534 static void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)
535 {
536 UnityTestResultsBegin(Unity.TestFile, line);
537 UnityPrint(UnityStrFail);
538 UNITY_OUTPUT_CHAR(':');
539 }
540
541 /*-----------------------------------------------*/
UnityConcludeTest(void)542 void UnityConcludeTest(void)
543 {
544 if (Unity.CurrentTestIgnored)
545 {
546 Unity.TestIgnores++;
547 }
548 else if (!Unity.CurrentTestFailed)
549 {
550 UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);
551 UnityPrint(UnityStrPass);
552 }
553 else
554 {
555 Unity.TestFailures++;
556 }
557
558 Unity.CurrentTestFailed = 0;
559 Unity.CurrentTestIgnored = 0;
560 UNITY_PRINT_EXEC_TIME();
561 UNITY_PRINT_EOL();
562 UNITY_FLUSH_CALL();
563 }
564
565 /*-----------------------------------------------*/
UnityAddMsgIfSpecified(const char * msg)566 static void UnityAddMsgIfSpecified(const char* msg)
567 {
568 if (msg)
569 {
570 UnityPrint(UnityStrSpacer);
571
572 #ifdef UNITY_PRINT_TEST_CONTEXT
573 UNITY_PRINT_TEST_CONTEXT();
574 #endif
575 #ifndef UNITY_EXCLUDE_DETAILS
576 if (Unity.CurrentDetail1)
577 {
578 UnityPrint(UnityStrDetail1Name);
579 UnityPrint(Unity.CurrentDetail1);
580 if (Unity.CurrentDetail2)
581 {
582 UnityPrint(UnityStrDetail2Name);
583 UnityPrint(Unity.CurrentDetail2);
584 }
585 UnityPrint(UnityStrSpacer);
586 }
587 #endif
588 UnityPrint(msg);
589 }
590 }
591
592 /*-----------------------------------------------*/
UnityPrintExpectedAndActualStrings(const char * expected,const char * actual)593 static void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual)
594 {
595 UnityPrint(UnityStrExpected);
596 if (expected != NULL)
597 {
598 UNITY_OUTPUT_CHAR('\'');
599 UnityPrint(expected);
600 UNITY_OUTPUT_CHAR('\'');
601 }
602 else
603 {
604 UnityPrint(UnityStrNull);
605 }
606 UnityPrint(UnityStrWas);
607 if (actual != NULL)
608 {
609 UNITY_OUTPUT_CHAR('\'');
610 UnityPrint(actual);
611 UNITY_OUTPUT_CHAR('\'');
612 }
613 else
614 {
615 UnityPrint(UnityStrNull);
616 }
617 }
618
619 /*-----------------------------------------------*/
UnityPrintExpectedAndActualStringsLen(const char * expected,const char * actual,const UNITY_UINT32 length)620 static void UnityPrintExpectedAndActualStringsLen(const char* expected,
621 const char* actual,
622 const UNITY_UINT32 length)
623 {
624 UnityPrint(UnityStrExpected);
625 if (expected != NULL)
626 {
627 UNITY_OUTPUT_CHAR('\'');
628 UnityPrintLen(expected, length);
629 UNITY_OUTPUT_CHAR('\'');
630 }
631 else
632 {
633 UnityPrint(UnityStrNull);
634 }
635 UnityPrint(UnityStrWas);
636 if (actual != NULL)
637 {
638 UNITY_OUTPUT_CHAR('\'');
639 UnityPrintLen(actual, length);
640 UNITY_OUTPUT_CHAR('\'');
641 }
642 else
643 {
644 UnityPrint(UnityStrNull);
645 }
646 }
647
648 /*-----------------------------------------------
649 * Assertion & Control Helpers
650 *-----------------------------------------------*/
651
652 /*-----------------------------------------------*/
UnityIsOneArrayNull(UNITY_INTERNAL_PTR expected,UNITY_INTERNAL_PTR actual,const UNITY_LINE_TYPE lineNumber,const char * msg)653 static int UnityIsOneArrayNull(UNITY_INTERNAL_PTR expected,
654 UNITY_INTERNAL_PTR actual,
655 const UNITY_LINE_TYPE lineNumber,
656 const char* msg)
657 {
658 /* Both are NULL or same pointer */
659 if (expected == actual) { return 0; }
660
661 /* print and return true if just expected is NULL */
662 if (expected == NULL)
663 {
664 UnityTestResultsFailBegin(lineNumber);
665 UnityPrint(UnityStrNullPointerForExpected);
666 UnityAddMsgIfSpecified(msg);
667 return 1;
668 }
669
670 /* print and return true if just actual is NULL */
671 if (actual == NULL)
672 {
673 UnityTestResultsFailBegin(lineNumber);
674 UnityPrint(UnityStrNullPointerForActual);
675 UnityAddMsgIfSpecified(msg);
676 return 1;
677 }
678
679 return 0; /* return false if neither is NULL */
680 }
681
682 /*-----------------------------------------------
683 * Assertion Functions
684 *-----------------------------------------------*/
685
686 /*-----------------------------------------------*/
UnityAssertBits(const UNITY_INT mask,const UNITY_INT expected,const UNITY_INT actual,const char * msg,const UNITY_LINE_TYPE lineNumber)687 void UnityAssertBits(const UNITY_INT mask,
688 const UNITY_INT expected,
689 const UNITY_INT actual,
690 const char* msg,
691 const UNITY_LINE_TYPE lineNumber)
692 {
693 RETURN_IF_FAIL_OR_IGNORE;
694
695 if ((mask & expected) != (mask & actual))
696 {
697 UnityTestResultsFailBegin(lineNumber);
698 UnityPrint(UnityStrExpected);
699 UnityPrintMask((UNITY_UINT)mask, (UNITY_UINT)expected);
700 UnityPrint(UnityStrWas);
701 UnityPrintMask((UNITY_UINT)mask, (UNITY_UINT)actual);
702 UnityAddMsgIfSpecified(msg);
703 UNITY_FAIL_AND_BAIL;
704 }
705 }
706
707 /*-----------------------------------------------*/
UnityAssertEqualNumber(const UNITY_INT expected,const UNITY_INT actual,const char * msg,const UNITY_LINE_TYPE lineNumber,const UNITY_DISPLAY_STYLE_T style)708 void UnityAssertEqualNumber(const UNITY_INT expected,
709 const UNITY_INT actual,
710 const char* msg,
711 const UNITY_LINE_TYPE lineNumber,
712 const UNITY_DISPLAY_STYLE_T style)
713 {
714 RETURN_IF_FAIL_OR_IGNORE;
715
716 if (expected != actual)
717 {
718 UnityTestResultsFailBegin(lineNumber);
719 UnityPrint(UnityStrExpected);
720 UnityPrintNumberByStyle(expected, style);
721 UnityPrint(UnityStrWas);
722 UnityPrintNumberByStyle(actual, style);
723 UnityAddMsgIfSpecified(msg);
724 UNITY_FAIL_AND_BAIL;
725 }
726 }
727
728 /*-----------------------------------------------*/
UnityAssertGreaterOrLessOrEqualNumber(const UNITY_INT threshold,const UNITY_INT actual,const UNITY_COMPARISON_T compare,const char * msg,const UNITY_LINE_TYPE lineNumber,const UNITY_DISPLAY_STYLE_T style)729 void UnityAssertGreaterOrLessOrEqualNumber(const UNITY_INT threshold,
730 const UNITY_INT actual,
731 const UNITY_COMPARISON_T compare,
732 const char *msg,
733 const UNITY_LINE_TYPE lineNumber,
734 const UNITY_DISPLAY_STYLE_T style)
735 {
736 int failed = 0;
737 RETURN_IF_FAIL_OR_IGNORE;
738
739 if ((threshold == actual) && (compare & UNITY_EQUAL_TO)) { return; }
740 if ((threshold == actual)) { failed = 1; }
741
742 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
743 {
744 if ((actual > threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
745 if ((actual < threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }
746 }
747 else /* UINT or HEX */
748 {
749 if (((UNITY_UINT)actual > (UNITY_UINT)threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
750 if (((UNITY_UINT)actual < (UNITY_UINT)threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }
751 }
752
753 if (failed)
754 {
755 UnityTestResultsFailBegin(lineNumber);
756 UnityPrint(UnityStrExpected);
757 UnityPrintNumberByStyle(actual, style);
758 if (compare & UNITY_GREATER_THAN) { UnityPrint(UnityStrGt); }
759 if (compare & UNITY_SMALLER_THAN) { UnityPrint(UnityStrLt); }
760 if (compare & UNITY_EQUAL_TO) { UnityPrint(UnityStrOrEqual); }
761 if (compare == UNITY_NOT_EQUAL) { UnityPrint(UnityStrNotEqual); }
762 UnityPrintNumberByStyle(threshold, style);
763 UnityAddMsgIfSpecified(msg);
764 UNITY_FAIL_AND_BAIL;
765 }
766 }
767
768 #define UnityPrintPointlessAndBail() \
769 { \
770 UnityTestResultsFailBegin(lineNumber); \
771 UnityPrint(UnityStrPointless); \
772 UnityAddMsgIfSpecified(msg); \
773 UNITY_FAIL_AND_BAIL; }
774
775 /*-----------------------------------------------*/
UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,UNITY_INTERNAL_PTR actual,const UNITY_UINT32 num_elements,const char * msg,const UNITY_LINE_TYPE lineNumber,const UNITY_DISPLAY_STYLE_T style,const UNITY_FLAGS_T flags)776 void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
777 UNITY_INTERNAL_PTR actual,
778 const UNITY_UINT32 num_elements,
779 const char* msg,
780 const UNITY_LINE_TYPE lineNumber,
781 const UNITY_DISPLAY_STYLE_T style,
782 const UNITY_FLAGS_T flags)
783 {
784 UNITY_UINT32 elements = num_elements;
785 unsigned int length = style & 0xF;
786 unsigned int increment = 0;
787
788 RETURN_IF_FAIL_OR_IGNORE;
789
790 if (num_elements == 0)
791 {
792 UnityPrintPointlessAndBail();
793 }
794
795 if (expected == actual)
796 {
797 return; /* Both are NULL or same pointer */
798 }
799
800 if (UnityIsOneArrayNull(expected, actual, lineNumber, msg))
801 {
802 UNITY_FAIL_AND_BAIL;
803 }
804
805 while ((elements > 0) && (elements--))
806 {
807 UNITY_INT expect_val;
808 UNITY_INT actual_val;
809
810 switch (length)
811 {
812 case 1:
813 expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT8*)expected;
814 actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT8*)actual;
815 increment = sizeof(UNITY_INT8);
816 break;
817
818 case 2:
819 expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT16*)expected;
820 actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT16*)actual;
821 increment = sizeof(UNITY_INT16);
822 break;
823
824 #ifdef UNITY_SUPPORT_64
825 case 8:
826 expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT64*)expected;
827 actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT64*)actual;
828 increment = sizeof(UNITY_INT64);
829 break;
830 #endif
831
832 default: /* default is length 4 bytes */
833 case 4:
834 expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT32*)expected;
835 actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT32*)actual;
836 increment = sizeof(UNITY_INT32);
837 length = 4;
838 break;
839 }
840
841 if (expect_val != actual_val)
842 {
843 if ((style & UNITY_DISPLAY_RANGE_UINT) && (length < (UNITY_INT_WIDTH / 8)))
844 { /* For UINT, remove sign extension (padding 1's) from signed type casts above */
845 UNITY_INT mask = 1;
846 mask = (mask << 8 * length) - 1;
847 expect_val &= mask;
848 actual_val &= mask;
849 }
850 UnityTestResultsFailBegin(lineNumber);
851 UnityPrint(UnityStrElement);
852 UnityPrintNumberUnsigned(num_elements - elements - 1);
853 UnityPrint(UnityStrExpected);
854 UnityPrintNumberByStyle(expect_val, style);
855 UnityPrint(UnityStrWas);
856 UnityPrintNumberByStyle(actual_val, style);
857 UnityAddMsgIfSpecified(msg);
858 UNITY_FAIL_AND_BAIL;
859 }
860 /* Walk through array by incrementing the pointers */
861 if (flags == UNITY_ARRAY_TO_ARRAY)
862 {
863 expected = (UNITY_INTERNAL_PTR)((const char*)expected + increment);
864 }
865 actual = (UNITY_INTERNAL_PTR)((const char*)actual + increment);
866 }
867 }
868
869 /*-----------------------------------------------*/
870 #ifndef UNITY_EXCLUDE_FLOAT
871 /* Wrap this define in a function with variable types as float or double */
872 #define UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff) \
873 if (isinf(expected) && isinf(actual) && (((expected) < 0) == ((actual) < 0))) return 1; \
874 if (UNITY_NAN_CHECK) return 1; \
875 (diff) = (actual) - (expected); \
876 if ((diff) < 0) (diff) = -(diff); \
877 if ((delta) < 0) (delta) = -(delta); \
878 return !(isnan(diff) || isinf(diff) || ((diff) > (delta)))
879 /* This first part of this condition will catch any NaN or Infinite values */
880 #ifndef UNITY_NAN_NOT_EQUAL_NAN
881 #define UNITY_NAN_CHECK isnan(expected) && isnan(actual)
882 #else
883 #define UNITY_NAN_CHECK 0
884 #endif
885
886 #ifndef UNITY_EXCLUDE_FLOAT_PRINT
887 #define UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(expected, actual) \
888 { \
889 UnityPrint(UnityStrExpected); \
890 UnityPrintFloat(expected); \
891 UnityPrint(UnityStrWas); \
892 UnityPrintFloat(actual); }
893 #else
894 #define UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(expected, actual) \
895 UnityPrint(UnityStrDelta)
896 #endif /* UNITY_EXCLUDE_FLOAT_PRINT */
897
898 /*-----------------------------------------------*/
UnityFloatsWithin(UNITY_FLOAT delta,UNITY_FLOAT expected,UNITY_FLOAT actual)899 static int UnityFloatsWithin(UNITY_FLOAT delta, UNITY_FLOAT expected, UNITY_FLOAT actual)
900 {
901 UNITY_FLOAT diff;
902 UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);
903 }
904
905 /*-----------------------------------------------*/
UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT * expected,UNITY_PTR_ATTRIBUTE const UNITY_FLOAT * actual,const UNITY_UINT32 num_elements,const char * msg,const UNITY_LINE_TYPE lineNumber,const UNITY_FLAGS_T flags)906 void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected,
907 UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* actual,
908 const UNITY_UINT32 num_elements,
909 const char* msg,
910 const UNITY_LINE_TYPE lineNumber,
911 const UNITY_FLAGS_T flags)
912 {
913 UNITY_UINT32 elements = num_elements;
914 UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* ptr_expected = expected;
915 UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* ptr_actual = actual;
916
917 RETURN_IF_FAIL_OR_IGNORE;
918
919 if (elements == 0)
920 {
921 UnityPrintPointlessAndBail();
922 }
923
924 if (expected == actual)
925 {
926 return; /* Both are NULL or same pointer */
927 }
928
929 if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg))
930 {
931 UNITY_FAIL_AND_BAIL;
932 }
933
934 while (elements--)
935 {
936 if (!UnityFloatsWithin(*ptr_expected * UNITY_FLOAT_PRECISION, *ptr_expected, *ptr_actual))
937 {
938 UnityTestResultsFailBegin(lineNumber);
939 UnityPrint(UnityStrElement);
940 UnityPrintNumberUnsigned(num_elements - elements - 1);
941 UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT((UNITY_DOUBLE)*ptr_expected, (UNITY_DOUBLE)*ptr_actual);
942 UnityAddMsgIfSpecified(msg);
943 UNITY_FAIL_AND_BAIL;
944 }
945 if (flags == UNITY_ARRAY_TO_ARRAY)
946 {
947 ptr_expected++;
948 }
949 ptr_actual++;
950 }
951 }
952
953 /*-----------------------------------------------*/
UnityAssertFloatsWithin(const UNITY_FLOAT delta,const UNITY_FLOAT expected,const UNITY_FLOAT actual,const char * msg,const UNITY_LINE_TYPE lineNumber)954 void UnityAssertFloatsWithin(const UNITY_FLOAT delta,
955 const UNITY_FLOAT expected,
956 const UNITY_FLOAT actual,
957 const char* msg,
958 const UNITY_LINE_TYPE lineNumber)
959 {
960 RETURN_IF_FAIL_OR_IGNORE;
961
962
963 if (!UnityFloatsWithin(delta, expected, actual))
964 {
965 UnityTestResultsFailBegin(lineNumber);
966 UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT((UNITY_DOUBLE)expected, (UNITY_DOUBLE)actual);
967 UnityAddMsgIfSpecified(msg);
968 UNITY_FAIL_AND_BAIL;
969 }
970 }
971
972 /*-----------------------------------------------*/
UnityAssertFloatSpecial(const UNITY_FLOAT actual,const char * msg,const UNITY_LINE_TYPE lineNumber,const UNITY_FLOAT_TRAIT_T style)973 void UnityAssertFloatSpecial(const UNITY_FLOAT actual,
974 const char* msg,
975 const UNITY_LINE_TYPE lineNumber,
976 const UNITY_FLOAT_TRAIT_T style)
977 {
978 const char* trait_names[] = {UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet};
979 UNITY_INT should_be_trait = ((UNITY_INT)style & 1);
980 UNITY_INT is_trait = !should_be_trait;
981 UNITY_INT trait_index = (UNITY_INT)(style >> 1);
982
983 RETURN_IF_FAIL_OR_IGNORE;
984
985 switch (style)
986 {
987 case UNITY_FLOAT_IS_INF:
988 case UNITY_FLOAT_IS_NOT_INF:
989 is_trait = isinf(actual) && (actual > 0);
990 break;
991 case UNITY_FLOAT_IS_NEG_INF:
992 case UNITY_FLOAT_IS_NOT_NEG_INF:
993 is_trait = isinf(actual) && (actual < 0);
994 break;
995
996 case UNITY_FLOAT_IS_NAN:
997 case UNITY_FLOAT_IS_NOT_NAN:
998 is_trait = isnan(actual) ? 1 : 0;
999 break;
1000
1001 case UNITY_FLOAT_IS_DET: /* A determinate number is non infinite and not NaN. */
1002 case UNITY_FLOAT_IS_NOT_DET:
1003 is_trait = !isinf(actual) && !isnan(actual);
1004 break;
1005
1006 default: /* including UNITY_FLOAT_INVALID_TRAIT */
1007 trait_index = 0;
1008 trait_names[0] = UnityStrInvalidFloatTrait;
1009 break;
1010 }
1011
1012 if (is_trait != should_be_trait)
1013 {
1014 UnityTestResultsFailBegin(lineNumber);
1015 UnityPrint(UnityStrExpected);
1016 if (!should_be_trait)
1017 {
1018 UnityPrint(UnityStrNot);
1019 }
1020 UnityPrint(trait_names[trait_index]);
1021 UnityPrint(UnityStrWas);
1022 #ifndef UNITY_EXCLUDE_FLOAT_PRINT
1023 UnityPrintFloat((UNITY_DOUBLE)actual);
1024 #else
1025 if (should_be_trait)
1026 {
1027 UnityPrint(UnityStrNot);
1028 }
1029 UnityPrint(trait_names[trait_index]);
1030 #endif
1031 UnityAddMsgIfSpecified(msg);
1032 UNITY_FAIL_AND_BAIL;
1033 }
1034 }
1035
1036 #endif /* not UNITY_EXCLUDE_FLOAT */
1037
1038 /*-----------------------------------------------*/
1039 #ifndef UNITY_EXCLUDE_DOUBLE
UnityDoublesWithin(UNITY_DOUBLE delta,UNITY_DOUBLE expected,UNITY_DOUBLE actual)1040 static int UnityDoublesWithin(UNITY_DOUBLE delta, UNITY_DOUBLE expected, UNITY_DOUBLE actual)
1041 {
1042 UNITY_DOUBLE diff;
1043 UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);
1044 }
1045
1046 /*-----------------------------------------------*/
UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE * expected,UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE * actual,const UNITY_UINT32 num_elements,const char * msg,const UNITY_LINE_TYPE lineNumber,const UNITY_FLAGS_T flags)1047 void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expected,
1048 UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* actual,
1049 const UNITY_UINT32 num_elements,
1050 const char* msg,
1051 const UNITY_LINE_TYPE lineNumber,
1052 const UNITY_FLAGS_T flags)
1053 {
1054 UNITY_UINT32 elements = num_elements;
1055 UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* ptr_expected = expected;
1056 UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* ptr_actual = actual;
1057
1058 RETURN_IF_FAIL_OR_IGNORE;
1059
1060 if (elements == 0)
1061 {
1062 UnityPrintPointlessAndBail();
1063 }
1064
1065 if (expected == actual)
1066 {
1067 return; /* Both are NULL or same pointer */
1068 }
1069
1070 if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg))
1071 {
1072 UNITY_FAIL_AND_BAIL;
1073 }
1074
1075 while (elements--)
1076 {
1077 if (!UnityDoublesWithin(*ptr_expected * UNITY_DOUBLE_PRECISION, *ptr_expected, *ptr_actual))
1078 {
1079 UnityTestResultsFailBegin(lineNumber);
1080 UnityPrint(UnityStrElement);
1081 UnityPrintNumberUnsigned(num_elements - elements - 1);
1082 UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(*ptr_expected, *ptr_actual);
1083 UnityAddMsgIfSpecified(msg);
1084 UNITY_FAIL_AND_BAIL;
1085 }
1086 if (flags == UNITY_ARRAY_TO_ARRAY)
1087 {
1088 ptr_expected++;
1089 }
1090 ptr_actual++;
1091 }
1092 }
1093
1094 /*-----------------------------------------------*/
UnityAssertDoublesWithin(const UNITY_DOUBLE delta,const UNITY_DOUBLE expected,const UNITY_DOUBLE actual,const char * msg,const UNITY_LINE_TYPE lineNumber)1095 void UnityAssertDoublesWithin(const UNITY_DOUBLE delta,
1096 const UNITY_DOUBLE expected,
1097 const UNITY_DOUBLE actual,
1098 const char* msg,
1099 const UNITY_LINE_TYPE lineNumber)
1100 {
1101 RETURN_IF_FAIL_OR_IGNORE;
1102
1103 if (!UnityDoublesWithin(delta, expected, actual))
1104 {
1105 UnityTestResultsFailBegin(lineNumber);
1106 UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(expected, actual);
1107 UnityAddMsgIfSpecified(msg);
1108 UNITY_FAIL_AND_BAIL;
1109 }
1110 }
1111
1112 /*-----------------------------------------------*/
UnityAssertDoubleSpecial(const UNITY_DOUBLE actual,const char * msg,const UNITY_LINE_TYPE lineNumber,const UNITY_FLOAT_TRAIT_T style)1113 void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual,
1114 const char* msg,
1115 const UNITY_LINE_TYPE lineNumber,
1116 const UNITY_FLOAT_TRAIT_T style)
1117 {
1118 const char* trait_names[] = {UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet};
1119 UNITY_INT should_be_trait = ((UNITY_INT)style & 1);
1120 UNITY_INT is_trait = !should_be_trait;
1121 UNITY_INT trait_index = (UNITY_INT)(style >> 1);
1122
1123 RETURN_IF_FAIL_OR_IGNORE;
1124
1125 switch (style)
1126 {
1127 case UNITY_FLOAT_IS_INF:
1128 case UNITY_FLOAT_IS_NOT_INF:
1129 is_trait = isinf(actual) && (actual > 0);
1130 break;
1131 case UNITY_FLOAT_IS_NEG_INF:
1132 case UNITY_FLOAT_IS_NOT_NEG_INF:
1133 is_trait = isinf(actual) && (actual < 0);
1134 break;
1135
1136 case UNITY_FLOAT_IS_NAN:
1137 case UNITY_FLOAT_IS_NOT_NAN:
1138 is_trait = isnan(actual) ? 1 : 0;
1139 break;
1140
1141 case UNITY_FLOAT_IS_DET: /* A determinate number is non infinite and not NaN. */
1142 case UNITY_FLOAT_IS_NOT_DET:
1143 is_trait = !isinf(actual) && !isnan(actual);
1144 break;
1145
1146 default: /* including UNITY_FLOAT_INVALID_TRAIT */
1147 trait_index = 0;
1148 trait_names[0] = UnityStrInvalidFloatTrait;
1149 break;
1150 }
1151
1152 if (is_trait != should_be_trait)
1153 {
1154 UnityTestResultsFailBegin(lineNumber);
1155 UnityPrint(UnityStrExpected);
1156 if (!should_be_trait)
1157 {
1158 UnityPrint(UnityStrNot);
1159 }
1160 UnityPrint(trait_names[trait_index]);
1161 UnityPrint(UnityStrWas);
1162 #ifndef UNITY_EXCLUDE_FLOAT_PRINT
1163 UnityPrintFloat(actual);
1164 #else
1165 if (should_be_trait)
1166 {
1167 UnityPrint(UnityStrNot);
1168 }
1169 UnityPrint(trait_names[trait_index]);
1170 #endif
1171 UnityAddMsgIfSpecified(msg);
1172 UNITY_FAIL_AND_BAIL;
1173 }
1174 }
1175
1176 #endif /* not UNITY_EXCLUDE_DOUBLE */
1177
1178 /*-----------------------------------------------*/
UnityAssertNumbersWithin(const UNITY_UINT delta,const UNITY_INT expected,const UNITY_INT actual,const char * msg,const UNITY_LINE_TYPE lineNumber,const UNITY_DISPLAY_STYLE_T style)1179 void UnityAssertNumbersWithin(const UNITY_UINT delta,
1180 const UNITY_INT expected,
1181 const UNITY_INT actual,
1182 const char* msg,
1183 const UNITY_LINE_TYPE lineNumber,
1184 const UNITY_DISPLAY_STYLE_T style)
1185 {
1186 RETURN_IF_FAIL_OR_IGNORE;
1187
1188 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
1189 {
1190 if (actual > expected)
1191 {
1192 Unity.CurrentTestFailed = (((UNITY_UINT)actual - (UNITY_UINT)expected) > delta);
1193 }
1194 else
1195 {
1196 Unity.CurrentTestFailed = (((UNITY_UINT)expected - (UNITY_UINT)actual) > delta);
1197 }
1198 }
1199 else
1200 {
1201 if ((UNITY_UINT)actual > (UNITY_UINT)expected)
1202 {
1203 Unity.CurrentTestFailed = (((UNITY_UINT)actual - (UNITY_UINT)expected) > delta);
1204 }
1205 else
1206 {
1207 Unity.CurrentTestFailed = (((UNITY_UINT)expected - (UNITY_UINT)actual) > delta);
1208 }
1209 }
1210
1211 if (Unity.CurrentTestFailed)
1212 {
1213 UnityTestResultsFailBegin(lineNumber);
1214 UnityPrint(UnityStrDelta);
1215 UnityPrintNumberByStyle((UNITY_INT)delta, style);
1216 UnityPrint(UnityStrExpected);
1217 UnityPrintNumberByStyle(expected, style);
1218 UnityPrint(UnityStrWas);
1219 UnityPrintNumberByStyle(actual, style);
1220 UnityAddMsgIfSpecified(msg);
1221 UNITY_FAIL_AND_BAIL;
1222 }
1223 }
1224
1225 /*-----------------------------------------------*/
UnityAssertNumbersArrayWithin(const UNITY_UINT delta,UNITY_INTERNAL_PTR expected,UNITY_INTERNAL_PTR actual,const UNITY_UINT32 num_elements,const char * msg,const UNITY_LINE_TYPE lineNumber,const UNITY_DISPLAY_STYLE_T style,const UNITY_FLAGS_T flags)1226 void UnityAssertNumbersArrayWithin(const UNITY_UINT delta,
1227 UNITY_INTERNAL_PTR expected,
1228 UNITY_INTERNAL_PTR actual,
1229 const UNITY_UINT32 num_elements,
1230 const char* msg,
1231 const UNITY_LINE_TYPE lineNumber,
1232 const UNITY_DISPLAY_STYLE_T style,
1233 const UNITY_FLAGS_T flags)
1234 {
1235 UNITY_UINT32 elements = num_elements;
1236 unsigned int length = style & 0xF;
1237 unsigned int increment = 0;
1238
1239 RETURN_IF_FAIL_OR_IGNORE;
1240
1241 if (num_elements == 0)
1242 {
1243 UnityPrintPointlessAndBail();
1244 }
1245
1246 if (expected == actual)
1247 {
1248 return; /* Both are NULL or same pointer */
1249 }
1250
1251 if (UnityIsOneArrayNull(expected, actual, lineNumber, msg))
1252 {
1253 UNITY_FAIL_AND_BAIL;
1254 }
1255
1256 while ((elements > 0) && (elements--))
1257 {
1258 UNITY_INT expect_val;
1259 UNITY_INT actual_val;
1260
1261 switch (length)
1262 {
1263 case 1:
1264 expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT8*)expected;
1265 actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT8*)actual;
1266 increment = sizeof(UNITY_INT8);
1267 break;
1268
1269 case 2:
1270 expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT16*)expected;
1271 actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT16*)actual;
1272 increment = sizeof(UNITY_INT16);
1273 break;
1274
1275 #ifdef UNITY_SUPPORT_64
1276 case 8:
1277 expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT64*)expected;
1278 actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT64*)actual;
1279 increment = sizeof(UNITY_INT64);
1280 break;
1281 #endif
1282
1283 default: /* default is length 4 bytes */
1284 case 4:
1285 expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT32*)expected;
1286 actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT32*)actual;
1287 increment = sizeof(UNITY_INT32);
1288 length = 4;
1289 break;
1290 }
1291
1292 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
1293 {
1294 if (actual_val > expect_val)
1295 {
1296 Unity.CurrentTestFailed = (((UNITY_UINT)actual_val - (UNITY_UINT)expect_val) > delta);
1297 }
1298 else
1299 {
1300 Unity.CurrentTestFailed = (((UNITY_UINT)expect_val - (UNITY_UINT)actual_val) > delta);
1301 }
1302 }
1303 else
1304 {
1305 if ((UNITY_UINT)actual_val > (UNITY_UINT)expect_val)
1306 {
1307 Unity.CurrentTestFailed = (((UNITY_UINT)actual_val - (UNITY_UINT)expect_val) > delta);
1308 }
1309 else
1310 {
1311 Unity.CurrentTestFailed = (((UNITY_UINT)expect_val - (UNITY_UINT)actual_val) > delta);
1312 }
1313 }
1314
1315 if (Unity.CurrentTestFailed)
1316 {
1317 if ((style & UNITY_DISPLAY_RANGE_UINT) && (length < (UNITY_INT_WIDTH / 8)))
1318 { /* For UINT, remove sign extension (padding 1's) from signed type casts above */
1319 UNITY_INT mask = 1;
1320 mask = (mask << 8 * length) - 1;
1321 expect_val &= mask;
1322 actual_val &= mask;
1323 }
1324 UnityTestResultsFailBegin(lineNumber);
1325 UnityPrint(UnityStrDelta);
1326 UnityPrintNumberByStyle((UNITY_INT)delta, style);
1327 UnityPrint(UnityStrElement);
1328 UnityPrintNumberUnsigned(num_elements - elements - 1);
1329 UnityPrint(UnityStrExpected);
1330 UnityPrintNumberByStyle(expect_val, style);
1331 UnityPrint(UnityStrWas);
1332 UnityPrintNumberByStyle(actual_val, style);
1333 UnityAddMsgIfSpecified(msg);
1334 UNITY_FAIL_AND_BAIL;
1335 }
1336 /* Walk through array by incrementing the pointers */
1337 if (flags == UNITY_ARRAY_TO_ARRAY)
1338 {
1339 expected = (UNITY_INTERNAL_PTR)((const char*)expected + increment);
1340 }
1341 actual = (UNITY_INTERNAL_PTR)((const char*)actual + increment);
1342 }
1343 }
1344
1345 /*-----------------------------------------------*/
UnityAssertEqualString(const char * expected,const char * actual,const char * msg,const UNITY_LINE_TYPE lineNumber)1346 void UnityAssertEqualString(const char* expected,
1347 const char* actual,
1348 const char* msg,
1349 const UNITY_LINE_TYPE lineNumber)
1350 {
1351 UNITY_UINT32 i;
1352
1353 RETURN_IF_FAIL_OR_IGNORE;
1354
1355 /* if both pointers not null compare the strings */
1356 if (expected && actual)
1357 {
1358 for (i = 0; expected[i] || actual[i]; i++)
1359 {
1360 if (expected[i] != actual[i])
1361 {
1362 Unity.CurrentTestFailed = 1;
1363 break;
1364 }
1365 }
1366 }
1367 else
1368 { /* handle case of one pointers being null (if both null, test should pass) */
1369 if (expected != actual)
1370 {
1371 Unity.CurrentTestFailed = 1;
1372 }
1373 }
1374
1375 if (Unity.CurrentTestFailed)
1376 {
1377 UnityTestResultsFailBegin(lineNumber);
1378 UnityPrintExpectedAndActualStrings(expected, actual);
1379 UnityAddMsgIfSpecified(msg);
1380 UNITY_FAIL_AND_BAIL;
1381 }
1382 }
1383
1384 /*-----------------------------------------------*/
UnityAssertEqualStringLen(const char * expected,const char * actual,const UNITY_UINT32 length,const char * msg,const UNITY_LINE_TYPE lineNumber)1385 void UnityAssertEqualStringLen(const char* expected,
1386 const char* actual,
1387 const UNITY_UINT32 length,
1388 const char* msg,
1389 const UNITY_LINE_TYPE lineNumber)
1390 {
1391 UNITY_UINT32 i;
1392
1393 RETURN_IF_FAIL_OR_IGNORE;
1394
1395 /* if both pointers not null compare the strings */
1396 if (expected && actual)
1397 {
1398 for (i = 0; (i < length) && (expected[i] || actual[i]); i++)
1399 {
1400 if (expected[i] != actual[i])
1401 {
1402 Unity.CurrentTestFailed = 1;
1403 break;
1404 }
1405 }
1406 }
1407 else
1408 { /* handle case of one pointers being null (if both null, test should pass) */
1409 if (expected != actual)
1410 {
1411 Unity.CurrentTestFailed = 1;
1412 }
1413 }
1414
1415 if (Unity.CurrentTestFailed)
1416 {
1417 UnityTestResultsFailBegin(lineNumber);
1418 UnityPrintExpectedAndActualStringsLen(expected, actual, length);
1419 UnityAddMsgIfSpecified(msg);
1420 UNITY_FAIL_AND_BAIL;
1421 }
1422 }
1423
1424 /*-----------------------------------------------*/
UnityAssertEqualStringArray(UNITY_INTERNAL_PTR expected,const char ** actual,const UNITY_UINT32 num_elements,const char * msg,const UNITY_LINE_TYPE lineNumber,const UNITY_FLAGS_T flags)1425 void UnityAssertEqualStringArray(UNITY_INTERNAL_PTR expected,
1426 const char** actual,
1427 const UNITY_UINT32 num_elements,
1428 const char* msg,
1429 const UNITY_LINE_TYPE lineNumber,
1430 const UNITY_FLAGS_T flags)
1431 {
1432 UNITY_UINT32 i = 0;
1433 UNITY_UINT32 j = 0;
1434 const char* expd = NULL;
1435 const char* act = NULL;
1436
1437 RETURN_IF_FAIL_OR_IGNORE;
1438
1439 /* if no elements, it's an error */
1440 if (num_elements == 0)
1441 {
1442 UnityPrintPointlessAndBail();
1443 }
1444
1445 if ((const void*)expected == (const void*)actual)
1446 {
1447 return; /* Both are NULL or same pointer */
1448 }
1449
1450 if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg))
1451 {
1452 UNITY_FAIL_AND_BAIL;
1453 }
1454
1455 if (flags != UNITY_ARRAY_TO_ARRAY)
1456 {
1457 expd = (const char*)expected;
1458 }
1459
1460 do
1461 {
1462 act = actual[j];
1463 if (flags == UNITY_ARRAY_TO_ARRAY)
1464 {
1465 expd = ((const char* const*)expected)[j];
1466 }
1467
1468 /* if both pointers not null compare the strings */
1469 if (expd && act)
1470 {
1471 for (i = 0; expd[i] || act[i]; i++)
1472 {
1473 if (expd[i] != act[i])
1474 {
1475 Unity.CurrentTestFailed = 1;
1476 break;
1477 }
1478 }
1479 }
1480 else
1481 { /* handle case of one pointers being null (if both null, test should pass) */
1482 if (expd != act)
1483 {
1484 Unity.CurrentTestFailed = 1;
1485 }
1486 }
1487
1488 if (Unity.CurrentTestFailed)
1489 {
1490 UnityTestResultsFailBegin(lineNumber);
1491 if (num_elements > 1)
1492 {
1493 UnityPrint(UnityStrElement);
1494 UnityPrintNumberUnsigned(j);
1495 }
1496 UnityPrintExpectedAndActualStrings(expd, act);
1497 UnityAddMsgIfSpecified(msg);
1498 UNITY_FAIL_AND_BAIL;
1499 }
1500 } while (++j < num_elements);
1501 }
1502
1503 /*-----------------------------------------------*/
UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected,UNITY_INTERNAL_PTR actual,const UNITY_UINT32 length,const UNITY_UINT32 num_elements,const char * msg,const UNITY_LINE_TYPE lineNumber,const UNITY_FLAGS_T flags)1504 void UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected,
1505 UNITY_INTERNAL_PTR actual,
1506 const UNITY_UINT32 length,
1507 const UNITY_UINT32 num_elements,
1508 const char* msg,
1509 const UNITY_LINE_TYPE lineNumber,
1510 const UNITY_FLAGS_T flags)
1511 {
1512 UNITY_PTR_ATTRIBUTE const unsigned char* ptr_exp = (UNITY_PTR_ATTRIBUTE const unsigned char*)expected;
1513 UNITY_PTR_ATTRIBUTE const unsigned char* ptr_act = (UNITY_PTR_ATTRIBUTE const unsigned char*)actual;
1514 UNITY_UINT32 elements = num_elements;
1515 UNITY_UINT32 bytes;
1516
1517 RETURN_IF_FAIL_OR_IGNORE;
1518
1519 if ((elements == 0) || (length == 0))
1520 {
1521 UnityPrintPointlessAndBail();
1522 }
1523
1524 if (expected == actual)
1525 {
1526 return; /* Both are NULL or same pointer */
1527 }
1528
1529 if (UnityIsOneArrayNull(expected, actual, lineNumber, msg))
1530 {
1531 UNITY_FAIL_AND_BAIL;
1532 }
1533
1534 while (elements--)
1535 {
1536 bytes = length;
1537 while (bytes--)
1538 {
1539 if (*ptr_exp != *ptr_act)
1540 {
1541 UnityTestResultsFailBegin(lineNumber);
1542 UnityPrint(UnityStrMemory);
1543 if (num_elements > 1)
1544 {
1545 UnityPrint(UnityStrElement);
1546 UnityPrintNumberUnsigned(num_elements - elements - 1);
1547 }
1548 UnityPrint(UnityStrByte);
1549 UnityPrintNumberUnsigned(length - bytes - 1);
1550 UnityPrint(UnityStrExpected);
1551 UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
1552 UnityPrint(UnityStrWas);
1553 UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
1554 UnityAddMsgIfSpecified(msg);
1555 UNITY_FAIL_AND_BAIL;
1556 }
1557 ptr_exp++;
1558 ptr_act++;
1559 }
1560 if (flags == UNITY_ARRAY_TO_VAL)
1561 {
1562 ptr_exp = (UNITY_PTR_ATTRIBUTE const unsigned char*)expected;
1563 }
1564 }
1565 }
1566
1567 /*-----------------------------------------------*/
1568
1569 static union
1570 {
1571 UNITY_INT8 i8;
1572 UNITY_INT16 i16;
1573 UNITY_INT32 i32;
1574 #ifdef UNITY_SUPPORT_64
1575 UNITY_INT64 i64;
1576 #endif
1577 #ifndef UNITY_EXCLUDE_FLOAT
1578 float f;
1579 #endif
1580 #ifndef UNITY_EXCLUDE_DOUBLE
1581 double d;
1582 #endif
1583 } UnityQuickCompare;
1584
UnityNumToPtr(const UNITY_INT num,const UNITY_UINT8 size)1585 UNITY_INTERNAL_PTR UnityNumToPtr(const UNITY_INT num, const UNITY_UINT8 size)
1586 {
1587 switch(size)
1588 {
1589 case 1:
1590 UnityQuickCompare.i8 = (UNITY_INT8)num;
1591 return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.i8);
1592
1593 case 2:
1594 UnityQuickCompare.i16 = (UNITY_INT16)num;
1595 return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.i16);
1596
1597 #ifdef UNITY_SUPPORT_64
1598 case 8:
1599 UnityQuickCompare.i64 = (UNITY_INT64)num;
1600 return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.i64);
1601 #endif
1602
1603 default: /* 4 bytes */
1604 UnityQuickCompare.i32 = (UNITY_INT32)num;
1605 return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.i32);
1606 }
1607 }
1608
1609 #ifndef UNITY_EXCLUDE_FLOAT
1610 /*-----------------------------------------------*/
UnityFloatToPtr(const float num)1611 UNITY_INTERNAL_PTR UnityFloatToPtr(const float num)
1612 {
1613 UnityQuickCompare.f = num;
1614 return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.f);
1615 }
1616 #endif
1617
1618 #ifndef UNITY_EXCLUDE_DOUBLE
1619 /*-----------------------------------------------*/
UnityDoubleToPtr(const double num)1620 UNITY_INTERNAL_PTR UnityDoubleToPtr(const double num)
1621 {
1622 UnityQuickCompare.d = num;
1623 return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.d);
1624 }
1625 #endif
1626
1627 /*-----------------------------------------------
1628 * printf helper function
1629 *-----------------------------------------------*/
1630 #ifdef UNITY_INCLUDE_PRINT_FORMATTED
UnityPrintFVA(const char * format,va_list va)1631 static void UnityPrintFVA(const char* format, va_list va)
1632 {
1633 const char* pch = format;
1634 if (pch != NULL)
1635 {
1636 while (*pch)
1637 {
1638 /* format identification character */
1639 if (*pch == '%')
1640 {
1641 pch++;
1642
1643 if (pch != NULL)
1644 {
1645 switch (*pch)
1646 {
1647 case 'd':
1648 case 'i':
1649 {
1650 const int number = va_arg(va, int);
1651 UnityPrintNumber((UNITY_INT)number);
1652 break;
1653 }
1654 #ifndef UNITY_EXCLUDE_FLOAT_PRINT
1655 case 'f':
1656 case 'g':
1657 {
1658 const double number = va_arg(va, double);
1659 UnityPrintFloat((UNITY_DOUBLE)number);
1660 break;
1661 }
1662 #endif
1663 case 'u':
1664 {
1665 const unsigned int number = va_arg(va, unsigned int);
1666 UnityPrintNumberUnsigned((UNITY_UINT)number);
1667 break;
1668 }
1669 case 'b':
1670 {
1671 const unsigned int number = va_arg(va, unsigned int);
1672 const UNITY_UINT mask = (UNITY_UINT)0 - (UNITY_UINT)1;
1673 UNITY_OUTPUT_CHAR('0');
1674 UNITY_OUTPUT_CHAR('b');
1675 UnityPrintMask(mask, (UNITY_UINT)number);
1676 break;
1677 }
1678 case 'x':
1679 case 'X':
1680 case 'p':
1681 {
1682 const unsigned int number = va_arg(va, unsigned int);
1683 UNITY_OUTPUT_CHAR('0');
1684 UNITY_OUTPUT_CHAR('x');
1685 UnityPrintNumberHex((UNITY_UINT)number, 8);
1686 break;
1687 }
1688 case 'c':
1689 {
1690 const int ch = va_arg(va, int);
1691 UnityPrintChar((const char *)&ch);
1692 break;
1693 }
1694 case 's':
1695 {
1696 const char * string = va_arg(va, const char *);
1697 UnityPrint(string);
1698 break;
1699 }
1700 case '%':
1701 {
1702 UnityPrintChar(pch);
1703 break;
1704 }
1705 default:
1706 {
1707 /* print the unknown format character */
1708 UNITY_OUTPUT_CHAR('%');
1709 UnityPrintChar(pch);
1710 break;
1711 }
1712 }
1713 }
1714 }
1715 #ifdef UNITY_OUTPUT_COLOR
1716 /* print ANSI escape code */
1717 else if ((*pch == 27) && (*(pch + 1) == '['))
1718 {
1719 pch += UnityPrintAnsiEscapeString(pch);
1720 continue;
1721 }
1722 #endif
1723 else if (*pch == '\n')
1724 {
1725 UNITY_PRINT_EOL();
1726 }
1727 else
1728 {
1729 UnityPrintChar(pch);
1730 }
1731
1732 pch++;
1733 }
1734 }
1735 }
1736
UnityPrintF(const UNITY_LINE_TYPE line,const char * format,...)1737 void UnityPrintF(const UNITY_LINE_TYPE line, const char* format, ...)
1738 {
1739 UnityTestResultsBegin(Unity.TestFile, line);
1740 UnityPrint("INFO");
1741 if(format != NULL)
1742 {
1743 UnityPrint(": ");
1744 va_list va;
1745 va_start(va, format);
1746 UnityPrintFVA(format, va);
1747 va_end(va);
1748 }
1749 UNITY_PRINT_EOL();
1750 }
1751 #endif /* ! UNITY_INCLUDE_PRINT_FORMATTED */
1752
1753
1754 /*-----------------------------------------------
1755 * Control Functions
1756 *-----------------------------------------------*/
1757
1758 /*-----------------------------------------------*/
UnityFail(const char * msg,const UNITY_LINE_TYPE line)1759 void UnityFail(const char* msg, const UNITY_LINE_TYPE line)
1760 {
1761 RETURN_IF_FAIL_OR_IGNORE;
1762
1763 UnityTestResultsBegin(Unity.TestFile, line);
1764 UnityPrint(UnityStrFail);
1765 if (msg != NULL)
1766 {
1767 UNITY_OUTPUT_CHAR(':');
1768
1769 #ifdef UNITY_PRINT_TEST_CONTEXT
1770 UNITY_PRINT_TEST_CONTEXT();
1771 #endif
1772 #ifndef UNITY_EXCLUDE_DETAILS
1773 if (Unity.CurrentDetail1)
1774 {
1775 UnityPrint(UnityStrDetail1Name);
1776 UnityPrint(Unity.CurrentDetail1);
1777 if (Unity.CurrentDetail2)
1778 {
1779 UnityPrint(UnityStrDetail2Name);
1780 UnityPrint(Unity.CurrentDetail2);
1781 }
1782 UnityPrint(UnityStrSpacer);
1783 }
1784 #endif
1785 if (msg[0] != ' ')
1786 {
1787 UNITY_OUTPUT_CHAR(' ');
1788 }
1789 UnityPrint(msg);
1790 }
1791
1792 UNITY_FAIL_AND_BAIL;
1793 }
1794
1795 /*-----------------------------------------------*/
UnityIgnore(const char * msg,const UNITY_LINE_TYPE line)1796 void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
1797 {
1798 RETURN_IF_FAIL_OR_IGNORE;
1799
1800 UnityTestResultsBegin(Unity.TestFile, line);
1801 UnityPrint(UnityStrIgnore);
1802 if (msg != NULL)
1803 {
1804 UNITY_OUTPUT_CHAR(':');
1805 UNITY_OUTPUT_CHAR(' ');
1806 UnityPrint(msg);
1807 }
1808 UNITY_IGNORE_AND_BAIL;
1809 }
1810
1811 /*-----------------------------------------------*/
UnityMessage(const char * msg,const UNITY_LINE_TYPE line)1812 void UnityMessage(const char* msg, const UNITY_LINE_TYPE line)
1813 {
1814 UnityTestResultsBegin(Unity.TestFile, line);
1815 UnityPrint("INFO");
1816 if (msg != NULL)
1817 {
1818 UNITY_OUTPUT_CHAR(':');
1819 UNITY_OUTPUT_CHAR(' ');
1820 UnityPrint(msg);
1821 }
1822 UNITY_PRINT_EOL();
1823 }
1824
1825 /*-----------------------------------------------*/
1826 /* If we have not defined our own test runner, then include our default test runner to make life easier */
1827 #ifndef UNITY_SKIP_DEFAULT_RUNNER
UnityDefaultTestRun(UnityTestFunction Func,const char * FuncName,const int FuncLineNum)1828 void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
1829 {
1830 Unity.CurrentTestName = FuncName;
1831 Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum;
1832 Unity.NumberOfTests++;
1833 UNITY_CLR_DETAILS();
1834 UNITY_EXEC_TIME_START();
1835 if (TEST_PROTECT())
1836 {
1837 setUp();
1838 Func();
1839 }
1840 if (TEST_PROTECT())
1841 {
1842 tearDown();
1843 }
1844 UNITY_EXEC_TIME_STOP();
1845 UnityConcludeTest();
1846 }
1847 #endif
1848
1849 /*-----------------------------------------------*/
UnitySetTestFile(const char * filename)1850 void UnitySetTestFile(const char* filename)
1851 {
1852 Unity.TestFile = filename;
1853 }
1854
1855 /*-----------------------------------------------*/
UnityBegin(const char * filename)1856 void UnityBegin(const char* filename)
1857 {
1858 Unity.TestFile = filename;
1859 Unity.CurrentTestName = NULL;
1860 Unity.CurrentTestLineNumber = 0;
1861 Unity.NumberOfTests = 0;
1862 Unity.TestFailures = 0;
1863 Unity.TestIgnores = 0;
1864 Unity.CurrentTestFailed = 0;
1865 Unity.CurrentTestIgnored = 0;
1866
1867 UNITY_CLR_DETAILS();
1868 UNITY_OUTPUT_START();
1869 }
1870
1871 /*-----------------------------------------------*/
UnityEnd(void)1872 int UnityEnd(void)
1873 {
1874 UNITY_PRINT_EOL();
1875 UnityPrint(UnityStrBreaker);
1876 UNITY_PRINT_EOL();
1877 UnityPrintNumber((UNITY_INT)(Unity.NumberOfTests));
1878 UnityPrint(UnityStrResultsTests);
1879 UnityPrintNumber((UNITY_INT)(Unity.TestFailures));
1880 UnityPrint(UnityStrResultsFailures);
1881 UnityPrintNumber((UNITY_INT)(Unity.TestIgnores));
1882 UnityPrint(UnityStrResultsIgnored);
1883 UNITY_PRINT_EOL();
1884 if (Unity.TestFailures == 0U)
1885 {
1886 UnityPrint(UnityStrOk);
1887 }
1888 else
1889 {
1890 UnityPrint(UnityStrFail);
1891 #ifdef UNITY_DIFFERENTIATE_FINAL_FAIL
1892 UNITY_OUTPUT_CHAR('E'); UNITY_OUTPUT_CHAR('D');
1893 #endif
1894 }
1895 UNITY_PRINT_EOL();
1896 UNITY_FLUSH_CALL();
1897 UNITY_OUTPUT_COMPLETE();
1898 return (int)(Unity.TestFailures);
1899 }
1900
1901 /*-----------------------------------------------
1902 * Command Line Argument Support
1903 *-----------------------------------------------*/
1904 #ifdef UNITY_USE_COMMAND_LINE_ARGS
1905
1906 char* UnityOptionIncludeNamed = NULL;
1907 char* UnityOptionExcludeNamed = NULL;
1908 int UnityVerbosity = 1;
1909
1910 /*-----------------------------------------------*/
UnityParseOptions(int argc,char ** argv)1911 int UnityParseOptions(int argc, char** argv)
1912 {
1913 int i;
1914 UnityOptionIncludeNamed = NULL;
1915 UnityOptionExcludeNamed = NULL;
1916
1917 for (i = 1; i < argc; i++)
1918 {
1919 if (argv[i][0] == '-')
1920 {
1921 switch (argv[i][1])
1922 {
1923 case 'l': /* list tests */
1924 return -1;
1925 case 'n': /* include tests with name including this string */
1926 case 'f': /* an alias for -n */
1927 if (argv[i][2] == '=')
1928 {
1929 UnityOptionIncludeNamed = &argv[i][3];
1930 }
1931 else if (++i < argc)
1932 {
1933 UnityOptionIncludeNamed = argv[i];
1934 }
1935 else
1936 {
1937 UnityPrint("ERROR: No Test String to Include Matches For");
1938 UNITY_PRINT_EOL();
1939 return 1;
1940 }
1941 break;
1942 case 'q': /* quiet */
1943 UnityVerbosity = 0;
1944 break;
1945 case 'v': /* verbose */
1946 UnityVerbosity = 2;
1947 break;
1948 case 'x': /* exclude tests with name including this string */
1949 if (argv[i][2] == '=')
1950 {
1951 UnityOptionExcludeNamed = &argv[i][3];
1952 }
1953 else if (++i < argc)
1954 {
1955 UnityOptionExcludeNamed = argv[i];
1956 }
1957 else
1958 {
1959 UnityPrint("ERROR: No Test String to Exclude Matches For");
1960 UNITY_PRINT_EOL();
1961 return 1;
1962 }
1963 break;
1964 default:
1965 UnityPrint("ERROR: Unknown Option ");
1966 UNITY_OUTPUT_CHAR(argv[i][1]);
1967 UNITY_PRINT_EOL();
1968 return 1;
1969 }
1970 }
1971 }
1972
1973 return 0;
1974 }
1975
1976 /*-----------------------------------------------*/
IsStringInBiggerString(const char * longstring,const char * shortstring)1977 int IsStringInBiggerString(const char* longstring, const char* shortstring)
1978 {
1979 const char* lptr = longstring;
1980 const char* sptr = shortstring;
1981 const char* lnext = lptr;
1982
1983 if (*sptr == '*')
1984 {
1985 return 1;
1986 }
1987
1988 while (*lptr)
1989 {
1990 lnext = lptr + 1;
1991
1992 /* If they current bytes match, go on to the next bytes */
1993 while (*lptr && *sptr && (*lptr == *sptr))
1994 {
1995 lptr++;
1996 sptr++;
1997
1998 /* We're done if we match the entire string or up to a wildcard */
1999 if (*sptr == '*')
2000 return 1;
2001 if (*sptr == ',')
2002 return 1;
2003 if (*sptr == '"')
2004 return 1;
2005 if (*sptr == '\'')
2006 return 1;
2007 if (*sptr == ':')
2008 return 2;
2009 if (*sptr == 0)
2010 return 1;
2011 }
2012
2013 /* Otherwise we start in the long pointer 1 character further and try again */
2014 lptr = lnext;
2015 sptr = shortstring;
2016 }
2017
2018 return 0;
2019 }
2020
2021 /*-----------------------------------------------*/
UnityStringArgumentMatches(const char * str)2022 int UnityStringArgumentMatches(const char* str)
2023 {
2024 int retval;
2025 const char* ptr1;
2026 const char* ptr2;
2027 const char* ptrf;
2028
2029 /* Go through the options and get the substrings for matching one at a time */
2030 ptr1 = str;
2031 while (ptr1[0] != 0)
2032 {
2033 if ((ptr1[0] == '"') || (ptr1[0] == '\''))
2034 {
2035 ptr1++;
2036 }
2037
2038 /* look for the start of the next partial */
2039 ptr2 = ptr1;
2040 ptrf = 0;
2041 do
2042 {
2043 ptr2++;
2044 if ((ptr2[0] == ':') && (ptr2[1] != 0) && (ptr2[0] != '\'') && (ptr2[0] != '"') && (ptr2[0] != ','))
2045 {
2046 ptrf = &ptr2[1];
2047 }
2048 } while ((ptr2[0] != 0) && (ptr2[0] != '\'') && (ptr2[0] != '"') && (ptr2[0] != ','));
2049
2050 while ((ptr2[0] != 0) && ((ptr2[0] == ':') || (ptr2[0] == '\'') || (ptr2[0] == '"') || (ptr2[0] == ',')))
2051 {
2052 ptr2++;
2053 }
2054
2055 /* done if complete filename match */
2056 retval = IsStringInBiggerString(Unity.TestFile, ptr1);
2057 if (retval == 1)
2058 {
2059 return retval;
2060 }
2061
2062 /* done if testname match after filename partial match */
2063 if ((retval == 2) && (ptrf != 0))
2064 {
2065 if (IsStringInBiggerString(Unity.CurrentTestName, ptrf))
2066 {
2067 return 1;
2068 }
2069 }
2070
2071 /* done if complete testname match */
2072 if (IsStringInBiggerString(Unity.CurrentTestName, ptr1) == 1)
2073 {
2074 return 1;
2075 }
2076
2077 ptr1 = ptr2;
2078 }
2079
2080 /* we couldn't find a match for any substrings */
2081 return 0;
2082 }
2083
2084 /*-----------------------------------------------*/
UnityTestMatches(void)2085 int UnityTestMatches(void)
2086 {
2087 /* Check if this test name matches the included test pattern */
2088 int retval;
2089 if (UnityOptionIncludeNamed)
2090 {
2091 retval = UnityStringArgumentMatches(UnityOptionIncludeNamed);
2092 }
2093 else
2094 {
2095 retval = 1;
2096 }
2097
2098 /* Check if this test name matches the excluded test pattern */
2099 if (UnityOptionExcludeNamed)
2100 {
2101 if (UnityStringArgumentMatches(UnityOptionExcludeNamed))
2102 {
2103 retval = 0;
2104 }
2105 }
2106
2107 return retval;
2108 }
2109
2110 #endif /* UNITY_USE_COMMAND_LINE_ARGS */
2111 /*-----------------------------------------------*/
2112 #endif /*LV_BUILD_TEST*/
2113
2114