1/* $NetBSD: unity.c,v 1.2 2020/05/25 20:47:35 christos Exp $ */ 2 3/* ========================================================================= 4 Unity Project - A Test Framework for C 5 Copyright (c) 2007-14 Mike Karlesky, Mark VanderVoord, Greg Williams 6 [Released under MIT License. Please refer to license.txt for details] 7============================================================================ */ 8 9#include "unity.h" 10 11#define UNITY_FAIL_AND_BAIL { Unity.CurrentTestFailed = 1; longjmp(Unity.AbortFrame, 1); } 12#define UNITY_IGNORE_AND_BAIL { Unity.CurrentTestIgnored = 1; longjmp(Unity.AbortFrame, 1); } 13/// return prematurely if we are already in failure or ignore state 14#define UNITY_SKIP_EXECUTION { if ((Unity.CurrentTestFailed != 0) || (Unity.CurrentTestIgnored != 0)) {return;} } 15#define UNITY_PRINT_EOL { UNITY_OUTPUT_CHAR('\n'); } 16 17struct _Unity Unity; 18 19const char UnityStrOk[] = "OK"; 20const char UnityStrPass[] = "PASS"; 21const char UnityStrFail[] = "FAIL"; 22const char UnityStrIgnore[] = "IGNORE"; 23const char UnityStrXPASS[] = "XPASS"; 24const char UnityStrXFAIL[] = "XFAIL"; 25const char UnityStrNull[] = "NULL"; 26const char UnityStrSpacer[] = ". "; 27const char UnityStrExpected[] = " Expected "; 28const char UnityStrWas[] = " Was "; 29const char UnityStrTo[] = " To "; 30const char UnityStrElement[] = " Element "; 31const char UnityStrByte[] = " Byte "; 32const char UnityStrMemory[] = " Memory Mismatch."; 33const char UnityStrDelta[] = " Values Not Within Delta "; 34const char UnityStrPointless[] = " You Asked Me To Compare Nothing, Which Was Pointless."; 35const char UnityStrNullPointerForExpected[] = " Expected pointer to be NULL"; 36const char UnityStrNullPointerForActual[] = " Actual pointer was NULL"; 37const char UnityStrNot[] = "Not "; 38const char UnityStrInf[] = "Infinity"; 39const char UnityStrNegInf[] = "Negative Infinity"; 40const char UnityStrNaN[] = "NaN"; 41const char UnityStrDet[] = "Determinate"; 42const char UnityStrErrFloat[] = "Unity Floating Point Disabled"; 43const char UnityStrErrDouble[] = "Unity Double Precision Disabled"; 44const char UnityStrErr64[] = "Unity 64-bit Support Disabled"; 45const char UnityStrBreaker[] = "-----------------------"; 46const char UnityStrResultsTests[] = " Tests: "; 47const char UnityStrResultsFailures[] = " Failures "; 48const char UnityStrResultsIgnored[] = " Ignored "; 49const char UnityStrResultsXFAIL[] = " XFAIL "; 50const char UnityStrResultsXPASS[] = " XPASS "; 51const char UnityStrResultsPass[] = " PASS "; 52 53 54#ifndef UNITY_EXCLUDE_FLOAT 55// Dividing by these constants produces +/- infinity. 56// The rationale is given in UnityAssertFloatIsInf's body. 57static const _UF f_zero = 0.0f; 58#ifndef UNITY_EXCLUDE_DOUBLE 59static const _UD d_zero = 0.0; 60#endif 61#endif 62 63// compiler-generic print formatting masks 64const _U_UINT UnitySizeMask[] = 65{ 66 255u, // 0xFF 67 65535u, // 0xFFFF 68 65535u, 69 4294967295u, // 0xFFFFFFFF 70 4294967295u, 71 4294967295u, 72 4294967295u 73#ifdef UNITY_SUPPORT_64 74 ,0xFFFFFFFFFFFFFFFF 75#endif 76}; 77 78void UnityPrintFail(void); 79void UnityPrintOk(void); 80 81//----------------------------------------------- 82// Pretty Printers & Test Result Output Handlers 83//----------------------------------------------- 84 85void UnityPrint(const char* string) 86{ 87 const char* pch = string; 88 89 if (pch != NULL) 90 { 91 while (*pch) 92 { 93 // printable characters plus CR & LF are printed 94 if ((*pch <= 126) && (*pch >= 32)) 95 { 96 UNITY_OUTPUT_CHAR(*pch); 97 } 98 //write escaped carriage returns 99 else if (*pch == 13) 100 { 101 UNITY_OUTPUT_CHAR('\\'); 102 UNITY_OUTPUT_CHAR('r'); 103 } 104 //write escaped line feeds 105 else if (*pch == 10) 106 { 107 UNITY_OUTPUT_CHAR('\\'); 108 UNITY_OUTPUT_CHAR('n'); 109 } 110 // unprintable characters are shown as codes 111 else 112 { 113 UNITY_OUTPUT_CHAR('\\'); 114 UnityPrintNumberHex((_U_UINT)*pch, 2); 115 } 116 pch++; 117 } 118 } 119} 120 121//----------------------------------------------- 122void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style) 123{ 124 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT) 125 { 126 UnityPrintNumber(number); 127 } 128 else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT) 129 { 130 UnityPrintNumberUnsigned( (_U_UINT)number & UnitySizeMask[((_U_UINT)style & (_U_UINT)0x0F) - 1] ); 131 } 132 else 133 { 134 UnityPrintNumberHex((_U_UINT)number, (char)((style & 0x000F) << 1)); 135 } 136} 137 138//----------------------------------------------- 139/// basically do an itoa using as little ram as possible 140void UnityPrintNumber(const _U_SINT number_to_print) 141{ 142 _U_SINT divisor = 1; 143 _U_SINT next_divisor; 144 _U_UINT number; 145 146 if (number_to_print == (1l << (UNITY_LONG_WIDTH-1))) 147 { 148 //The largest representable negative number 149 UNITY_OUTPUT_CHAR('-'); 150 number = (1ul << (UNITY_LONG_WIDTH-1)); 151 } 152 else if (number_to_print < 0) 153 { 154 //Some other negative number 155 UNITY_OUTPUT_CHAR('-'); 156 number = (_U_UINT)(-number_to_print); 157 } 158 else 159 { 160 //Positive number 161 number = (_U_UINT)number_to_print; 162 } 163 164 // figure out initial divisor 165 while (number / divisor > 9) 166 { 167 next_divisor = divisor * 10; 168 if (next_divisor > divisor) 169 divisor = next_divisor; 170 else 171 break; 172 } 173 174 // now mod and print, then divide divisor 175 do 176 { 177 UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10))); 178 divisor /= 10; 179 } 180 while (divisor > 0); 181} 182 183//----------------------------------------------- 184/// basically do an itoa using as little ram as possible 185void UnityPrintNumberUnsigned(const _U_UINT number) 186{ 187 _U_UINT divisor = 1; 188 _U_UINT next_divisor; 189 190 // figure out initial divisor 191 while (number / divisor > 9) 192 { 193 next_divisor = divisor * 10; 194 if (next_divisor > divisor) 195 divisor = next_divisor; 196 else 197 break; 198 } 199 200 // now mod and print, then divide divisor 201 do 202 { 203 UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10))); 204 divisor /= 10; 205 } 206 while (divisor > 0); 207} 208 209//----------------------------------------------- 210void UnityPrintNumberHex(const _U_UINT number, const char nibbles_to_print) 211{ 212 _U_UINT nibble; 213 char nibbles = nibbles_to_print; 214 UNITY_OUTPUT_CHAR('0'); 215 UNITY_OUTPUT_CHAR('x'); 216 217 while (nibbles > 0) 218 { 219 nibble = (number >> (--nibbles << 2)) & 0x0000000F; 220 if (nibble <= 9) 221 { 222 UNITY_OUTPUT_CHAR((char)('0' + nibble)); 223 } 224 else 225 { 226 UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble)); 227 } 228 } 229} 230 231//----------------------------------------------- 232void UnityPrintMask(const _U_UINT mask, const _U_UINT number) 233{ 234 _U_UINT current_bit = (_U_UINT)1 << (UNITY_INT_WIDTH - 1); 235 _US32 i; 236 237 for (i = 0; i < UNITY_INT_WIDTH; i++) 238 { 239 if (current_bit & mask) 240 { 241 if (current_bit & number) 242 { 243 UNITY_OUTPUT_CHAR('1'); 244 } 245 else 246 { 247 UNITY_OUTPUT_CHAR('0'); 248 } 249 } 250 else 251 { 252 UNITY_OUTPUT_CHAR('X'); 253 } 254 current_bit = current_bit >> 1; 255 } 256} 257 258//----------------------------------------------- 259#ifdef UNITY_FLOAT_VERBOSE 260#include <string.h> 261void UnityPrintFloat(_UF number) 262{ 263 char TempBuffer[32]; 264 sprintf(TempBuffer, "%.6f", number); 265 UnityPrint(TempBuffer); 266} 267#endif 268 269//----------------------------------------------- 270 271void UnityPrintFail(void) 272{ 273 UnityPrint(UnityStrFail); 274} 275 276void UnityPrintOk(void) 277{ 278 UnityPrint(UnityStrOk); 279} 280 281//----------------------------------------------- 282static void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line) 283{ 284 UnityPrint(file); 285 UNITY_OUTPUT_CHAR(':'); 286 UnityPrintNumber((_U_SINT)line); 287 UNITY_OUTPUT_CHAR(':'); 288 UnityPrint(Unity.CurrentTestName); 289 UNITY_OUTPUT_CHAR(':'); 290} 291 292//----------------------------------------------- 293static void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line) 294{ 295 UnityTestResultsBegin(Unity.TestFile, line); 296 if (Unity.isExpectingFail) 297 { 298 UnityPrint(UnityStrXFAIL); 299 } 300 else 301 { 302 UnityPrint(UnityStrFail); 303 } 304 305 UNITY_OUTPUT_CHAR(':'); 306} 307 308//----------------------------------------------- 309void UnityConcludeTest(void) 310{ 311#if 0 312 if (Unity.isExpectingFail == 1 && Unity.CurrentTestFailed == 0) 313 { 314 printf("FAIL WAS EXPECTED, BUT IT DIDN'T HAPPEN?!"); 315 Unity.TestXPASSES++; 316 } 317 318 else 319#endif 320 //cant be ignored and accepting fail at the same time! 321 if (Unity.isExpectingFail == 1 && Unity.CurrentTestFailed == 1) 322 { 323 Unity.TestXFAILS++; //error message?! 324 if (Unity.XFAILMessage != NULL) 325 { 326 if (Unity.XFAILMessage[0] != ' ') 327 { 328 printf(" "); 329 } 330 331 printf("| "); 332 printf("%s", Unity.XFAILMessage); 333 Unity.XFAILMessage = NULL; 334 } 335 else 336 { 337 printf(" - EXPECTED FAIL!"); 338 } 339 } 340 else 341 342 if (Unity.CurrentTestIgnored) 343 { 344 Unity.TestIgnores++; 345 } 346 else if (!Unity.CurrentTestFailed) 347 { 348 if(Unity.isExpectingFail == 0) { 349 UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber); 350 UnityPrint(UnityStrPass); 351 Unity.TestPasses++; 352 } 353 354 //probably should remove the if... part 355 else if (Unity.isExpectingFail == 1 && Unity.CurrentTestFailed == 0) 356 { 357 358 UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber); 359 UnityPrint(UnityStrXPASS); 360 Unity.TestXPASSES++; 361 362 printf(" - FAIL WAS EXPECTED, BUT DIDN'T HAPPEN?!"); 363 //if (Unity.TestPasses > 0) { Unity.TestPasses--; } 364 } 365 } 366 else 367 { 368 Unity.TestFailures++; 369 } 370 371 Unity.CurrentTestFailed = 0; 372 Unity.CurrentTestIgnored = 0; 373 Unity.isExpectingFail = 0; 374 375 UNITY_PRINT_EOL; 376} 377 378//----------------------------------------------- 379static void UnityAddMsgIfSpecified(const char* msg) 380{ 381 if (msg) 382 { 383 UnityPrint(UnityStrSpacer); 384 UnityPrint(msg); 385 } 386} 387 388//----------------------------------------------- 389static void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual) 390{ 391 UnityPrint(UnityStrExpected); 392 if (expected != NULL) 393 { 394 UNITY_OUTPUT_CHAR('\''); 395 UnityPrint(expected); 396 UNITY_OUTPUT_CHAR('\''); 397 } 398 else 399 { 400 UnityPrint(UnityStrNull); 401 } 402 UnityPrint(UnityStrWas); 403 if (actual != NULL) 404 { 405 UNITY_OUTPUT_CHAR('\''); 406 UnityPrint(actual); 407 UNITY_OUTPUT_CHAR('\''); 408 } 409 else 410 { 411 UnityPrint(UnityStrNull); 412 } 413} 414 415//----------------------------------------------- 416// Assertion & Control Helpers 417//----------------------------------------------- 418 419static int UnityCheckArraysForNull(UNITY_PTR_ATTRIBUTE const void* expected, UNITY_PTR_ATTRIBUTE const void* actual, const UNITY_LINE_TYPE lineNumber, const char* msg) 420{ 421 //return true if they are both NULL 422 if ((expected == NULL) && (actual == NULL)) 423 return 1; 424 425 //throw error if just expected is NULL 426 if (expected == NULL) 427 { 428 UnityTestResultsFailBegin(lineNumber); 429 UnityPrint(UnityStrNullPointerForExpected); 430 UnityAddMsgIfSpecified(msg); 431 UNITY_FAIL_AND_BAIL; 432 } 433 434 //throw error if just actual is NULL 435 if (actual == NULL) 436 { 437 UnityTestResultsFailBegin(lineNumber); 438 UnityPrint(UnityStrNullPointerForActual); 439 UnityAddMsgIfSpecified(msg); 440 UNITY_FAIL_AND_BAIL; 441 } 442 443 //return false if neither is NULL 444 return 0; 445} 446 447//----------------------------------------------- 448// Assertion Functions 449//----------------------------------------------- 450 451void UnityAssertBits(const _U_SINT mask, 452 const _U_SINT expected, 453 const _U_SINT actual, 454 const char* msg, 455 const UNITY_LINE_TYPE lineNumber) 456{ 457 UNITY_SKIP_EXECUTION; 458 459 if ((mask & expected) != (mask & actual)) 460 { 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); 467 UNITY_FAIL_AND_BAIL; 468 } 469} 470 471//----------------------------------------------- 472void UnityAssertEqualNumber(const _U_SINT expected, 473 const _U_SINT actual, 474 const char* msg, 475 const UNITY_LINE_TYPE lineNumber, 476 const UNITY_DISPLAY_STYLE_T style) 477{ 478 UNITY_SKIP_EXECUTION; 479 480 if (expected != actual) 481 { 482 UnityTestResultsFailBegin(lineNumber); 483 UnityPrint(UnityStrExpected); 484 UnityPrintNumberByStyle(expected, style); 485 UnityPrint(UnityStrWas); 486 UnityPrintNumberByStyle(actual, style); 487 UnityAddMsgIfSpecified(msg); 488 UNITY_FAIL_AND_BAIL; 489 } 490} 491 492//----------------------------------------------- 493void UnityAssertEqualIntArray(UNITY_PTR_ATTRIBUTE const void* expected, 494 UNITY_PTR_ATTRIBUTE const void* actual, 495 const _UU32 num_elements, 496 const char* msg, 497 const UNITY_LINE_TYPE lineNumber, 498 const UNITY_DISPLAY_STYLE_T style) 499{ 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; 503 504 UNITY_SKIP_EXECUTION; 505 506 if (elements == 0) 507 { 508 UnityTestResultsFailBegin(lineNumber); 509 UnityPrint(UnityStrPointless); 510 UnityAddMsgIfSpecified(msg); 511 UNITY_FAIL_AND_BAIL; 512 } 513 514 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE const void*)expected, (UNITY_PTR_ATTRIBUTE const void*)actual, lineNumber, msg) == 1) 515 return; 516 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)) 521 { 522 case UNITY_DISPLAY_STYLE_HEX8: 523 case UNITY_DISPLAY_STYLE_INT8: 524 case UNITY_DISPLAY_STYLE_UINT8: 525 while (elements--) 526 { 527 if (*ptr_exp != *ptr_act) 528 { 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); 537 UNITY_FAIL_AND_BAIL; 538 } 539 ptr_exp += 1; 540 ptr_act += 1; 541 } 542 break; 543 case UNITY_DISPLAY_STYLE_HEX16: 544 case UNITY_DISPLAY_STYLE_INT16: 545 case UNITY_DISPLAY_STYLE_UINT16: 546 while (elements--) 547 { 548 if (*(UNITY_PTR_ATTRIBUTE const _US16*)(void*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US16*)(void*)ptr_act) 549 { 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); 558 UNITY_FAIL_AND_BAIL; 559 } 560 ptr_exp += 2; 561 ptr_act += 2; 562 } 563 break; 564#ifdef UNITY_SUPPORT_64 565 case UNITY_DISPLAY_STYLE_HEX64: 566 case UNITY_DISPLAY_STYLE_INT64: 567 case UNITY_DISPLAY_STYLE_UINT64: 568 while (elements--) 569 { 570 if (*(UNITY_PTR_ATTRIBUTE const _US64*)(void*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US64*)(void*)ptr_act) 571 { 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); 580 UNITY_FAIL_AND_BAIL; 581 } 582 ptr_exp += 8; 583 ptr_act += 8; 584 } 585 break; 586#endif 587 default: 588 while (elements--) 589 { 590 if (*(UNITY_PTR_ATTRIBUTE const _US32*)(void*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US32*)(void*)ptr_act) 591 { 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); 600 UNITY_FAIL_AND_BAIL; 601 } 602 ptr_exp += 4; 603 ptr_act += 4; 604 } 605 break; 606 } 607} 608 609//----------------------------------------------- 610#ifndef UNITY_EXCLUDE_FLOAT 611void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const _UF* expected, 612 UNITY_PTR_ATTRIBUTE const _UF* actual, 613 const _UU32 num_elements, 614 const char* msg, 615 const UNITY_LINE_TYPE lineNumber) 616{ 617 _UU32 elements = num_elements; 618 UNITY_PTR_ATTRIBUTE const _UF* ptr_expected = expected; 619 UNITY_PTR_ATTRIBUTE const _UF* ptr_actual = actual; 620 _UF diff, tol; 621 622 UNITY_SKIP_EXECUTION; 623 624 if (elements == 0) 625 { 626 UnityTestResultsFailBegin(lineNumber); 627 UnityPrint(UnityStrPointless); 628 UnityAddMsgIfSpecified(msg); 629 UNITY_FAIL_AND_BAIL; 630 } 631 632 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE const void*)expected, (UNITY_PTR_ATTRIBUTE const void*)actual, lineNumber, msg) == 1) 633 return; 634 635 while (elements--) 636 { 637 diff = *ptr_expected - *ptr_actual; 638 if (diff < 0.0f) 639 diff = 0.0f - diff; 640 tol = UNITY_FLOAT_PRECISION * *ptr_expected; 641 if (tol < 0.0f) 642 tol = 0.0f - tol; 643 644 //This first part of this condition will catch any NaN or Infinite values 645 if ((diff * 0.0f != 0.0f) || (diff > tol)) 646 { 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); 655#else 656 UnityPrint(UnityStrDelta); 657#endif 658 UnityAddMsgIfSpecified(msg); 659 UNITY_FAIL_AND_BAIL; 660 } 661 ptr_expected++; 662 ptr_actual++; 663 } 664} 665 666//----------------------------------------------- 667void UnityAssertFloatsWithin(const _UF delta, 668 const _UF expected, 669 const _UF actual, 670 const char* msg, 671 const UNITY_LINE_TYPE lineNumber) 672{ 673 _UF diff = actual - expected; 674 _UF pos_delta = delta; 675 676 UNITY_SKIP_EXECUTION; 677 678 if (diff < 0.0f) 679 { 680 diff = 0.0f - diff; 681 } 682 if (pos_delta < 0.0f) 683 { 684 pos_delta = 0.0f - pos_delta; 685 } 686 687 //This first part of this condition will catch any NaN or Infinite values 688 if ((diff * 0.0f != 0.0f) || (pos_delta < diff)) 689 { 690 UnityTestResultsFailBegin(lineNumber); 691#ifdef UNITY_FLOAT_VERBOSE 692 UnityPrint(UnityStrExpected); 693 UnityPrintFloat(expected); 694 UnityPrint(UnityStrWas); 695 UnityPrintFloat(actual); 696#else 697 UnityPrint(UnityStrDelta); 698#endif 699 UnityAddMsgIfSpecified(msg); 700 UNITY_FAIL_AND_BAIL; 701 } 702} 703 704//----------------------------------------------- 705void UnityAssertFloatSpecial(const _UF actual, 706 const char* msg, 707 const UNITY_LINE_TYPE lineNumber, 708 const UNITY_FLOAT_TRAIT_T style) 709{ 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; 714 715 UNITY_SKIP_EXECUTION; 716 717 switch(style) 718 { 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; 724 break; 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; 728 break; 729 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; 734 break; 735 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) ) 740 is_trait = 0; 741 else 742 is_trait = 1; 743 break; 744 default: 745 ; 746 } 747 748 if (is_trait != should_be_trait) 749 { 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); 758#else 759 if (should_be_trait) 760 UnityPrint(UnityStrNot); 761 UnityPrint(trait_names[trait_index]); 762#endif 763 UnityAddMsgIfSpecified(msg); 764 UNITY_FAIL_AND_BAIL; 765 } 766} 767 768#endif //not UNITY_EXCLUDE_FLOAT 769 770//----------------------------------------------- 771#ifndef UNITY_EXCLUDE_DOUBLE 772void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected, 773 UNITY_PTR_ATTRIBUTE const _UD* actual, 774 const _UU32 num_elements, 775 const char* msg, 776 const UNITY_LINE_TYPE lineNumber) 777{ 778 _UU32 elements = num_elements; 779 UNITY_PTR_ATTRIBUTE const _UD* ptr_expected = expected; 780 UNITY_PTR_ATTRIBUTE const _UD* ptr_actual = actual; 781 _UD diff, tol; 782 783 UNITY_SKIP_EXECUTION; 784 785 if (elements == 0) 786 { 787 UnityTestResultsFailBegin(lineNumber); 788 UnityPrint(UnityStrPointless); 789 UnityAddMsgIfSpecified(msg); 790 UNITY_FAIL_AND_BAIL; 791 } 792 793 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE void*)expected, (UNITY_PTR_ATTRIBUTE void*)actual, lineNumber, msg) == 1) 794 return; 795 796 while (elements--) 797 { 798 diff = *ptr_expected - *ptr_actual; 799 if (diff < 0.0) 800 diff = 0.0 - diff; 801 tol = UNITY_DOUBLE_PRECISION * *ptr_expected; 802 if (tol < 0.0) 803 tol = 0.0 - tol; 804 805 //This first part of this condition will catch any NaN or Infinite values 806 if ((diff * 0.0 != 0.0) || (diff > tol)) 807 { 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)); 816#else 817 UnityPrint(UnityStrDelta); 818#endif 819 UnityAddMsgIfSpecified(msg); 820 UNITY_FAIL_AND_BAIL; 821 } 822 ptr_expected++; 823 ptr_actual++; 824 } 825} 826 827//----------------------------------------------- 828void UnityAssertDoublesWithin(const _UD delta, 829 const _UD expected, 830 const _UD actual, 831 const char* msg, 832 const UNITY_LINE_TYPE lineNumber) 833{ 834 _UD diff = actual - expected; 835 _UD pos_delta = delta; 836 837 UNITY_SKIP_EXECUTION; 838 839 if (diff < 0.0) 840 { 841 diff = 0.0 - diff; 842 } 843 if (pos_delta < 0.0) 844 { 845 pos_delta = 0.0 - pos_delta; 846 } 847 848 //This first part of this condition will catch any NaN or Infinite values 849 if ((diff * 0.0 != 0.0) || (pos_delta < diff)) 850 { 851 UnityTestResultsFailBegin(lineNumber); 852#ifdef UNITY_DOUBLE_VERBOSE 853 UnityPrint(UnityStrExpected); 854 UnityPrintFloat((float)expected); 855 UnityPrint(UnityStrWas); 856 UnityPrintFloat((float)actual); 857#else 858 UnityPrint(UnityStrDelta); 859#endif 860 UnityAddMsgIfSpecified(msg); 861 UNITY_FAIL_AND_BAIL; 862 } 863} 864 865//----------------------------------------------- 866 867void UnityAssertDoubleSpecial(const _UD actual, 868 const char* msg, 869 const UNITY_LINE_TYPE lineNumber, 870 const UNITY_FLOAT_TRAIT_T style) 871{ 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; 876 877 UNITY_SKIP_EXECUTION; 878 879 switch(style) 880 { 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; 886 break; 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; 890 break; 891 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; 896 break; 897 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) ) 902 is_trait = 0; 903 else 904 is_trait = 1; 905 break; 906 default: 907 ; 908 } 909 910 if (is_trait != should_be_trait) 911 { 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); 920#else 921 if (should_be_trait) 922 UnityPrint(UnityStrNot); 923 UnityPrint(trait_names[trait_index]); 924#endif 925 UnityAddMsgIfSpecified(msg); 926 UNITY_FAIL_AND_BAIL; 927 } 928} 929 930 931#endif // not UNITY_EXCLUDE_DOUBLE 932 933//----------------------------------------------- 934void UnityAssertNumbersWithin( const _U_SINT delta, 935 const _U_SINT expected, 936 const _U_SINT actual, 937 const char* msg, 938 const UNITY_LINE_TYPE lineNumber, 939 const UNITY_DISPLAY_STYLE_T style) 940{ 941 UNITY_SKIP_EXECUTION; 942 943 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT) 944 { 945 if (actual > expected) 946 Unity.CurrentTestFailed = ((actual - expected) > delta); 947 else 948 Unity.CurrentTestFailed = ((expected - actual) > delta); 949 } 950 else 951 { 952 if ((_U_UINT)actual > (_U_UINT)expected) 953 Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > (_U_UINT)delta); 954 else 955 Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > (_U_UINT)delta); 956 } 957 958 if (Unity.CurrentTestFailed) 959 { 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); 968 UNITY_FAIL_AND_BAIL; 969 } 970} 971 972//----------------------------------------------- 973void UnityAssertEqualString(const char* expected, 974 const char* actual, 975 const char* msg, 976 const UNITY_LINE_TYPE lineNumber) 977{ 978 _UU32 i; 979 980 UNITY_SKIP_EXECUTION; 981 982 // if both pointers not null compare the strings 983 if (expected && actual) 984 { 985 for (i = 0; expected[i] || actual[i]; i++) 986 { 987 if (expected[i] != actual[i]) 988 { 989 Unity.CurrentTestFailed = 1; 990 break; 991 } 992 } 993 } 994 else 995 { // handle case of one pointers being null (if both null, test should pass) 996 if (expected != actual) 997 { 998 Unity.CurrentTestFailed = 1; 999 } 1000 } 1001 1002 if (Unity.CurrentTestFailed) 1003 { 1004 UnityTestResultsFailBegin(lineNumber); 1005 UnityPrintExpectedAndActualStrings(expected, actual); 1006 UnityAddMsgIfSpecified(msg); 1007 UNITY_FAIL_AND_BAIL; 1008 } 1009} 1010 1011//----------------------------------------------- 1012void UnityAssertEqualStringArray( const char** expected, 1013 const char** actual, 1014 const _UU32 num_elements, 1015 const char* msg, 1016 const UNITY_LINE_TYPE lineNumber) 1017{ 1018 _UU32 i, j = 0; 1019 1020 UNITY_SKIP_EXECUTION; 1021 1022 // if no elements, it's an error 1023 if (num_elements == 0) 1024 { 1025 UnityTestResultsFailBegin(lineNumber); 1026 UnityPrint(UnityStrPointless); 1027 UnityAddMsgIfSpecified(msg); 1028 UNITY_FAIL_AND_BAIL; 1029 } 1030 1031 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE void*)expected, (UNITY_PTR_ATTRIBUTE void*)actual, lineNumber, msg) == 1) 1032 return; 1033 1034 do 1035 { 1036 // if both pointers not null compare the strings 1037 if (expected[j] && actual[j]) 1038 { 1039 for (i = 0; expected[j][i] || actual[j][i]; i++) 1040 { 1041 if (expected[j][i] != actual[j][i]) 1042 { 1043 Unity.CurrentTestFailed = 1; 1044 break; 1045 } 1046 } 1047 } 1048 else 1049 { // handle case of one pointers being null (if both null, test should pass) 1050 if (expected[j] != actual[j]) 1051 { 1052 Unity.CurrentTestFailed = 1; 1053 } 1054 } 1055 1056 if (Unity.CurrentTestFailed) 1057 { 1058 UnityTestResultsFailBegin(lineNumber); 1059 if (num_elements > 1) 1060 { 1061 UnityPrint(UnityStrElement); 1062 UnityPrintNumberByStyle((j), UNITY_DISPLAY_STYLE_UINT); 1063 } 1064 UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j])); 1065 UnityAddMsgIfSpecified(msg); 1066 UNITY_FAIL_AND_BAIL; 1067 } 1068 } while (++j < num_elements); 1069} 1070 1071//----------------------------------------------- 1072void UnityAssertEqualMemory( UNITY_PTR_ATTRIBUTE const void* expected, 1073 UNITY_PTR_ATTRIBUTE const void* actual, 1074 const _UU32 length, 1075 const _UU32 num_elements, 1076 const char* msg, 1077 const UNITY_LINE_TYPE lineNumber) 1078{ 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; 1082 _UU32 bytes; 1083 1084 UNITY_SKIP_EXECUTION; 1085 1086 if ((elements == 0) || (length == 0)) 1087 { 1088 UnityTestResultsFailBegin(lineNumber); 1089 UnityPrint(UnityStrPointless); 1090 UnityAddMsgIfSpecified(msg); 1091 UNITY_FAIL_AND_BAIL; 1092 } 1093 1094 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE const void*)expected, (UNITY_PTR_ATTRIBUTE const void*)actual, lineNumber, msg) == 1) 1095 return; 1096 1097 while (elements--) 1098 { 1099 ///////////////////////////////////// 1100 bytes = length; 1101 while (bytes--) 1102 { 1103 if (*ptr_exp != *ptr_act) 1104 { 1105 UnityTestResultsFailBegin(lineNumber); 1106 UnityPrint(UnityStrMemory); 1107 if (num_elements > 1) 1108 { 1109 UnityPrint(UnityStrElement); 1110 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT); 1111 } 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; 1120 } 1121 ptr_exp += 1; 1122 ptr_act += 1; 1123 } 1124 ///////////////////////////////////// 1125 1126 } 1127} 1128 1129//----------------------------------------------- 1130// Control Functions 1131//----------------------------------------------- 1132 1133void UnityFail(const char* msg, const UNITY_LINE_TYPE line) 1134{ 1135 UNITY_SKIP_EXECUTION; 1136 1137 UnityTestResultsBegin(Unity.TestFile, line); 1138 UnityPrintFail(); 1139 if (msg != NULL) 1140 { 1141 UNITY_OUTPUT_CHAR(':'); 1142 if (msg[0] != ' ') 1143 { 1144 UNITY_OUTPUT_CHAR(' '); 1145 } 1146 UnityPrint(msg); 1147 } 1148 UNITY_FAIL_AND_BAIL; 1149} 1150 1151//----------------------------------------------- 1152void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line) 1153{ 1154 UNITY_SKIP_EXECUTION; 1155 1156 UnityTestResultsBegin(Unity.TestFile, line); 1157 UnityPrint(UnityStrIgnore); 1158 if (msg != NULL) 1159 { 1160 UNITY_OUTPUT_CHAR(':'); 1161 UNITY_OUTPUT_CHAR(' '); 1162 UnityPrint(msg); 1163 } 1164 UNITY_IGNORE_AND_BAIL; 1165} 1166 1167//---------------------------------------------- 1168 1169void UnityExpectFail(){ 1170 1171 Unity.isExpectingFail = 1; 1172 1173} 1174 1175void UnityExpectFailMessage(const char* msg, const UNITY_LINE_TYPE line ){ 1176 1177 Unity.isExpectingFail = 1; 1178 if (msg != NULL) 1179 { 1180 Unity.XFAILMessage = msg; 1181 } 1182} 1183 1184//----------------------------------------------- 1185#if defined(UNITY_WEAK_ATTRIBUTE) 1186 void setUp(void); 1187 void tearDown(void); 1188 UNITY_WEAK_ATTRIBUTE void setUp(void) { } 1189 UNITY_WEAK_ATTRIBUTE void tearDown(void) { } 1190#elif defined(UNITY_WEAK_PRAGMA) 1191# pragma weak setUp 1192 void setUp(void); 1193# pragma weak tearDown 1194 void tearDown(void); 1195#else 1196 void setUp(void); 1197 void tearDown(void); 1198#endif 1199 1200//----------------------------------------------- 1201void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum) 1202{ 1203 Unity.CurrentTestName = FuncName; 1204 Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum; 1205 Unity.NumberOfTests++; 1206 1207 if (TEST_PROTECT()) 1208 { 1209 setUp(); 1210 Func(); 1211 } 1212 if (TEST_PROTECT() && !(Unity.CurrentTestIgnored)) 1213 { 1214 tearDown(); 1215 } 1216 1217 UnityConcludeTest(); 1218} 1219 1220 1221//----------------------------------------------- 1222void UnityBegin(const char* filename) 1223{ 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; 1237 1238 UNITY_OUTPUT_START(); 1239} 1240 1241 1242//----------------------------------------------- 1243int UnityEnd(void) 1244{ 1245 UNITY_PRINT_EOL; 1246 UnityPrint(UnityStrBreaker); 1247 UNITY_PRINT_EOL; 1248 UnityPrintNumber((_U_SINT)(Unity.NumberOfTests)); 1249 UnityPrint(UnityStrResultsTests); 1250 UNITY_PRINT_EOL; 1251 UnityPrintNumber((_U_SINT)(Unity.TestPasses)); 1252 UnityPrint(UnityStrResultsPass); 1253 UNITY_PRINT_EOL; 1254 UnityPrintNumber((_U_SINT)(Unity.TestXFAILS)); 1255 UnityPrint(UnityStrResultsXFAIL); 1256 UNITY_PRINT_EOL; 1257 UnityPrintNumber((_U_SINT)(Unity.TestFailures)); 1258 UnityPrint(UnityStrResultsFailures); 1259 UNITY_PRINT_EOL; 1260 UnityPrintNumber((_U_SINT)(Unity.TestXPASSES)); 1261 UnityPrint(UnityStrResultsXPASS); 1262 UNITY_PRINT_EOL; 1263 UnityPrintNumber((_U_SINT)(Unity.TestIgnores)); 1264 UnityPrint(UnityStrResultsIgnored); 1265 UNITY_PRINT_EOL; 1266 1267 UNITY_PRINT_EOL; 1268 if (Unity.TestFailures == 0U && Unity.TestXPASSES == 0U) 1269 { 1270 UnityPrintOk(); 1271 } 1272 else 1273 { 1274 UnityPrintFail(); 1275 } 1276 UNITY_PRINT_EOL; 1277 UNITY_OUTPUT_COMPLETE(); 1278 return (int)(Unity.TestFailures); 1279} 1280 1281 1282//----------------------------------------------- 1283