11 #ifdef UNITY_OMIT_OUTPUT_CHAR_HEADER_DECLARATION
12 void UNITY_OUTPUT_CHAR(
int);
16 #define UNITY_FAIL_AND_BAIL { Unity.CurrentTestFailed = 1; TEST_ABORT(); }
17 #define UNITY_IGNORE_AND_BAIL { Unity.CurrentTestIgnored = 1; TEST_ABORT(); }
18 #define RETURN_IF_FAIL_OR_IGNORE if (Unity.CurrentTestFailed || Unity.CurrentTestIgnored) return
22 static const char UnityStrOk[] =
"OK";
23 static const char UnityStrPass[] =
"PASS";
24 static const char UnityStrFail[] =
"FAIL";
25 static const char UnityStrIgnore[] =
"IGNORE";
26 static const char UnityStrNull[] =
"NULL";
27 static const char UnityStrSpacer[] =
". ";
28 static const char UnityStrExpected[] =
" Expected ";
29 static const char UnityStrWas[] =
" Was ";
30 static const char UnityStrGt[] =
" to be greater than ";
31 static const char UnityStrLt[] =
" to be less than ";
32 static const char UnityStrElement[] =
" Element ";
33 static const char UnityStrByte[] =
" Byte ";
34 static const char UnityStrMemory[] =
" Memory Mismatch.";
35 static const char UnityStrDelta[] =
" Values Not Within Delta ";
36 static const char UnityStrPointless[] =
" You Asked Me To Compare Nothing, Which Was Pointless.";
37 static const char UnityStrNullPointerForExpected[] =
" Expected pointer to be NULL";
38 static const char UnityStrNullPointerForActual[] =
" Actual pointer was NULL";
39 #ifndef UNITY_EXCLUDE_FLOAT
40 static const char UnityStrNot[] =
"Not ";
41 static const char UnityStrInf[] =
"Infinity";
42 static const char UnityStrNegInf[] =
"Negative Infinity";
43 static const char UnityStrNaN[] =
"NaN";
44 static const char UnityStrDet[] =
"Determinate";
45 static const char UnityStrInvalidFloatTrait[] =
"Invalid Float Trait";
47 const char UnityStrErrFloat[] =
"Unity Floating Point Disabled";
48 const char UnityStrErrDouble[] =
"Unity Double Precision Disabled";
49 const char UnityStrErr64[] =
"Unity 64-bit Support Disabled";
50 static const char UnityStrBreaker[] =
"-----------------------";
51 static const char UnityStrResultsTests[] =
" Tests ";
52 static const char UnityStrResultsFailures[] =
" Failures ";
53 static const char UnityStrResultsIgnored[] =
" Ignored ";
54 static const char UnityStrDetail1Name[] = UNITY_DETAIL1_NAME
" ";
55 static const char UnityStrDetail2Name[] =
" " UNITY_DETAIL2_NAME
" ";
61 void UnityPrint(
const char*
string)
63 const char* pch = string;
70 if ((*pch <= 126) && (*pch >= 32))
72 UNITY_OUTPUT_CHAR(*pch);
77 UNITY_OUTPUT_CHAR(
'\\');
78 UNITY_OUTPUT_CHAR(
'r');
83 UNITY_OUTPUT_CHAR(
'\\');
84 UNITY_OUTPUT_CHAR(
'n');
89 UNITY_OUTPUT_CHAR(
'\\');
90 UNITY_OUTPUT_CHAR(
'x');
91 UnityPrintNumberHex((UNITY_UINT)*pch, 2);
98 void UnityPrintLen(
const char*
string,
const UNITY_UINT32 length)
100 const char* pch = string;
104 while (*pch && (UNITY_UINT32)(pch -
string) < length)
107 if ((*pch <= 126) && (*pch >= 32))
109 UNITY_OUTPUT_CHAR(*pch);
114 UNITY_OUTPUT_CHAR(
'\\');
115 UNITY_OUTPUT_CHAR(
'r');
120 UNITY_OUTPUT_CHAR(
'\\');
121 UNITY_OUTPUT_CHAR(
'n');
126 UNITY_OUTPUT_CHAR(
'\\');
127 UNITY_OUTPUT_CHAR(
'x');
128 UnityPrintNumberHex((UNITY_UINT)*pch, 2);
136 void UnityPrintNumberByStyle(
const UNITY_INT number,
const UNITY_DISPLAY_STYLE_T style)
138 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
140 UnityPrintNumber(number);
142 else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
144 UnityPrintNumberUnsigned((UNITY_UINT)number);
148 UNITY_OUTPUT_CHAR(
'0');
149 UNITY_OUTPUT_CHAR(
'x');
150 UnityPrintNumberHex((UNITY_UINT)number, (
char)((style & 0xF) * 2));
155 void UnityPrintNumber(
const UNITY_INT number_to_print)
157 UNITY_UINT number = (UNITY_UINT)number_to_print;
159 if (number_to_print < 0)
162 UNITY_OUTPUT_CHAR(
'-');
163 number = (UNITY_UINT)(-number_to_print);
165 UnityPrintNumberUnsigned(number);
170 void UnityPrintNumberUnsigned(
const UNITY_UINT number)
172 UNITY_UINT divisor = 1;
175 while (number / divisor > 9)
183 UNITY_OUTPUT_CHAR((
char)(
'0' + (number / divisor % 10)));
185 }
while (divisor > 0);
189 void UnityPrintNumberHex(
const UNITY_UINT number,
const char nibbles_to_print)
192 char nibbles = nibbles_to_print;
193 if ((
unsigned)nibbles > (2 *
sizeof(number)))
194 nibbles = 2 *
sizeof(number);
199 nibble = (int)(number >> (nibbles * 4)) & 0x0F;
202 UNITY_OUTPUT_CHAR((
char)(
'0' + nibble));
206 UNITY_OUTPUT_CHAR((
char)(
'A' - 10 + nibble));
212 void UnityPrintMask(
const UNITY_UINT mask,
const UNITY_UINT number)
214 UNITY_UINT current_bit = (UNITY_UINT)1 << (UNITY_INT_WIDTH - 1);
217 for (i = 0; i < UNITY_INT_WIDTH; i++)
219 if (current_bit & mask)
221 if (current_bit & number)
223 UNITY_OUTPUT_CHAR(
'1');
227 UNITY_OUTPUT_CHAR(
'0');
232 UNITY_OUTPUT_CHAR(
'X');
234 current_bit = current_bit >> 1;
239 #ifndef UNITY_EXCLUDE_FLOAT_PRINT
245 void UnityPrintFloat(
const UNITY_DOUBLE input_number)
247 UNITY_DOUBLE number = input_number;
250 if (number < 0.0f || (number == 0.0f && 1.0f / number < 0.0f))
252 UNITY_OUTPUT_CHAR(
'-');
257 if (number == 0.0f) UnityPrint(
"0");
258 else if (isnan(number)) UnityPrint(
"nan");
259 else if (isinf(number)) UnityPrint(
"inf");
263 int decimals, digits;
268 while (number < 100000.0f / 1e6f) { number *= 1e6f; exponent -= 6; }
269 while (number < 100000.0f) { number *= 10.0f; exponent--; }
270 while (number > 1000000.0f * 1e6f) { number /= 1e6f; exponent += 6; }
271 while (number > 1000000.0f) { number /= 10.0f; exponent++; }
274 n = ((UNITY_INT32)(number + number) + 1) / 2;
282 decimals = (exponent <= 0 && exponent >= -9) ? -exponent : 5;
283 exponent += decimals;
286 while (decimals > 0 && n % 10 == 0)
294 while (n != 0 || digits < decimals + 1)
296 buf[digits++] = (char)(
'0' + n % 10);
301 if(digits == decimals) UNITY_OUTPUT_CHAR(
'.');
302 UNITY_OUTPUT_CHAR(buf[--digits]);
308 UNITY_OUTPUT_CHAR(
'e');
312 UNITY_OUTPUT_CHAR(
'-');
313 exponent = -exponent;
317 UNITY_OUTPUT_CHAR(
'+');
321 while (exponent != 0 || digits < 2)
323 buf[digits++] = (char)(
'0' + exponent % 10);
328 UNITY_OUTPUT_CHAR(buf[--digits]);
336 static void UnityTestResultsBegin(
const char* file,
const UNITY_LINE_TYPE line)
339 UNITY_OUTPUT_CHAR(
':');
340 UnityPrintNumber((UNITY_INT)line);
341 UNITY_OUTPUT_CHAR(
':');
342 UnityPrint(Unity.CurrentTestName);
343 UNITY_OUTPUT_CHAR(
':');
347 static void UnityTestResultsFailBegin(
const UNITY_LINE_TYPE line)
349 UnityTestResultsBegin(Unity.TestFile, line);
350 UnityPrint(UnityStrFail);
351 UNITY_OUTPUT_CHAR(
':');
355 void UnityConcludeTest(
void)
357 if (Unity.CurrentTestIgnored)
361 else if (!Unity.CurrentTestFailed)
363 UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);
364 UnityPrint(UnityStrPass);
368 Unity.TestFailures++;
371 Unity.CurrentTestFailed = 0;
372 Unity.CurrentTestIgnored = 0;
378 static void UnityAddMsgIfSpecified(
const char* msg)
382 UnityPrint(UnityStrSpacer);
383 #ifndef UNITY_EXCLUDE_DETAILS
384 if (Unity.CurrentDetail1)
386 UnityPrint(UnityStrDetail1Name);
387 UnityPrint(Unity.CurrentDetail1);
388 if (Unity.CurrentDetail2)
390 UnityPrint(UnityStrDetail2Name);
391 UnityPrint(Unity.CurrentDetail2);
393 UnityPrint(UnityStrSpacer);
401 static void UnityPrintExpectedAndActualStrings(
const char* expected,
const char* actual)
403 UnityPrint(UnityStrExpected);
404 if (expected != NULL)
406 UNITY_OUTPUT_CHAR(
'\'');
407 UnityPrint(expected);
408 UNITY_OUTPUT_CHAR(
'\'');
412 UnityPrint(UnityStrNull);
414 UnityPrint(UnityStrWas);
417 UNITY_OUTPUT_CHAR(
'\'');
419 UNITY_OUTPUT_CHAR(
'\'');
423 UnityPrint(UnityStrNull);
428 static void UnityPrintExpectedAndActualStringsLen(
const char* expected,
430 const UNITY_UINT32 length)
432 UnityPrint(UnityStrExpected);
433 if (expected != NULL)
435 UNITY_OUTPUT_CHAR(
'\'');
436 UnityPrintLen(expected, length);
437 UNITY_OUTPUT_CHAR(
'\'');
441 UnityPrint(UnityStrNull);
443 UnityPrint(UnityStrWas);
446 UNITY_OUTPUT_CHAR(
'\'');
447 UnityPrintLen(actual, length);
448 UNITY_OUTPUT_CHAR(
'\'');
452 UnityPrint(UnityStrNull);
460 static int UnityIsOneArrayNull(UNITY_INTERNAL_PTR expected,
461 UNITY_INTERNAL_PTR actual,
462 const UNITY_LINE_TYPE lineNumber,
465 if (expected == actual)
return 0;
468 if (expected == NULL)
470 UnityTestResultsFailBegin(lineNumber);
471 UnityPrint(UnityStrNullPointerForExpected);
472 UnityAddMsgIfSpecified(msg);
479 UnityTestResultsFailBegin(lineNumber);
480 UnityPrint(UnityStrNullPointerForActual);
481 UnityAddMsgIfSpecified(msg);
492 void UnityAssertBits(
const UNITY_INT mask,
493 const UNITY_INT expected,
494 const UNITY_INT actual,
496 const UNITY_LINE_TYPE lineNumber)
498 RETURN_IF_FAIL_OR_IGNORE;
500 if ((mask & expected) != (mask & actual))
502 UnityTestResultsFailBegin(lineNumber);
503 UnityPrint(UnityStrExpected);
504 UnityPrintMask((UNITY_UINT)mask, (UNITY_UINT)expected);
505 UnityPrint(UnityStrWas);
506 UnityPrintMask((UNITY_UINT)mask, (UNITY_UINT)actual);
507 UnityAddMsgIfSpecified(msg);
513 void UnityAssertEqualNumber(
const UNITY_INT expected,
514 const UNITY_INT actual,
516 const UNITY_LINE_TYPE lineNumber,
517 const UNITY_DISPLAY_STYLE_T style)
519 RETURN_IF_FAIL_OR_IGNORE;
521 if (expected != actual)
523 UnityTestResultsFailBegin(lineNumber);
524 UnityPrint(UnityStrExpected);
525 UnityPrintNumberByStyle(expected, style);
526 UnityPrint(UnityStrWas);
527 UnityPrintNumberByStyle(actual, style);
528 UnityAddMsgIfSpecified(msg);
534 void UnityAssertGreaterNumber(
const UNITY_INT threshold,
535 const UNITY_INT actual,
537 const UNITY_LINE_TYPE lineNumber,
538 const UNITY_DISPLAY_STYLE_T style)
540 RETURN_IF_FAIL_OR_IGNORE;
542 if (!(actual > threshold))
544 UnityTestResultsFailBegin(lineNumber);
545 UnityPrint(UnityStrExpected);
546 UnityPrintNumberByStyle(actual, style);
547 UnityPrint(UnityStrGt);
548 UnityPrintNumberByStyle(threshold, style);
549 UnityAddMsgIfSpecified(msg);
555 void UnityAssertSmallerNumber(
const UNITY_INT threshold,
556 const UNITY_INT actual,
558 const UNITY_LINE_TYPE lineNumber,
559 const UNITY_DISPLAY_STYLE_T style)
561 RETURN_IF_FAIL_OR_IGNORE;
563 if (!(actual < threshold))
565 UnityTestResultsFailBegin(lineNumber);
566 UnityPrint(UnityStrExpected);
567 UnityPrintNumberByStyle(actual, style);
568 UnityPrint(UnityStrLt);
569 UnityPrintNumberByStyle(threshold, style);
570 UnityAddMsgIfSpecified(msg);
577 #define UnityPrintPointlessAndBail() \
579 UnityTestResultsFailBegin(lineNumber); \
580 UnityPrint(UnityStrPointless); \
581 UnityAddMsgIfSpecified(msg); \
582 UNITY_FAIL_AND_BAIL; }
585 void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
586 UNITY_INTERNAL_PTR actual,
587 const UNITY_UINT32 num_elements,
589 const UNITY_LINE_TYPE lineNumber,
590 const UNITY_DISPLAY_STYLE_T style,
591 const UNITY_FLAGS_T flags)
593 UNITY_UINT32 elements = num_elements;
594 unsigned int length = style & 0xF;
596 RETURN_IF_FAIL_OR_IGNORE;
598 if (num_elements == 0)
600 UnityPrintPointlessAndBail();
603 if (expected == actual)
return;
604 if (UnityIsOneArrayNull(expected, actual, lineNumber, msg))
609 UNITY_INT expect_val;
610 UNITY_INT actual_val;
614 expect_val = *(UNITY_PTR_ATTRIBUTE
const UNITY_INT8*)expected;
615 actual_val = *(UNITY_PTR_ATTRIBUTE
const UNITY_INT8*)actual;
618 expect_val = *(UNITY_PTR_ATTRIBUTE
const UNITY_INT16*)expected;
619 actual_val = *(UNITY_PTR_ATTRIBUTE
const UNITY_INT16*)actual;
621 #ifdef UNITY_SUPPORT_64
623 expect_val = *(UNITY_PTR_ATTRIBUTE
const UNITY_INT64*)expected;
624 actual_val = *(UNITY_PTR_ATTRIBUTE
const UNITY_INT64*)actual;
628 expect_val = *(UNITY_PTR_ATTRIBUTE
const UNITY_INT32*)expected;
629 actual_val = *(UNITY_PTR_ATTRIBUTE
const UNITY_INT32*)actual;
634 if (expect_val != actual_val)
636 if (style & UNITY_DISPLAY_RANGE_UINT && length <
sizeof(expect_val))
639 mask = (mask << 8 * length) - 1;
643 UnityTestResultsFailBegin(lineNumber);
644 UnityPrint(UnityStrElement);
645 UnityPrintNumberUnsigned(num_elements - elements - 1);
646 UnityPrint(UnityStrExpected);
647 UnityPrintNumberByStyle(expect_val, style);
648 UnityPrint(UnityStrWas);
649 UnityPrintNumberByStyle(actual_val, style);
650 UnityAddMsgIfSpecified(msg);
653 if (flags == UNITY_ARRAY_TO_ARRAY)
655 expected = (UNITY_INTERNAL_PTR)(length + (
const char*)expected);
657 actual = (UNITY_INTERNAL_PTR)(length + (
const char*)actual);
662 #ifndef UNITY_EXCLUDE_FLOAT
664 #define UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff) \
665 if (isinf(expected) && isinf(actual) && ((expected < 0) == (actual < 0))) return 1; \
666 if (UNITY_NAN_CHECK) return 1; \
667 diff = actual - expected; \
668 if (diff < 0) diff = -diff; \
669 if (delta < 0) delta = -delta; \
670 return !(isnan(diff) || isinf(diff) || (diff > delta))
672 #ifndef UNITY_NAN_NOT_EQUAL_NAN
673 #define UNITY_NAN_CHECK isnan(expected) && isnan(actual)
675 #define UNITY_NAN_CHECK 0
678 #ifndef UNITY_EXCLUDE_FLOAT_PRINT
679 #define UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(expected, actual) \
681 UnityPrint(UnityStrExpected); \
682 UnityPrintFloat(expected); \
683 UnityPrint(UnityStrWas); \
684 UnityPrintFloat(actual); }
686 #define UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(expected, actual) \
687 UnityPrint(UnityStrDelta)
690 static int UnityFloatsWithin(UNITY_FLOAT delta, UNITY_FLOAT expected, UNITY_FLOAT actual)
693 UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);
696 void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE
const UNITY_FLOAT* expected,
697 UNITY_PTR_ATTRIBUTE
const UNITY_FLOAT* actual,
698 const UNITY_UINT32 num_elements,
700 const UNITY_LINE_TYPE lineNumber,
701 const UNITY_FLAGS_T flags)
703 UNITY_UINT32 elements = num_elements;
704 UNITY_PTR_ATTRIBUTE
const UNITY_FLOAT* ptr_expected = expected;
705 UNITY_PTR_ATTRIBUTE
const UNITY_FLOAT* ptr_actual = actual;
707 RETURN_IF_FAIL_OR_IGNORE;
711 UnityPrintPointlessAndBail();
714 if (expected == actual)
return;
715 if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg))
720 if (!UnityFloatsWithin(*ptr_expected * UNITY_FLOAT_PRECISION, *ptr_expected, *ptr_actual))
722 UnityTestResultsFailBegin(lineNumber);
723 UnityPrint(UnityStrElement);
724 UnityPrintNumberUnsigned(num_elements - elements - 1);
725 UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT((UNITY_DOUBLE)*ptr_expected, (UNITY_DOUBLE)*ptr_actual);
726 UnityAddMsgIfSpecified(msg);
729 if (flags == UNITY_ARRAY_TO_ARRAY)
738 void UnityAssertFloatsWithin(
const UNITY_FLOAT delta,
739 const UNITY_FLOAT expected,
740 const UNITY_FLOAT actual,
742 const UNITY_LINE_TYPE lineNumber)
744 RETURN_IF_FAIL_OR_IGNORE;
747 if (!UnityFloatsWithin(delta, expected, actual))
749 UnityTestResultsFailBegin(lineNumber);
750 UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT((UNITY_DOUBLE)expected, (UNITY_DOUBLE)actual);
751 UnityAddMsgIfSpecified(msg);
757 void UnityAssertFloatSpecial(
const UNITY_FLOAT actual,
759 const UNITY_LINE_TYPE lineNumber,
760 const UNITY_FLOAT_TRAIT_T style)
762 const char* trait_names[] = {UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet};
763 UNITY_INT should_be_trait = ((UNITY_INT)style & 1);
764 UNITY_INT is_trait = !should_be_trait;
765 UNITY_INT trait_index = (UNITY_INT)(style >> 1);
767 RETURN_IF_FAIL_OR_IGNORE;
771 case UNITY_FLOAT_IS_INF:
772 case UNITY_FLOAT_IS_NOT_INF:
773 is_trait = isinf(actual) && (actual > 0);
775 case UNITY_FLOAT_IS_NEG_INF:
776 case UNITY_FLOAT_IS_NOT_NEG_INF:
777 is_trait = isinf(actual) && (actual < 0);
780 case UNITY_FLOAT_IS_NAN:
781 case UNITY_FLOAT_IS_NOT_NAN:
782 is_trait = isnan(actual) ? 1 : 0;
785 case UNITY_FLOAT_IS_DET:
786 case UNITY_FLOAT_IS_NOT_DET:
787 is_trait = !isinf(actual) && !isnan(actual);
792 trait_names[0] = UnityStrInvalidFloatTrait;
796 if (is_trait != should_be_trait)
798 UnityTestResultsFailBegin(lineNumber);
799 UnityPrint(UnityStrExpected);
800 if (!should_be_trait)
801 UnityPrint(UnityStrNot);
802 UnityPrint(trait_names[trait_index]);
803 UnityPrint(UnityStrWas);
804 #ifndef UNITY_EXCLUDE_FLOAT_PRINT
805 UnityPrintFloat((UNITY_DOUBLE)actual);
808 UnityPrint(UnityStrNot);
809 UnityPrint(trait_names[trait_index]);
811 UnityAddMsgIfSpecified(msg);
819 #ifndef UNITY_EXCLUDE_DOUBLE
820 static int UnityDoublesWithin(UNITY_DOUBLE delta, UNITY_DOUBLE expected, UNITY_DOUBLE actual)
823 UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);
826 void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE
const UNITY_DOUBLE* expected,
827 UNITY_PTR_ATTRIBUTE
const UNITY_DOUBLE* actual,
828 const UNITY_UINT32 num_elements,
830 const UNITY_LINE_TYPE lineNumber,
831 const UNITY_FLAGS_T flags)
833 UNITY_UINT32 elements = num_elements;
834 UNITY_PTR_ATTRIBUTE
const UNITY_DOUBLE* ptr_expected = expected;
835 UNITY_PTR_ATTRIBUTE
const UNITY_DOUBLE* ptr_actual = actual;
837 RETURN_IF_FAIL_OR_IGNORE;
841 UnityPrintPointlessAndBail();
844 if (expected == actual)
return;
845 if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg))
850 if (!UnityDoublesWithin(*ptr_expected * UNITY_DOUBLE_PRECISION, *ptr_expected, *ptr_actual))
852 UnityTestResultsFailBegin(lineNumber);
853 UnityPrint(UnityStrElement);
854 UnityPrintNumberUnsigned(num_elements - elements - 1);
855 UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(*ptr_expected, *ptr_actual);
856 UnityAddMsgIfSpecified(msg);
859 if (flags == UNITY_ARRAY_TO_ARRAY)
868 void UnityAssertDoublesWithin(
const UNITY_DOUBLE delta,
869 const UNITY_DOUBLE expected,
870 const UNITY_DOUBLE actual,
872 const UNITY_LINE_TYPE lineNumber)
874 RETURN_IF_FAIL_OR_IGNORE;
876 if (!UnityDoublesWithin(delta, expected, actual))
878 UnityTestResultsFailBegin(lineNumber);
879 UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(expected, actual);
880 UnityAddMsgIfSpecified(msg);
887 void UnityAssertDoubleSpecial(
const UNITY_DOUBLE actual,
889 const UNITY_LINE_TYPE lineNumber,
890 const UNITY_FLOAT_TRAIT_T style)
892 const char* trait_names[] = {UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet};
893 UNITY_INT should_be_trait = ((UNITY_INT)style & 1);
894 UNITY_INT is_trait = !should_be_trait;
895 UNITY_INT trait_index = (UNITY_INT)(style >> 1);
897 RETURN_IF_FAIL_OR_IGNORE;
901 case UNITY_FLOAT_IS_INF:
902 case UNITY_FLOAT_IS_NOT_INF:
903 is_trait = isinf(actual) && (actual > 0);
905 case UNITY_FLOAT_IS_NEG_INF:
906 case UNITY_FLOAT_IS_NOT_NEG_INF:
907 is_trait = isinf(actual) && (actual < 0);
910 case UNITY_FLOAT_IS_NAN:
911 case UNITY_FLOAT_IS_NOT_NAN:
912 is_trait = isnan(actual) ? 1 : 0;
915 case UNITY_FLOAT_IS_DET:
916 case UNITY_FLOAT_IS_NOT_DET:
917 is_trait = !isinf(actual) && !isnan(actual);
922 trait_names[0] = UnityStrInvalidFloatTrait;
926 if (is_trait != should_be_trait)
928 UnityTestResultsFailBegin(lineNumber);
929 UnityPrint(UnityStrExpected);
930 if (!should_be_trait)
931 UnityPrint(UnityStrNot);
932 UnityPrint(trait_names[trait_index]);
933 UnityPrint(UnityStrWas);
934 #ifndef UNITY_EXCLUDE_FLOAT_PRINT
935 UnityPrintFloat(actual);
938 UnityPrint(UnityStrNot);
939 UnityPrint(trait_names[trait_index]);
941 UnityAddMsgIfSpecified(msg);
949 void UnityAssertNumbersWithin(
const UNITY_UINT delta,
950 const UNITY_INT expected,
951 const UNITY_INT actual,
953 const UNITY_LINE_TYPE lineNumber,
954 const UNITY_DISPLAY_STYLE_T style)
956 RETURN_IF_FAIL_OR_IGNORE;
958 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
960 if (actual > expected)
961 Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(actual - expected) > delta);
963 Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(expected - actual) > delta);
967 if ((UNITY_UINT)actual > (UNITY_UINT)expected)
968 Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(actual - expected) > delta);
970 Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(expected - actual) > delta);
973 if (Unity.CurrentTestFailed)
975 UnityTestResultsFailBegin(lineNumber);
976 UnityPrint(UnityStrDelta);
977 UnityPrintNumberByStyle((UNITY_INT)delta, style);
978 UnityPrint(UnityStrExpected);
979 UnityPrintNumberByStyle(expected, style);
980 UnityPrint(UnityStrWas);
981 UnityPrintNumberByStyle(actual, style);
982 UnityAddMsgIfSpecified(msg);
988 void UnityAssertEqualString(
const char* expected,
991 const UNITY_LINE_TYPE lineNumber)
995 RETURN_IF_FAIL_OR_IGNORE;
998 if (expected && actual)
1000 for (i = 0; expected[i] || actual[i]; i++)
1002 if (expected[i] != actual[i])
1004 Unity.CurrentTestFailed = 1;
1011 if (expected != actual)
1013 Unity.CurrentTestFailed = 1;
1017 if (Unity.CurrentTestFailed)
1019 UnityTestResultsFailBegin(lineNumber);
1020 UnityPrintExpectedAndActualStrings(expected, actual);
1021 UnityAddMsgIfSpecified(msg);
1022 UNITY_FAIL_AND_BAIL;
1027 void UnityAssertEqualStringLen(
const char* expected,
1029 const UNITY_UINT32 length,
1031 const UNITY_LINE_TYPE lineNumber)
1035 RETURN_IF_FAIL_OR_IGNORE;
1038 if (expected && actual)
1040 for (i = 0; (i < length) && (expected[i] || actual[i]); i++)
1042 if (expected[i] != actual[i])
1044 Unity.CurrentTestFailed = 1;
1051 if (expected != actual)
1053 Unity.CurrentTestFailed = 1;
1057 if (Unity.CurrentTestFailed)
1059 UnityTestResultsFailBegin(lineNumber);
1060 UnityPrintExpectedAndActualStringsLen(expected, actual, length);
1061 UnityAddMsgIfSpecified(msg);
1062 UNITY_FAIL_AND_BAIL;
1067 void UnityAssertEqualStringArray(UNITY_INTERNAL_PTR expected,
1068 const char** actual,
1069 const UNITY_UINT32 num_elements,
1071 const UNITY_LINE_TYPE lineNumber,
1072 const UNITY_FLAGS_T flags)
1076 const char* exp = NULL;
1077 const char* act = NULL;
1079 RETURN_IF_FAIL_OR_IGNORE;
1082 if (num_elements == 0)
1084 UnityPrintPointlessAndBail();
1087 if ((
const void*)expected == (
const void*)actual)
1092 if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg))
1094 UNITY_FAIL_AND_BAIL;
1097 if (flags != UNITY_ARRAY_TO_ARRAY)
1099 exp = (
const char*)expected;
1105 if (flags == UNITY_ARRAY_TO_ARRAY)
1107 exp = ((
const char*
const*)expected)[j];
1113 for (i = 0; exp[i] || act[i]; i++)
1115 if (exp[i] != act[i])
1117 Unity.CurrentTestFailed = 1;
1126 Unity.CurrentTestFailed = 1;
1130 if (Unity.CurrentTestFailed)
1132 UnityTestResultsFailBegin(lineNumber);
1133 if (num_elements > 1)
1135 UnityPrint(UnityStrElement);
1136 UnityPrintNumberUnsigned(j);
1138 UnityPrintExpectedAndActualStrings(exp, act);
1139 UnityAddMsgIfSpecified(msg);
1140 UNITY_FAIL_AND_BAIL;
1142 }
while (++j < num_elements);
1146 void UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected,
1147 UNITY_INTERNAL_PTR actual,
1148 const UNITY_UINT32 length,
1149 const UNITY_UINT32 num_elements,
1151 const UNITY_LINE_TYPE lineNumber,
1152 const UNITY_FLAGS_T flags)
1154 UNITY_PTR_ATTRIBUTE
const unsigned char* ptr_exp = (UNITY_PTR_ATTRIBUTE
const unsigned char*)expected;
1155 UNITY_PTR_ATTRIBUTE
const unsigned char* ptr_act = (UNITY_PTR_ATTRIBUTE
const unsigned char*)actual;
1156 UNITY_UINT32 elements = num_elements;
1159 RETURN_IF_FAIL_OR_IGNORE;
1161 if ((elements == 0) || (length == 0))
1163 UnityPrintPointlessAndBail();
1166 if (expected == actual)
return;
1167 if (UnityIsOneArrayNull(expected, actual, lineNumber, msg))
1168 UNITY_FAIL_AND_BAIL;
1175 if (*ptr_exp != *ptr_act)
1177 UnityTestResultsFailBegin(lineNumber);
1178 UnityPrint(UnityStrMemory);
1179 if (num_elements > 1)
1181 UnityPrint(UnityStrElement);
1182 UnityPrintNumberUnsigned(num_elements - elements - 1);
1184 UnityPrint(UnityStrByte);
1185 UnityPrintNumberUnsigned(length - bytes - 1);
1186 UnityPrint(UnityStrExpected);
1187 UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
1188 UnityPrint(UnityStrWas);
1189 UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
1190 UnityAddMsgIfSpecified(msg);
1191 UNITY_FAIL_AND_BAIL;
1196 if (flags == UNITY_ARRAY_TO_VAL)
1198 ptr_exp = (UNITY_PTR_ATTRIBUTE
const unsigned char*)expected;
1210 #ifdef UNITY_SUPPORT_64
1213 #ifndef UNITY_EXCLUDE_FLOAT
1216 #ifndef UNITY_EXCLUDE_DOUBLE
1219 } UnityQuickCompare;
1221 UNITY_INTERNAL_PTR UnityNumToPtr(
const UNITY_INT num,
const UNITY_UINT8 size)
1226 UnityQuickCompare.i8 = (UNITY_INT8)num;
1227 return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.i8);
1230 UnityQuickCompare.i16 = (UNITY_INT16)num;
1231 return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.i16);
1233 #ifdef UNITY_SUPPORT_64
1235 UnityQuickCompare.i64 = (UNITY_INT64)num;
1236 return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.i64);
1239 UnityQuickCompare.i32 = (UNITY_INT32)num;
1240 return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.i32);
1244 #ifndef UNITY_EXCLUDE_FLOAT
1245 UNITY_INTERNAL_PTR UnityFloatToPtr(
const float num)
1247 UnityQuickCompare.f = num;
1248 return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.f);
1252 #ifndef UNITY_EXCLUDE_DOUBLE
1253 UNITY_INTERNAL_PTR UnityDoubleToPtr(
const double num)
1255 UnityQuickCompare.d = num;
1256 return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.d);
1264 void UnityFail(
const char* msg,
const UNITY_LINE_TYPE line)
1266 RETURN_IF_FAIL_OR_IGNORE;
1268 UnityTestResultsBegin(Unity.TestFile, line);
1269 UnityPrint(UnityStrFail);
1272 UNITY_OUTPUT_CHAR(
':');
1274 #ifndef UNITY_EXCLUDE_DETAILS
1275 if (Unity.CurrentDetail1)
1277 UnityPrint(UnityStrDetail1Name);
1278 UnityPrint(Unity.CurrentDetail1);
1279 if (Unity.CurrentDetail2)
1281 UnityPrint(UnityStrDetail2Name);
1282 UnityPrint(Unity.CurrentDetail2);
1284 UnityPrint(UnityStrSpacer);
1289 UNITY_OUTPUT_CHAR(
' ');
1294 UNITY_FAIL_AND_BAIL;
1298 void UnityIgnore(
const char* msg,
const UNITY_LINE_TYPE line)
1300 RETURN_IF_FAIL_OR_IGNORE;
1302 UnityTestResultsBegin(Unity.TestFile, line);
1303 UnityPrint(UnityStrIgnore);
1306 UNITY_OUTPUT_CHAR(
':');
1307 UNITY_OUTPUT_CHAR(
' ');
1310 UNITY_IGNORE_AND_BAIL;
1325 void UnityDefaultTestRun(UnityTestFunction Func,
const char* FuncName,
const int FuncLineNum)
1327 Unity.CurrentTestName = FuncName;
1328 Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum;
1329 Unity.NumberOfTests++;
1330 UNITY_CLR_DETAILS();
1340 UnityConcludeTest();
1344 void UnityBegin(
const char* filename)
1346 Unity.TestFile = filename;
1347 Unity.CurrentTestName = NULL;
1348 Unity.CurrentTestLineNumber = 0;
1349 Unity.NumberOfTests = 0;
1350 Unity.TestFailures = 0;
1351 Unity.TestIgnores = 0;
1352 Unity.CurrentTestFailed = 0;
1353 Unity.CurrentTestIgnored = 0;
1355 UNITY_CLR_DETAILS();
1356 UNITY_OUTPUT_START();
1363 UnityPrint(UnityStrBreaker);
1365 UnityPrintNumber((UNITY_INT)(Unity.NumberOfTests));
1366 UnityPrint(UnityStrResultsTests);
1367 UnityPrintNumber((UNITY_INT)(Unity.TestFailures));
1368 UnityPrint(UnityStrResultsFailures);
1369 UnityPrintNumber((UNITY_INT)(Unity.TestIgnores));
1370 UnityPrint(UnityStrResultsIgnored);
1372 if (Unity.TestFailures == 0U)
1374 UnityPrint(UnityStrOk);
1378 UnityPrint(UnityStrFail);
1379 #ifdef UNITY_DIFFERENTIATE_FINAL_FAIL
1380 UNITY_OUTPUT_CHAR(
'E'); UNITY_OUTPUT_CHAR(
'D');
1385 UNITY_OUTPUT_COMPLETE();
1392 #ifdef UNITY_USE_COMMAND_LINE_ARGS
1394 char* UnityOptionIncludeNamed = NULL;
1395 char* UnityOptionExcludeNamed = NULL;
1396 int UnityVerbosity = 1;
1398 int UnityParseOptions(
int argc,
char** argv)
1400 UnityOptionIncludeNamed = NULL;
1401 UnityOptionExcludeNamed = NULL;
1403 for (
int i = 1; i < argc; i++)
1405 if (argv[i][0] ==
'-')
1413 if (argv[i][2] ==
'=')
1414 UnityOptionIncludeNamed = &argv[i][3];
1415 else if (++i < argc)
1416 UnityOptionIncludeNamed = argv[i];
1419 UnityPrint(
"ERROR: No Test String to Include Matches For");
1431 if (argv[i][2] ==
'=')
1432 UnityOptionExcludeNamed = &argv[i][3];
1433 else if (++i < argc)
1434 UnityOptionExcludeNamed = argv[i];
1437 UnityPrint(
"ERROR: No Test String to Exclude Matches For");
1443 UnityPrint(
"ERROR: Unknown Option ");
1444 UNITY_OUTPUT_CHAR(argv[i][1]);
1454 int IsStringInBiggerString(
const char* longstring,
const char* shortstring)
1456 const char* lptr = longstring;
1457 const char* sptr = shortstring;
1458 const char* lnext = lptr;
1468 while (*lptr && *sptr && (*lptr == *sptr))
1495 int UnityStringArgumentMatches(
const char* str)
1504 while (ptr1[0] != 0)
1506 if ((ptr1[0] ==
'"') || (ptr1[0] ==
'\''))
1515 if ((ptr2[0] ==
':') && (ptr2[1] != 0) && (ptr2[0] !=
'\'') && (ptr2[0] !=
'"') && (ptr2[0] !=
','))
1517 }
while ((ptr2[0] != 0) && (ptr2[0] !=
'\'') && (ptr2[0] !=
'"') && (ptr2[0] !=
','));
1518 while ((ptr2[0] != 0) && ((ptr2[0] ==
':') || (ptr2[0] ==
'\'') || (ptr2[0] ==
'"') || (ptr2[0] ==
',')))
1522 retval = IsStringInBiggerString(Unity.TestFile, ptr1);
1527 if ((retval == 2) && (ptrf != 0))
1529 if (IsStringInBiggerString(Unity.CurrentTestName, ptrf))
1534 if (IsStringInBiggerString(Unity.CurrentTestName, ptr1) == 1)
1544 int UnityTestMatches(
void)
1548 if (UnityOptionIncludeNamed)
1550 retval = UnityStringArgumentMatches(UnityOptionIncludeNamed);
1556 if (UnityOptionExcludeNamed)
1558 if (UnityStringArgumentMatches(UnityOptionExcludeNamed))