1 /* =========================================================================
2 Unity Project - A Test Framework for C
3 Copyright (c) 2007-14 Mike Karlesky, Mark VanderVoord, Greg Williams
4 [Released under MIT License. Please refer to license.txt for details]
5 ============================================================================ */
9 #define UNITY_FAIL_AND_BAIL { Unity.CurrentTestFailed = 1; longjmp(Unity.AbortFrame, 1); }
10 #define UNITY_IGNORE_AND_BAIL { Unity.CurrentTestIgnored = 1; longjmp(Unity.AbortFrame, 1); }
11 /// return prematurely if we are already in failure or ignore state
12 #define UNITY_SKIP_EXECUTION { if ((Unity.CurrentTestFailed != 0) || (Unity.CurrentTestIgnored != 0)) {return;} }
13 #define UNITY_PRINT_EOL { UNITY_OUTPUT_CHAR('\n'); }
17 const char UnityStrOk[] = "OK";
18 const char UnityStrPass[] = "PASS";
19 const char UnityStrFail[] = "FAIL";
20 const char UnityStrIgnore[] = "IGNORE";
21 const char UnityStrXPASS[] = "XPASS";
22 const char UnityStrXFAIL[] = "XFAIL";
23 const char UnityStrNull[] = "NULL";
24 const char UnityStrSpacer[] = ". ";
25 const char UnityStrExpected[] = " Expected ";
26 const char UnityStrWas[] = " Was ";
27 const char UnityStrTo[] = " To ";
28 const char UnityStrElement[] = " Element ";
29 const char UnityStrByte[] = " Byte ";
30 const char UnityStrMemory[] = " Memory Mismatch.";
31 const char UnityStrDelta[] = " Values Not Within Delta ";
32 const char UnityStrPointless[] = " You Asked Me To Compare Nothing, Which Was Pointless.";
33 const char UnityStrNullPointerForExpected[] = " Expected pointer to be NULL";
34 const char UnityStrNullPointerForActual[] = " Actual pointer was NULL";
35 const char UnityStrNot[] = "Not ";
36 const char UnityStrInf[] = "Infinity";
37 const char UnityStrNegInf[] = "Negative Infinity";
38 const char UnityStrNaN[] = "NaN";
39 const char UnityStrDet[] = "Determinate";
40 const char UnityStrErrFloat[] = "Unity Floating Point Disabled";
41 const char UnityStrErrDouble[] = "Unity Double Precision Disabled";
42 const char UnityStrErr64[] = "Unity 64-bit Support Disabled";
43 const char UnityStrBreaker[] = "-----------------------";
44 const char UnityStrResultsTests[] = " Tests: ";
45 const char UnityStrResultsFailures[] = " Failures ";
46 const char UnityStrResultsIgnored[] = " Ignored ";
47 const char UnityStrResultsXFAIL[] = " XFAIL ";
48 const char UnityStrResultsXPASS[] = " XPASS ";
49 const char UnityStrResultsPass[] = " PASS ";
52 #ifndef UNITY_EXCLUDE_FLOAT
53 // Dividing by these constants produces +/- infinity.
54 // The rationale is given in UnityAssertFloatIsInf's body.
55 static const _UF f_zero = 0.0f;
56 #ifndef UNITY_EXCLUDE_DOUBLE
57 static const _UD d_zero = 0.0;
61 // compiler-generic print formatting masks
62 const _U_UINT UnitySizeMask[] =
67 4294967295u, // 0xFFFFFFFF
71 #ifdef UNITY_SUPPORT_64
76 void UnityPrintFail(void);
77 void UnityPrintOk(void);
79 //-----------------------------------------------
80 // Pretty Printers & Test Result Output Handlers
81 //-----------------------------------------------
83 void UnityPrint(const char* string)
85 const char* pch = string;
91 // printable characters plus CR & LF are printed
92 if ((*pch <= 126) && (*pch >= 32))
94 UNITY_OUTPUT_CHAR(*pch);
96 //write escaped carriage returns
99 UNITY_OUTPUT_CHAR('\\');
100 UNITY_OUTPUT_CHAR('r');
102 //write escaped line feeds
105 UNITY_OUTPUT_CHAR('\\');
106 UNITY_OUTPUT_CHAR('n');
108 // unprintable characters are shown as codes
111 UNITY_OUTPUT_CHAR('\\');
112 UnityPrintNumberHex((_U_UINT)*pch, 2);
119 //-----------------------------------------------
120 void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style)
122 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
124 UnityPrintNumber(number);
126 else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
128 UnityPrintNumberUnsigned( (_U_UINT)number & UnitySizeMask[((_U_UINT)style & (_U_UINT)0x0F) - 1] );
132 UnityPrintNumberHex((_U_UINT)number, (char)((style & 0x000F) << 1));
136 //-----------------------------------------------
137 /// basically do an itoa using as little ram as possible
138 void UnityPrintNumber(const _U_SINT number_to_print)
141 _U_SINT next_divisor;
144 if (number_to_print == (1l << (UNITY_LONG_WIDTH-1)))
146 //The largest representable negative number
147 UNITY_OUTPUT_CHAR('-');
148 number = (1ul << (UNITY_LONG_WIDTH-1));
150 else if (number_to_print < 0)
152 //Some other negative number
153 UNITY_OUTPUT_CHAR('-');
154 number = (_U_UINT)(-number_to_print);
159 number = (_U_UINT)number_to_print;
162 // figure out initial divisor
163 while (number / divisor > 9)
165 next_divisor = divisor * 10;
166 if (next_divisor > divisor)
167 divisor = next_divisor;
172 // now mod and print, then divide divisor
175 UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
181 //-----------------------------------------------
182 /// basically do an itoa using as little ram as possible
183 void UnityPrintNumberUnsigned(const _U_UINT number)
186 _U_UINT next_divisor;
188 // figure out initial divisor
189 while (number / divisor > 9)
191 next_divisor = divisor * 10;
192 if (next_divisor > divisor)
193 divisor = next_divisor;
198 // now mod and print, then divide divisor
201 UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
207 //-----------------------------------------------
208 void UnityPrintNumberHex(const _U_UINT number, const char nibbles_to_print)
211 char nibbles = nibbles_to_print;
212 UNITY_OUTPUT_CHAR('0');
213 UNITY_OUTPUT_CHAR('x');
217 nibble = (number >> (--nibbles << 2)) & 0x0000000F;
220 UNITY_OUTPUT_CHAR((char)('0' + nibble));
224 UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble));
229 //-----------------------------------------------
230 void UnityPrintMask(const _U_UINT mask, const _U_UINT number)
232 _U_UINT current_bit = (_U_UINT)1 << (UNITY_INT_WIDTH - 1);
235 for (i = 0; i < UNITY_INT_WIDTH; i++)
237 if (current_bit & mask)
239 if (current_bit & number)
241 UNITY_OUTPUT_CHAR('1');
245 UNITY_OUTPUT_CHAR('0');
250 UNITY_OUTPUT_CHAR('X');
252 current_bit = current_bit >> 1;
256 //-----------------------------------------------
257 #ifdef UNITY_FLOAT_VERBOSE
259 void UnityPrintFloat(_UF number)
262 sprintf(TempBuffer, "%.6f", number);
263 UnityPrint(TempBuffer);
267 //-----------------------------------------------
269 void UnityPrintFail(void)
271 UnityPrint(UnityStrFail);
274 void UnityPrintOk(void)
276 UnityPrint(UnityStrOk);
279 //-----------------------------------------------
280 static void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line)
283 UNITY_OUTPUT_CHAR(':');
284 UnityPrintNumber((_U_SINT)line);
285 UNITY_OUTPUT_CHAR(':');
286 UnityPrint(Unity.CurrentTestName);
287 UNITY_OUTPUT_CHAR(':');
290 //-----------------------------------------------
291 static void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)
293 UnityTestResultsBegin(Unity.TestFile, line);
294 if (Unity.isExpectingFail)
296 UnityPrint(UnityStrXFAIL);
300 UnityPrint(UnityStrFail);
303 UNITY_OUTPUT_CHAR(':');
306 //-----------------------------------------------
307 void UnityConcludeTest(void)
310 if (Unity.isExpectingFail == 1 && Unity.CurrentTestFailed == 0)
312 printf("FAIL WAS EXPECTED, BUT IT DIDN'T HAPPEN?!");
318 //cant be ignored and accepting fail at the same time!
319 if (Unity.isExpectingFail == 1 && Unity.CurrentTestFailed == 1)
321 Unity.TestXFAILS++; //error message?!
322 if (Unity.XFAILMessage != NULL)
324 if (Unity.XFAILMessage[0] != ' ')
330 printf(Unity.XFAILMessage);
331 Unity.XFAILMessage = NULL;
335 printf(" - EXPECTED FAIL!");
342 if (Unity.CurrentTestIgnored)
346 else if (!Unity.CurrentTestFailed)
348 if(Unity.isExpectingFail == 0) {
349 UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);
350 UnityPrint(UnityStrPass);
354 //probably should remove the if... part
355 else if (Unity.isExpectingFail == 1 && Unity.CurrentTestFailed == 0)
358 UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);
359 UnityPrint(UnityStrXPASS);
362 printf(" - FAIL WAS EXPECTED, BUT DIDN'T HAPPEN?!");
363 //if (Unity.TestPasses > 0) { Unity.TestPasses--; }
368 Unity.TestFailures++;
371 Unity.CurrentTestFailed = 0;
372 Unity.CurrentTestIgnored = 0;
373 Unity.isExpectingFail = 0;
378 //-----------------------------------------------
379 static void UnityAddMsgIfSpecified(const char* msg)
383 UnityPrint(UnityStrSpacer);
388 //-----------------------------------------------
389 static void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual)
391 UnityPrint(UnityStrExpected);
392 if (expected != NULL)
394 UNITY_OUTPUT_CHAR('\'');
395 UnityPrint(expected);
396 UNITY_OUTPUT_CHAR('\'');
400 UnityPrint(UnityStrNull);
402 UnityPrint(UnityStrWas);
405 UNITY_OUTPUT_CHAR('\'');
407 UNITY_OUTPUT_CHAR('\'');
411 UnityPrint(UnityStrNull);
415 //-----------------------------------------------
416 // Assertion & Control Helpers
417 //-----------------------------------------------
419 static int UnityCheckArraysForNull(UNITY_PTR_ATTRIBUTE const void* expected, UNITY_PTR_ATTRIBUTE const void* actual, const UNITY_LINE_TYPE lineNumber, const char* msg)
421 //return true if they are both NULL
422 if ((expected == NULL) && (actual == NULL))
425 //throw error if just expected is NULL
426 if (expected == NULL)
428 UnityTestResultsFailBegin(lineNumber);
429 UnityPrint(UnityStrNullPointerForExpected);
430 UnityAddMsgIfSpecified(msg);
434 //throw error if just actual is NULL
437 UnityTestResultsFailBegin(lineNumber);
438 UnityPrint(UnityStrNullPointerForActual);
439 UnityAddMsgIfSpecified(msg);
443 //return false if neither is NULL
447 //-----------------------------------------------
448 // Assertion Functions
449 //-----------------------------------------------
451 void UnityAssertBits(const _U_SINT mask,
452 const _U_SINT expected,
453 const _U_SINT actual,
455 const UNITY_LINE_TYPE lineNumber)
457 UNITY_SKIP_EXECUTION;
459 if ((mask & expected) != (mask & actual))
461 UnityTestResultsFailBegin(lineNumber);
462 UnityPrint(UnityStrExpected);
463 UnityPrintMask((_U_UINT)mask, (_U_UINT)expected);
464 UnityPrint(UnityStrWas);
465 UnityPrintMask((_U_UINT)mask, (_U_UINT)actual);
466 UnityAddMsgIfSpecified(msg);
471 //-----------------------------------------------
472 void UnityAssertEqualNumber(const _U_SINT expected,
473 const _U_SINT actual,
475 const UNITY_LINE_TYPE lineNumber,
476 const UNITY_DISPLAY_STYLE_T style)
478 UNITY_SKIP_EXECUTION;
480 if (expected != actual)
482 UnityTestResultsFailBegin(lineNumber);
483 UnityPrint(UnityStrExpected);
484 UnityPrintNumberByStyle(expected, style);
485 UnityPrint(UnityStrWas);
486 UnityPrintNumberByStyle(actual, style);
487 UnityAddMsgIfSpecified(msg);
492 //-----------------------------------------------
493 void UnityAssertEqualIntArray(UNITY_PTR_ATTRIBUTE const void* expected,
494 UNITY_PTR_ATTRIBUTE const void* actual,
495 const _UU32 num_elements,
497 const UNITY_LINE_TYPE lineNumber,
498 const UNITY_DISPLAY_STYLE_T style)
500 _UU32 elements = num_elements;
501 UNITY_PTR_ATTRIBUTE const _US8* ptr_exp = (UNITY_PTR_ATTRIBUTE const _US8*)expected;
502 UNITY_PTR_ATTRIBUTE const _US8* ptr_act = (UNITY_PTR_ATTRIBUTE const _US8*)actual;
504 UNITY_SKIP_EXECUTION;
508 UnityTestResultsFailBegin(lineNumber);
509 UnityPrint(UnityStrPointless);
510 UnityAddMsgIfSpecified(msg);
514 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE const void*)expected, (UNITY_PTR_ATTRIBUTE const void*)actual, lineNumber, msg) == 1)
517 // If style is UNITY_DISPLAY_STYLE_INT, we'll fall into the default case rather than the INT16 or INT32 (etc) case
518 // as UNITY_DISPLAY_STYLE_INT includes a flag for UNITY_DISPLAY_RANGE_AUTO, which the width-specific
519 // variants do not. Therefore remove this flag.
520 switch(style & (UNITY_DISPLAY_STYLE_T)(~UNITY_DISPLAY_RANGE_AUTO))
522 case UNITY_DISPLAY_STYLE_HEX8:
523 case UNITY_DISPLAY_STYLE_INT8:
524 case UNITY_DISPLAY_STYLE_UINT8:
527 if (*ptr_exp != *ptr_act)
529 UnityTestResultsFailBegin(lineNumber);
530 UnityPrint(UnityStrElement);
531 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
532 UnityPrint(UnityStrExpected);
533 UnityPrintNumberByStyle(*ptr_exp, style);
534 UnityPrint(UnityStrWas);
535 UnityPrintNumberByStyle(*ptr_act, style);
536 UnityAddMsgIfSpecified(msg);
543 case UNITY_DISPLAY_STYLE_HEX16:
544 case UNITY_DISPLAY_STYLE_INT16:
545 case UNITY_DISPLAY_STYLE_UINT16:
548 if (*(UNITY_PTR_ATTRIBUTE const _US16*)(void*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US16*)(void*)ptr_act)
550 UnityTestResultsFailBegin(lineNumber);
551 UnityPrint(UnityStrElement);
552 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
553 UnityPrint(UnityStrExpected);
554 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US16*)(void*)ptr_exp, style);
555 UnityPrint(UnityStrWas);
556 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US16*)(void*)ptr_act, style);
557 UnityAddMsgIfSpecified(msg);
564 #ifdef UNITY_SUPPORT_64
565 case UNITY_DISPLAY_STYLE_HEX64:
566 case UNITY_DISPLAY_STYLE_INT64:
567 case UNITY_DISPLAY_STYLE_UINT64:
570 if (*(UNITY_PTR_ATTRIBUTE const _US64*)(void*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US64*)(void*)ptr_act)
572 UnityTestResultsFailBegin(lineNumber);
573 UnityPrint(UnityStrElement);
574 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
575 UnityPrint(UnityStrExpected);
576 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US64*)(void*)ptr_exp, style);
577 UnityPrint(UnityStrWas);
578 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US64*)(void*)ptr_act, style);
579 UnityAddMsgIfSpecified(msg);
590 if (*(UNITY_PTR_ATTRIBUTE const _US32*)(void*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US32*)(void*)ptr_act)
592 UnityTestResultsFailBegin(lineNumber);
593 UnityPrint(UnityStrElement);
594 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
595 UnityPrint(UnityStrExpected);
596 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US32*)(void*)ptr_exp, style);
597 UnityPrint(UnityStrWas);
598 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US32*)(void*)ptr_act, style);
599 UnityAddMsgIfSpecified(msg);
609 //-----------------------------------------------
610 #ifndef UNITY_EXCLUDE_FLOAT
611 void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const _UF* expected,
612 UNITY_PTR_ATTRIBUTE const _UF* actual,
613 const _UU32 num_elements,
615 const UNITY_LINE_TYPE lineNumber)
617 _UU32 elements = num_elements;
618 UNITY_PTR_ATTRIBUTE const _UF* ptr_expected = expected;
619 UNITY_PTR_ATTRIBUTE const _UF* ptr_actual = actual;
622 UNITY_SKIP_EXECUTION;
626 UnityTestResultsFailBegin(lineNumber);
627 UnityPrint(UnityStrPointless);
628 UnityAddMsgIfSpecified(msg);
632 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE const void*)expected, (UNITY_PTR_ATTRIBUTE const void*)actual, lineNumber, msg) == 1)
637 diff = *ptr_expected - *ptr_actual;
640 tol = UNITY_FLOAT_PRECISION * *ptr_expected;
644 //This first part of this condition will catch any NaN or Infinite values
645 if ((diff * 0.0f != 0.0f) || (diff > tol))
647 UnityTestResultsFailBegin(lineNumber);
648 UnityPrint(UnityStrElement);
649 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
650 #ifdef UNITY_FLOAT_VERBOSE
651 UnityPrint(UnityStrExpected);
652 UnityPrintFloat(*ptr_expected);
653 UnityPrint(UnityStrWas);
654 UnityPrintFloat(*ptr_actual);
656 UnityPrint(UnityStrDelta);
658 UnityAddMsgIfSpecified(msg);
666 //-----------------------------------------------
667 void UnityAssertFloatsWithin(const _UF delta,
671 const UNITY_LINE_TYPE lineNumber)
673 _UF diff = actual - expected;
674 _UF pos_delta = delta;
676 UNITY_SKIP_EXECUTION;
682 if (pos_delta < 0.0f)
684 pos_delta = 0.0f - pos_delta;
687 //This first part of this condition will catch any NaN or Infinite values
688 if ((diff * 0.0f != 0.0f) || (pos_delta < diff))
690 UnityTestResultsFailBegin(lineNumber);
691 #ifdef UNITY_FLOAT_VERBOSE
692 UnityPrint(UnityStrExpected);
693 UnityPrintFloat(expected);
694 UnityPrint(UnityStrWas);
695 UnityPrintFloat(actual);
697 UnityPrint(UnityStrDelta);
699 UnityAddMsgIfSpecified(msg);
704 //-----------------------------------------------
705 void UnityAssertFloatSpecial(const _UF actual,
707 const UNITY_LINE_TYPE lineNumber,
708 const UNITY_FLOAT_TRAIT_T style)
710 const char* trait_names[] = { UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet };
711 _U_SINT should_be_trait = ((_U_SINT)style & 1);
712 _U_SINT is_trait = !should_be_trait;
713 _U_SINT trait_index = style >> 1;
715 UNITY_SKIP_EXECUTION;
719 //To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly
720 //We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise
721 case UNITY_FLOAT_IS_INF:
722 case UNITY_FLOAT_IS_NOT_INF:
723 is_trait = ((1.0f / f_zero) == actual) ? 1 : 0;
725 case UNITY_FLOAT_IS_NEG_INF:
726 case UNITY_FLOAT_IS_NOT_NEG_INF:
727 is_trait = ((-1.0f / f_zero) == actual) ? 1 : 0;
730 //NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN.
731 case UNITY_FLOAT_IS_NAN:
732 case UNITY_FLOAT_IS_NOT_NAN:
733 is_trait = (actual == actual) ? 0 : 1;
736 //A determinate number is non infinite and not NaN. (therefore the opposite of the two above)
737 case UNITY_FLOAT_IS_DET:
738 case UNITY_FLOAT_IS_NOT_DET:
739 if ( (actual != actual) || ((1.0f / f_zero) == actual) || ((-1.0f / f_zero) == actual) )
748 if (is_trait != should_be_trait)
750 UnityTestResultsFailBegin(lineNumber);
751 UnityPrint(UnityStrExpected);
752 if (!should_be_trait)
753 UnityPrint(UnityStrNot);
754 UnityPrint(trait_names[trait_index]);
755 UnityPrint(UnityStrWas);
756 #ifdef UNITY_FLOAT_VERBOSE
757 UnityPrintFloat(actual);
760 UnityPrint(UnityStrNot);
761 UnityPrint(trait_names[trait_index]);
763 UnityAddMsgIfSpecified(msg);
768 #endif //not UNITY_EXCLUDE_FLOAT
770 //-----------------------------------------------
771 #ifndef UNITY_EXCLUDE_DOUBLE
772 void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected,
773 UNITY_PTR_ATTRIBUTE const _UD* actual,
774 const _UU32 num_elements,
776 const UNITY_LINE_TYPE lineNumber)
778 _UU32 elements = num_elements;
779 UNITY_PTR_ATTRIBUTE const _UD* ptr_expected = expected;
780 UNITY_PTR_ATTRIBUTE const _UD* ptr_actual = actual;
783 UNITY_SKIP_EXECUTION;
787 UnityTestResultsFailBegin(lineNumber);
788 UnityPrint(UnityStrPointless);
789 UnityAddMsgIfSpecified(msg);
793 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE void*)expected, (UNITY_PTR_ATTRIBUTE void*)actual, lineNumber, msg) == 1)
798 diff = *ptr_expected - *ptr_actual;
801 tol = UNITY_DOUBLE_PRECISION * *ptr_expected;
805 //This first part of this condition will catch any NaN or Infinite values
806 if ((diff * 0.0 != 0.0) || (diff > tol))
808 UnityTestResultsFailBegin(lineNumber);
809 UnityPrint(UnityStrElement);
810 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
811 #ifdef UNITY_DOUBLE_VERBOSE
812 UnityPrint(UnityStrExpected);
813 UnityPrintFloat((float)(*ptr_expected));
814 UnityPrint(UnityStrWas);
815 UnityPrintFloat((float)(*ptr_actual));
817 UnityPrint(UnityStrDelta);
819 UnityAddMsgIfSpecified(msg);
827 //-----------------------------------------------
828 void UnityAssertDoublesWithin(const _UD delta,
832 const UNITY_LINE_TYPE lineNumber)
834 _UD diff = actual - expected;
835 _UD pos_delta = delta;
837 UNITY_SKIP_EXECUTION;
845 pos_delta = 0.0 - pos_delta;
848 //This first part of this condition will catch any NaN or Infinite values
849 if ((diff * 0.0 != 0.0) || (pos_delta < diff))
851 UnityTestResultsFailBegin(lineNumber);
852 #ifdef UNITY_DOUBLE_VERBOSE
853 UnityPrint(UnityStrExpected);
854 UnityPrintFloat((float)expected);
855 UnityPrint(UnityStrWas);
856 UnityPrintFloat((float)actual);
858 UnityPrint(UnityStrDelta);
860 UnityAddMsgIfSpecified(msg);
865 //-----------------------------------------------
867 void UnityAssertDoubleSpecial(const _UD actual,
869 const UNITY_LINE_TYPE lineNumber,
870 const UNITY_FLOAT_TRAIT_T style)
872 const char* trait_names[] = { UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet };
873 _U_SINT should_be_trait = ((_U_SINT)style & 1);
874 _U_SINT is_trait = !should_be_trait;
875 _U_SINT trait_index = style >> 1;
877 UNITY_SKIP_EXECUTION;
881 //To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly
882 //We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise
883 case UNITY_FLOAT_IS_INF:
884 case UNITY_FLOAT_IS_NOT_INF:
885 is_trait = ((1.0 / d_zero) == actual) ? 1 : 0;
887 case UNITY_FLOAT_IS_NEG_INF:
888 case UNITY_FLOAT_IS_NOT_NEG_INF:
889 is_trait = ((-1.0 / d_zero) == actual) ? 1 : 0;
892 //NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN.
893 case UNITY_FLOAT_IS_NAN:
894 case UNITY_FLOAT_IS_NOT_NAN:
895 is_trait = (actual == actual) ? 0 : 1;
898 //A determinate number is non infinite and not NaN. (therefore the opposite of the two above)
899 case UNITY_FLOAT_IS_DET:
900 case UNITY_FLOAT_IS_NOT_DET:
901 if ( (actual != actual) || ((1.0 / d_zero) == actual) || ((-1.0 / d_zero) == actual) )
910 if (is_trait != should_be_trait)
912 UnityTestResultsFailBegin(lineNumber);
913 UnityPrint(UnityStrExpected);
914 if (!should_be_trait)
915 UnityPrint(UnityStrNot);
916 UnityPrint(trait_names[trait_index]);
917 UnityPrint(UnityStrWas);
918 #ifdef UNITY_DOUBLE_VERBOSE
919 UnityPrintFloat(actual);
922 UnityPrint(UnityStrNot);
923 UnityPrint(trait_names[trait_index]);
925 UnityAddMsgIfSpecified(msg);
931 #endif // not UNITY_EXCLUDE_DOUBLE
933 //-----------------------------------------------
934 void UnityAssertNumbersWithin( const _U_SINT delta,
935 const _U_SINT expected,
936 const _U_SINT actual,
938 const UNITY_LINE_TYPE lineNumber,
939 const UNITY_DISPLAY_STYLE_T style)
941 UNITY_SKIP_EXECUTION;
943 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
945 if (actual > expected)
946 Unity.CurrentTestFailed = ((actual - expected) > delta);
948 Unity.CurrentTestFailed = ((expected - actual) > delta);
952 if ((_U_UINT)actual > (_U_UINT)expected)
953 Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > (_U_UINT)delta);
955 Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > (_U_UINT)delta);
958 if (Unity.CurrentTestFailed)
960 UnityTestResultsFailBegin(lineNumber);
961 UnityPrint(UnityStrDelta);
962 UnityPrintNumberByStyle(delta, style);
963 UnityPrint(UnityStrExpected);
964 UnityPrintNumberByStyle(expected, style);
965 UnityPrint(UnityStrWas);
966 UnityPrintNumberByStyle(actual, style);
967 UnityAddMsgIfSpecified(msg);
972 //-----------------------------------------------
973 void UnityAssertEqualString(const char* expected,
976 const UNITY_LINE_TYPE lineNumber)
980 UNITY_SKIP_EXECUTION;
982 // if both pointers not null compare the strings
983 if (expected && actual)
985 for (i = 0; expected[i] || actual[i]; i++)
987 if (expected[i] != actual[i])
989 Unity.CurrentTestFailed = 1;
995 { // handle case of one pointers being null (if both null, test should pass)
996 if (expected != actual)
998 Unity.CurrentTestFailed = 1;
1002 if (Unity.CurrentTestFailed)
1004 UnityTestResultsFailBegin(lineNumber);
1005 UnityPrintExpectedAndActualStrings(expected, actual);
1006 UnityAddMsgIfSpecified(msg);
1007 UNITY_FAIL_AND_BAIL;
1011 //-----------------------------------------------
1012 void UnityAssertEqualStringArray( const char** expected,
1013 const char** actual,
1014 const _UU32 num_elements,
1016 const UNITY_LINE_TYPE lineNumber)
1020 UNITY_SKIP_EXECUTION;
1022 // if no elements, it's an error
1023 if (num_elements == 0)
1025 UnityTestResultsFailBegin(lineNumber);
1026 UnityPrint(UnityStrPointless);
1027 UnityAddMsgIfSpecified(msg);
1028 UNITY_FAIL_AND_BAIL;
1031 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE void*)expected, (UNITY_PTR_ATTRIBUTE void*)actual, lineNumber, msg) == 1)
1036 // if both pointers not null compare the strings
1037 if (expected[j] && actual[j])
1039 for (i = 0; expected[j][i] || actual[j][i]; i++)
1041 if (expected[j][i] != actual[j][i])
1043 Unity.CurrentTestFailed = 1;
1049 { // handle case of one pointers being null (if both null, test should pass)
1050 if (expected[j] != actual[j])
1052 Unity.CurrentTestFailed = 1;
1056 if (Unity.CurrentTestFailed)
1058 UnityTestResultsFailBegin(lineNumber);
1059 if (num_elements > 1)
1061 UnityPrint(UnityStrElement);
1062 UnityPrintNumberByStyle((j), UNITY_DISPLAY_STYLE_UINT);
1064 UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j]));
1065 UnityAddMsgIfSpecified(msg);
1066 UNITY_FAIL_AND_BAIL;
1068 } while (++j < num_elements);
1071 //-----------------------------------------------
1072 void UnityAssertEqualMemory( UNITY_PTR_ATTRIBUTE const void* expected,
1073 UNITY_PTR_ATTRIBUTE const void* actual,
1075 const _UU32 num_elements,
1077 const UNITY_LINE_TYPE lineNumber)
1079 UNITY_PTR_ATTRIBUTE const unsigned char* ptr_exp = (UNITY_PTR_ATTRIBUTE const unsigned char*)expected;
1080 UNITY_PTR_ATTRIBUTE const unsigned char* ptr_act = (UNITY_PTR_ATTRIBUTE const unsigned char*)actual;
1081 _UU32 elements = num_elements;
1084 UNITY_SKIP_EXECUTION;
1086 if ((elements == 0) || (length == 0))
1088 UnityTestResultsFailBegin(lineNumber);
1089 UnityPrint(UnityStrPointless);
1090 UnityAddMsgIfSpecified(msg);
1091 UNITY_FAIL_AND_BAIL;
1094 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE const void*)expected, (UNITY_PTR_ATTRIBUTE const void*)actual, lineNumber, msg) == 1)
1099 /////////////////////////////////////
1103 if (*ptr_exp != *ptr_act)
1105 UnityTestResultsFailBegin(lineNumber);
1106 UnityPrint(UnityStrMemory);
1107 if (num_elements > 1)
1109 UnityPrint(UnityStrElement);
1110 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
1112 UnityPrint(UnityStrByte);
1113 UnityPrintNumberByStyle((length - bytes - 1), UNITY_DISPLAY_STYLE_UINT);
1114 UnityPrint(UnityStrExpected);
1115 UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
1116 UnityPrint(UnityStrWas);
1117 UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
1118 UnityAddMsgIfSpecified(msg);
1119 UNITY_FAIL_AND_BAIL;
1124 /////////////////////////////////////
1129 //-----------------------------------------------
1130 // Control Functions
1131 //-----------------------------------------------
1133 void UnityFail(const char* msg, const UNITY_LINE_TYPE line)
1135 UNITY_SKIP_EXECUTION;
1137 UnityTestResultsBegin(Unity.TestFile, line);
1141 UNITY_OUTPUT_CHAR(':');
1144 UNITY_OUTPUT_CHAR(' ');
1148 UNITY_FAIL_AND_BAIL;
1151 //-----------------------------------------------
1152 void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
1154 UNITY_SKIP_EXECUTION;
1156 UnityTestResultsBegin(Unity.TestFile, line);
1157 UnityPrint(UnityStrIgnore);
1160 UNITY_OUTPUT_CHAR(':');
1161 UNITY_OUTPUT_CHAR(' ');
1164 UNITY_IGNORE_AND_BAIL;
1167 //----------------------------------------------
1169 void UnityExpectFail(){
1171 Unity.isExpectingFail = 1;
1175 void UnityExpectFailMessage(const char* msg, const UNITY_LINE_TYPE line ){
1177 Unity.isExpectingFail = 1;
1180 Unity.XFAILMessage = msg;
1184 //-----------------------------------------------
1185 #if defined(UNITY_WEAK_ATTRIBUTE)
1187 void tearDown(void);
1188 UNITY_WEAK_ATTRIBUTE void setUp(void) { }
1189 UNITY_WEAK_ATTRIBUTE void tearDown(void) { }
1190 #elif defined(UNITY_WEAK_PRAGMA)
1193 # pragma weak tearDown
1194 void tearDown(void);
1197 void tearDown(void);
1200 //-----------------------------------------------
1201 void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
1203 Unity.CurrentTestName = FuncName;
1204 Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum;
1205 Unity.NumberOfTests++;
1212 if (TEST_PROTECT() && !(Unity.CurrentTestIgnored))
1217 UnityConcludeTest();
1221 //-----------------------------------------------
1222 void UnityBegin(const char* filename)
1224 Unity.TestFile = filename;
1225 Unity.CurrentTestName = NULL;
1226 Unity.CurrentTestLineNumber = 0;
1227 Unity.NumberOfTests = 0;
1228 Unity.TestFailures = 0;
1229 Unity.TestIgnores = 0;
1230 Unity.CurrentTestFailed = 0;
1231 Unity.CurrentTestIgnored = 0;
1232 Unity.TestXFAILS = 0;
1233 Unity.isExpectingFail = 0;
1234 Unity.TestPasses = 0;
1235 Unity.TestXPASSES = 0;
1236 Unity.XFAILMessage = NULL;
1238 UNITY_OUTPUT_START();
1242 //-----------------------------------------------
1246 UnityPrint(UnityStrBreaker);
1248 UnityPrintNumber((_U_SINT)(Unity.NumberOfTests));
1249 UnityPrint(UnityStrResultsTests);
1251 UnityPrintNumber((_U_SINT)(Unity.TestPasses));
1252 UnityPrint(UnityStrResultsPass);
1254 UnityPrintNumber((_U_SINT)(Unity.TestXFAILS));
1255 UnityPrint(UnityStrResultsXFAIL);
1257 UnityPrintNumber((_U_SINT)(Unity.TestFailures));
1258 UnityPrint(UnityStrResultsFailures);
1260 UnityPrintNumber((_U_SINT)(Unity.TestXPASSES));
1261 UnityPrint(UnityStrResultsXPASS);
1263 UnityPrintNumber((_U_SINT)(Unity.TestIgnores));
1264 UnityPrint(UnityStrResultsIgnored);
1268 if (Unity.TestFailures == 0U && Unity.TestXPASSES == 0U)
1277 UNITY_OUTPUT_COMPLETE();
1278 return (int)(Unity.TestFailures);
1282 //-----------------------------------------------