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("%s", Unity.XFAILMessage); 331 Unity.XFAILMessage = NULL; 332 } 333 else 334 { 335 printf(" - EXPECTED FAIL!"); 336 } 337 } 338 else 339 340 if (Unity.CurrentTestIgnored) 341 { 342 Unity.TestIgnores++; 343 } 344 else if (!Unity.CurrentTestFailed) 345 { 346 if(Unity.isExpectingFail == 0) { 347 UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber); 348 UnityPrint(UnityStrPass); 349 Unity.TestPasses++; 350 } 351 352 //probably should remove the if... part 353 else if (Unity.isExpectingFail == 1 && Unity.CurrentTestFailed == 0) 354 { 355 356 UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber); 357 UnityPrint(UnityStrXPASS); 358 Unity.TestXPASSES++; 359 360 printf(" - FAIL WAS EXPECTED, BUT DIDN'T HAPPEN?!"); 361 //if (Unity.TestPasses > 0) { Unity.TestPasses--; } 362 } 363 } 364 else 365 { 366 Unity.TestFailures++; 367 } 368 369 Unity.CurrentTestFailed = 0; 370 Unity.CurrentTestIgnored = 0; 371 Unity.isExpectingFail = 0; 372 373 UNITY_PRINT_EOL; 374 } 375 376 //----------------------------------------------- 377 static void UnityAddMsgIfSpecified(const char* msg) 378 { 379 if (msg) 380 { 381 UnityPrint(UnityStrSpacer); 382 UnityPrint(msg); 383 } 384 } 385 386 //----------------------------------------------- 387 static void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual) 388 { 389 UnityPrint(UnityStrExpected); 390 if (expected != NULL) 391 { 392 UNITY_OUTPUT_CHAR('\''); 393 UnityPrint(expected); 394 UNITY_OUTPUT_CHAR('\''); 395 } 396 else 397 { 398 UnityPrint(UnityStrNull); 399 } 400 UnityPrint(UnityStrWas); 401 if (actual != NULL) 402 { 403 UNITY_OUTPUT_CHAR('\''); 404 UnityPrint(actual); 405 UNITY_OUTPUT_CHAR('\''); 406 } 407 else 408 { 409 UnityPrint(UnityStrNull); 410 } 411 } 412 413 //----------------------------------------------- 414 // Assertion & Control Helpers 415 //----------------------------------------------- 416 417 static int UnityCheckArraysForNull(UNITY_PTR_ATTRIBUTE const void* expected, UNITY_PTR_ATTRIBUTE const void* actual, const UNITY_LINE_TYPE lineNumber, const char* msg) 418 { 419 //return true if they are both NULL 420 if ((expected == NULL) && (actual == NULL)) 421 return 1; 422 423 //throw error if just expected is NULL 424 if (expected == NULL) 425 { 426 UnityTestResultsFailBegin(lineNumber); 427 UnityPrint(UnityStrNullPointerForExpected); 428 UnityAddMsgIfSpecified(msg); 429 UNITY_FAIL_AND_BAIL; 430 } 431 432 //throw error if just actual is NULL 433 if (actual == NULL) 434 { 435 UnityTestResultsFailBegin(lineNumber); 436 UnityPrint(UnityStrNullPointerForActual); 437 UnityAddMsgIfSpecified(msg); 438 UNITY_FAIL_AND_BAIL; 439 } 440 441 //return false if neither is NULL 442 return 0; 443 } 444 445 //----------------------------------------------- 446 // Assertion Functions 447 //----------------------------------------------- 448 449 void UnityAssertBits(const _U_SINT mask, 450 const _U_SINT expected, 451 const _U_SINT actual, 452 const char* msg, 453 const UNITY_LINE_TYPE lineNumber) 454 { 455 UNITY_SKIP_EXECUTION; 456 457 if ((mask & expected) != (mask & actual)) 458 { 459 UnityTestResultsFailBegin(lineNumber); 460 UnityPrint(UnityStrExpected); 461 UnityPrintMask((_U_UINT)mask, (_U_UINT)expected); 462 UnityPrint(UnityStrWas); 463 UnityPrintMask((_U_UINT)mask, (_U_UINT)actual); 464 UnityAddMsgIfSpecified(msg); 465 UNITY_FAIL_AND_BAIL; 466 } 467 } 468 469 //----------------------------------------------- 470 void UnityAssertEqualNumber(const _U_SINT expected, 471 const _U_SINT actual, 472 const char* msg, 473 const UNITY_LINE_TYPE lineNumber, 474 const UNITY_DISPLAY_STYLE_T style) 475 { 476 UNITY_SKIP_EXECUTION; 477 478 if (expected != actual) 479 { 480 UnityTestResultsFailBegin(lineNumber); 481 UnityPrint(UnityStrExpected); 482 UnityPrintNumberByStyle(expected, style); 483 UnityPrint(UnityStrWas); 484 UnityPrintNumberByStyle(actual, style); 485 UnityAddMsgIfSpecified(msg); 486 UNITY_FAIL_AND_BAIL; 487 } 488 } 489 490 //----------------------------------------------- 491 void UnityAssertEqualIntArray(UNITY_PTR_ATTRIBUTE const void* expected, 492 UNITY_PTR_ATTRIBUTE const void* actual, 493 const _UU32 num_elements, 494 const char* msg, 495 const UNITY_LINE_TYPE lineNumber, 496 const UNITY_DISPLAY_STYLE_T style) 497 { 498 _UU32 elements = num_elements; 499 UNITY_PTR_ATTRIBUTE const _US8* ptr_exp = (UNITY_PTR_ATTRIBUTE const _US8*)expected; 500 UNITY_PTR_ATTRIBUTE const _US8* ptr_act = (UNITY_PTR_ATTRIBUTE const _US8*)actual; 501 502 UNITY_SKIP_EXECUTION; 503 504 if (elements == 0) 505 { 506 UnityTestResultsFailBegin(lineNumber); 507 UnityPrint(UnityStrPointless); 508 UnityAddMsgIfSpecified(msg); 509 UNITY_FAIL_AND_BAIL; 510 } 511 512 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE const void*)expected, (UNITY_PTR_ATTRIBUTE const void*)actual, lineNumber, msg) == 1) 513 return; 514 515 // If style is UNITY_DISPLAY_STYLE_INT, we'll fall into the default case rather than the INT16 or INT32 (etc) case 516 // as UNITY_DISPLAY_STYLE_INT includes a flag for UNITY_DISPLAY_RANGE_AUTO, which the width-specific 517 // variants do not. Therefore remove this flag. 518 switch(style & (UNITY_DISPLAY_STYLE_T)(~UNITY_DISPLAY_RANGE_AUTO)) 519 { 520 case UNITY_DISPLAY_STYLE_HEX8: 521 case UNITY_DISPLAY_STYLE_INT8: 522 case UNITY_DISPLAY_STYLE_UINT8: 523 while (elements--) 524 { 525 if (*ptr_exp != *ptr_act) 526 { 527 UnityTestResultsFailBegin(lineNumber); 528 UnityPrint(UnityStrElement); 529 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT); 530 UnityPrint(UnityStrExpected); 531 UnityPrintNumberByStyle(*ptr_exp, style); 532 UnityPrint(UnityStrWas); 533 UnityPrintNumberByStyle(*ptr_act, style); 534 UnityAddMsgIfSpecified(msg); 535 UNITY_FAIL_AND_BAIL; 536 } 537 ptr_exp += 1; 538 ptr_act += 1; 539 } 540 break; 541 case UNITY_DISPLAY_STYLE_HEX16: 542 case UNITY_DISPLAY_STYLE_INT16: 543 case UNITY_DISPLAY_STYLE_UINT16: 544 while (elements--) 545 { 546 if (*(UNITY_PTR_ATTRIBUTE const _US16*)(void*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US16*)(void*)ptr_act) 547 { 548 UnityTestResultsFailBegin(lineNumber); 549 UnityPrint(UnityStrElement); 550 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT); 551 UnityPrint(UnityStrExpected); 552 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US16*)(void*)ptr_exp, style); 553 UnityPrint(UnityStrWas); 554 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US16*)(void*)ptr_act, style); 555 UnityAddMsgIfSpecified(msg); 556 UNITY_FAIL_AND_BAIL; 557 } 558 ptr_exp += 2; 559 ptr_act += 2; 560 } 561 break; 562 #ifdef UNITY_SUPPORT_64 563 case UNITY_DISPLAY_STYLE_HEX64: 564 case UNITY_DISPLAY_STYLE_INT64: 565 case UNITY_DISPLAY_STYLE_UINT64: 566 while (elements--) 567 { 568 if (*(UNITY_PTR_ATTRIBUTE const _US64*)(void*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US64*)(void*)ptr_act) 569 { 570 UnityTestResultsFailBegin(lineNumber); 571 UnityPrint(UnityStrElement); 572 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT); 573 UnityPrint(UnityStrExpected); 574 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US64*)(void*)ptr_exp, style); 575 UnityPrint(UnityStrWas); 576 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US64*)(void*)ptr_act, style); 577 UnityAddMsgIfSpecified(msg); 578 UNITY_FAIL_AND_BAIL; 579 } 580 ptr_exp += 8; 581 ptr_act += 8; 582 } 583 break; 584 #endif 585 default: 586 while (elements--) 587 { 588 if (*(UNITY_PTR_ATTRIBUTE const _US32*)(void*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US32*)(void*)ptr_act) 589 { 590 UnityTestResultsFailBegin(lineNumber); 591 UnityPrint(UnityStrElement); 592 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT); 593 UnityPrint(UnityStrExpected); 594 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US32*)(void*)ptr_exp, style); 595 UnityPrint(UnityStrWas); 596 UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US32*)(void*)ptr_act, style); 597 UnityAddMsgIfSpecified(msg); 598 UNITY_FAIL_AND_BAIL; 599 } 600 ptr_exp += 4; 601 ptr_act += 4; 602 } 603 break; 604 } 605 } 606 607 //----------------------------------------------- 608 #ifndef UNITY_EXCLUDE_FLOAT 609 void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const _UF* expected, 610 UNITY_PTR_ATTRIBUTE const _UF* actual, 611 const _UU32 num_elements, 612 const char* msg, 613 const UNITY_LINE_TYPE lineNumber) 614 { 615 _UU32 elements = num_elements; 616 UNITY_PTR_ATTRIBUTE const _UF* ptr_expected = expected; 617 UNITY_PTR_ATTRIBUTE const _UF* ptr_actual = actual; 618 _UF diff, tol; 619 620 UNITY_SKIP_EXECUTION; 621 622 if (elements == 0) 623 { 624 UnityTestResultsFailBegin(lineNumber); 625 UnityPrint(UnityStrPointless); 626 UnityAddMsgIfSpecified(msg); 627 UNITY_FAIL_AND_BAIL; 628 } 629 630 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE const void*)expected, (UNITY_PTR_ATTRIBUTE const void*)actual, lineNumber, msg) == 1) 631 return; 632 633 while (elements--) 634 { 635 diff = *ptr_expected - *ptr_actual; 636 if (diff < 0.0f) 637 diff = 0.0f - diff; 638 tol = UNITY_FLOAT_PRECISION * *ptr_expected; 639 if (tol < 0.0f) 640 tol = 0.0f - tol; 641 642 //This first part of this condition will catch any NaN or Infinite values 643 if ((diff * 0.0f != 0.0f) || (diff > tol)) 644 { 645 UnityTestResultsFailBegin(lineNumber); 646 UnityPrint(UnityStrElement); 647 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT); 648 #ifdef UNITY_FLOAT_VERBOSE 649 UnityPrint(UnityStrExpected); 650 UnityPrintFloat(*ptr_expected); 651 UnityPrint(UnityStrWas); 652 UnityPrintFloat(*ptr_actual); 653 #else 654 UnityPrint(UnityStrDelta); 655 #endif 656 UnityAddMsgIfSpecified(msg); 657 UNITY_FAIL_AND_BAIL; 658 } 659 ptr_expected++; 660 ptr_actual++; 661 } 662 } 663 664 //----------------------------------------------- 665 void UnityAssertFloatsWithin(const _UF delta, 666 const _UF expected, 667 const _UF actual, 668 const char* msg, 669 const UNITY_LINE_TYPE lineNumber) 670 { 671 _UF diff = actual - expected; 672 _UF pos_delta = delta; 673 674 UNITY_SKIP_EXECUTION; 675 676 if (diff < 0.0f) 677 { 678 diff = 0.0f - diff; 679 } 680 if (pos_delta < 0.0f) 681 { 682 pos_delta = 0.0f - pos_delta; 683 } 684 685 //This first part of this condition will catch any NaN or Infinite values 686 if ((diff * 0.0f != 0.0f) || (pos_delta < diff)) 687 { 688 UnityTestResultsFailBegin(lineNumber); 689 #ifdef UNITY_FLOAT_VERBOSE 690 UnityPrint(UnityStrExpected); 691 UnityPrintFloat(expected); 692 UnityPrint(UnityStrWas); 693 UnityPrintFloat(actual); 694 #else 695 UnityPrint(UnityStrDelta); 696 #endif 697 UnityAddMsgIfSpecified(msg); 698 UNITY_FAIL_AND_BAIL; 699 } 700 } 701 702 //----------------------------------------------- 703 void UnityAssertFloatSpecial(const _UF actual, 704 const char* msg, 705 const UNITY_LINE_TYPE lineNumber, 706 const UNITY_FLOAT_TRAIT_T style) 707 { 708 const char* trait_names[] = { UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet }; 709 _U_SINT should_be_trait = ((_U_SINT)style & 1); 710 _U_SINT is_trait = !should_be_trait; 711 _U_SINT trait_index = style >> 1; 712 713 UNITY_SKIP_EXECUTION; 714 715 switch(style) 716 { 717 //To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly 718 //We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise 719 case UNITY_FLOAT_IS_INF: 720 case UNITY_FLOAT_IS_NOT_INF: 721 is_trait = ((1.0f / f_zero) == actual) ? 1 : 0; 722 break; 723 case UNITY_FLOAT_IS_NEG_INF: 724 case UNITY_FLOAT_IS_NOT_NEG_INF: 725 is_trait = ((-1.0f / f_zero) == actual) ? 1 : 0; 726 break; 727 728 //NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN. 729 case UNITY_FLOAT_IS_NAN: 730 case UNITY_FLOAT_IS_NOT_NAN: 731 is_trait = (actual == actual) ? 0 : 1; 732 break; 733 734 //A determinate number is non infinite and not NaN. (therefore the opposite of the two above) 735 case UNITY_FLOAT_IS_DET: 736 case UNITY_FLOAT_IS_NOT_DET: 737 if ( (actual != actual) || ((1.0f / f_zero) == actual) || ((-1.0f / f_zero) == actual) ) 738 is_trait = 0; 739 else 740 is_trait = 1; 741 break; 742 default: 743 ; 744 } 745 746 if (is_trait != should_be_trait) 747 { 748 UnityTestResultsFailBegin(lineNumber); 749 UnityPrint(UnityStrExpected); 750 if (!should_be_trait) 751 UnityPrint(UnityStrNot); 752 UnityPrint(trait_names[trait_index]); 753 UnityPrint(UnityStrWas); 754 #ifdef UNITY_FLOAT_VERBOSE 755 UnityPrintFloat(actual); 756 #else 757 if (should_be_trait) 758 UnityPrint(UnityStrNot); 759 UnityPrint(trait_names[trait_index]); 760 #endif 761 UnityAddMsgIfSpecified(msg); 762 UNITY_FAIL_AND_BAIL; 763 } 764 } 765 766 #endif //not UNITY_EXCLUDE_FLOAT 767 768 //----------------------------------------------- 769 #ifndef UNITY_EXCLUDE_DOUBLE 770 void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected, 771 UNITY_PTR_ATTRIBUTE const _UD* actual, 772 const _UU32 num_elements, 773 const char* msg, 774 const UNITY_LINE_TYPE lineNumber) 775 { 776 _UU32 elements = num_elements; 777 UNITY_PTR_ATTRIBUTE const _UD* ptr_expected = expected; 778 UNITY_PTR_ATTRIBUTE const _UD* ptr_actual = actual; 779 _UD diff, tol; 780 781 UNITY_SKIP_EXECUTION; 782 783 if (elements == 0) 784 { 785 UnityTestResultsFailBegin(lineNumber); 786 UnityPrint(UnityStrPointless); 787 UnityAddMsgIfSpecified(msg); 788 UNITY_FAIL_AND_BAIL; 789 } 790 791 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE void*)expected, (UNITY_PTR_ATTRIBUTE void*)actual, lineNumber, msg) == 1) 792 return; 793 794 while (elements--) 795 { 796 diff = *ptr_expected - *ptr_actual; 797 if (diff < 0.0) 798 diff = 0.0 - diff; 799 tol = UNITY_DOUBLE_PRECISION * *ptr_expected; 800 if (tol < 0.0) 801 tol = 0.0 - tol; 802 803 //This first part of this condition will catch any NaN or Infinite values 804 if ((diff * 0.0 != 0.0) || (diff > tol)) 805 { 806 UnityTestResultsFailBegin(lineNumber); 807 UnityPrint(UnityStrElement); 808 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT); 809 #ifdef UNITY_DOUBLE_VERBOSE 810 UnityPrint(UnityStrExpected); 811 UnityPrintFloat((float)(*ptr_expected)); 812 UnityPrint(UnityStrWas); 813 UnityPrintFloat((float)(*ptr_actual)); 814 #else 815 UnityPrint(UnityStrDelta); 816 #endif 817 UnityAddMsgIfSpecified(msg); 818 UNITY_FAIL_AND_BAIL; 819 } 820 ptr_expected++; 821 ptr_actual++; 822 } 823 } 824 825 //----------------------------------------------- 826 void UnityAssertDoublesWithin(const _UD delta, 827 const _UD expected, 828 const _UD actual, 829 const char* msg, 830 const UNITY_LINE_TYPE lineNumber) 831 { 832 _UD diff = actual - expected; 833 _UD pos_delta = delta; 834 835 UNITY_SKIP_EXECUTION; 836 837 if (diff < 0.0) 838 { 839 diff = 0.0 - diff; 840 } 841 if (pos_delta < 0.0) 842 { 843 pos_delta = 0.0 - pos_delta; 844 } 845 846 //This first part of this condition will catch any NaN or Infinite values 847 if ((diff * 0.0 != 0.0) || (pos_delta < diff)) 848 { 849 UnityTestResultsFailBegin(lineNumber); 850 #ifdef UNITY_DOUBLE_VERBOSE 851 UnityPrint(UnityStrExpected); 852 UnityPrintFloat((float)expected); 853 UnityPrint(UnityStrWas); 854 UnityPrintFloat((float)actual); 855 #else 856 UnityPrint(UnityStrDelta); 857 #endif 858 UnityAddMsgIfSpecified(msg); 859 UNITY_FAIL_AND_BAIL; 860 } 861 } 862 863 //----------------------------------------------- 864 865 void UnityAssertDoubleSpecial(const _UD actual, 866 const char* msg, 867 const UNITY_LINE_TYPE lineNumber, 868 const UNITY_FLOAT_TRAIT_T style) 869 { 870 const char* trait_names[] = { UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet }; 871 _U_SINT should_be_trait = ((_U_SINT)style & 1); 872 _U_SINT is_trait = !should_be_trait; 873 _U_SINT trait_index = style >> 1; 874 875 UNITY_SKIP_EXECUTION; 876 877 switch(style) 878 { 879 //To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly 880 //We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise 881 case UNITY_FLOAT_IS_INF: 882 case UNITY_FLOAT_IS_NOT_INF: 883 is_trait = ((1.0 / d_zero) == actual) ? 1 : 0; 884 break; 885 case UNITY_FLOAT_IS_NEG_INF: 886 case UNITY_FLOAT_IS_NOT_NEG_INF: 887 is_trait = ((-1.0 / d_zero) == actual) ? 1 : 0; 888 break; 889 890 //NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN. 891 case UNITY_FLOAT_IS_NAN: 892 case UNITY_FLOAT_IS_NOT_NAN: 893 is_trait = (actual == actual) ? 0 : 1; 894 break; 895 896 //A determinate number is non infinite and not NaN. (therefore the opposite of the two above) 897 case UNITY_FLOAT_IS_DET: 898 case UNITY_FLOAT_IS_NOT_DET: 899 if ( (actual != actual) || ((1.0 / d_zero) == actual) || ((-1.0 / d_zero) == actual) ) 900 is_trait = 0; 901 else 902 is_trait = 1; 903 break; 904 default: 905 ; 906 } 907 908 if (is_trait != should_be_trait) 909 { 910 UnityTestResultsFailBegin(lineNumber); 911 UnityPrint(UnityStrExpected); 912 if (!should_be_trait) 913 UnityPrint(UnityStrNot); 914 UnityPrint(trait_names[trait_index]); 915 UnityPrint(UnityStrWas); 916 #ifdef UNITY_DOUBLE_VERBOSE 917 UnityPrintFloat(actual); 918 #else 919 if (should_be_trait) 920 UnityPrint(UnityStrNot); 921 UnityPrint(trait_names[trait_index]); 922 #endif 923 UnityAddMsgIfSpecified(msg); 924 UNITY_FAIL_AND_BAIL; 925 } 926 } 927 928 929 #endif // not UNITY_EXCLUDE_DOUBLE 930 931 //----------------------------------------------- 932 void UnityAssertNumbersWithin( const _U_SINT delta, 933 const _U_SINT expected, 934 const _U_SINT actual, 935 const char* msg, 936 const UNITY_LINE_TYPE lineNumber, 937 const UNITY_DISPLAY_STYLE_T style) 938 { 939 UNITY_SKIP_EXECUTION; 940 941 if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT) 942 { 943 if (actual > expected) 944 Unity.CurrentTestFailed = ((actual - expected) > delta); 945 else 946 Unity.CurrentTestFailed = ((expected - actual) > delta); 947 } 948 else 949 { 950 if ((_U_UINT)actual > (_U_UINT)expected) 951 Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > (_U_UINT)delta); 952 else 953 Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > (_U_UINT)delta); 954 } 955 956 if (Unity.CurrentTestFailed) 957 { 958 UnityTestResultsFailBegin(lineNumber); 959 UnityPrint(UnityStrDelta); 960 UnityPrintNumberByStyle(delta, style); 961 UnityPrint(UnityStrExpected); 962 UnityPrintNumberByStyle(expected, style); 963 UnityPrint(UnityStrWas); 964 UnityPrintNumberByStyle(actual, style); 965 UnityAddMsgIfSpecified(msg); 966 UNITY_FAIL_AND_BAIL; 967 } 968 } 969 970 //----------------------------------------------- 971 void UnityAssertEqualString(const char* expected, 972 const char* actual, 973 const char* msg, 974 const UNITY_LINE_TYPE lineNumber) 975 { 976 _UU32 i; 977 978 UNITY_SKIP_EXECUTION; 979 980 // if both pointers not null compare the strings 981 if (expected && actual) 982 { 983 for (i = 0; expected[i] || actual[i]; i++) 984 { 985 if (expected[i] != actual[i]) 986 { 987 Unity.CurrentTestFailed = 1; 988 break; 989 } 990 } 991 } 992 else 993 { // handle case of one pointers being null (if both null, test should pass) 994 if (expected != actual) 995 { 996 Unity.CurrentTestFailed = 1; 997 } 998 } 999 1000 if (Unity.CurrentTestFailed) 1001 { 1002 UnityTestResultsFailBegin(lineNumber); 1003 UnityPrintExpectedAndActualStrings(expected, actual); 1004 UnityAddMsgIfSpecified(msg); 1005 UNITY_FAIL_AND_BAIL; 1006 } 1007 } 1008 1009 //----------------------------------------------- 1010 void UnityAssertEqualStringArray( const char** expected, 1011 const char** actual, 1012 const _UU32 num_elements, 1013 const char* msg, 1014 const UNITY_LINE_TYPE lineNumber) 1015 { 1016 _UU32 i, j = 0; 1017 1018 UNITY_SKIP_EXECUTION; 1019 1020 // if no elements, it's an error 1021 if (num_elements == 0) 1022 { 1023 UnityTestResultsFailBegin(lineNumber); 1024 UnityPrint(UnityStrPointless); 1025 UnityAddMsgIfSpecified(msg); 1026 UNITY_FAIL_AND_BAIL; 1027 } 1028 1029 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE void*)expected, (UNITY_PTR_ATTRIBUTE void*)actual, lineNumber, msg) == 1) 1030 return; 1031 1032 do 1033 { 1034 // if both pointers not null compare the strings 1035 if (expected[j] && actual[j]) 1036 { 1037 for (i = 0; expected[j][i] || actual[j][i]; i++) 1038 { 1039 if (expected[j][i] != actual[j][i]) 1040 { 1041 Unity.CurrentTestFailed = 1; 1042 break; 1043 } 1044 } 1045 } 1046 else 1047 { // handle case of one pointers being null (if both null, test should pass) 1048 if (expected[j] != actual[j]) 1049 { 1050 Unity.CurrentTestFailed = 1; 1051 } 1052 } 1053 1054 if (Unity.CurrentTestFailed) 1055 { 1056 UnityTestResultsFailBegin(lineNumber); 1057 if (num_elements > 1) 1058 { 1059 UnityPrint(UnityStrElement); 1060 UnityPrintNumberByStyle((j), UNITY_DISPLAY_STYLE_UINT); 1061 } 1062 UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j])); 1063 UnityAddMsgIfSpecified(msg); 1064 UNITY_FAIL_AND_BAIL; 1065 } 1066 } while (++j < num_elements); 1067 } 1068 1069 //----------------------------------------------- 1070 void UnityAssertEqualMemory( UNITY_PTR_ATTRIBUTE const void* expected, 1071 UNITY_PTR_ATTRIBUTE const void* actual, 1072 const _UU32 length, 1073 const _UU32 num_elements, 1074 const char* msg, 1075 const UNITY_LINE_TYPE lineNumber) 1076 { 1077 UNITY_PTR_ATTRIBUTE const unsigned char* ptr_exp = (UNITY_PTR_ATTRIBUTE const unsigned char*)expected; 1078 UNITY_PTR_ATTRIBUTE const unsigned char* ptr_act = (UNITY_PTR_ATTRIBUTE const unsigned char*)actual; 1079 _UU32 elements = num_elements; 1080 _UU32 bytes; 1081 1082 UNITY_SKIP_EXECUTION; 1083 1084 if ((elements == 0) || (length == 0)) 1085 { 1086 UnityTestResultsFailBegin(lineNumber); 1087 UnityPrint(UnityStrPointless); 1088 UnityAddMsgIfSpecified(msg); 1089 UNITY_FAIL_AND_BAIL; 1090 } 1091 1092 if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE const void*)expected, (UNITY_PTR_ATTRIBUTE const void*)actual, lineNumber, msg) == 1) 1093 return; 1094 1095 while (elements--) 1096 { 1097 ///////////////////////////////////// 1098 bytes = length; 1099 while (bytes--) 1100 { 1101 if (*ptr_exp != *ptr_act) 1102 { 1103 UnityTestResultsFailBegin(lineNumber); 1104 UnityPrint(UnityStrMemory); 1105 if (num_elements > 1) 1106 { 1107 UnityPrint(UnityStrElement); 1108 UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT); 1109 } 1110 UnityPrint(UnityStrByte); 1111 UnityPrintNumberByStyle((length - bytes - 1), UNITY_DISPLAY_STYLE_UINT); 1112 UnityPrint(UnityStrExpected); 1113 UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8); 1114 UnityPrint(UnityStrWas); 1115 UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8); 1116 UnityAddMsgIfSpecified(msg); 1117 UNITY_FAIL_AND_BAIL; 1118 } 1119 ptr_exp += 1; 1120 ptr_act += 1; 1121 } 1122 ///////////////////////////////////// 1123 1124 } 1125 } 1126 1127 //----------------------------------------------- 1128 // Control Functions 1129 //----------------------------------------------- 1130 1131 void UnityFail(const char* msg, const UNITY_LINE_TYPE line) 1132 { 1133 UNITY_SKIP_EXECUTION; 1134 1135 UnityTestResultsBegin(Unity.TestFile, line); 1136 UnityPrintFail(); 1137 if (msg != NULL) 1138 { 1139 UNITY_OUTPUT_CHAR(':'); 1140 if (msg[0] != ' ') 1141 { 1142 UNITY_OUTPUT_CHAR(' '); 1143 } 1144 UnityPrint(msg); 1145 } 1146 UNITY_FAIL_AND_BAIL; 1147 } 1148 1149 //----------------------------------------------- 1150 void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line) 1151 { 1152 UNITY_SKIP_EXECUTION; 1153 1154 UnityTestResultsBegin(Unity.TestFile, line); 1155 UnityPrint(UnityStrIgnore); 1156 if (msg != NULL) 1157 { 1158 UNITY_OUTPUT_CHAR(':'); 1159 UNITY_OUTPUT_CHAR(' '); 1160 UnityPrint(msg); 1161 } 1162 UNITY_IGNORE_AND_BAIL; 1163 } 1164 1165 //---------------------------------------------- 1166 1167 void UnityExpectFail(){ 1168 1169 Unity.isExpectingFail = 1; 1170 1171 } 1172 1173 void UnityExpectFailMessage(const char* msg, const UNITY_LINE_TYPE line ){ 1174 1175 Unity.isExpectingFail = 1; 1176 if (msg != NULL) 1177 { 1178 Unity.XFAILMessage = msg; 1179 } 1180 } 1181 1182 //----------------------------------------------- 1183 #if defined(UNITY_WEAK_ATTRIBUTE) 1184 void setUp(void); 1185 void tearDown(void); 1186 UNITY_WEAK_ATTRIBUTE void setUp(void) { } 1187 UNITY_WEAK_ATTRIBUTE void tearDown(void) { } 1188 #elif defined(UNITY_WEAK_PRAGMA) 1189 # pragma weak setUp 1190 void setUp(void); 1191 # pragma weak tearDown 1192 void tearDown(void); 1193 #else 1194 void setUp(void); 1195 void tearDown(void); 1196 #endif 1197 1198 //----------------------------------------------- 1199 void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum) 1200 { 1201 Unity.CurrentTestName = FuncName; 1202 Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum; 1203 Unity.NumberOfTests++; 1204 1205 if (TEST_PROTECT()) 1206 { 1207 setUp(); 1208 Func(); 1209 } 1210 if (TEST_PROTECT() && !(Unity.CurrentTestIgnored)) 1211 { 1212 tearDown(); 1213 } 1214 1215 UnityConcludeTest(); 1216 } 1217 1218 1219 //----------------------------------------------- 1220 void UnityBegin(const char* filename) 1221 { 1222 Unity.TestFile = filename; 1223 Unity.CurrentTestName = NULL; 1224 Unity.CurrentTestLineNumber = 0; 1225 Unity.NumberOfTests = 0; 1226 Unity.TestFailures = 0; 1227 Unity.TestIgnores = 0; 1228 Unity.CurrentTestFailed = 0; 1229 Unity.CurrentTestIgnored = 0; 1230 Unity.TestXFAILS = 0; 1231 Unity.isExpectingFail = 0; 1232 Unity.TestPasses = 0; 1233 Unity.TestXPASSES = 0; 1234 Unity.XFAILMessage = NULL; 1235 1236 UNITY_OUTPUT_START(); 1237 } 1238 1239 1240 //----------------------------------------------- 1241 int UnityEnd(void) 1242 { 1243 UNITY_PRINT_EOL; 1244 UnityPrint(UnityStrBreaker); 1245 UNITY_PRINT_EOL; 1246 UnityPrintNumber((_U_SINT)(Unity.NumberOfTests)); 1247 UnityPrint(UnityStrResultsTests); 1248 UNITY_PRINT_EOL; 1249 UnityPrintNumber((_U_SINT)(Unity.TestPasses)); 1250 UnityPrint(UnityStrResultsPass); 1251 UNITY_PRINT_EOL; 1252 UnityPrintNumber((_U_SINT)(Unity.TestXFAILS)); 1253 UnityPrint(UnityStrResultsXFAIL); 1254 UNITY_PRINT_EOL; 1255 UnityPrintNumber((_U_SINT)(Unity.TestFailures)); 1256 UnityPrint(UnityStrResultsFailures); 1257 UNITY_PRINT_EOL; 1258 UnityPrintNumber((_U_SINT)(Unity.TestXPASSES)); 1259 UnityPrint(UnityStrResultsXPASS); 1260 UNITY_PRINT_EOL; 1261 UnityPrintNumber((_U_SINT)(Unity.TestIgnores)); 1262 UnityPrint(UnityStrResultsIgnored); 1263 UNITY_PRINT_EOL; 1264 1265 UNITY_PRINT_EOL; 1266 if (Unity.TestFailures == 0U && Unity.TestXPASSES == 0U) 1267 { 1268 UnityPrintOk(); 1269 } 1270 else 1271 { 1272 UnityPrintFail(); 1273 } 1274 UNITY_PRINT_EOL; 1275 UNITY_OUTPUT_COMPLETE(); 1276 return (int)(Unity.TestFailures); 1277 } 1278 1279 1280 //----------------------------------------------- 1281