1 /* =========================================================================
\r
2 Unity Project - A Test Framework for C
\r
3 Copyright (c) 2007-14 Mike Karlesky, Mark VanderVoord, Greg Williams
\r
4 [Released under MIT License. Please refer to license.txt for details]
\r
5 ============================================================================ */
\r
9 #define UNITY_FAIL_AND_BAIL { Unity.CurrentTestFailed = 1; longjmp(Unity.AbortFrame, 1); }
\r
10 #define UNITY_IGNORE_AND_BAIL { Unity.CurrentTestIgnored = 1; longjmp(Unity.AbortFrame, 1); }
\r
11 /// return prematurely if we are already in failure or ignore state
\r
12 #define UNITY_SKIP_EXECUTION { if ((Unity.CurrentTestFailed != 0) || (Unity.CurrentTestIgnored != 0)) {return;} }
\r
13 #define UNITY_PRINT_EOL { UNITY_OUTPUT_CHAR('\n'); }
\r
15 struct _Unity Unity;
\r
17 const char UnityStrOk[] = "OK";
\r
18 const char UnityStrPass[] = "PASS";
\r
19 const char UnityStrFail[] = "FAIL";
\r
20 const char UnityStrIgnore[] = "IGNORE";
\r
21 const char UnityStrNull[] = "NULL";
\r
22 const char UnityStrSpacer[] = ". ";
\r
23 const char UnityStrExpected[] = " Expected ";
\r
24 const char UnityStrWas[] = " Was ";
\r
25 const char UnityStrTo[] = " To ";
\r
26 const char UnityStrElement[] = " Element ";
\r
27 const char UnityStrByte[] = " Byte ";
\r
28 const char UnityStrMemory[] = " Memory Mismatch.";
\r
29 const char UnityStrDelta[] = " Values Not Within Delta ";
\r
30 const char UnityStrPointless[] = " You Asked Me To Compare Nothing, Which Was Pointless.";
\r
31 const char UnityStrNullPointerForExpected[] = " Expected pointer to be NULL";
\r
32 const char UnityStrNullPointerForActual[] = " Actual pointer was NULL";
\r
33 const char UnityStrNot[] = "Not ";
\r
34 const char UnityStrInf[] = "Infinity";
\r
35 const char UnityStrNegInf[] = "Negative Infinity";
\r
36 const char UnityStrNaN[] = "NaN";
\r
37 const char UnityStrDet[] = "Determinate";
\r
38 const char UnityStrErrFloat[] = "Unity Floating Point Disabled";
\r
39 const char UnityStrErrDouble[] = "Unity Double Precision Disabled";
\r
40 const char UnityStrErr64[] = "Unity 64-bit Support Disabled";
\r
41 const char UnityStrBreaker[] = "-----------------------";
\r
42 const char UnityStrResultsTests[] = " Tests ";
\r
43 const char UnityStrResultsFailures[] = " Failures ";
\r
44 const char UnityStrResultsIgnored[] = " Ignored ";
\r
46 #ifndef UNITY_EXCLUDE_FLOAT
\r
47 // Dividing by these constants produces +/- infinity.
\r
48 // The rationale is given in UnityAssertFloatIsInf's body.
\r
49 static const _UF f_zero = 0.0f;
\r
50 #ifndef UNITY_EXCLUDE_DOUBLE
\r
51 static const _UD d_zero = 0.0;
\r
55 // compiler-generic print formatting masks
\r
56 const _U_UINT UnitySizeMask[] =
\r
61 4294967295u, // 0xFFFFFFFF
\r
65 #ifdef UNITY_SUPPORT_64
\r
70 void UnityPrintFail(void);
\r
71 void UnityPrintOk(void);
\r
73 //-----------------------------------------------
\r
74 // Pretty Printers & Test Result Output Handlers
\r
75 //-----------------------------------------------
\r
77 void UnityPrint(const char* string)
\r
79 const char* pch = string;
\r
85 // printable characters plus CR & LF are printed
\r
86 if ((*pch <= 126) && (*pch >= 32))
\r
88 UNITY_OUTPUT_CHAR(*pch);
\r
90 //write escaped carriage returns
\r
91 else if (*pch == 13)
\r
93 UNITY_OUTPUT_CHAR('\\');
\r
94 UNITY_OUTPUT_CHAR('r');
\r
96 //write escaped line feeds
\r
97 else if (*pch == 10)
\r
99 UNITY_OUTPUT_CHAR('\\');
\r
100 UNITY_OUTPUT_CHAR('n');
\r
102 // unprintable characters are shown as codes
\r
105 UNITY_OUTPUT_CHAR('\\');
\r
106 UnityPrintNumberHex((_U_UINT)*pch, 2);
\r
113 //-----------------------------------------------
\r
114 void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style)
\r
116 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
\r
118 UnityPrintNumber(number);
\r
120 else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
\r
122 UnityPrintNumberUnsigned( (_U_UINT)number & UnitySizeMask[((_U_UINT)style & (_U_UINT)0x0F) - 1] );
\r
126 UnityPrintNumberHex((_U_UINT)number, (char)((style & 0x000F) << 1));
\r
130 //-----------------------------------------------
\r
131 /// basically do an itoa using as little ram as possible
\r
132 void UnityPrintNumber(const _U_SINT number_to_print)
\r
134 _U_SINT divisor = 1;
\r
135 _U_SINT next_divisor;
\r
138 if (number_to_print == (1l << (UNITY_LONG_WIDTH-1)))
\r
140 //The largest representable negative number
\r
141 UNITY_OUTPUT_CHAR('-');
\r
142 number = (1ul << (UNITY_LONG_WIDTH-1));
\r
144 else if (number_to_print < 0)
\r
146 //Some other negative number
\r
147 UNITY_OUTPUT_CHAR('-');
\r
148 number = (_U_UINT)(-number_to_print);
\r
153 number = (_U_UINT)number_to_print;
\r
156 // figure out initial divisor
\r
157 while (number / divisor > 9)
\r
159 next_divisor = divisor * 10;
\r
160 if (next_divisor > divisor)
\r
161 divisor = next_divisor;
\r
166 // now mod and print, then divide divisor
\r
169 UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
\r
172 while (divisor > 0);
\r
175 //-----------------------------------------------
\r
176 /// basically do an itoa using as little ram as possible
\r
177 void UnityPrintNumberUnsigned(const _U_UINT number)
\r
179 _U_UINT divisor = 1;
\r
180 _U_UINT next_divisor;
\r
182 // figure out initial divisor
\r
183 while (number / divisor > 9)
\r
185 next_divisor = divisor * 10;
\r
186 if (next_divisor > divisor)
\r
187 divisor = next_divisor;
\r
192 // now mod and print, then divide divisor
\r
195 UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
\r
198 while (divisor > 0);
\r
201 //-----------------------------------------------
\r
202 void UnityPrintNumberHex(const _U_UINT number, const char nibbles_to_print)
\r
205 char nibbles = nibbles_to_print;
\r
206 UNITY_OUTPUT_CHAR('0');
\r
207 UNITY_OUTPUT_CHAR('x');
\r
209 while (nibbles > 0)
\r
211 nibble = (number >> (--nibbles << 2)) & 0x0000000F;
\r
214 UNITY_OUTPUT_CHAR((char)('0' + nibble));
\r
218 UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble));
\r
223 //-----------------------------------------------
\r
224 void UnityPrintMask(const _U_UINT mask, const _U_UINT number)
\r
226 _U_UINT current_bit = (_U_UINT)1 << (UNITY_INT_WIDTH - 1);
\r
229 for (i = 0; i < UNITY_INT_WIDTH; i++)
\r
231 if (current_bit & mask)
\r
233 if (current_bit & number)
\r
235 UNITY_OUTPUT_CHAR('1');
\r
239 UNITY_OUTPUT_CHAR('0');
\r
244 UNITY_OUTPUT_CHAR('X');
\r
246 current_bit = current_bit >> 1;
\r
250 //-----------------------------------------------
\r
251 #ifdef UNITY_FLOAT_VERBOSE
\r
252 #include <string.h>
\r
253 void UnityPrintFloat(_UF number)
\r
255 char TempBuffer[32];
\r
256 sprintf(TempBuffer, "%.6f", number);
\r
257 UnityPrint(TempBuffer);
\r
261 //-----------------------------------------------
\r
263 void UnityPrintFail(void)
\r
265 UnityPrint(UnityStrFail);
\r
268 void UnityPrintOk(void)
\r
270 UnityPrint(UnityStrOk);
\r
273 //-----------------------------------------------
\r
274 void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line)
\r
278 UNITY_OUTPUT_CHAR(':');
\r
279 UnityPrintNumber((_U_SINT)line);
\r
280 UNITY_OUTPUT_CHAR(':');
\r
281 UnityPrint(Unity.CurrentTestName);
\r
282 UNITY_OUTPUT_CHAR(':');
\r
285 //-----------------------------------------------
\r
286 void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)
\r
288 UnityTestResultsBegin(Unity.TestFile, line);
\r
289 UnityPrint(UnityStrFail);
\r
290 UNITY_OUTPUT_CHAR(':');
\r
293 //-----------------------------------------------
\r
294 void UnityConcludeTest(void)
\r
296 if (Unity.CurrentTestIgnored)
\r
298 Unity.TestIgnores++;
\r
300 else if (!Unity.CurrentTestFailed)
\r
302 UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);
\r
303 UnityPrint(UnityStrPass);
\r
307 Unity.TestFailures++;
\r
310 Unity.CurrentTestFailed = 0;
\r
311 Unity.CurrentTestIgnored = 0;
\r
314 //-----------------------------------------------
\r
315 void UnityAddMsgIfSpecified(const char* msg)
\r
319 UnityPrint(UnityStrSpacer);
\r
324 //-----------------------------------------------
\r
325 void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual)
\r
327 UnityPrint(UnityStrExpected);
\r
328 if (expected != NULL)
\r
330 UNITY_OUTPUT_CHAR('\'');
\r
331 UnityPrint(expected);
\r
332 UNITY_OUTPUT_CHAR('\'');
\r
336 UnityPrint(UnityStrNull);
\r
338 UnityPrint(UnityStrWas);
\r
339 if (actual != NULL)
\r
341 UNITY_OUTPUT_CHAR('\'');
\r
342 UnityPrint(actual);
\r
343 UNITY_OUTPUT_CHAR('\'');
\r
347 UnityPrint(UnityStrNull);
\r
351 //-----------------------------------------------
\r
352 // Assertion & Control Helpers
\r
353 //-----------------------------------------------
\r
355 int UnityCheckArraysForNull(UNITY_PTR_ATTRIBUTE const void* expected, UNITY_PTR_ATTRIBUTE const void* actual, const UNITY_LINE_TYPE lineNumber, const char* msg)
\r
357 //return true if they are both NULL
\r
358 if ((expected == NULL) && (actual == NULL))
\r
361 //throw error if just expected is NULL
\r
362 if (expected == NULL)
\r
364 UnityTestResultsFailBegin(lineNumber);
\r
365 UnityPrint(UnityStrNullPointerForExpected);
\r
366 UnityAddMsgIfSpecified(msg);
\r
367 UNITY_FAIL_AND_BAIL;
\r
370 //throw error if just actual is NULL
\r
371 if (actual == NULL)
\r
373 UnityTestResultsFailBegin(lineNumber);
\r
374 UnityPrint(UnityStrNullPointerForActual);
\r
375 UnityAddMsgIfSpecified(msg);
\r
376 UNITY_FAIL_AND_BAIL;
\r
379 //return false if neither is NULL
\r
383 //-----------------------------------------------
\r
384 // Assertion Functions
\r
385 //-----------------------------------------------
\r
387 void UnityAssertBits(const _U_SINT mask,
\r
388 const _U_SINT expected,
\r
389 const _U_SINT actual,
\r
391 const UNITY_LINE_TYPE lineNumber)
\r
393 UNITY_SKIP_EXECUTION;
\r
395 if ((mask & expected) != (mask & actual))
\r
397 UnityTestResultsFailBegin(lineNumber);
\r
398 UnityPrint(UnityStrExpected);
\r
399 UnityPrintMask((_U_UINT)mask, (_U_UINT)expected);
\r
400 UnityPrint(UnityStrWas);
\r
401 UnityPrintMask((_U_UINT)mask, (_U_UINT)actual);
\r
402 UnityAddMsgIfSpecified(msg);
\r
403 UNITY_FAIL_AND_BAIL;
\r
407 //-----------------------------------------------
\r
408 void UnityAssertEqualNumber(const _U_SINT expected,
\r
409 const _U_SINT actual,
\r
411 const UNITY_LINE_TYPE lineNumber,
\r
412 const UNITY_DISPLAY_STYLE_T style)
\r
414 UNITY_SKIP_EXECUTION;
\r
416 if (expected != actual)
\r
418 UnityTestResultsFailBegin(lineNumber);
\r
419 UnityPrint(UnityStrExpected);
\r
420 UnityPrintNumberByStyle(expected, style);
\r
421 UnityPrint(UnityStrWas);
\r
422 UnityPrintNumberByStyle(actual, style);
\r
423 UnityAddMsgIfSpecified(msg);
\r
424 UNITY_FAIL_AND_BAIL;
\r
428 //-----------------------------------------------
\r
429 void UnityAssertEqualIntArray(UNITY_PTR_ATTRIBUTE const void* expected,
\r
430 UNITY_PTR_ATTRIBUTE const void* actual,
\r
431 const _UU32 num_elements,
\r
433 const UNITY_LINE_TYPE lineNumber,
\r
434 const UNITY_DISPLAY_STYLE_T style)
\r
436 _UU32 elements = num_elements;
\r
437 UNITY_PTR_ATTRIBUTE const _US8* ptr_exp = (UNITY_PTR_ATTRIBUTE const _US8*)expected;
\r
438 UNITY_PTR_ATTRIBUTE const _US8* ptr_act = (UNITY_PTR_ATTRIBUTE const _US8*)actual;
\r
440 UNITY_SKIP_EXECUTION;
\r
444 UnityTestResultsFailBegin(lineNumber);
\r
445 UnityPrint(UnityStrPointless);
\r
446 UnityAddMsgIfSpecified(msg);
\r
447 UNITY_FAIL_AND_BAIL;
\r
450 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE const void*)expected, (UNITY_PTR_ATTRIBUTE const void*)actual, lineNumber, msg) == 1)
\r
453 // If style is UNITY_DISPLAY_STYLE_INT, we'll fall into the default case rather than the INT16 or INT32 (etc) case
\r
454 // as UNITY_DISPLAY_STYLE_INT includes a flag for UNITY_DISPLAY_RANGE_AUTO, which the width-specific
\r
455 // variants do not. Therefore remove this flag.
\r
456 switch(style & (UNITY_DISPLAY_STYLE_T)(~UNITY_DISPLAY_RANGE_AUTO))
\r
458 case UNITY_DISPLAY_STYLE_HEX8:
\r
459 case UNITY_DISPLAY_STYLE_INT8:
\r
460 case UNITY_DISPLAY_STYLE_UINT8:
\r
463 if (*ptr_exp != *ptr_act)
\r
465 UnityTestResultsFailBegin(lineNumber);
\r
466 UnityPrint(UnityStrElement);
\r
467 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
\r
468 UnityPrint(UnityStrExpected);
\r
469 UnityPrintNumberByStyle(*ptr_exp, style);
\r
470 UnityPrint(UnityStrWas);
\r
471 UnityPrintNumberByStyle(*ptr_act, style);
\r
472 UnityAddMsgIfSpecified(msg);
\r
473 UNITY_FAIL_AND_BAIL;
\r
479 case UNITY_DISPLAY_STYLE_HEX16:
\r
480 case UNITY_DISPLAY_STYLE_INT16:
\r
481 case UNITY_DISPLAY_STYLE_UINT16:
\r
484 if (*(UNITY_PTR_ATTRIBUTE const _US16*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US16*)ptr_act)
\r
486 UnityTestResultsFailBegin(lineNumber);
\r
487 UnityPrint(UnityStrElement);
\r
488 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
\r
489 UnityPrint(UnityStrExpected);
\r
490 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US16*)ptr_exp, style);
\r
491 UnityPrint(UnityStrWas);
\r
492 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US16*)ptr_act, style);
\r
493 UnityAddMsgIfSpecified(msg);
\r
494 UNITY_FAIL_AND_BAIL;
\r
500 #ifdef UNITY_SUPPORT_64
\r
501 case UNITY_DISPLAY_STYLE_HEX64:
\r
502 case UNITY_DISPLAY_STYLE_INT64:
\r
503 case UNITY_DISPLAY_STYLE_UINT64:
\r
506 if (*(UNITY_PTR_ATTRIBUTE const _US64*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US64*)ptr_act)
\r
508 UnityTestResultsFailBegin(lineNumber);
\r
509 UnityPrint(UnityStrElement);
\r
510 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
\r
511 UnityPrint(UnityStrExpected);
\r
512 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US64*)ptr_exp, style);
\r
513 UnityPrint(UnityStrWas);
\r
514 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US64*)ptr_act, style);
\r
515 UnityAddMsgIfSpecified(msg);
\r
516 UNITY_FAIL_AND_BAIL;
\r
526 if (*(UNITY_PTR_ATTRIBUTE const _US32*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US32*)ptr_act)
\r
528 UnityTestResultsFailBegin(lineNumber);
\r
529 UnityPrint(UnityStrElement);
\r
530 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
\r
531 UnityPrint(UnityStrExpected);
\r
532 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US32*)ptr_exp, style);
\r
533 UnityPrint(UnityStrWas);
\r
534 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US32*)ptr_act, style);
\r
535 UnityAddMsgIfSpecified(msg);
\r
536 UNITY_FAIL_AND_BAIL;
\r
545 //-----------------------------------------------
\r
546 #ifndef UNITY_EXCLUDE_FLOAT
\r
547 void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const _UF* expected,
\r
548 UNITY_PTR_ATTRIBUTE const _UF* actual,
\r
549 const _UU32 num_elements,
\r
551 const UNITY_LINE_TYPE lineNumber)
\r
553 _UU32 elements = num_elements;
\r
554 UNITY_PTR_ATTRIBUTE const _UF* ptr_expected = expected;
\r
555 UNITY_PTR_ATTRIBUTE const _UF* ptr_actual = actual;
\r
558 UNITY_SKIP_EXECUTION;
\r
562 UnityTestResultsFailBegin(lineNumber);
\r
563 UnityPrint(UnityStrPointless);
\r
564 UnityAddMsgIfSpecified(msg);
\r
565 UNITY_FAIL_AND_BAIL;
\r
568 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE const void*)expected, (UNITY_PTR_ATTRIBUTE const void*)actual, lineNumber, msg) == 1)
\r
573 diff = *ptr_expected - *ptr_actual;
\r
575 diff = 0.0f - diff;
\r
576 tol = UNITY_FLOAT_PRECISION * *ptr_expected;
\r
580 //This first part of this condition will catch any NaN or Infinite values
\r
581 if ((diff * 0.0f != 0.0f) || (diff > tol))
\r
583 UnityTestResultsFailBegin(lineNumber);
\r
584 UnityPrint(UnityStrElement);
\r
585 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
\r
586 #ifdef UNITY_FLOAT_VERBOSE
\r
587 UnityPrint(UnityStrExpected);
\r
588 UnityPrintFloat(*ptr_expected);
\r
589 UnityPrint(UnityStrWas);
\r
590 UnityPrintFloat(*ptr_actual);
\r
592 UnityPrint(UnityStrDelta);
\r
594 UnityAddMsgIfSpecified(msg);
\r
595 UNITY_FAIL_AND_BAIL;
\r
602 //-----------------------------------------------
\r
603 void UnityAssertFloatsWithin(const _UF delta,
\r
604 const _UF expected,
\r
607 const UNITY_LINE_TYPE lineNumber)
\r
609 _UF diff = actual - expected;
\r
610 _UF pos_delta = delta;
\r
612 UNITY_SKIP_EXECUTION;
\r
616 diff = 0.0f - diff;
\r
618 if (pos_delta < 0.0f)
\r
620 pos_delta = 0.0f - pos_delta;
\r
623 //This first part of this condition will catch any NaN or Infinite values
\r
624 if ((diff * 0.0f != 0.0f) || (pos_delta < diff))
\r
626 UnityTestResultsFailBegin(lineNumber);
\r
627 #ifdef UNITY_FLOAT_VERBOSE
\r
628 UnityPrint(UnityStrExpected);
\r
629 UnityPrintFloat(expected);
\r
630 UnityPrint(UnityStrWas);
\r
631 UnityPrintFloat(actual);
\r
633 UnityPrint(UnityStrDelta);
\r
635 UnityAddMsgIfSpecified(msg);
\r
636 UNITY_FAIL_AND_BAIL;
\r
640 //-----------------------------------------------
\r
641 void UnityAssertFloatSpecial(const _UF actual,
\r
643 const UNITY_LINE_TYPE lineNumber,
\r
644 const UNITY_FLOAT_TRAIT_T style)
\r
646 UNITY_SKIP_EXECUTION;
\r
648 const char* trait_names[] = { UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet };
\r
649 _U_SINT should_be_trait = ((_U_SINT)style & 1);
\r
650 _U_SINT is_trait = !should_be_trait;
\r
651 _U_SINT trait_index = style >> 1;
\r
655 //To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly
\r
656 //We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise
\r
657 case UNITY_FLOAT_IS_INF:
\r
658 case UNITY_FLOAT_IS_NOT_INF:
\r
659 is_trait = ((1.0f / f_zero) == actual) ? 1 : 0;
\r
661 case UNITY_FLOAT_IS_NEG_INF:
\r
662 case UNITY_FLOAT_IS_NOT_NEG_INF:
\r
663 is_trait = ((-1.0f / f_zero) == actual) ? 1 : 0;
\r
666 //NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN.
\r
667 case UNITY_FLOAT_IS_NAN:
\r
668 case UNITY_FLOAT_IS_NOT_NAN:
\r
669 is_trait = (actual == actual) ? 0 : 1;
\r
672 //A determinate number is non infinite and not NaN. (therefore the opposite of the two above)
\r
673 case UNITY_FLOAT_IS_DET:
\r
674 case UNITY_FLOAT_IS_NOT_DET:
\r
675 if ( (actual != actual) || ((1.0f / f_zero) == actual) || ((-1.0f / f_zero) == actual) )
\r
682 if (is_trait != should_be_trait)
\r
684 UnityTestResultsFailBegin(lineNumber);
\r
685 UnityPrint(UnityStrExpected);
\r
686 if (!should_be_trait)
\r
687 UnityPrint(UnityStrNot);
\r
688 UnityPrint(trait_names[trait_index]);
\r
689 UnityPrint(UnityStrWas);
\r
690 #ifdef UNITY_FLOAT_VERBOSE
\r
691 UnityPrintFloat(actual);
\r
693 if (should_be_trait)
\r
694 UnityPrint(UnityStrNot);
\r
695 UnityPrint(trait_names[trait_index]);
\r
697 UnityAddMsgIfSpecified(msg);
\r
698 UNITY_FAIL_AND_BAIL;
\r
702 #endif //not UNITY_EXCLUDE_FLOAT
\r
704 //-----------------------------------------------
\r
705 #ifndef UNITY_EXCLUDE_DOUBLE
\r
706 void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected,
\r
707 UNITY_PTR_ATTRIBUTE const _UD* actual,
\r
708 const _UU32 num_elements,
\r
710 const UNITY_LINE_TYPE lineNumber)
\r
712 _UU32 elements = num_elements;
\r
713 UNITY_PTR_ATTRIBUTE const _UD* ptr_expected = expected;
\r
714 UNITY_PTR_ATTRIBUTE const _UD* ptr_actual = actual;
\r
717 UNITY_SKIP_EXECUTION;
\r
721 UnityTestResultsFailBegin(lineNumber);
\r
722 UnityPrint(UnityStrPointless);
\r
723 UnityAddMsgIfSpecified(msg);
\r
724 UNITY_FAIL_AND_BAIL;
\r
727 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE void*)expected, (UNITY_PTR_ATTRIBUTE void*)actual, lineNumber, msg) == 1)
\r
732 diff = *ptr_expected - *ptr_actual;
\r
735 tol = UNITY_DOUBLE_PRECISION * *ptr_expected;
\r
739 //This first part of this condition will catch any NaN or Infinite values
\r
740 if ((diff * 0.0 != 0.0) || (diff > tol))
\r
742 UnityTestResultsFailBegin(lineNumber);
\r
743 UnityPrint(UnityStrElement);
\r
744 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
\r
745 #ifdef UNITY_DOUBLE_VERBOSE
\r
746 UnityPrint(UnityStrExpected);
\r
747 UnityPrintFloat((float)(*ptr_expected));
\r
748 UnityPrint(UnityStrWas);
\r
749 UnityPrintFloat((float)(*ptr_actual));
\r
751 UnityPrint(UnityStrDelta);
\r
753 UnityAddMsgIfSpecified(msg);
\r
754 UNITY_FAIL_AND_BAIL;
\r
761 //-----------------------------------------------
\r
762 void UnityAssertDoublesWithin(const _UD delta,
\r
763 const _UD expected,
\r
766 const UNITY_LINE_TYPE lineNumber)
\r
768 _UD diff = actual - expected;
\r
769 _UD pos_delta = delta;
\r
771 UNITY_SKIP_EXECUTION;
\r
777 if (pos_delta < 0.0)
\r
779 pos_delta = 0.0 - pos_delta;
\r
782 //This first part of this condition will catch any NaN or Infinite values
\r
783 if ((diff * 0.0 != 0.0) || (pos_delta < diff))
\r
785 UnityTestResultsFailBegin(lineNumber);
\r
786 #ifdef UNITY_DOUBLE_VERBOSE
\r
787 UnityPrint(UnityStrExpected);
\r
788 UnityPrintFloat((float)expected);
\r
789 UnityPrint(UnityStrWas);
\r
790 UnityPrintFloat((float)actual);
\r
792 UnityPrint(UnityStrDelta);
\r
794 UnityAddMsgIfSpecified(msg);
\r
795 UNITY_FAIL_AND_BAIL;
\r
799 //-----------------------------------------------
\r
801 void UnityAssertDoubleSpecial(const _UD actual,
\r
803 const UNITY_LINE_TYPE lineNumber,
\r
804 const UNITY_FLOAT_TRAIT_T style)
\r
806 UNITY_SKIP_EXECUTION;
\r
808 const char* trait_names[] = { UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet };
\r
809 _U_SINT should_be_trait = ((_U_SINT)style & 1);
\r
810 _U_SINT is_trait = !should_be_trait;
\r
811 _U_SINT trait_index = style >> 1;
\r
815 //To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly
\r
816 //We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise
\r
817 case UNITY_FLOAT_IS_INF:
\r
818 case UNITY_FLOAT_IS_NOT_INF:
\r
819 is_trait = ((1.0 / d_zero) == actual) ? 1 : 0;
\r
821 case UNITY_FLOAT_IS_NEG_INF:
\r
822 case UNITY_FLOAT_IS_NOT_NEG_INF:
\r
823 is_trait = ((-1.0 / d_zero) == actual) ? 1 : 0;
\r
826 //NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN.
\r
827 case UNITY_FLOAT_IS_NAN:
\r
828 case UNITY_FLOAT_IS_NOT_NAN:
\r
829 is_trait = (actual == actual) ? 0 : 1;
\r
832 //A determinate number is non infinite and not NaN. (therefore the opposite of the two above)
\r
833 case UNITY_FLOAT_IS_DET:
\r
834 case UNITY_FLOAT_IS_NOT_DET:
\r
835 if ( (actual != actual) || ((1.0 / d_zero) == actual) || ((-1.0 / d_zero) == actual) )
\r
842 if (is_trait != should_be_trait)
\r
844 UnityTestResultsFailBegin(lineNumber);
\r
845 UnityPrint(UnityStrExpected);
\r
846 if (!should_be_trait)
\r
847 UnityPrint(UnityStrNot);
\r
848 UnityPrint(trait_names[trait_index]);
\r
849 UnityPrint(UnityStrWas);
\r
850 #ifdef UNITY_DOUBLE_VERBOSE
\r
851 UnityPrintFloat(actual);
\r
853 if (should_be_trait)
\r
854 UnityPrint(UnityStrNot);
\r
855 UnityPrint(trait_names[trait_index]);
\r
857 UnityAddMsgIfSpecified(msg);
\r
858 UNITY_FAIL_AND_BAIL;
\r
863 #endif // not UNITY_EXCLUDE_DOUBLE
\r
865 //-----------------------------------------------
\r
866 void UnityAssertNumbersWithin( const _U_SINT delta,
\r
867 const _U_SINT expected,
\r
868 const _U_SINT actual,
\r
870 const UNITY_LINE_TYPE lineNumber,
\r
871 const UNITY_DISPLAY_STYLE_T style)
\r
873 UNITY_SKIP_EXECUTION;
\r
875 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
\r
877 if (actual > expected)
\r
878 Unity.CurrentTestFailed = ((actual - expected) > delta);
\r
880 Unity.CurrentTestFailed = ((expected - actual) > delta);
\r
884 if ((_U_UINT)actual > (_U_UINT)expected)
\r
885 Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > (_U_UINT)delta);
\r
887 Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > (_U_UINT)delta);
\r
890 if (Unity.CurrentTestFailed)
\r
892 UnityTestResultsFailBegin(lineNumber);
\r
893 UnityPrint(UnityStrDelta);
\r
894 UnityPrintNumberByStyle(delta, style);
\r
895 UnityPrint(UnityStrExpected);
\r
896 UnityPrintNumberByStyle(expected, style);
\r
897 UnityPrint(UnityStrWas);
\r
898 UnityPrintNumberByStyle(actual, style);
\r
899 UnityAddMsgIfSpecified(msg);
\r
900 UNITY_FAIL_AND_BAIL;
\r
904 //-----------------------------------------------
\r
905 void UnityAssertEqualString(const char* expected,
\r
906 const char* actual,
\r
908 const UNITY_LINE_TYPE lineNumber)
\r
912 UNITY_SKIP_EXECUTION;
\r
914 // if both pointers not null compare the strings
\r
915 if (expected && actual)
\r
917 for (i = 0; expected[i] || actual[i]; i++)
\r
919 if (expected[i] != actual[i])
\r
921 Unity.CurrentTestFailed = 1;
\r
927 { // handle case of one pointers being null (if both null, test should pass)
\r
928 if (expected != actual)
\r
930 Unity.CurrentTestFailed = 1;
\r
934 if (Unity.CurrentTestFailed)
\r
936 UnityTestResultsFailBegin(lineNumber);
\r
937 UnityPrintExpectedAndActualStrings(expected, actual);
\r
938 UnityAddMsgIfSpecified(msg);
\r
939 UNITY_FAIL_AND_BAIL;
\r
943 //-----------------------------------------------
\r
944 void UnityAssertEqualStringArray( const char** expected,
\r
945 const char** actual,
\r
946 const _UU32 num_elements,
\r
948 const UNITY_LINE_TYPE lineNumber)
\r
952 UNITY_SKIP_EXECUTION;
\r
954 // if no elements, it's an error
\r
955 if (num_elements == 0)
\r
957 UnityTestResultsFailBegin(lineNumber);
\r
958 UnityPrint(UnityStrPointless);
\r
959 UnityAddMsgIfSpecified(msg);
\r
960 UNITY_FAIL_AND_BAIL;
\r
963 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE void*)expected, (UNITY_PTR_ATTRIBUTE void*)actual, lineNumber, msg) == 1)
\r
968 // if both pointers not null compare the strings
\r
969 if (expected[j] && actual[j])
\r
971 for (i = 0; expected[j][i] || actual[j][i]; i++)
\r
973 if (expected[j][i] != actual[j][i])
\r
975 Unity.CurrentTestFailed = 1;
\r
981 { // handle case of one pointers being null (if both null, test should pass)
\r
982 if (expected[j] != actual[j])
\r
984 Unity.CurrentTestFailed = 1;
\r
988 if (Unity.CurrentTestFailed)
\r
990 UnityTestResultsFailBegin(lineNumber);
\r
991 if (num_elements > 1)
\r
993 UnityPrint(UnityStrElement);
\r
994 UnityPrintNumberByStyle((j), UNITY_DISPLAY_STYLE_UINT);
\r
996 UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j]));
\r
997 UnityAddMsgIfSpecified(msg);
\r
998 UNITY_FAIL_AND_BAIL;
\r
1000 } while (++j < num_elements);
\r
1003 //-----------------------------------------------
\r
1004 void UnityAssertEqualMemory( UNITY_PTR_ATTRIBUTE const void* expected,
\r
1005 UNITY_PTR_ATTRIBUTE const void* actual,
\r
1006 const _UU32 length,
\r
1007 const _UU32 num_elements,
\r
1009 const UNITY_LINE_TYPE lineNumber)
\r
1011 UNITY_PTR_ATTRIBUTE const unsigned char* ptr_exp = (UNITY_PTR_ATTRIBUTE const unsigned char*)expected;
\r
1012 UNITY_PTR_ATTRIBUTE const unsigned char* ptr_act = (UNITY_PTR_ATTRIBUTE const unsigned char*)actual;
\r
1013 _UU32 elements = num_elements;
\r
1016 UNITY_SKIP_EXECUTION;
\r
1018 if ((elements == 0) || (length == 0))
\r
1020 UnityTestResultsFailBegin(lineNumber);
\r
1021 UnityPrint(UnityStrPointless);
\r
1022 UnityAddMsgIfSpecified(msg);
\r
1023 UNITY_FAIL_AND_BAIL;
\r
1026 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE const void*)expected, (UNITY_PTR_ATTRIBUTE const void*)actual, lineNumber, msg) == 1)
\r
1029 while (elements--)
\r
1031 /////////////////////////////////////
\r
1035 if (*ptr_exp != *ptr_act)
\r
1037 UnityTestResultsFailBegin(lineNumber);
\r
1038 UnityPrint(UnityStrMemory);
\r
1039 if (num_elements > 1)
\r
1041 UnityPrint(UnityStrElement);
\r
1042 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
\r
1044 UnityPrint(UnityStrByte);
\r
1045 UnityPrintNumberByStyle((length - bytes - 1), UNITY_DISPLAY_STYLE_UINT);
\r
1046 UnityPrint(UnityStrExpected);
\r
1047 UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
\r
1048 UnityPrint(UnityStrWas);
\r
1049 UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
\r
1050 UnityAddMsgIfSpecified(msg);
\r
1051 UNITY_FAIL_AND_BAIL;
\r
1056 /////////////////////////////////////
\r
1061 //-----------------------------------------------
\r
1062 // Control Functions
\r
1063 //-----------------------------------------------
\r
1065 void UnityFail(const char* msg, const UNITY_LINE_TYPE line)
\r
1067 UNITY_SKIP_EXECUTION;
\r
1069 UnityTestResultsBegin(Unity.TestFile, line);
\r
1073 UNITY_OUTPUT_CHAR(':');
\r
1074 if (msg[0] != ' ')
\r
1076 UNITY_OUTPUT_CHAR(' ');
\r
1080 UNITY_FAIL_AND_BAIL;
\r
1083 //-----------------------------------------------
\r
1084 void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
\r
1086 UNITY_SKIP_EXECUTION;
\r
1088 UnityTestResultsBegin(Unity.TestFile, line);
\r
1089 UnityPrint(UnityStrIgnore);
\r
1092 UNITY_OUTPUT_CHAR(':');
\r
1093 UNITY_OUTPUT_CHAR(' ');
\r
1096 UNITY_IGNORE_AND_BAIL;
\r
1099 //-----------------------------------------------
\r
1100 #ifdef UNITY_SUPPORT_WEAK
\r
1101 UNITY_WEAK void setUp(void) { }
\r
1102 UNITY_WEAK void tearDown(void) { }
\r
1105 void tearDown(void);
\r
1107 //-----------------------------------------------
\r
1108 void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
\r
1110 Unity.CurrentTestName = FuncName;
\r
1111 Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum;
\r
1112 Unity.NumberOfTests++;
\r
1113 if (TEST_PROTECT())
\r
1118 if (TEST_PROTECT() && !(Unity.CurrentTestIgnored))
\r
1122 UnityConcludeTest();
\r
1125 //-----------------------------------------------
\r
1126 void UnityBegin(const char* filename)
\r
1128 Unity.TestFile = filename;
\r
1129 Unity.CurrentTestName = NULL;
\r
1130 Unity.CurrentTestLineNumber = 0;
\r
1131 Unity.NumberOfTests = 0;
\r
1132 Unity.TestFailures = 0;
\r
1133 Unity.TestIgnores = 0;
\r
1134 Unity.CurrentTestFailed = 0;
\r
1135 Unity.CurrentTestIgnored = 0;
\r
1137 UNITY_OUTPUT_START();
\r
1140 //-----------------------------------------------
\r
1141 int UnityEnd(void)
\r
1144 UnityPrint(UnityStrBreaker);
\r
1146 UnityPrintNumber((_U_SINT)(Unity.NumberOfTests));
\r
1147 UnityPrint(UnityStrResultsTests);
\r
1148 UnityPrintNumber((_U_SINT)(Unity.TestFailures));
\r
1149 UnityPrint(UnityStrResultsFailures);
\r
1150 UnityPrintNumber((_U_SINT)(Unity.TestIgnores));
\r
1151 UnityPrint(UnityStrResultsIgnored);
\r
1153 if (Unity.TestFailures == 0U)
\r
1162 UNITY_OUTPUT_COMPLETE();
\r
1163 return (int)(Unity.TestFailures);
\r
1166 //-----------------------------------------------
\r