unity.c 33.4 KB
Newer Older
1 2 3 4 5 6
/* ==========================================
    Unity Project - A Test Framework for C
    Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
    [Released under MIT License. Please refer to license.txt for details]
========================================== */

G
greg-williams 已提交
7 8
#include "unity.h"
#include <stdio.h>
M
mvandervoord 已提交
9
#include <string.h>
G
greg-williams 已提交
10

M
mvandervoord 已提交
11 12
#define UNITY_FAIL_AND_BAIL   { Unity.CurrentTestFailed  = 1; UNITY_OUTPUT_CHAR('\n'); longjmp(Unity.AbortFrame, 1); }
#define UNITY_IGNORE_AND_BAIL { Unity.CurrentTestIgnored = 1; UNITY_OUTPUT_CHAR('\n'); longjmp(Unity.AbortFrame, 1); }
13 14
/// return prematurely if we are already in failure or ignore state
#define UNITY_SKIP_EXECUTION  { if ((Unity.CurrentTestFailed != 0) || (Unity.CurrentTestIgnored != 0)) {return;} }
M
mvandervoord 已提交
15
#define UNITY_PRINT_EOL       { UNITY_OUTPUT_CHAR('\n'); }
M
mvandervoord 已提交
16

J
John Van Enk 已提交
17
struct _Unity Unity = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , { 0 } };
G
greg-williams 已提交
18

19
const char* UnityStrNull     = "NULL";
20
const char* UnityStrSpacer   = ". ";
M
mvandervoord 已提交
21 22 23 24
const char* UnityStrExpected = " Expected ";
const char* UnityStrWas      = " Was ";
const char* UnityStrTo       = " To ";
const char* UnityStrElement  = " Element ";
25 26
const char* UnityStrByte     = " Byte ";
const char* UnityStrMemory   = " Memory Mismatch.";
M
mvandervoord 已提交
27
const char* UnityStrDelta    = " Values Not Within Delta ";
28
const char* UnityStrPointless= " You Asked Me To Compare Nothing, Which Was Pointless.";
29 30
const char* UnityStrNullPointerForExpected= " Expected pointer to be NULL";
const char* UnityStrNullPointerForActual  = " Actual pointer was NULL";
31 32 33
const char* UnityStrInf      = "Infinity";
const char* UnityStrNegInf   = "Negative Infinity";
const char* UnityStrNaN      = "NaN";
M
mvandervoord 已提交
34

35 36 37 38 39 40 41
// Dividing by these constants produces +/- infinity.
// The rationale is given in UnityAssertFloatIsInf's body.
static const _UF f_zero = 0.0f;
#ifndef UNITY_EXCLUDE_DOUBLE
static const _UD d_zero = 0.0;
#endif

42
// compiler-generic print formatting masks
43 44
const _U_UINT UnitySizeMask[] = 
{
45 46
    255u,         // 0xFF
    65535u,       // 0xFFFF
M
 
mvandervoord 已提交
47
    65535u,
48
    4294967295u,  // 0xFFFFFFFF
M
 
mvandervoord 已提交
49 50 51
    4294967295u,
    4294967295u,
    4294967295u
52 53 54 55 56
#ifdef UNITY_SUPPORT_64
    ,0xFFFFFFFFFFFFFFFF
#endif
};

57 58 59
void UnityPrintFail(void);
void UnityPrintOk(void);

M
mvandervoord 已提交
60
//-----------------------------------------------
61
// Pretty Printers & Test Result Output Handlers
M
mvandervoord 已提交
62 63
//-----------------------------------------------

64
void UnityPrint(const char* string)
G
greg-williams 已提交
65
{
66
    const char* pch = string;
G
greg-williams 已提交
67 68 69 70 71

    if (pch != NULL)
    {
        while (*pch)
        {
M
mvandervoord 已提交
72
            // printable characters plus CR & LF are printed
M
mvandervoord 已提交
73
            if ((*pch <= 126) && (*pch >= 32))
M
mvandervoord 已提交
74 75 76
            {
                UNITY_OUTPUT_CHAR(*pch);
            }
M
mvandervoord 已提交
77 78 79 80 81 82 83 84 85 86 87 88
            //write escaped carriage returns
            else if (*pch == 13)
            {
                UNITY_OUTPUT_CHAR('\\');
                UNITY_OUTPUT_CHAR('r');
            }
            //write escaped line feeds
            else if (*pch == 10)
            {
                UNITY_OUTPUT_CHAR('\\');
                UNITY_OUTPUT_CHAR('n');
            }
M
mvandervoord 已提交
89 90 91 92
            // unprintable characters are shown as codes
            else
            {
                UNITY_OUTPUT_CHAR('\\');
M
mvandervoord 已提交
93
                UnityPrintNumberHex((_U_SINT)*pch, 2);
M
mvandervoord 已提交
94
            }
G
greg-williams 已提交
95 96 97 98 99
            pch++;
        }
    }
}

M
mvandervoord 已提交
100
//-----------------------------------------------
M
mvandervoord 已提交
101
void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style)
G
greg-williams 已提交
102
{
M
mvandervoord 已提交
103 104 105 106 107 108
    if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
    {
        UnityPrintNumber(number);
    }
    else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
    {
109
        UnityPrintNumberUnsigned(  (_U_UINT)number  &  UnitySizeMask[((_U_UINT)style & (_U_UINT)0x0F) - 1]  );
M
mvandervoord 已提交
110 111
    }
    else
G
greg-williams 已提交
112
    {
M
mvandervoord 已提交
113
        UnityPrintNumberHex((_U_UINT)number, (style & 0x000F) << 1);
G
greg-williams 已提交
114 115 116
    }
}

M
mvandervoord 已提交
117
//-----------------------------------------------
G
greg-williams 已提交
118
/// basically do an itoa using as little ram as possible
M
mvandervoord 已提交
119
void UnityPrintNumber(const _U_SINT number_to_print)
G
greg-williams 已提交
120
{
M
mvandervoord 已提交
121 122 123
    _U_SINT divisor = 1;
    _U_SINT next_divisor;
    _U_SINT number = number_to_print;
G
greg-williams 已提交
124 125 126

    if (number < 0)
    {
127
        UNITY_OUTPUT_CHAR('-');
G
greg-williams 已提交
128 129 130 131
        number = -number;
    }

    // figure out initial divisor
M
mvandervoord 已提交
132
    while (number / divisor > 9)
G
greg-williams 已提交
133
    {
M
mvandervoord 已提交
134 135 136 137 138
        next_divisor = divisor * 10;
        if (next_divisor > divisor)
            divisor = next_divisor;
        else
            break;
G
greg-williams 已提交
139 140
    }

M
mvandervoord 已提交
141
    // now mod and print, then divide divisor
G
greg-williams 已提交
142 143
    do
    {
144
        UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
M
mvandervoord 已提交
145
        divisor /= 10;
G
greg-williams 已提交
146
    }
M
mvandervoord 已提交
147
    while (divisor > 0);
G
greg-williams 已提交
148 149
}

M
mvandervoord 已提交
150
//-----------------------------------------------
G
greg-williams 已提交
151
/// basically do an itoa using as little ram as possible
M
mvandervoord 已提交
152
void UnityPrintNumberUnsigned(const _U_UINT number)
G
greg-williams 已提交
153
{
M
mvandervoord 已提交
154 155
    _U_UINT divisor = 1;
    _U_UINT next_divisor;
G
greg-williams 已提交
156 157

    // figure out initial divisor
M
mvandervoord 已提交
158
    while (number / divisor > 9)
G
greg-williams 已提交
159
    {
M
mvandervoord 已提交
160 161 162 163 164
        next_divisor = divisor * 10;
        if (next_divisor > divisor)
            divisor = next_divisor;
        else
            break;
G
greg-williams 已提交
165 166
    }

M
mvandervoord 已提交
167
    // now mod and print, then divide divisor
G
greg-williams 已提交
168 169
    do
    {
170
        UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
M
mvandervoord 已提交
171
        divisor /= 10;
G
greg-williams 已提交
172
    }
M
mvandervoord 已提交
173
    while (divisor > 0);
G
greg-williams 已提交
174 175
}

M
mvandervoord 已提交
176
//-----------------------------------------------
M
mvandervoord 已提交
177
void UnityPrintNumberHex(const _U_UINT number, const char nibbles_to_print)
G
greg-williams 已提交
178
{
M
mvandervoord 已提交
179
    _U_UINT nibble;
M
mvandervoord 已提交
180
    char nibbles = nibbles_to_print;
181 182
    UNITY_OUTPUT_CHAR('0');
    UNITY_OUTPUT_CHAR('x');
G
greg-williams 已提交
183 184 185 186 187 188

    while (nibbles > 0)
    {
        nibble = (number >> (--nibbles << 2)) & 0x0000000F;
        if (nibble <= 9)
        {
189
            UNITY_OUTPUT_CHAR((char)('0' + nibble));
G
greg-williams 已提交
190 191 192
        }
        else
        {
193
            UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble));
G
greg-williams 已提交
194 195 196 197
        }
    }
}

M
mvandervoord 已提交
198
//-----------------------------------------------
M
mvandervoord 已提交
199
void UnityPrintMask(const _U_UINT mask, const _U_UINT number)
G
greg-williams 已提交
200
{
201
    _U_UINT current_bit = (_U_UINT)1 << (UNITY_INT_WIDTH - 1);
202
    _US32 i;
G
greg-williams 已提交
203

M
mvandervoord 已提交
204
    for (i = 0; i < UNITY_INT_WIDTH; i++)
G
greg-williams 已提交
205
    {
206
        if (current_bit & mask)
G
greg-williams 已提交
207
        {
208
            if (current_bit & number)
G
greg-williams 已提交
209
            {
210
                UNITY_OUTPUT_CHAR('1');
G
greg-williams 已提交
211 212 213
            }
            else
            {
214
                UNITY_OUTPUT_CHAR('0');
G
greg-williams 已提交
215 216 217 218
            }
        }
        else
        {
219
            UNITY_OUTPUT_CHAR('X');
G
greg-williams 已提交
220
        }
221
        current_bit = current_bit >> 1;
G
greg-williams 已提交
222 223 224
    }
}

M
mvandervoord 已提交
225
//-----------------------------------------------
226 227 228 229 230 231 232 233 234
#ifdef UNITY_FLOAT_VERBOSE
void UnityPrintFloat(_UF number)
{
    char TempBuffer[32];
    sprintf(TempBuffer, "%.6f", number);
    UnityPrint(TempBuffer);
}
#endif

235 236 237 238 239 240 241 242 243 244 245 246
//-----------------------------------------------

void UnityPrintFail(void)
{
    UnityPrint("FAIL");
}

void UnityPrintOk(void)
{
    UnityPrint("OK");
}

M
mvandervoord 已提交
247
//-----------------------------------------------
M
mvandervoord 已提交
248
void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line)
G
greg-williams 已提交
249
{
250
    UnityPrint(file);
251
    UNITY_OUTPUT_CHAR(':');
G
greg-williams 已提交
252
    UnityPrintNumber(line);
253
    UNITY_OUTPUT_CHAR(':');
G
greg-williams 已提交
254
    UnityPrint(Unity.CurrentTestName);
255
    UNITY_OUTPUT_CHAR(':');
G
greg-williams 已提交
256 257
}

M
mvandervoord 已提交
258
//-----------------------------------------------
M
mvandervoord 已提交
259
void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)
260
{
M
mvandervoord 已提交
261
    UnityTestResultsBegin(Unity.TestFile, line);
262 263 264
    UnityPrint("FAIL:");
}

M
mvandervoord 已提交
265
//-----------------------------------------------
266
void UnityConcludeTest(void)
G
greg-williams 已提交
267 268 269 270 271 272 273
{
    if (Unity.CurrentTestIgnored)
    {
        Unity.TestIgnores++;
    }
    else if (!Unity.CurrentTestFailed)
    {
274
        UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);
M
mvandervoord 已提交
275
        UnityPrint("PASS");
M
mvandervoord 已提交
276
        UNITY_PRINT_EOL;
G
greg-williams 已提交
277 278 279 280 281 282 283 284 285 286
    }
    else
    {
        Unity.TestFailures++;
    }

    Unity.CurrentTestFailed = 0;
    Unity.CurrentTestIgnored = 0;
}

M
mvandervoord 已提交
287
//-----------------------------------------------
M
mvandervoord 已提交
288 289 290 291 292 293 294 295 296
void UnityAddMsgIfSpecified(const char* msg)
{
    if (msg)
    {
        UnityPrint(UnityStrSpacer);
        UnityPrint(msg);
    }
}

297 298 299
//-----------------------------------------------
void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual)
{
M
mvandervoord 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    UnityPrint(UnityStrExpected);
    if (expected != NULL)
    {
        UNITY_OUTPUT_CHAR('\'');
        UnityPrint(expected);
        UNITY_OUTPUT_CHAR('\'');
    }
    else
    {
      UnityPrint(UnityStrNull);          
    }
    UnityPrint(UnityStrWas);
    if (actual != NULL)
    {
        UNITY_OUTPUT_CHAR('\'');
        UnityPrint(actual);
        UNITY_OUTPUT_CHAR('\'');
    }
    else
    {
      UnityPrint(UnityStrNull);          
    }
322 323
}

324 325 326 327
//-----------------------------------------------
// Assertion & Control Helpers
//-----------------------------------------------

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
int UnityCheckArraysForNull(const void* expected, const void* actual, const UNITY_LINE_TYPE lineNumber, const char* msg)
{
    //return true if they are both NULL
    if ((expected == NULL) && (actual == NULL))
        return 1;
        
    //throw error if just expected is NULL
    if (expected == NULL)
    {
        UnityTestResultsFailBegin(lineNumber);
        UnityPrint(UnityStrNullPointerForExpected);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }

    //throw error if just actual is NULL
    if (actual == NULL)
    {
        UnityTestResultsFailBegin(lineNumber);
        UnityPrint(UnityStrNullPointerForActual);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }
    
    //return false if neither is NULL
    return 0;
}
355

M
mvandervoord 已提交
356 357 358 359
//-----------------------------------------------
// Assertion Functions
//-----------------------------------------------

M
mvandervoord 已提交
360 361 362
void UnityAssertBits(const _U_SINT mask,
                     const _U_SINT expected,
                     const _U_SINT actual,
363
                     const char* msg,
M
mvandervoord 已提交
364
                     const UNITY_LINE_TYPE lineNumber)
G
greg-williams 已提交
365
{
366 367
    UNITY_SKIP_EXECUTION;
  
G
greg-williams 已提交
368 369
    if ((mask & expected) != (mask & actual))
    {
370
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
371
        UnityPrint(UnityStrExpected);
G
greg-williams 已提交
372
        UnityPrintMask(mask, expected);
M
mvandervoord 已提交
373
        UnityPrint(UnityStrWas);
G
greg-williams 已提交
374
        UnityPrintMask(mask, actual);
M
mvandervoord 已提交
375 376
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
377 378 379
    }
}

M
mvandervoord 已提交
380
//-----------------------------------------------
M
mvandervoord 已提交
381 382
void UnityAssertEqualNumber(const _U_SINT expected,
                            const _U_SINT actual,
M
mvandervoord 已提交
383 384 385
                            const char* msg,
                            const UNITY_LINE_TYPE lineNumber,
                            const UNITY_DISPLAY_STYLE_T style)
386
{
387 388
    UNITY_SKIP_EXECUTION;

389 390
    if (expected != actual)
    {
391
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
392
        UnityPrint(UnityStrExpected);
393
        UnityPrintNumberByStyle(expected, style);
M
mvandervoord 已提交
394
        UnityPrint(UnityStrWas);
395
        UnityPrintNumberByStyle(actual, style);
M
mvandervoord 已提交
396 397
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
398 399 400
    }
}

M
mvandervoord 已提交
401
//-----------------------------------------------
M
mvandervoord 已提交
402 403
void UnityAssertEqualIntArray(const _U_SINT* expected,
                              const _U_SINT* actual,
404
                              const _UU32 num_elements,
405
                              const char* msg,
M
mvandervoord 已提交
406
                              const UNITY_LINE_TYPE lineNumber,
407 408
                              const UNITY_DISPLAY_STYLE_T style)
{
409
    _UU32 elements = num_elements;
M
mvandervoord 已提交
410 411
    const _US8* ptr_exp = (_US8*)expected;
    const _US8* ptr_act = (_US8*)actual;
412

413 414
    UNITY_SKIP_EXECUTION;
  
415 416 417 418 419 420 421
    if (elements == 0)
    {
        UnityTestResultsFailBegin(lineNumber);
        UnityPrint(UnityStrPointless);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }
422 423 424
    
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;
425 426 427 428

    switch(style)
    {
        case UNITY_DISPLAY_STYLE_HEX8:
M
mvandervoord 已提交
429 430
        case UNITY_DISPLAY_STYLE_INT8:
        case UNITY_DISPLAY_STYLE_UINT8:
431 432
            while (elements--)
            {
M
mvandervoord 已提交
433
                if (*ptr_exp != *ptr_act)
434 435 436 437 438
                {
                    UnityTestResultsFailBegin(lineNumber);
                    UnityPrint(UnityStrElement);
                    UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
                    UnityPrint(UnityStrExpected);
M
mvandervoord 已提交
439
                    UnityPrintNumberByStyle(*ptr_exp, style);
440
                    UnityPrint(UnityStrWas);
M
mvandervoord 已提交
441
                    UnityPrintNumberByStyle(*ptr_act, style);
442 443 444
                    UnityAddMsgIfSpecified(msg);
                    UNITY_FAIL_AND_BAIL;
                }
M
mvandervoord 已提交
445 446
                ptr_exp += 1;
                ptr_act += 1;
447 448 449
            }
            break;
        case UNITY_DISPLAY_STYLE_HEX16:
M
mvandervoord 已提交
450 451
        case UNITY_DISPLAY_STYLE_INT16:
        case UNITY_DISPLAY_STYLE_UINT16:
452 453
            while (elements--)
            {
M
mvandervoord 已提交
454
                if (*(_US16*)ptr_exp != *(_US16*)ptr_act)
455 456 457 458 459
                {
                    UnityTestResultsFailBegin(lineNumber);
                    UnityPrint(UnityStrElement);
                    UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
                    UnityPrint(UnityStrExpected);
M
mvandervoord 已提交
460
                    UnityPrintNumberByStyle(*(_US16*)ptr_exp, style);
461
                    UnityPrint(UnityStrWas);
M
mvandervoord 已提交
462
                    UnityPrintNumberByStyle(*(_US16*)ptr_act, style);
463 464 465
                    UnityAddMsgIfSpecified(msg);
                    UNITY_FAIL_AND_BAIL;
                }
M
mvandervoord 已提交
466 467
                ptr_exp += 2;
                ptr_act += 2;
468 469
            }
            break;
M
mvandervoord 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
#ifdef UNITY_SUPPORT_64
        case UNITY_DISPLAY_STYLE_HEX64:
        case UNITY_DISPLAY_STYLE_INT64:
        case UNITY_DISPLAY_STYLE_UINT64:
            while (elements--)
            {
                if (*(_US64*)ptr_exp != *(_US64*)ptr_act)
                {
                    UnityTestResultsFailBegin(lineNumber);
                    UnityPrint(UnityStrElement);
                    UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
                    UnityPrint(UnityStrExpected);
                    UnityPrintNumberByStyle(*(_US64*)ptr_exp, style);
                    UnityPrint(UnityStrWas);
                    UnityPrintNumberByStyle(*(_US64*)ptr_act, style);
                    UnityAddMsgIfSpecified(msg);
                    UNITY_FAIL_AND_BAIL;
                }
                ptr_exp += 8;
                ptr_act += 8;
            }
            break;
#endif
493 494 495
        default:
            while (elements--)
            {
M
mvandervoord 已提交
496
                if (*(_US32*)ptr_exp != *(_US32*)ptr_act)
497 498 499 500 501
                {
                    UnityTestResultsFailBegin(lineNumber);
                    UnityPrint(UnityStrElement);
                    UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
                    UnityPrint(UnityStrExpected);
M
mvandervoord 已提交
502
                    UnityPrintNumberByStyle(*(_US32*)ptr_exp, style);
503
                    UnityPrint(UnityStrWas);
M
mvandervoord 已提交
504
                    UnityPrintNumberByStyle(*(_US32*)ptr_act, style);
505 506 507
                    UnityAddMsgIfSpecified(msg);
                    UNITY_FAIL_AND_BAIL;
                }
M
mvandervoord 已提交
508 509
                ptr_exp += 4;
                ptr_act += 4;
510 511 512 513 514
            }
            break;
    }
}

M
mvandervoord 已提交
515
//-----------------------------------------------
516
#ifndef UNITY_EXCLUDE_FLOAT
517 518
void UnityAssertEqualFloatArray(const _UF* expected,
                                const _UF* actual,
519
                                const _UU32 num_elements,
520 521 522
                                const char* msg,
                                const UNITY_LINE_TYPE lineNumber)
{
523
    _UU32 elements = num_elements;
524 525
    const _UF* ptr_expected = expected;
    const _UF* ptr_actual = actual;
M
mvandervoord 已提交
526
    _UF diff, tol;
527

528 529
    UNITY_SKIP_EXECUTION;
  
530 531
    if (elements == 0)
    {
532
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
533 534 535
        UnityPrint(UnityStrPointless);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
536
    }
537 538 539
    
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;
540 541 542

    while (elements--)
    {
543
        diff = *ptr_expected - *ptr_actual;
544 545
        if (diff < 0.0f)
          diff = 0.0f - diff;
M
mvandervoord 已提交
546
        tol = UNITY_FLOAT_PRECISION * *ptr_expected;
547 548
        if (tol < 0.0f)
            tol = 0.0f - tol;
549 550 551
        
        //This first part of this condition will catch any NaN or Infinite values
        if ((diff * 0.0f != 0.0f) || (diff > tol))
552
        {
553
            UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
554
            UnityPrint(UnityStrElement);
555
            UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
556 557 558 559 560 561
#ifdef UNITY_FLOAT_VERBOSE
            UnityPrint(UnityStrExpected);
            UnityPrintFloat(*ptr_expected);
            UnityPrint(UnityStrWas);
            UnityPrintFloat(*ptr_actual);
#else
562
            UnityPrint(UnityStrDelta);
563
#endif
M
mvandervoord 已提交
564 565
            UnityAddMsgIfSpecified(msg);
            UNITY_FAIL_AND_BAIL;
M
mvandervoord 已提交
566
        }
567 568
        ptr_expected++;
        ptr_actual++;
M
mvandervoord 已提交
569 570 571
    }
}

M
mvandervoord 已提交
572
//-----------------------------------------------
573 574 575
void UnityAssertFloatsWithin(const _UF delta,
                             const _UF expected,
                             const _UF actual,
576
                             const char* msg,
M
mvandervoord 已提交
577
                             const UNITY_LINE_TYPE lineNumber)
G
greg-williams 已提交
578
{
579 580
    _UF diff = actual - expected;
    _UF pos_delta = delta;
581

582 583
    UNITY_SKIP_EXECUTION;
  
584
    if (diff < 0.0f)
G
greg-williams 已提交
585
    {
586
        diff = 0.0f - diff;
G
greg-williams 已提交
587
    }
588
    if (pos_delta < 0.0f)
589 590 591
    {
        pos_delta = 0.0f - pos_delta;
    }
592

593 594
    //This first part of this condition will catch any NaN or Infinite values
    if ((diff * 0.0f != 0.0f) || (pos_delta < diff))
G
greg-williams 已提交
595
    {
596
        UnityTestResultsFailBegin(lineNumber);
597 598 599 600 601 602
#ifdef UNITY_FLOAT_VERBOSE
        UnityPrint(UnityStrExpected);
        UnityPrintFloat(expected);
        UnityPrint(UnityStrWas);
        UnityPrintFloat(actual);
#else
M
mvandervoord 已提交
603
        UnityPrint(UnityStrDelta);
604
#endif
M
mvandervoord 已提交
605 606
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
607 608
    }
}
M
mvandervoord 已提交
609

610 611 612 613 614 615 616
//-----------------------------------------------
void UnityAssertFloatIsInf(const _UF actual,
                           const char* msg,
                           const UNITY_LINE_TYPE lineNumber)
{
    UNITY_SKIP_EXECUTION;

617 618 619 620 621 622
    // In Microsoft Visual C++ Express Edition 2008,
    //   if ((1.0f / f_zero) != actual)
    // produces
    //   error C2124: divide or mod by zero
    // As a workaround, place 0 into a variable.
    if ((1.0f / f_zero) != actual)
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
    {
        UnityTestResultsFailBegin(lineNumber);
#ifdef UNITY_FLOAT_VERBOSE
        UnityPrint(UnityStrExpected);
        UnityPrint(UnityStrInf);
        UnityPrint(UnityStrWas);
        UnityPrintFloat(actual);
#else
        UnityPrint(UnityStrDelta);
#endif
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }
}

//-----------------------------------------------
void UnityAssertFloatIsNegInf(const _UF actual,
                              const char* msg,
                              const UNITY_LINE_TYPE lineNumber)
{
    UNITY_SKIP_EXECUTION;

645 646
    // The rationale for not using 1.0f/0.0f is given in UnityAssertFloatIsInf's body.
    if ((-1.0f / f_zero) != actual)
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
    {
        UnityTestResultsFailBegin(lineNumber);
#ifdef UNITY_FLOAT_VERBOSE
        UnityPrint(UnityStrExpected);
        UnityPrint(UnityStrNegInf);
        UnityPrint(UnityStrWas);
        UnityPrintFloat(actual);
#else
        UnityPrint(UnityStrDelta);
#endif
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }
}

//-----------------------------------------------
void UnityAssertFloatIsNaN(const _UF actual,
                           const char* msg,
                           const UNITY_LINE_TYPE lineNumber)
{
    UNITY_SKIP_EXECUTION;

    if (actual == actual)
    {
        UnityTestResultsFailBegin(lineNumber);
#ifdef UNITY_FLOAT_VERBOSE
        UnityPrint(UnityStrExpected);
        UnityPrint(UnityStrNaN);
        UnityPrint(UnityStrWas);
        UnityPrintFloat(actual);
#else
        UnityPrint(UnityStrDelta);
#endif
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }
}

M
mvandervoord 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
#endif //not UNITY_EXCLUDE_FLOAT

//-----------------------------------------------
#ifndef UNITY_EXCLUDE_DOUBLE
void UnityAssertEqualDoubleArray(const _UD* expected,
                                 const _UD* actual,
                                 const _UU32 num_elements,
                                 const char* msg,
                                 const UNITY_LINE_TYPE lineNumber)
{
    _UU32 elements = num_elements;
    const _UD* ptr_expected = expected;
    const _UD* ptr_actual = actual;
    _UD diff, tol;

    UNITY_SKIP_EXECUTION;
  
    if (elements == 0)
    {
        UnityTestResultsFailBegin(lineNumber);
        UnityPrint(UnityStrPointless);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }
    
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;

    while (elements--)
    {
        diff = *ptr_expected - *ptr_actual;
        if (diff < 0.0)
          diff = 0.0 - diff;
        tol = UNITY_DOUBLE_PRECISION * *ptr_expected;
        if (tol < 0.0)
            tol = 0.0 - tol;
721 722
        
        //This first part of this condition will catch any NaN or Infinite values
723
        if ((diff * 0.0 != 0.0) || (diff > tol))
M
mvandervoord 已提交
724 725 726 727 728 729 730 731 732 733 734
        {
            UnityTestResultsFailBegin(lineNumber);
            UnityPrint(UnityStrElement);
            UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
#ifdef UNITY_DOUBLE_VERBOSE
            UnityPrint(UnityStrExpected);
            UnityPrintFloat((float)(*ptr_expected));
            UnityPrint(UnityStrWas);
            UnityPrintFloat((float)(*ptr_actual));
#else
            UnityPrint(UnityStrDelta);
735
#endif
M
mvandervoord 已提交
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
            UnityAddMsgIfSpecified(msg);
            UNITY_FAIL_AND_BAIL;
        }
        ptr_expected++;
        ptr_actual++;
    }
}

//-----------------------------------------------
void UnityAssertDoublesWithin(const _UD delta,
                              const _UD expected,
                              const _UD actual,
                              const char* msg,
                              const UNITY_LINE_TYPE lineNumber)
{
    _UD diff = actual - expected;
    _UD pos_delta = delta;

    UNITY_SKIP_EXECUTION;
  
756
    if (diff < 0.0)
M
mvandervoord 已提交
757
    {
758
        diff = 0.0 - diff;
M
mvandervoord 已提交
759
    }
760
    if (pos_delta < 0.0)
M
mvandervoord 已提交
761
    {
762
        pos_delta = 0.0 - pos_delta;
M
mvandervoord 已提交
763 764
    }

765
    //This first part of this condition will catch any NaN or Infinite values
766
    if ((diff * 0.0 != 0.0) || (pos_delta < diff))
M
mvandervoord 已提交
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
    {
        UnityTestResultsFailBegin(lineNumber);
#ifdef UNITY_DOUBLE_VERBOSE
        UnityPrint(UnityStrExpected);
        UnityPrintFloat((float)expected);
        UnityPrint(UnityStrWas);
        UnityPrintFloat((float)actual);
#else
        UnityPrint(UnityStrDelta);
#endif
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }
}

782 783 784 785 786 787 788
//-----------------------------------------------
void UnityAssertDoubleIsInf(const _UD actual,
                            const char* msg,
                            const UNITY_LINE_TYPE lineNumber)
{
    UNITY_SKIP_EXECUTION;

789 790
    // The rationale for not using 1.0/0.0 is given in UnityAssertFloatIsInf's body.
    if ((1.0 / d_zero) != actual)
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
    {
        UnityTestResultsFailBegin(lineNumber);
#ifdef UNITY_DOUBLE_VERBOSE
        UnityPrint(UnityStrExpected);
        UnityPrint(UnityStrInf);
        UnityPrint(UnityStrWas);
        UnityPrintFloat((float)actual);
#else
        UnityPrint(UnityStrDelta);
#endif
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }
}

//-----------------------------------------------
void UnityAssertDoubleIsNegInf(const _UD actual,
                               const char* msg,
                               const UNITY_LINE_TYPE lineNumber)
{
    UNITY_SKIP_EXECUTION;

813 814
    // The rationale for not using 1.0/0.0 is given in UnityAssertFloatIsInf's body.
    if ((-1.0 / d_zero) != actual)
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
    {
        UnityTestResultsFailBegin(lineNumber);
#ifdef UNITY_DOUBLE_VERBOSE
        UnityPrint(UnityStrExpected);
        UnityPrint(UnityStrNegInf);
        UnityPrint(UnityStrWas);
        UnityPrintFloat((float)actual);
#else
        UnityPrint(UnityStrDelta);
#endif
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }
}

//-----------------------------------------------
void UnityAssertDoubleIsNaN(const _UD actual,
                            const char* msg,
                            const UNITY_LINE_TYPE lineNumber)
{
    UNITY_SKIP_EXECUTION;

    if (actual == actual)
    {
        UnityTestResultsFailBegin(lineNumber);
#ifdef UNITY_DOUBLE_VERBOSE
        UnityPrint(UnityStrExpected);
        UnityPrint(UnityStrNaN);
        UnityPrint(UnityStrWas);
        UnityPrintFloat((float)actual);
#else
        UnityPrint(UnityStrDelta);
#endif
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }
}

M
mvandervoord 已提交
853
#endif // not UNITY_EXCLUDE_DOUBLE
G
greg-williams 已提交
854

M
mvandervoord 已提交
855
//-----------------------------------------------
M
mvandervoord 已提交
856 857 858
void UnityAssertNumbersWithin( const _U_SINT delta,
                               const _U_SINT expected,
                               const _U_SINT actual,
M
mvandervoord 已提交
859 860 861
                               const char* msg,
                               const UNITY_LINE_TYPE lineNumber,
                               const UNITY_DISPLAY_STYLE_T style)
G
greg-williams 已提交
862
{
863
    UNITY_SKIP_EXECUTION;
M
mvandervoord 已提交
864 865
    
    if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
G
greg-williams 已提交
866
    {
M
mvandervoord 已提交
867 868 869 870
        if (actual > expected)
          Unity.CurrentTestFailed = ((actual - expected) > delta);
        else
          Unity.CurrentTestFailed = ((expected - actual) > delta);
G
greg-williams 已提交
871
    }
M
mvandervoord 已提交
872
    else
G
greg-williams 已提交
873
    {
M
mvandervoord 已提交
874 875
        if ((_U_UINT)actual > (_U_UINT)expected)
            Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > (_U_UINT)delta);
M
mvandervoord 已提交
876
        else
M
mvandervoord 已提交
877
            Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > (_U_UINT)delta);
878 879
    }

M
mvandervoord 已提交
880
    if (Unity.CurrentTestFailed)
881
    {
882
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
883 884 885 886 887 888 889 890
        UnityPrint(UnityStrDelta);
        UnityPrintNumberByStyle(delta, style);
        UnityPrint(UnityStrExpected);
        UnityPrintNumberByStyle(expected, style);
        UnityPrint(UnityStrWas);
        UnityPrintNumberByStyle(actual, style);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
891 892 893
    }
}

M
mvandervoord 已提交
894
//-----------------------------------------------
895 896 897
void UnityAssertEqualString(const char* expected,
                            const char* actual,
                            const char* msg,
M
mvandervoord 已提交
898
                            const UNITY_LINE_TYPE lineNumber)
G
greg-williams 已提交
899
{
900
    _UU32 i;
G
greg-williams 已提交
901

902 903
    UNITY_SKIP_EXECUTION;
  
G
greg-williams 已提交
904 905 906 907 908 909 910 911
    // if both pointers not null compare the strings
    if (expected && actual)
    {
        for (i = 0; expected[i] || actual[i]; i++)
        {
            if (expected[i] != actual[i])
            {
                Unity.CurrentTestFailed = 1;
912
                break;
G
greg-williams 已提交
913 914 915 916 917 918 919 920 921 922 923 924 925
            }
        }
    }
    else
    { // handle case of one pointers being null (if both null, test should pass)
        if (expected != actual)
        {
            Unity.CurrentTestFailed = 1;
        }
    }

    if (Unity.CurrentTestFailed)
    {
926
      UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
927
      UnityPrintExpectedAndActualStrings(expected, actual);
928 929
      UnityAddMsgIfSpecified(msg);
      UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
930 931 932
    }
}

M
mvandervoord 已提交
933 934 935 936 937 938 939 940 941
//-----------------------------------------------
void UnityAssertEqualStringArray( const char** expected,
                                  const char** actual,
                                  const _UU32 num_elements,
                                  const char* msg,
                                  const UNITY_LINE_TYPE lineNumber)
{
    _UU32 i, j = 0;
    
942 943
    UNITY_SKIP_EXECUTION;
  
M
mvandervoord 已提交
944 945 946 947 948 949 950 951 952
    // if no elements, it's an error
    if (num_elements == 0)
    {
        UnityTestResultsFailBegin(lineNumber);
        UnityPrint(UnityStrPointless);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }

953 954
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;
M
mvandervoord 已提交
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
    
    do
    {
        // if both pointers not null compare the strings
        if (expected[j] && actual[j])
        {
            for (i = 0; expected[j][i] || actual[j][i]; i++)
            {
                if (expected[j][i] != actual[j][i])
                {
                    Unity.CurrentTestFailed = 1;
                    break;
                }
            }
        }
        else
        { // handle case of one pointers being null (if both null, test should pass)
            if (expected[j] != actual[j])
            {
                Unity.CurrentTestFailed = 1;
            }
        }

        if (Unity.CurrentTestFailed)
        {
            UnityTestResultsFailBegin(lineNumber);
            if (num_elements > 1)
            {
                UnityPrint(UnityStrElement);
                UnityPrintNumberByStyle((num_elements - j - 1), UNITY_DISPLAY_STYLE_UINT);
            }
M
mvandervoord 已提交
986
            UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j]));
M
mvandervoord 已提交
987 988 989 990 991 992 993
            UnityAddMsgIfSpecified(msg);
            UNITY_FAIL_AND_BAIL;
        } 
    } while (++j < num_elements);
}

//-----------------------------------------------
M
mvandervoord 已提交
994 995
void UnityAssertEqualMemory( const void* expected,
                             const void* actual,
996 997
                             const _UU32 length,
                             const _UU32 num_elements,
M
mvandervoord 已提交
998 999
                             const char* msg,
                             const UNITY_LINE_TYPE lineNumber)
1000
{
1001 1002
    unsigned char* ptr_exp = (unsigned char*)expected;
    unsigned char* ptr_act = (unsigned char*)actual;
1003
    _UU32 elements = num_elements;
1004
    _UU32 bytes;
1005 1006 1007

    UNITY_SKIP_EXECUTION;
  
1008 1009
    if ((elements == 0) || (length == 0))
    {
1010
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
1011 1012 1013
        UnityPrint(UnityStrPointless);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
1014
    }
1015

1016 1017 1018 1019
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;
        
    while (elements--)
1020
    {
1021 1022 1023
        /////////////////////////////////////
        bytes = length;
        while (bytes--)
1024
        {
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
            if (*ptr_exp != *ptr_act)
            {
                UnityTestResultsFailBegin(lineNumber);
                UnityPrint(UnityStrMemory);
                if (num_elements > 1)
                {
                    UnityPrint(UnityStrElement);
                    UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
                }
                UnityPrint(UnityStrByte);
                UnityPrintNumberByStyle((length - bytes - 1), UNITY_DISPLAY_STYLE_UINT);
                UnityPrint(UnityStrExpected);
                UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
                UnityPrint(UnityStrWas);
                UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
                UnityAddMsgIfSpecified(msg);
                UNITY_FAIL_AND_BAIL;
            }
            ptr_exp += 1;
            ptr_act += 1;
1045
        }
1046 1047
        /////////////////////////////////////
        
1048 1049 1050
    }
}

M
mvandervoord 已提交
1051 1052 1053 1054 1055
//-----------------------------------------------
// Control Functions
//-----------------------------------------------

void UnityFail(const char* msg, const UNITY_LINE_TYPE line)
G
greg-williams 已提交
1056
{
1057 1058
    UNITY_SKIP_EXECUTION;

M
mvandervoord 已提交
1059
    UnityTestResultsBegin(Unity.TestFile, line);
1060
    UnityPrintFail();
M
mvandervoord 已提交
1061
    if (msg != NULL)
1062 1063
    {
      UNITY_OUTPUT_CHAR(':');
1064 1065
      if (msg[0] != ' ')
      {
M
mvandervoord 已提交
1066 1067
        UNITY_OUTPUT_CHAR(' ');  
      }
M
mvandervoord 已提交
1068
      UnityPrint(msg);
1069
    }
M
mvandervoord 已提交
1070
    UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
1071 1072
}

M
mvandervoord 已提交
1073
//-----------------------------------------------
M
mvandervoord 已提交
1074
void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
G
greg-williams 已提交
1075
{
1076 1077
    UNITY_SKIP_EXECUTION;

M
mvandervoord 已提交
1078
    UnityTestResultsBegin(Unity.TestFile, line);
1079
    UnityPrint("IGNORE");
M
mvandervoord 已提交
1080
    if (msg != NULL)
1081 1082
    {
      UNITY_OUTPUT_CHAR(':');
1083
      UNITY_OUTPUT_CHAR(' ');
M
mvandervoord 已提交
1084
      UnityPrint(msg);
1085
    }
M
mvandervoord 已提交
1086
    UNITY_IGNORE_AND_BAIL;
G
greg-williams 已提交
1087 1088
}

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
//-----------------------------------------------
void setUp(void);
void tearDown(void);
void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
{
    Unity.CurrentTestName = FuncName;
    Unity.CurrentTestLineNumber = FuncLineNum;
    Unity.NumberOfTests++; 
    if (TEST_PROTECT())
    {
        setUp();
        Func();
    }
    if (TEST_PROTECT() && !(Unity.CurrentTestIgnored))
    {
        tearDown();
    }
    UnityConcludeTest();
}

M
mvandervoord 已提交
1109
//-----------------------------------------------
1110
void UnityBegin(void)
G
greg-williams 已提交
1111 1112
{
    Unity.NumberOfTests = 0;
1113 1114 1115 1116
    Unity.TestFailures = 0;
    Unity.TestIgnores = 0;
    Unity.CurrentTestFailed = 0;
    Unity.CurrentTestIgnored = 0;
G
greg-williams 已提交
1117 1118
}

M
mvandervoord 已提交
1119
//-----------------------------------------------
1120
int UnityEnd(void)
G
greg-williams 已提交
1121
{
M
mvandervoord 已提交
1122
    UnityPrint("-----------------------");
M
mvandervoord 已提交
1123
    UNITY_PRINT_EOL;
G
greg-williams 已提交
1124 1125 1126 1127 1128
    UnityPrintNumber(Unity.NumberOfTests);
    UnityPrint(" Tests ");
    UnityPrintNumber(Unity.TestFailures);
    UnityPrint(" Failures ");
    UnityPrintNumber(Unity.TestIgnores);
M
mvandervoord 已提交
1129
    UnityPrint(" Ignored");
M
mvandervoord 已提交
1130
    UNITY_PRINT_EOL;
G
greg-williams 已提交
1131 1132
    if (Unity.TestFailures == 0U)
    {
1133
        UnityPrintOk();
G
greg-williams 已提交
1134 1135 1136
    }
    else
    {
1137
        UnityPrintFail();
G
greg-williams 已提交
1138
    }
M
mvandervoord 已提交
1139
    UNITY_PRINT_EOL;
1140
    return Unity.TestFailures;
G
greg-williams 已提交
1141
}