unity.c 24.5 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

17
struct _Unity Unity = { 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
const char* UnityStrMemory   = " Memory Mismatch";
M
mvandervoord 已提交
26
const char* UnityStrDelta    = " Values Not Within Delta ";
27
const char* UnityStrPointless= " You Asked Me To Compare Nothing, Which Was Pointless.";
28 29
const char* UnityStrNullPointerForExpected= " Expected pointer to be NULL";
const char* UnityStrNullPointerForActual  = " Actual pointer was NULL";
M
mvandervoord 已提交
30 31

//-----------------------------------------------
32
// Pretty Printers & Test Result Output Handlers
M
mvandervoord 已提交
33 34
//-----------------------------------------------

35
void UnityPrint(const char* string)
G
greg-williams 已提交
36
{
37
    const char* pch = string;
G
greg-williams 已提交
38 39 40 41 42

    if (pch != NULL)
    {
        while (*pch)
        {
M
mvandervoord 已提交
43
            // printable characters plus CR & LF are printed
M
mvandervoord 已提交
44
            if ((*pch <= 126) && (*pch >= 32))
M
mvandervoord 已提交
45 46 47
            {
                UNITY_OUTPUT_CHAR(*pch);
            }
M
mvandervoord 已提交
48 49 50 51 52 53 54 55 56 57 58 59
            //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 已提交
60 61 62 63
            // unprintable characters are shown as codes
            else
            {
                UNITY_OUTPUT_CHAR('\\');
M
mvandervoord 已提交
64
                UnityPrintNumberHex((_U_SINT)*pch, 2);
M
mvandervoord 已提交
65
            }
G
greg-williams 已提交
66 67 68 69 70
            pch++;
        }
    }
}

M
mvandervoord 已提交
71
//-----------------------------------------------
M
mvandervoord 已提交
72
void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style)
G
greg-williams 已提交
73
{
M
mvandervoord 已提交
74 75 76 77 78 79 80 81 82
    if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
    {
        UnityPrintNumber(number);
    }
    else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
    {
        UnityPrintNumberUnsigned((_U_UINT)number);
    }
    else
G
greg-williams 已提交
83
    {
M
mvandervoord 已提交
84
        UnityPrintNumberHex((_U_UINT)number, (style & 0x000F) << 1);
G
greg-williams 已提交
85 86 87
    }
}

M
mvandervoord 已提交
88
//-----------------------------------------------
G
greg-williams 已提交
89
/// basically do an itoa using as little ram as possible
M
mvandervoord 已提交
90
void UnityPrintNumber(const _U_SINT number_to_print)
G
greg-williams 已提交
91
{
M
mvandervoord 已提交
92 93 94
    _U_SINT divisor = 1;
    _U_SINT next_divisor;
    _U_SINT number = number_to_print;
G
greg-williams 已提交
95 96 97

    if (number < 0)
    {
98
        UNITY_OUTPUT_CHAR('-');
G
greg-williams 已提交
99 100 101 102
        number = -number;
    }

    // figure out initial divisor
M
mvandervoord 已提交
103
    while (number / divisor > 9)
G
greg-williams 已提交
104
    {
M
mvandervoord 已提交
105 106 107 108 109
        next_divisor = divisor * 10;
        if (next_divisor > divisor)
            divisor = next_divisor;
        else
            break;
G
greg-williams 已提交
110 111
    }

M
mvandervoord 已提交
112
    // now mod and print, then divide divisor
G
greg-williams 已提交
113 114
    do
    {
115
        UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
M
mvandervoord 已提交
116
        divisor /= 10;
G
greg-williams 已提交
117
    }
M
mvandervoord 已提交
118
    while (divisor > 0);
G
greg-williams 已提交
119 120
}

M
mvandervoord 已提交
121
//-----------------------------------------------
G
greg-williams 已提交
122
/// basically do an itoa using as little ram as possible
M
mvandervoord 已提交
123
void UnityPrintNumberUnsigned(const _U_UINT number)
G
greg-williams 已提交
124
{
M
mvandervoord 已提交
125 126
    _U_UINT divisor = 1;
    _U_UINT next_divisor;
G
greg-williams 已提交
127 128

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

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

M
mvandervoord 已提交
147
//-----------------------------------------------
M
mvandervoord 已提交
148
void UnityPrintNumberHex(const _U_UINT number, const char nibbles_to_print)
G
greg-williams 已提交
149
{
M
mvandervoord 已提交
150
    _U_UINT nibble;
M
mvandervoord 已提交
151
    char nibbles = nibbles_to_print;
152 153
    UNITY_OUTPUT_CHAR('0');
    UNITY_OUTPUT_CHAR('x');
G
greg-williams 已提交
154 155 156 157 158 159

    while (nibbles > 0)
    {
        nibble = (number >> (--nibbles << 2)) & 0x0000000F;
        if (nibble <= 9)
        {
160
            UNITY_OUTPUT_CHAR((char)('0' + nibble));
G
greg-williams 已提交
161 162 163
        }
        else
        {
164
            UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble));
G
greg-williams 已提交
165 166 167 168
        }
    }
}

M
mvandervoord 已提交
169
//-----------------------------------------------
M
mvandervoord 已提交
170
void UnityPrintMask(const _U_UINT mask, const _U_UINT number)
G
greg-williams 已提交
171
{
172
    _U_UINT current_bit = (_U_UINT)1 << (UNITY_INT_WIDTH - 1);
173
    _US32 i;
G
greg-williams 已提交
174

M
mvandervoord 已提交
175
    for (i = 0; i < UNITY_INT_WIDTH; i++)
G
greg-williams 已提交
176
    {
177
        if (current_bit & mask)
G
greg-williams 已提交
178
        {
179
            if (current_bit & number)
G
greg-williams 已提交
180
            {
181
                UNITY_OUTPUT_CHAR('1');
G
greg-williams 已提交
182 183 184
            }
            else
            {
185
                UNITY_OUTPUT_CHAR('0');
G
greg-williams 已提交
186 187 188 189
            }
        }
        else
        {
190
            UNITY_OUTPUT_CHAR('X');
G
greg-williams 已提交
191
        }
192
        current_bit = current_bit >> 1;
G
greg-williams 已提交
193 194 195
    }
}

M
mvandervoord 已提交
196
//-----------------------------------------------
197 198 199 200 201 202 203 204 205
#ifdef UNITY_FLOAT_VERBOSE
void UnityPrintFloat(_UF number)
{
    char TempBuffer[32];
    sprintf(TempBuffer, "%.6f", number);
    UnityPrint(TempBuffer);
}
#endif

M
mvandervoord 已提交
206
//-----------------------------------------------
M
mvandervoord 已提交
207
void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line)
G
greg-williams 已提交
208
{
209
    UnityPrint(file);
210
    UNITY_OUTPUT_CHAR(':');
G
greg-williams 已提交
211
    UnityPrintNumber(line);
212
    UNITY_OUTPUT_CHAR(':');
G
greg-williams 已提交
213
    UnityPrint(Unity.CurrentTestName);
214
    UNITY_OUTPUT_CHAR(':');
G
greg-williams 已提交
215 216
}

M
mvandervoord 已提交
217
//-----------------------------------------------
M
mvandervoord 已提交
218
void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)
219
{
M
mvandervoord 已提交
220
    UnityTestResultsBegin(Unity.TestFile, line);
221 222 223
    UnityPrint("FAIL:");
}

M
mvandervoord 已提交
224
//-----------------------------------------------
225
void UnityConcludeTest(void)
G
greg-williams 已提交
226 227 228 229 230 231 232
{
    if (Unity.CurrentTestIgnored)
    {
        Unity.TestIgnores++;
    }
    else if (!Unity.CurrentTestFailed)
    {
233
        UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);
M
mvandervoord 已提交
234
        UnityPrint("PASS");
M
mvandervoord 已提交
235
        UNITY_PRINT_EOL;
G
greg-williams 已提交
236 237 238 239 240 241 242 243 244 245
    }
    else
    {
        Unity.TestFailures++;
    }

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

M
mvandervoord 已提交
246
//-----------------------------------------------
M
mvandervoord 已提交
247 248 249 250 251 252 253 254 255
void UnityAddMsgIfSpecified(const char* msg)
{
    if (msg)
    {
        UnityPrint(UnityStrSpacer);
        UnityPrint(msg);
    }
}

256 257 258
//-----------------------------------------------
void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual)
{
M
mvandervoord 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    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);          
    }
281 282
}

283 284 285 286
//-----------------------------------------------
// Assertion & Control Helpers
//-----------------------------------------------

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
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;
}
314

M
mvandervoord 已提交
315 316 317 318
//-----------------------------------------------
// Assertion Functions
//-----------------------------------------------

M
mvandervoord 已提交
319 320 321
void UnityAssertBits(const _U_SINT mask,
                     const _U_SINT expected,
                     const _U_SINT actual,
322
                     const char* msg,
M
mvandervoord 已提交
323
                     const UNITY_LINE_TYPE lineNumber)
G
greg-williams 已提交
324
{
325 326
    UNITY_SKIP_EXECUTION;
  
G
greg-williams 已提交
327 328
    if ((mask & expected) != (mask & actual))
    {
329
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
330
        UnityPrint(UnityStrExpected);
G
greg-williams 已提交
331
        UnityPrintMask(mask, expected);
M
mvandervoord 已提交
332
        UnityPrint(UnityStrWas);
G
greg-williams 已提交
333
        UnityPrintMask(mask, actual);
M
mvandervoord 已提交
334 335
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
336 337 338
    }
}

M
mvandervoord 已提交
339
//-----------------------------------------------
M
mvandervoord 已提交
340 341
void UnityAssertEqualNumber(const _U_SINT expected,
                            const _U_SINT actual,
M
mvandervoord 已提交
342 343 344
                            const char* msg,
                            const UNITY_LINE_TYPE lineNumber,
                            const UNITY_DISPLAY_STYLE_T style)
345
{
346 347
    UNITY_SKIP_EXECUTION;

348 349
    if (expected != actual)
    {
350
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
351
        UnityPrint(UnityStrExpected);
352
        UnityPrintNumberByStyle(expected, style);
M
mvandervoord 已提交
353
        UnityPrint(UnityStrWas);
354
        UnityPrintNumberByStyle(actual, style);
M
mvandervoord 已提交
355 356
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
357 358 359
    }
}

M
mvandervoord 已提交
360
//-----------------------------------------------
M
mvandervoord 已提交
361 362
void UnityAssertEqualIntArray(const _U_SINT* expected,
                              const _U_SINT* actual,
363
                              const _UU32 num_elements,
364
                              const char* msg,
M
mvandervoord 已提交
365
                              const UNITY_LINE_TYPE lineNumber,
366 367
                              const UNITY_DISPLAY_STYLE_T style)
{
368
    _UU32 elements = num_elements;
M
mvandervoord 已提交
369 370
    const _US8* ptr_exp = (_US8*)expected;
    const _US8* ptr_act = (_US8*)actual;
371

372 373
    UNITY_SKIP_EXECUTION;
  
374 375 376 377 378 379 380
    if (elements == 0)
    {
        UnityTestResultsFailBegin(lineNumber);
        UnityPrint(UnityStrPointless);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }
381 382 383
    
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;
384 385 386 387

    switch(style)
    {
        case UNITY_DISPLAY_STYLE_HEX8:
M
mvandervoord 已提交
388 389
        case UNITY_DISPLAY_STYLE_INT8:
        case UNITY_DISPLAY_STYLE_UINT8:
390 391
            while (elements--)
            {
M
mvandervoord 已提交
392
                if (*ptr_exp != *ptr_act)
393 394 395 396 397
                {
                    UnityTestResultsFailBegin(lineNumber);
                    UnityPrint(UnityStrElement);
                    UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
                    UnityPrint(UnityStrExpected);
M
mvandervoord 已提交
398
                    UnityPrintNumberByStyle(*ptr_exp, style);
399
                    UnityPrint(UnityStrWas);
M
mvandervoord 已提交
400
                    UnityPrintNumberByStyle(*ptr_act, style);
401 402 403
                    UnityAddMsgIfSpecified(msg);
                    UNITY_FAIL_AND_BAIL;
                }
M
mvandervoord 已提交
404 405
                ptr_exp += 1;
                ptr_act += 1;
406 407 408
            }
            break;
        case UNITY_DISPLAY_STYLE_HEX16:
M
mvandervoord 已提交
409 410
        case UNITY_DISPLAY_STYLE_INT16:
        case UNITY_DISPLAY_STYLE_UINT16:
411 412
            while (elements--)
            {
M
mvandervoord 已提交
413
                if (*(_US16*)ptr_exp != *(_US16*)ptr_act)
414 415 416 417 418
                {
                    UnityTestResultsFailBegin(lineNumber);
                    UnityPrint(UnityStrElement);
                    UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
                    UnityPrint(UnityStrExpected);
M
mvandervoord 已提交
419
                    UnityPrintNumberByStyle(*(_US16*)ptr_exp, style);
420
                    UnityPrint(UnityStrWas);
M
mvandervoord 已提交
421
                    UnityPrintNumberByStyle(*(_US16*)ptr_act, style);
422 423 424
                    UnityAddMsgIfSpecified(msg);
                    UNITY_FAIL_AND_BAIL;
                }
M
mvandervoord 已提交
425 426
                ptr_exp += 2;
                ptr_act += 2;
427 428
            }
            break;
M
mvandervoord 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
#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
452 453 454
        default:
            while (elements--)
            {
M
mvandervoord 已提交
455
                if (*(_US32*)ptr_exp != *(_US32*)ptr_act)
456 457 458 459 460
                {
                    UnityTestResultsFailBegin(lineNumber);
                    UnityPrint(UnityStrElement);
                    UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
                    UnityPrint(UnityStrExpected);
M
mvandervoord 已提交
461
                    UnityPrintNumberByStyle(*(_US32*)ptr_exp, style);
462
                    UnityPrint(UnityStrWas);
M
mvandervoord 已提交
463
                    UnityPrintNumberByStyle(*(_US32*)ptr_act, style);
464 465 466
                    UnityAddMsgIfSpecified(msg);
                    UNITY_FAIL_AND_BAIL;
                }
M
mvandervoord 已提交
467 468
                ptr_exp += 4;
                ptr_act += 4;
469 470 471 472 473
            }
            break;
    }
}

M
mvandervoord 已提交
474
//-----------------------------------------------
475
#ifndef UNITY_EXCLUDE_FLOAT
476 477
void UnityAssertEqualFloatArray(const _UF* expected,
                                const _UF* actual,
478
                                const _UU32 num_elements,
479 480 481
                                const char* msg,
                                const UNITY_LINE_TYPE lineNumber)
{
482
    _UU32 elements = num_elements;
483 484
    const _UF* ptr_expected = expected;
    const _UF* ptr_actual = actual;
M
mvandervoord 已提交
485
    _UF diff, tol;
486

487 488
    UNITY_SKIP_EXECUTION;
  
489 490
    if (elements == 0)
    {
491
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
492 493 494
        UnityPrint(UnityStrPointless);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
495
    }
496 497 498
    
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;
499 500 501

    while (elements--)
    {
502 503 504
        diff = *ptr_expected - *ptr_actual;
        if (diff < 0.0)
          diff = 0.0 - diff;
M
mvandervoord 已提交
505 506 507 508
        tol = UNITY_FLOAT_PRECISION * *ptr_expected;
        if (tol < 0.0)
            tol = 0.0 - tol;
        if (diff > tol)
509
        {
510
            UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
511
            UnityPrint(UnityStrElement);
512
            UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
513 514 515 516 517 518
#ifdef UNITY_FLOAT_VERBOSE
            UnityPrint(UnityStrExpected);
            UnityPrintFloat(*ptr_expected);
            UnityPrint(UnityStrWas);
            UnityPrintFloat(*ptr_actual);
#else
519
            UnityPrint(UnityStrDelta);
520
#endif
M
mvandervoord 已提交
521 522
            UnityAddMsgIfSpecified(msg);
            UNITY_FAIL_AND_BAIL;
M
mvandervoord 已提交
523
        }
524 525
        ptr_expected++;
        ptr_actual++;
M
mvandervoord 已提交
526 527 528
    }
}

M
mvandervoord 已提交
529
//-----------------------------------------------
530 531 532
void UnityAssertFloatsWithin(const _UF delta,
                             const _UF expected,
                             const _UF actual,
533
                             const char* msg,
M
mvandervoord 已提交
534
                             const UNITY_LINE_TYPE lineNumber)
G
greg-williams 已提交
535
{
536 537
    _UF diff = actual - expected;
    _UF pos_delta = delta;
538

539 540
    UNITY_SKIP_EXECUTION;
  
G
greg-williams 已提交
541 542
    if (diff < 0)
    {
543
        diff = 0.0f - diff;
G
greg-williams 已提交
544
    }
545 546 547 548
    if (pos_delta < 0)
    {
        pos_delta = 0.0f - pos_delta;
    }
549

550
    if (pos_delta < diff)
G
greg-williams 已提交
551
    {
552
        UnityTestResultsFailBegin(lineNumber);
553 554 555 556 557 558
#ifdef UNITY_FLOAT_VERBOSE
        UnityPrint(UnityStrExpected);
        UnityPrintFloat(expected);
        UnityPrint(UnityStrWas);
        UnityPrintFloat(actual);
#else
M
mvandervoord 已提交
559
        UnityPrint(UnityStrDelta);
560
#endif
M
mvandervoord 已提交
561 562
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
563 564
    }
}
565
#endif
G
greg-williams 已提交
566

M
mvandervoord 已提交
567
//-----------------------------------------------
M
mvandervoord 已提交
568 569 570
void UnityAssertNumbersWithin( const _U_SINT delta,
                               const _U_SINT expected,
                               const _U_SINT actual,
M
mvandervoord 已提交
571 572 573
                               const char* msg,
                               const UNITY_LINE_TYPE lineNumber,
                               const UNITY_DISPLAY_STYLE_T style)
G
greg-williams 已提交
574
{
575
    UNITY_SKIP_EXECUTION;
M
mvandervoord 已提交
576 577
    
    if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
G
greg-williams 已提交
578
    {
M
mvandervoord 已提交
579 580 581 582
        if (actual > expected)
          Unity.CurrentTestFailed = ((actual - expected) > delta);
        else
          Unity.CurrentTestFailed = ((expected - actual) > delta);
G
greg-williams 已提交
583
    }
M
mvandervoord 已提交
584
    else
G
greg-williams 已提交
585
    {
M
mvandervoord 已提交
586 587
        if ((_U_UINT)actual > (_U_UINT)expected)
            Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > (_U_UINT)delta);
M
mvandervoord 已提交
588
        else
M
mvandervoord 已提交
589
            Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > (_U_UINT)delta);
590 591
    }

M
mvandervoord 已提交
592
    if (Unity.CurrentTestFailed)
593
    {
594
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
595 596 597 598 599 600 601 602
        UnityPrint(UnityStrDelta);
        UnityPrintNumberByStyle(delta, style);
        UnityPrint(UnityStrExpected);
        UnityPrintNumberByStyle(expected, style);
        UnityPrint(UnityStrWas);
        UnityPrintNumberByStyle(actual, style);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
603 604 605
    }
}

M
mvandervoord 已提交
606
//-----------------------------------------------
607 608 609
void UnityAssertEqualString(const char* expected,
                            const char* actual,
                            const char* msg,
M
mvandervoord 已提交
610
                            const UNITY_LINE_TYPE lineNumber)
G
greg-williams 已提交
611
{
612
    _UU32 i;
G
greg-williams 已提交
613

614 615
    UNITY_SKIP_EXECUTION;
  
G
greg-williams 已提交
616 617 618 619 620 621 622 623
    // 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;
624
                break;
G
greg-williams 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637
            }
        }
    }
    else
    { // handle case of one pointers being null (if both null, test should pass)
        if (expected != actual)
        {
            Unity.CurrentTestFailed = 1;
        }
    }

    if (Unity.CurrentTestFailed)
    {
638
      UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
639
      UnityPrintExpectedAndActualStrings(expected, actual);
640 641
      UnityAddMsgIfSpecified(msg);
      UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
642 643 644
    }
}

M
mvandervoord 已提交
645 646 647 648 649 650 651 652 653
//-----------------------------------------------
void UnityAssertEqualStringArray( const char** expected,
                                  const char** actual,
                                  const _UU32 num_elements,
                                  const char* msg,
                                  const UNITY_LINE_TYPE lineNumber)
{
    _UU32 i, j = 0;
    
654 655
    UNITY_SKIP_EXECUTION;
  
M
mvandervoord 已提交
656 657 658 659 660 661 662 663 664
    // if no elements, it's an error
    if (num_elements == 0)
    {
        UnityTestResultsFailBegin(lineNumber);
        UnityPrint(UnityStrPointless);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }

665 666
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;
M
mvandervoord 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
    
    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 已提交
698
            UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j]));
M
mvandervoord 已提交
699 700 701 702 703 704 705
            UnityAddMsgIfSpecified(msg);
            UNITY_FAIL_AND_BAIL;
        } 
    } while (++j < num_elements);
}

//-----------------------------------------------
M
mvandervoord 已提交
706 707
void UnityAssertEqualMemory( const void* expected,
                             const void* actual,
708 709
                             _UU32 length,
                             _UU32 num_elements,
M
mvandervoord 已提交
710 711
                             const char* msg,
                             const UNITY_LINE_TYPE lineNumber)
712
{
M
mvandervoord 已提交
713 714
    unsigned char* expected_ptr = (unsigned char*)expected;
    unsigned char* actual_ptr = (unsigned char*)actual;
715
    _UU32 elements = num_elements;
716 717 718

    UNITY_SKIP_EXECUTION;
  
719 720
    if ((elements == 0) || (length == 0))
    {
721
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
722 723 724
        UnityPrint(UnityStrPointless);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
725
    }
726

727 728 729 730
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;
        
    while (elements--)
731
    {
732
        if (memcmp((const void*)expected_ptr, (const void*)actual_ptr, length) != 0)
733 734
        {
            Unity.CurrentTestFailed = 1;
735
            break;
736
        }
737 738
        expected_ptr += length;
        actual_ptr += length;
739 740 741 742
    }

    if (Unity.CurrentTestFailed)
    {
743
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
744
        if (num_elements > 1)
745
        {
M
mvandervoord 已提交
746 747
            UnityPrint(UnityStrElement);
            UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
748
        }
M
mvandervoord 已提交
749 750 751
        UnityPrint(UnityStrMemory);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
752 753 754
    }
}

M
mvandervoord 已提交
755 756 757 758 759
//-----------------------------------------------
// Control Functions
//-----------------------------------------------

void UnityFail(const char* msg, const UNITY_LINE_TYPE line)
G
greg-williams 已提交
760
{
761 762
    UNITY_SKIP_EXECUTION;

M
mvandervoord 已提交
763
    UnityTestResultsBegin(Unity.TestFile, line);
764
    UnityPrint("FAIL");
M
mvandervoord 已提交
765
    if (msg != NULL)
766 767
    {
      UNITY_OUTPUT_CHAR(':');
768 769
      if (msg[0] != ' ')
      {
M
mvandervoord 已提交
770 771
        UNITY_OUTPUT_CHAR(' ');  
      }
M
mvandervoord 已提交
772
      UnityPrint(msg);
773
    }
M
mvandervoord 已提交
774
    UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
775 776
}

M
mvandervoord 已提交
777
//-----------------------------------------------
M
mvandervoord 已提交
778
void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
G
greg-williams 已提交
779
{
780 781
    UNITY_SKIP_EXECUTION;

M
mvandervoord 已提交
782
    UnityTestResultsBegin(Unity.TestFile, line);
783
    UnityPrint("IGNORE");
M
mvandervoord 已提交
784
    if (msg != NULL)
785 786
    {
      UNITY_OUTPUT_CHAR(':');
787
      UNITY_OUTPUT_CHAR(' ');
M
mvandervoord 已提交
788
      UnityPrint(msg);
789
    }
M
mvandervoord 已提交
790
    UNITY_IGNORE_AND_BAIL;
G
greg-williams 已提交
791 792
}

793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
//-----------------------------------------------
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 已提交
813
//-----------------------------------------------
814
void UnityBegin(void)
G
greg-williams 已提交
815 816 817 818
{
    Unity.NumberOfTests = 0;
}

M
mvandervoord 已提交
819
//-----------------------------------------------
820
int UnityEnd(void)
G
greg-williams 已提交
821
{
M
mvandervoord 已提交
822
    UnityPrint("-----------------------");
M
mvandervoord 已提交
823
    UNITY_PRINT_EOL;
G
greg-williams 已提交
824 825 826 827 828
    UnityPrintNumber(Unity.NumberOfTests);
    UnityPrint(" Tests ");
    UnityPrintNumber(Unity.TestFailures);
    UnityPrint(" Failures ");
    UnityPrintNumber(Unity.TestIgnores);
M
mvandervoord 已提交
829
    UnityPrint(" Ignored");
M
mvandervoord 已提交
830
    UNITY_PRINT_EOL;
G
greg-williams 已提交
831 832
    if (Unity.TestFailures == 0U)
    {
M
mvandervoord 已提交
833
        UnityPrint("OK");
G
greg-williams 已提交
834 835 836
    }
    else
    {
M
mvandervoord 已提交
837
        UnityPrint("FAIL");
G
greg-williams 已提交
838
    }
M
mvandervoord 已提交
839
    UNITY_PRINT_EOL;
840
    return Unity.TestFailures;
G
greg-williams 已提交
841
}