You need to sign in or sign up before continuing.
unity.c 24.6 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
    if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
    {
        UnityPrintNumber(number);
    }
    else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
    {
80
        UnityPrintNumberUnsigned((_U_UINT)number & ((_U_UINT)1 << ((((int)style & 0x000F) << 3) - 1)));
M
mvandervoord 已提交
81 82
    }
    else
G
greg-williams 已提交
83
    {
M
mvandervoord 已提交
84
        UnityPrintNumberHex((_U_UINT)number, (style & 0x000F) << 1);
G
greg-williams 已提交
85
    }
86
    
G
greg-williams 已提交
87 88
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 314
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;
}
315

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

666 667
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;
M
mvandervoord 已提交
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 698
    
    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 已提交
699
            UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j]));
M
mvandervoord 已提交
700 701 702 703 704 705 706
            UnityAddMsgIfSpecified(msg);
            UNITY_FAIL_AND_BAIL;
        } 
    } while (++j < num_elements);
}

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

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

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

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

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

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

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

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

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

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

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