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("%s", Unity.XFAILMessage);
331 Unity.XFAILMessage = NULL;
335 printf(" - EXPECTED FAIL!");
340 if (Unity.CurrentTestIgnored)
344 else if (!Unity.CurrentTestFailed)
346 if(Unity.isExpectingFail == 0) {
347 UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);
348 UnityPrint(UnityStrPass);
352 //probably should remove the if... part
353 else if (Unity.isExpectingFail == 1 && Unity.CurrentTestFailed == 0)
356 UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);
357 UnityPrint(UnityStrXPASS);
360 printf(" - FAIL WAS EXPECTED, BUT DIDN'T HAPPEN?!");
361 //if (Unity.TestPasses > 0) { Unity.TestPasses--; }
366 Unity.TestFailures++;
369 Unity.CurrentTestFailed = 0;
370 Unity.CurrentTestIgnored = 0;
371 Unity.isExpectingFail = 0;
376 //-----------------------------------------------
377 static void UnityAddMsgIfSpecified(const char* msg)
381 UnityPrint(UnityStrSpacer);
386 //-----------------------------------------------
387 static void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual)
389 UnityPrint(UnityStrExpected);
390 if (expected != NULL)
392 UNITY_OUTPUT_CHAR('\'');
393 UnityPrint(expected);
394 UNITY_OUTPUT_CHAR('\'');
398 UnityPrint(UnityStrNull);
400 UnityPrint(UnityStrWas);
403 UNITY_OUTPUT_CHAR('\'');
405 UNITY_OUTPUT_CHAR('\'');
409 UnityPrint(UnityStrNull);
413 //-----------------------------------------------
414 // Assertion & Control Helpers
415 //-----------------------------------------------
417 static int UnityCheckArraysForNull(UNITY_PTR_ATTRIBUTE const void* expected, UNITY_PTR_ATTRIBUTE const void* actual, const UNITY_LINE_TYPE lineNumber, const char* msg)
419 //return true if they are both NULL
420 if ((expected == NULL) && (actual == NULL))
423 //throw error if just expected is NULL
424 if (expected == NULL)
426 UnityTestResultsFailBegin(lineNumber);
427 UnityPrint(UnityStrNullPointerForExpected);
428 UnityAddMsgIfSpecified(msg);
432 //throw error if just actual is NULL
435 UnityTestResultsFailBegin(lineNumber);
436 UnityPrint(UnityStrNullPointerForActual);
437 UnityAddMsgIfSpecified(msg);
441 //return false if neither is NULL
445 //-----------------------------------------------
446 // Assertion Functions
447 //-----------------------------------------------
449 void UnityAssertBits(const _U_SINT mask,
450 const _U_SINT expected,
451 const _U_SINT actual,
453 const UNITY_LINE_TYPE lineNumber)
455 UNITY_SKIP_EXECUTION;
457 if ((mask & expected) != (mask & actual))
459 UnityTestResultsFailBegin(lineNumber);
460 UnityPrint(UnityStrExpected);
461 UnityPrintMask((_U_UINT)mask, (_U_UINT)expected);
462 UnityPrint(UnityStrWas);
463 UnityPrintMask((_U_UINT)mask, (_U_UINT)actual);
464 UnityAddMsgIfSpecified(msg);
469 //-----------------------------------------------
470 void UnityAssertEqualNumber(const _U_SINT expected,
471 const _U_SINT actual,
473 const UNITY_LINE_TYPE lineNumber,
474 const UNITY_DISPLAY_STYLE_T style)
476 UNITY_SKIP_EXECUTION;
478 if (expected != actual)
480 UnityTestResultsFailBegin(lineNumber);
481 UnityPrint(UnityStrExpected);
482 UnityPrintNumberByStyle(expected, style);
483 UnityPrint(UnityStrWas);
484 UnityPrintNumberByStyle(actual, style);
485 UnityAddMsgIfSpecified(msg);
490 //-----------------------------------------------
491 void UnityAssertEqualIntArray(UNITY_PTR_ATTRIBUTE const void* expected,
492 UNITY_PTR_ATTRIBUTE const void* actual,
493 const _UU32 num_elements,
495 const UNITY_LINE_TYPE lineNumber,
496 const UNITY_DISPLAY_STYLE_T style)
498 _UU32 elements = num_elements;
499 UNITY_PTR_ATTRIBUTE const _US8* ptr_exp = (UNITY_PTR_ATTRIBUTE const _US8*)expected;
500 UNITY_PTR_ATTRIBUTE const _US8* ptr_act = (UNITY_PTR_ATTRIBUTE const _US8*)actual;
502 UNITY_SKIP_EXECUTION;
506 UnityTestResultsFailBegin(lineNumber);
507 UnityPrint(UnityStrPointless);
508 UnityAddMsgIfSpecified(msg);
512 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE const void*)expected, (UNITY_PTR_ATTRIBUTE const void*)actual, lineNumber, msg) == 1)
515 // If style is UNITY_DISPLAY_STYLE_INT, we'll fall into the default case rather than the INT16 or INT32 (etc) case
516 // as UNITY_DISPLAY_STYLE_INT includes a flag for UNITY_DISPLAY_RANGE_AUTO, which the width-specific
517 // variants do not. Therefore remove this flag.
518 switch(style & (UNITY_DISPLAY_STYLE_T)(~UNITY_DISPLAY_RANGE_AUTO))
520 case UNITY_DISPLAY_STYLE_HEX8:
521 case UNITY_DISPLAY_STYLE_INT8:
522 case UNITY_DISPLAY_STYLE_UINT8:
525 if (*ptr_exp != *ptr_act)
527 UnityTestResultsFailBegin(lineNumber);
528 UnityPrint(UnityStrElement);
529 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
530 UnityPrint(UnityStrExpected);
531 UnityPrintNumberByStyle(*ptr_exp, style);
532 UnityPrint(UnityStrWas);
533 UnityPrintNumberByStyle(*ptr_act, style);
534 UnityAddMsgIfSpecified(msg);
541 case UNITY_DISPLAY_STYLE_HEX16:
542 case UNITY_DISPLAY_STYLE_INT16:
543 case UNITY_DISPLAY_STYLE_UINT16:
546 if (*(UNITY_PTR_ATTRIBUTE const _US16*)(void*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US16*)(void*)ptr_act)
548 UnityTestResultsFailBegin(lineNumber);
549 UnityPrint(UnityStrElement);
550 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
551 UnityPrint(UnityStrExpected);
552 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US16*)(void*)ptr_exp, style);
553 UnityPrint(UnityStrWas);
554 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US16*)(void*)ptr_act, style);
555 UnityAddMsgIfSpecified(msg);
562 #ifdef UNITY_SUPPORT_64
563 case UNITY_DISPLAY_STYLE_HEX64:
564 case UNITY_DISPLAY_STYLE_INT64:
565 case UNITY_DISPLAY_STYLE_UINT64:
568 if (*(UNITY_PTR_ATTRIBUTE const _US64*)(void*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US64*)(void*)ptr_act)
570 UnityTestResultsFailBegin(lineNumber);
571 UnityPrint(UnityStrElement);
572 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
573 UnityPrint(UnityStrExpected);
574 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US64*)(void*)ptr_exp, style);
575 UnityPrint(UnityStrWas);
576 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US64*)(void*)ptr_act, style);
577 UnityAddMsgIfSpecified(msg);
588 if (*(UNITY_PTR_ATTRIBUTE const _US32*)(void*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US32*)(void*)ptr_act)
590 UnityTestResultsFailBegin(lineNumber);
591 UnityPrint(UnityStrElement);
592 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
593 UnityPrint(UnityStrExpected);
594 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US32*)(void*)ptr_exp, style);
595 UnityPrint(UnityStrWas);
596 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US32*)(void*)ptr_act, style);
597 UnityAddMsgIfSpecified(msg);
607 //-----------------------------------------------
608 #ifndef UNITY_EXCLUDE_FLOAT
609 void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const _UF* expected,
610 UNITY_PTR_ATTRIBUTE const _UF* actual,
611 const _UU32 num_elements,
613 const UNITY_LINE_TYPE lineNumber)
615 _UU32 elements = num_elements;
616 UNITY_PTR_ATTRIBUTE const _UF* ptr_expected = expected;
617 UNITY_PTR_ATTRIBUTE const _UF* ptr_actual = actual;
620 UNITY_SKIP_EXECUTION;
624 UnityTestResultsFailBegin(lineNumber);
625 UnityPrint(UnityStrPointless);
626 UnityAddMsgIfSpecified(msg);
630 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE const void*)expected, (UNITY_PTR_ATTRIBUTE const void*)actual, lineNumber, msg) == 1)
635 diff = *ptr_expected - *ptr_actual;
638 tol = UNITY_FLOAT_PRECISION * *ptr_expected;
642 //This first part of this condition will catch any NaN or Infinite values
643 if ((diff * 0.0f != 0.0f) || (diff > tol))
645 UnityTestResultsFailBegin(lineNumber);
646 UnityPrint(UnityStrElement);
647 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
648 #ifdef UNITY_FLOAT_VERBOSE
649 UnityPrint(UnityStrExpected);
650 UnityPrintFloat(*ptr_expected);
651 UnityPrint(UnityStrWas);
652 UnityPrintFloat(*ptr_actual);
654 UnityPrint(UnityStrDelta);
656 UnityAddMsgIfSpecified(msg);
664 //-----------------------------------------------
665 void UnityAssertFloatsWithin(const _UF delta,
669 const UNITY_LINE_TYPE lineNumber)
671 _UF diff = actual - expected;
672 _UF pos_delta = delta;
674 UNITY_SKIP_EXECUTION;
680 if (pos_delta < 0.0f)
682 pos_delta = 0.0f - pos_delta;
685 //This first part of this condition will catch any NaN or Infinite values
686 if ((diff * 0.0f != 0.0f) || (pos_delta < diff))
688 UnityTestResultsFailBegin(lineNumber);
689 #ifdef UNITY_FLOAT_VERBOSE
690 UnityPrint(UnityStrExpected);
691 UnityPrintFloat(expected);
692 UnityPrint(UnityStrWas);
693 UnityPrintFloat(actual);
695 UnityPrint(UnityStrDelta);
697 UnityAddMsgIfSpecified(msg);
702 //-----------------------------------------------
703 void UnityAssertFloatSpecial(const _UF actual,
705 const UNITY_LINE_TYPE lineNumber,
706 const UNITY_FLOAT_TRAIT_T style)
708 const char* trait_names[] = { UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet };
709 _U_SINT should_be_trait = ((_U_SINT)style & 1);
710 _U_SINT is_trait = !should_be_trait;
711 _U_SINT trait_index = style >> 1;
713 UNITY_SKIP_EXECUTION;
717 //To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly
718 //We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise
719 case UNITY_FLOAT_IS_INF:
720 case UNITY_FLOAT_IS_NOT_INF:
721 is_trait = ((1.0f / f_zero) == actual) ? 1 : 0;
723 case UNITY_FLOAT_IS_NEG_INF:
724 case UNITY_FLOAT_IS_NOT_NEG_INF:
725 is_trait = ((-1.0f / f_zero) == actual) ? 1 : 0;
728 //NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN.
729 case UNITY_FLOAT_IS_NAN:
730 case UNITY_FLOAT_IS_NOT_NAN:
731 is_trait = (actual == actual) ? 0 : 1;
734 //A determinate number is non infinite and not NaN. (therefore the opposite of the two above)
735 case UNITY_FLOAT_IS_DET:
736 case UNITY_FLOAT_IS_NOT_DET:
737 if ( (actual != actual) || ((1.0f / f_zero) == actual) || ((-1.0f / f_zero) == actual) )
746 if (is_trait != should_be_trait)
748 UnityTestResultsFailBegin(lineNumber);
749 UnityPrint(UnityStrExpected);
750 if (!should_be_trait)
751 UnityPrint(UnityStrNot);
752 UnityPrint(trait_names[trait_index]);
753 UnityPrint(UnityStrWas);
754 #ifdef UNITY_FLOAT_VERBOSE
755 UnityPrintFloat(actual);
758 UnityPrint(UnityStrNot);
759 UnityPrint(trait_names[trait_index]);
761 UnityAddMsgIfSpecified(msg);
766 #endif //not UNITY_EXCLUDE_FLOAT
768 //-----------------------------------------------
769 #ifndef UNITY_EXCLUDE_DOUBLE
770 void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected,
771 UNITY_PTR_ATTRIBUTE const _UD* actual,
772 const _UU32 num_elements,
774 const UNITY_LINE_TYPE lineNumber)
776 _UU32 elements = num_elements;
777 UNITY_PTR_ATTRIBUTE const _UD* ptr_expected = expected;
778 UNITY_PTR_ATTRIBUTE const _UD* ptr_actual = actual;
781 UNITY_SKIP_EXECUTION;
785 UnityTestResultsFailBegin(lineNumber);
786 UnityPrint(UnityStrPointless);
787 UnityAddMsgIfSpecified(msg);
791 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE void*)expected, (UNITY_PTR_ATTRIBUTE void*)actual, lineNumber, msg) == 1)
796 diff = *ptr_expected - *ptr_actual;
799 tol = UNITY_DOUBLE_PRECISION * *ptr_expected;
803 //This first part of this condition will catch any NaN or Infinite values
804 if ((diff * 0.0 != 0.0) || (diff > tol))
806 UnityTestResultsFailBegin(lineNumber);
807 UnityPrint(UnityStrElement);
808 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
809 #ifdef UNITY_DOUBLE_VERBOSE
810 UnityPrint(UnityStrExpected);
811 UnityPrintFloat((float)(*ptr_expected));
812 UnityPrint(UnityStrWas);
813 UnityPrintFloat((float)(*ptr_actual));
815 UnityPrint(UnityStrDelta);
817 UnityAddMsgIfSpecified(msg);
825 //-----------------------------------------------
826 void UnityAssertDoublesWithin(const _UD delta,
830 const UNITY_LINE_TYPE lineNumber)
832 _UD diff = actual - expected;
833 _UD pos_delta = delta;
835 UNITY_SKIP_EXECUTION;
843 pos_delta = 0.0 - pos_delta;
846 //This first part of this condition will catch any NaN or Infinite values
847 if ((diff * 0.0 != 0.0) || (pos_delta < diff))
849 UnityTestResultsFailBegin(lineNumber);
850 #ifdef UNITY_DOUBLE_VERBOSE
851 UnityPrint(UnityStrExpected);
852 UnityPrintFloat((float)expected);
853 UnityPrint(UnityStrWas);
854 UnityPrintFloat((float)actual);
856 UnityPrint(UnityStrDelta);
858 UnityAddMsgIfSpecified(msg);
863 //-----------------------------------------------
865 void UnityAssertDoubleSpecial(const _UD actual,
867 const UNITY_LINE_TYPE lineNumber,
868 const UNITY_FLOAT_TRAIT_T style)
870 const char* trait_names[] = { UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet };
871 _U_SINT should_be_trait = ((_U_SINT)style & 1);
872 _U_SINT is_trait = !should_be_trait;
873 _U_SINT trait_index = style >> 1;
875 UNITY_SKIP_EXECUTION;
879 //To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly
880 //We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise
881 case UNITY_FLOAT_IS_INF:
882 case UNITY_FLOAT_IS_NOT_INF:
883 is_trait = ((1.0 / d_zero) == actual) ? 1 : 0;
885 case UNITY_FLOAT_IS_NEG_INF:
886 case UNITY_FLOAT_IS_NOT_NEG_INF:
887 is_trait = ((-1.0 / d_zero) == actual) ? 1 : 0;
890 //NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN.
891 case UNITY_FLOAT_IS_NAN:
892 case UNITY_FLOAT_IS_NOT_NAN:
893 is_trait = (actual == actual) ? 0 : 1;
896 //A determinate number is non infinite and not NaN. (therefore the opposite of the two above)
897 case UNITY_FLOAT_IS_DET:
898 case UNITY_FLOAT_IS_NOT_DET:
899 if ( (actual != actual) || ((1.0 / d_zero) == actual) || ((-1.0 / d_zero) == actual) )
908 if (is_trait != should_be_trait)
910 UnityTestResultsFailBegin(lineNumber);
911 UnityPrint(UnityStrExpected);
912 if (!should_be_trait)
913 UnityPrint(UnityStrNot);
914 UnityPrint(trait_names[trait_index]);
915 UnityPrint(UnityStrWas);
916 #ifdef UNITY_DOUBLE_VERBOSE
917 UnityPrintFloat(actual);
920 UnityPrint(UnityStrNot);
921 UnityPrint(trait_names[trait_index]);
923 UnityAddMsgIfSpecified(msg);
929 #endif // not UNITY_EXCLUDE_DOUBLE
931 //-----------------------------------------------
932 void UnityAssertNumbersWithin( const _U_SINT delta,
933 const _U_SINT expected,
934 const _U_SINT actual,
936 const UNITY_LINE_TYPE lineNumber,
937 const UNITY_DISPLAY_STYLE_T style)
939 UNITY_SKIP_EXECUTION;
941 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
943 if (actual > expected)
944 Unity.CurrentTestFailed = ((actual - expected) > delta);
946 Unity.CurrentTestFailed = ((expected - actual) > delta);
950 if ((_U_UINT)actual > (_U_UINT)expected)
951 Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > (_U_UINT)delta);
953 Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > (_U_UINT)delta);
956 if (Unity.CurrentTestFailed)
958 UnityTestResultsFailBegin(lineNumber);
959 UnityPrint(UnityStrDelta);
960 UnityPrintNumberByStyle(delta, style);
961 UnityPrint(UnityStrExpected);
962 UnityPrintNumberByStyle(expected, style);
963 UnityPrint(UnityStrWas);
964 UnityPrintNumberByStyle(actual, style);
965 UnityAddMsgIfSpecified(msg);
970 //-----------------------------------------------
971 void UnityAssertEqualString(const char* expected,
974 const UNITY_LINE_TYPE lineNumber)
978 UNITY_SKIP_EXECUTION;
980 // if both pointers not null compare the strings
981 if (expected && actual)
983 for (i = 0; expected[i] || actual[i]; i++)
985 if (expected[i] != actual[i])
987 Unity.CurrentTestFailed = 1;
993 { // handle case of one pointers being null (if both null, test should pass)
994 if (expected != actual)
996 Unity.CurrentTestFailed = 1;
1000 if (Unity.CurrentTestFailed)
1002 UnityTestResultsFailBegin(lineNumber);
1003 UnityPrintExpectedAndActualStrings(expected, actual);
1004 UnityAddMsgIfSpecified(msg);
1005 UNITY_FAIL_AND_BAIL;
1009 //-----------------------------------------------
1010 void UnityAssertEqualStringArray( const char** expected,
1011 const char** actual,
1012 const _UU32 num_elements,
1014 const UNITY_LINE_TYPE lineNumber)
1018 UNITY_SKIP_EXECUTION;
1020 // if no elements, it's an error
1021 if (num_elements == 0)
1023 UnityTestResultsFailBegin(lineNumber);
1024 UnityPrint(UnityStrPointless);
1025 UnityAddMsgIfSpecified(msg);
1026 UNITY_FAIL_AND_BAIL;
1029 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE void*)expected, (UNITY_PTR_ATTRIBUTE void*)actual, lineNumber, msg) == 1)
1034 // if both pointers not null compare the strings
1035 if (expected[j] && actual[j])
1037 for (i = 0; expected[j][i] || actual[j][i]; i++)
1039 if (expected[j][i] != actual[j][i])
1041 Unity.CurrentTestFailed = 1;
1047 { // handle case of one pointers being null (if both null, test should pass)
1048 if (expected[j] != actual[j])
1050 Unity.CurrentTestFailed = 1;
1054 if (Unity.CurrentTestFailed)
1056 UnityTestResultsFailBegin(lineNumber);
1057 if (num_elements > 1)
1059 UnityPrint(UnityStrElement);
1060 UnityPrintNumberByStyle((j), UNITY_DISPLAY_STYLE_UINT);
1062 UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j]));
1063 UnityAddMsgIfSpecified(msg);
1064 UNITY_FAIL_AND_BAIL;
1066 } while (++j < num_elements);
1069 //-----------------------------------------------
1070 void UnityAssertEqualMemory( UNITY_PTR_ATTRIBUTE const void* expected,
1071 UNITY_PTR_ATTRIBUTE const void* actual,
1073 const _UU32 num_elements,
1075 const UNITY_LINE_TYPE lineNumber)
1077 UNITY_PTR_ATTRIBUTE const unsigned char* ptr_exp = (UNITY_PTR_ATTRIBUTE const unsigned char*)expected;
1078 UNITY_PTR_ATTRIBUTE const unsigned char* ptr_act = (UNITY_PTR_ATTRIBUTE const unsigned char*)actual;
1079 _UU32 elements = num_elements;
1082 UNITY_SKIP_EXECUTION;
1084 if ((elements == 0) || (length == 0))
1086 UnityTestResultsFailBegin(lineNumber);
1087 UnityPrint(UnityStrPointless);
1088 UnityAddMsgIfSpecified(msg);
1089 UNITY_FAIL_AND_BAIL;
1092 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE const void*)expected, (UNITY_PTR_ATTRIBUTE const void*)actual, lineNumber, msg) == 1)
1097 /////////////////////////////////////
1101 if (*ptr_exp != *ptr_act)
1103 UnityTestResultsFailBegin(lineNumber);
1104 UnityPrint(UnityStrMemory);
1105 if (num_elements > 1)
1107 UnityPrint(UnityStrElement);
1108 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
1110 UnityPrint(UnityStrByte);
1111 UnityPrintNumberByStyle((length - bytes - 1), UNITY_DISPLAY_STYLE_UINT);
1112 UnityPrint(UnityStrExpected);
1113 UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
1114 UnityPrint(UnityStrWas);
1115 UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
1116 UnityAddMsgIfSpecified(msg);
1117 UNITY_FAIL_AND_BAIL;
1122 /////////////////////////////////////
1127 //-----------------------------------------------
1128 // Control Functions
1129 //-----------------------------------------------
1131 void UnityFail(const char* msg, const UNITY_LINE_TYPE line)
1133 UNITY_SKIP_EXECUTION;
1135 UnityTestResultsBegin(Unity.TestFile, line);
1139 UNITY_OUTPUT_CHAR(':');
1142 UNITY_OUTPUT_CHAR(' ');
1146 UNITY_FAIL_AND_BAIL;
1149 //-----------------------------------------------
1150 void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
1152 UNITY_SKIP_EXECUTION;
1154 UnityTestResultsBegin(Unity.TestFile, line);
1155 UnityPrint(UnityStrIgnore);
1158 UNITY_OUTPUT_CHAR(':');
1159 UNITY_OUTPUT_CHAR(' ');
1162 UNITY_IGNORE_AND_BAIL;
1165 //----------------------------------------------
1167 void UnityExpectFail(){
1169 Unity.isExpectingFail = 1;
1173 void UnityExpectFailMessage(const char* msg, const UNITY_LINE_TYPE line ){
1175 Unity.isExpectingFail = 1;
1178 Unity.XFAILMessage = msg;
1182 //-----------------------------------------------
1183 #if defined(UNITY_WEAK_ATTRIBUTE)
1185 void tearDown(void);
1186 UNITY_WEAK_ATTRIBUTE void setUp(void) { }
1187 UNITY_WEAK_ATTRIBUTE void tearDown(void) { }
1188 #elif defined(UNITY_WEAK_PRAGMA)
1191 # pragma weak tearDown
1192 void tearDown(void);
1195 void tearDown(void);
1198 //-----------------------------------------------
1199 void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
1201 Unity.CurrentTestName = FuncName;
1202 Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum;
1203 Unity.NumberOfTests++;
1210 if (TEST_PROTECT() && !(Unity.CurrentTestIgnored))
1215 UnityConcludeTest();
1219 //-----------------------------------------------
1220 void UnityBegin(const char* filename)
1222 Unity.TestFile = filename;
1223 Unity.CurrentTestName = NULL;
1224 Unity.CurrentTestLineNumber = 0;
1225 Unity.NumberOfTests = 0;
1226 Unity.TestFailures = 0;
1227 Unity.TestIgnores = 0;
1228 Unity.CurrentTestFailed = 0;
1229 Unity.CurrentTestIgnored = 0;
1230 Unity.TestXFAILS = 0;
1231 Unity.isExpectingFail = 0;
1232 Unity.TestPasses = 0;
1233 Unity.TestXPASSES = 0;
1234 Unity.XFAILMessage = NULL;
1236 UNITY_OUTPUT_START();
1240 //-----------------------------------------------
1244 UnityPrint(UnityStrBreaker);
1246 UnityPrintNumber((_U_SINT)(Unity.NumberOfTests));
1247 UnityPrint(UnityStrResultsTests);
1249 UnityPrintNumber((_U_SINT)(Unity.TestPasses));
1250 UnityPrint(UnityStrResultsPass);
1252 UnityPrintNumber((_U_SINT)(Unity.TestXFAILS));
1253 UnityPrint(UnityStrResultsXFAIL);
1255 UnityPrintNumber((_U_SINT)(Unity.TestFailures));
1256 UnityPrint(UnityStrResultsFailures);
1258 UnityPrintNumber((_U_SINT)(Unity.TestXPASSES));
1259 UnityPrint(UnityStrResultsXPASS);
1261 UnityPrintNumber((_U_SINT)(Unity.TestIgnores));
1262 UnityPrint(UnityStrResultsIgnored);
1266 if (Unity.TestFailures == 0U && Unity.TestXPASSES == 0U)
1275 UNITY_OUTPUT_COMPLETE();
1276 return (int)(Unity.TestFailures);
1280 //-----------------------------------------------