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