1 /* ========================================================================= 2 Unity Project - A Test Framework for C 3 Copyright (c) 2007-14 Mike Karlesky, Mark VanderVoord, Greg Williams 4 [Released under MIT License. Please refer to license.txt for details] 5 ============================================================================ */ 6 7 #include "unity.h" 8 9 #define UNITY_FAIL_AND_BAIL { Unity.CurrentTestFailed = 1; longjmp(Unity.AbortFrame, 1); } 10 #define UNITY_IGNORE_AND_BAIL { Unity.CurrentTestIgnored = 1; longjmp(Unity.AbortFrame, 1); } 11 /// return prematurely if we are already in failure or ignore state 12 #define UNITY_SKIP_EXECUTION { if ((Unity.CurrentTestFailed != 0) || (Unity.CurrentTestIgnored != 0)) {return;} } 13 #define UNITY_PRINT_EOL { UNITY_OUTPUT_CHAR('\n'); } 14 15 struct _Unity Unity; 16 17 const char UnityStrOk[] = "OK"; 18 const char UnityStrPass[] = "PASS"; 19 const char UnityStrFail[] = "FAIL"; 20 const char UnityStrIgnore[] = "IGNORE"; 21 const char UnityStrXPASS[] = "XPASS"; 22 const char UnityStrXFAIL[] = "XFAIL"; 23 const char UnityStrNull[] = "NULL"; 24 const char UnityStrSpacer[] = ". "; 25 const char UnityStrExpected[] = " Expected "; 26 const char UnityStrWas[] = " Was "; 27 const char UnityStrTo[] = " To "; 28 const char UnityStrElement[] = " Element "; 29 const char UnityStrByte[] = " Byte "; 30 const char UnityStrMemory[] = " Memory Mismatch."; 31 const char UnityStrDelta[] = " Values Not Within Delta "; 32 const char UnityStrPointless[] = " You Asked Me To Compare Nothing, Which Was Pointless."; 33 const char UnityStrNullPointerForExpected[] = " Expected pointer to be NULL"; 34 const char UnityStrNullPointerForActual[] = " Actual pointer was NULL"; 35 const char UnityStrNot[] = "Not "; 36 const char UnityStrInf[] = "Infinity"; 37 const char UnityStrNegInf[] = "Negative Infinity"; 38 const char UnityStrNaN[] = "NaN"; 39 const char UnityStrDet[] = "Determinate"; 40 const char UnityStrErrFloat[] = "Unity Floating Point Disabled"; 41 const char UnityStrErrDouble[] = "Unity Double Precision Disabled"; 42 const char UnityStrErr64[] = "Unity 64-bit Support Disabled"; 43 const char UnityStrBreaker[] = "-----------------------"; 44 const char UnityStrResultsTests[] = " Tests: "; 45 const char UnityStrResultsFailures[] = " Failures "; 46 const char UnityStrResultsIgnored[] = " Ignored "; 47 const char UnityStrResultsXFAIL[] = " XFAIL "; 48 const char UnityStrResultsXPASS[] = " XPASS "; 49 const char UnityStrResultsPass[] = " PASS "; 50 51 52 #ifndef UNITY_EXCLUDE_FLOAT 53 // Dividing by these constants produces +/- infinity. 54 // The rationale is given in UnityAssertFloatIsInf's body. 55 static const _UF f_zero = 0.0f; 56 #ifndef UNITY_EXCLUDE_DOUBLE 57 static const _UD d_zero = 0.0; 58 #endif 59 #endif 60 61 // compiler-generic print formatting masks 62 const _U_UINT UnitySizeMask[] = 63 { 64 255u, // 0xFF 65 65535u, // 0xFFFF 66 65535u, 67 4294967295u, // 0xFFFFFFFF 68 4294967295u, 69 4294967295u, 70 4294967295u 71 #ifdef UNITY_SUPPORT_64 72 ,0xFFFFFFFFFFFFFFFF 73 #endif 74 }; 75 76 void UnityPrintFail(void); 77 void UnityPrintOk(void); 78 79 //----------------------------------------------- 80 // Pretty Printers & Test Result Output Handlers 81 //----------------------------------------------- 82 83 void UnityPrint(const char* string) 84 { 85 const char* pch = string; 86 87 if (pch != NULL) 88 { 89 while (*pch) 90 { 91 // printable characters plus CR & LF are printed 92 if ((*pch <= 126) && (*pch >= 32)) 93 { 94 UNITY_OUTPUT_CHAR(*pch); 95 } 96 //write escaped carriage returns 97 else if (*pch == 13) 98 { 99 UNITY_OUTPUT_CHAR('\\'); 100 UNITY_OUTPUT_CHAR('r'); 101 } 102 //write escaped line feeds 103 else if (*pch == 10) 104 { 105 UNITY_OUTPUT_CHAR('\\'); 106 UNITY_OUTPUT_CHAR('n'); 107 } 108 // unprintable characters are shown as codes 109 else 110 { 111 UNITY_OUTPUT_CHAR('\\'); 112 UnityPrintNumberHex((_U_UINT)*pch, 2); 113 } 114 pch++; 115 } 116 } 117 } 118 119 //----------------------------------------------- 120 void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style) 121 { 122 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT) 123 { 124 UnityPrintNumber(number); 125 } 126 else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT) 127 { 128 UnityPrintNumberUnsigned( (_U_UINT)number & UnitySizeMask[((_U_UINT)style & (_U_UINT)0x0F) - 1] ); 129 } 130 else 131 { 132 UnityPrintNumberHex((_U_UINT)number, (char)((style & 0x000F) << 1)); 133 } 134 } 135 136 //----------------------------------------------- 137 /// basically do an itoa using as little ram as possible 138 void UnityPrintNumber(const _U_SINT number_to_print) 139 { 140 _U_SINT divisor = 1; 141 _U_SINT next_divisor; 142 _U_UINT number; 143 144 if (number_to_print == (1l << (UNITY_LONG_WIDTH-1))) 145 { 146 //The largest representable negative number 147 UNITY_OUTPUT_CHAR('-'); 148 number = (1ul << (UNITY_LONG_WIDTH-1)); 149 } 150 else if (number_to_print < 0) 151 { 152 //Some other negative number 153 UNITY_OUTPUT_CHAR('-'); 154 number = (_U_UINT)(-number_to_print); 155 } 156 else 157 { 158 //Positive number 159 number = (_U_UINT)number_to_print; 160 } 161 162 // figure out initial divisor 163 while (number / divisor > 9) 164 { 165 next_divisor = divisor * 10; 166 if (next_divisor > divisor) 167 divisor = next_divisor; 168 else 169 break; 170 } 171 172 // now mod and print, then divide divisor 173 do 174 { 175 UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10))); 176 divisor /= 10; 177 } 178 while (divisor > 0); 179 } 180 181 //----------------------------------------------- 182 /// basically do an itoa using as little ram as possible 183 void UnityPrintNumberUnsigned(const _U_UINT number) 184 { 185 _U_UINT divisor = 1; 186 _U_UINT next_divisor; 187 188 // figure out initial divisor 189 while (number / divisor > 9) 190 { 191 next_divisor = divisor * 10; 192 if (next_divisor > divisor) 193 divisor = next_divisor; 194 else 195 break; 196 } 197 198 // now mod and print, then divide divisor 199 do 200 { 201 UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10))); 202 divisor /= 10; 203 } 204 while (divisor > 0); 205 } 206 207 //----------------------------------------------- 208 void UnityPrintNumberHex(const _U_UINT number, const char nibbles_to_print) 209 { 210 _U_UINT nibble; 211 char nibbles = nibbles_to_print; 212 UNITY_OUTPUT_CHAR('0'); 213 UNITY_OUTPUT_CHAR('x'); 214 215 while (nibbles > 0) 216 { 217 nibble = (number >> (--nibbles << 2)) & 0x0000000F; 218 if (nibble <= 9) 219 { 220 UNITY_OUTPUT_CHAR((char)('0' + nibble)); 221 } 222 else 223 { 224 UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble)); 225 } 226 } 227 } 228 229 //----------------------------------------------- 230 void UnityPrintMask(const _U_UINT mask, const _U_UINT number) 231 { 232 _U_UINT current_bit = (_U_UINT)1 << (UNITY_INT_WIDTH - 1); 233 _US32 i; 234 235 for (i = 0; i < UNITY_INT_WIDTH; i++) 236 { 237 if (current_bit & mask) 238 { 239 if (current_bit & number) 240 { 241 UNITY_OUTPUT_CHAR('1'); 242 } 243 else 244 { 245 UNITY_OUTPUT_CHAR('0'); 246 } 247 } 248 else 249 { 250 UNITY_OUTPUT_CHAR('X'); 251 } 252 current_bit = current_bit >> 1; 253 } 254 } 255 256 //----------------------------------------------- 257 #ifdef UNITY_FLOAT_VERBOSE 258 #include <string.h> 259 void UnityPrintFloat(_UF number) 260 { 261 char TempBuffer[32]; 262 sprintf(TempBuffer, "%.6f", number); 263 UnityPrint(TempBuffer); 264 } 265 #endif 266 267 //----------------------------------------------- 268 269 void UnityPrintFail(void) 270 { 271 UnityPrint(UnityStrFail); 272 } 273 274 void UnityPrintOk(void) 275 { 276 UnityPrint(UnityStrOk); 277 } 278 279 //----------------------------------------------- 280 static void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line) 281 { 282 UnityPrint(file); 283 UNITY_OUTPUT_CHAR(':'); 284 UnityPrintNumber((_U_SINT)line); 285 UNITY_OUTPUT_CHAR(':'); 286 UnityPrint(Unity.CurrentTestName); 287 UNITY_OUTPUT_CHAR(':'); 288 } 289 290 //----------------------------------------------- 291 static void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line) 292 { 293 UnityTestResultsBegin(Unity.TestFile, line); 294 if (Unity.isExpectingFail) 295 { 296 UnityPrint(UnityStrXFAIL); 297 } 298 else 299 { 300 UnityPrint(UnityStrFail); 301 } 302 303 UNITY_OUTPUT_CHAR(':'); 304 } 305 306 //----------------------------------------------- 307 void UnityConcludeTest(void) 308 { 309 #if 0 310 if (Unity.isExpectingFail == 1 && Unity.CurrentTestFailed == 0) 311 { 312 printf("FAIL WAS EXPECTED, BUT IT DIDN'T HAPPEN?!"); 313 Unity.TestXPASSES++; 314 } 315 316 else 317 #endif 318 //cant be ignored and accepting fail at the same time! 319 if (Unity.isExpectingFail == 1 && Unity.CurrentTestFailed == 1) 320 { 321 Unity.TestXFAILS++; //error message?! 322 if (Unity.XFAILMessage != NULL) 323 { 324 if (Unity.XFAILMessage[0] != ' ') 325 { 326 printf(" "); 327 } 328 329 printf("| "); 330 printf(Unity.XFAILMessage); 331 Unity.XFAILMessage = NULL; 332 } 333 else 334 { 335 printf(" - EXPECTED FAIL!"); 336 } 337 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 //----------------------------------------------- 379 static void UnityAddMsgIfSpecified(const char* msg) 380 { 381 if (msg) 382 { 383 UnityPrint(UnityStrSpacer); 384 UnityPrint(msg); 385 } 386 } 387 388 //----------------------------------------------- 389 static 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 419 static 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 451 void 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 //----------------------------------------------- 472 void 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 //----------------------------------------------- 493 void 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 611 void 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 //----------------------------------------------- 667 void 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 //----------------------------------------------- 705 void 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 772 void 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 //----------------------------------------------- 828 void 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 867 void 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 //----------------------------------------------- 934 void 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 //----------------------------------------------- 973 void 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 //----------------------------------------------- 1012 void 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 //----------------------------------------------- 1072 void 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 1133 void 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 //----------------------------------------------- 1152 void 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 1169 void UnityExpectFail(){ 1170 1171 Unity.isExpectingFail = 1; 1172 1173 } 1174 1175 void 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 //----------------------------------------------- 1201 void 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 //----------------------------------------------- 1222 void 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 //----------------------------------------------- 1243 int 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