unity.c 28.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";
M
mvandervoord 已提交
31

32
// compiler-generic print formatting masks
33 34
const _U_UINT UnitySizeMask[] = 
{
35 36
    255u,         // 0xFF
    65535u,       // 0xFFFF
M
 
mvandervoord 已提交
37
    65535u,
38
    4294967295u,  // 0xFFFFFFFF
M
 
mvandervoord 已提交
39 40 41
    4294967295u,
    4294967295u,
    4294967295u
42 43 44 45 46
#ifdef UNITY_SUPPORT_64
    ,0xFFFFFFFFFFFFFFFF
#endif
};

47 48 49
void UnityPrintFail(void);
void UnityPrintOk(void);

M
mvandervoord 已提交
50
//-----------------------------------------------
51
// Pretty Printers & Test Result Output Handlers
M
mvandervoord 已提交
52 53
//-----------------------------------------------

54
void UnityPrint(const char* string)
G
greg-williams 已提交
55
{
56
    const char* pch = string;
G
greg-williams 已提交
57 58 59 60 61

    if (pch != NULL)
    {
        while (*pch)
        {
M
mvandervoord 已提交
62
            // printable characters plus CR & LF are printed
M
mvandervoord 已提交
63
            if ((*pch <= 126) && (*pch >= 32))
M
mvandervoord 已提交
64 65 66
            {
                UNITY_OUTPUT_CHAR(*pch);
            }
M
mvandervoord 已提交
67 68 69 70 71 72 73 74 75 76 77 78
            //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 已提交
79 80 81 82
            // unprintable characters are shown as codes
            else
            {
                UNITY_OUTPUT_CHAR('\\');
M
mvandervoord 已提交
83
                UnityPrintNumberHex((_U_SINT)*pch, 2);
M
mvandervoord 已提交
84
            }
G
greg-williams 已提交
85 86 87 88 89
            pch++;
        }
    }
}

M
mvandervoord 已提交
90
//-----------------------------------------------
M
mvandervoord 已提交
91
void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style)
G
greg-williams 已提交
92
{
M
mvandervoord 已提交
93 94 95 96 97 98
    if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
    {
        UnityPrintNumber(number);
    }
    else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
    {
99
        UnityPrintNumberUnsigned(  (_U_UINT)number  &  UnitySizeMask[((_U_UINT)style & (_U_UINT)0x0F) - 1]  );
M
mvandervoord 已提交
100 101
    }
    else
G
greg-williams 已提交
102
    {
M
mvandervoord 已提交
103
        UnityPrintNumberHex((_U_UINT)number, (style & 0x000F) << 1);
G
greg-williams 已提交
104 105 106
    }
}

M
mvandervoord 已提交
107
//-----------------------------------------------
G
greg-williams 已提交
108
/// basically do an itoa using as little ram as possible
M
mvandervoord 已提交
109
void UnityPrintNumber(const _U_SINT number_to_print)
G
greg-williams 已提交
110
{
M
mvandervoord 已提交
111 112 113
    _U_SINT divisor = 1;
    _U_SINT next_divisor;
    _U_SINT number = number_to_print;
G
greg-williams 已提交
114 115 116

    if (number < 0)
    {
117
        UNITY_OUTPUT_CHAR('-');
G
greg-williams 已提交
118 119 120 121
        number = -number;
    }

    // figure out initial divisor
M
mvandervoord 已提交
122
    while (number / divisor > 9)
G
greg-williams 已提交
123
    {
M
mvandervoord 已提交
124 125 126 127 128
        next_divisor = divisor * 10;
        if (next_divisor > divisor)
            divisor = next_divisor;
        else
            break;
G
greg-williams 已提交
129 130
    }

M
mvandervoord 已提交
131
    // now mod and print, then divide divisor
G
greg-williams 已提交
132 133
    do
    {
134
        UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
M
mvandervoord 已提交
135
        divisor /= 10;
G
greg-williams 已提交
136
    }
M
mvandervoord 已提交
137
    while (divisor > 0);
G
greg-williams 已提交
138 139
}

M
mvandervoord 已提交
140
//-----------------------------------------------
G
greg-williams 已提交
141
/// basically do an itoa using as little ram as possible
M
mvandervoord 已提交
142
void UnityPrintNumberUnsigned(const _U_UINT number)
G
greg-williams 已提交
143
{
M
mvandervoord 已提交
144 145
    _U_UINT divisor = 1;
    _U_UINT next_divisor;
G
greg-williams 已提交
146 147

    // figure out initial divisor
M
mvandervoord 已提交
148
    while (number / divisor > 9)
G
greg-williams 已提交
149
    {
M
mvandervoord 已提交
150 151 152 153 154
        next_divisor = divisor * 10;
        if (next_divisor > divisor)
            divisor = next_divisor;
        else
            break;
G
greg-williams 已提交
155 156
    }

M
mvandervoord 已提交
157
    // now mod and print, then divide divisor
G
greg-williams 已提交
158 159
    do
    {
160
        UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
M
mvandervoord 已提交
161
        divisor /= 10;
G
greg-williams 已提交
162
    }
M
mvandervoord 已提交
163
    while (divisor > 0);
G
greg-williams 已提交
164 165
}

M
mvandervoord 已提交
166
//-----------------------------------------------
M
mvandervoord 已提交
167
void UnityPrintNumberHex(const _U_UINT number, const char nibbles_to_print)
G
greg-williams 已提交
168
{
M
mvandervoord 已提交
169
    _U_UINT nibble;
M
mvandervoord 已提交
170
    char nibbles = nibbles_to_print;
171 172
    UNITY_OUTPUT_CHAR('0');
    UNITY_OUTPUT_CHAR('x');
G
greg-williams 已提交
173 174 175 176 177 178

    while (nibbles > 0)
    {
        nibble = (number >> (--nibbles << 2)) & 0x0000000F;
        if (nibble <= 9)
        {
179
            UNITY_OUTPUT_CHAR((char)('0' + nibble));
G
greg-williams 已提交
180 181 182
        }
        else
        {
183
            UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble));
G
greg-williams 已提交
184 185 186 187
        }
    }
}

M
mvandervoord 已提交
188
//-----------------------------------------------
M
mvandervoord 已提交
189
void UnityPrintMask(const _U_UINT mask, const _U_UINT number)
G
greg-williams 已提交
190
{
191
    _U_UINT current_bit = (_U_UINT)1 << (UNITY_INT_WIDTH - 1);
192
    _US32 i;
G
greg-williams 已提交
193

M
mvandervoord 已提交
194
    for (i = 0; i < UNITY_INT_WIDTH; i++)
G
greg-williams 已提交
195
    {
196
        if (current_bit & mask)
G
greg-williams 已提交
197
        {
198
            if (current_bit & number)
G
greg-williams 已提交
199
            {
200
                UNITY_OUTPUT_CHAR('1');
G
greg-williams 已提交
201 202 203
            }
            else
            {
204
                UNITY_OUTPUT_CHAR('0');
G
greg-williams 已提交
205 206 207 208
            }
        }
        else
        {
209
            UNITY_OUTPUT_CHAR('X');
G
greg-williams 已提交
210
        }
211
        current_bit = current_bit >> 1;
G
greg-williams 已提交
212 213 214
    }
}

M
mvandervoord 已提交
215
//-----------------------------------------------
216 217 218 219 220 221 222 223 224
#ifdef UNITY_FLOAT_VERBOSE
void UnityPrintFloat(_UF number)
{
    char TempBuffer[32];
    sprintf(TempBuffer, "%.6f", number);
    UnityPrint(TempBuffer);
}
#endif

225 226 227 228 229 230 231 232 233 234 235 236
//-----------------------------------------------

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

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

M
mvandervoord 已提交
237
//-----------------------------------------------
M
mvandervoord 已提交
238
void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line)
G
greg-williams 已提交
239
{
240
    UnityPrint(file);
241
    UNITY_OUTPUT_CHAR(':');
G
greg-williams 已提交
242
    UnityPrintNumber(line);
243
    UNITY_OUTPUT_CHAR(':');
G
greg-williams 已提交
244
    UnityPrint(Unity.CurrentTestName);
245
    UNITY_OUTPUT_CHAR(':');
G
greg-williams 已提交
246 247
}

M
mvandervoord 已提交
248
//-----------------------------------------------
M
mvandervoord 已提交
249
void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)
250
{
M
mvandervoord 已提交
251
    UnityTestResultsBegin(Unity.TestFile, line);
252 253 254
    UnityPrint("FAIL:");
}

M
mvandervoord 已提交
255
//-----------------------------------------------
256
void UnityConcludeTest(void)
G
greg-williams 已提交
257 258 259 260 261 262 263
{
    if (Unity.CurrentTestIgnored)
    {
        Unity.TestIgnores++;
    }
    else if (!Unity.CurrentTestFailed)
    {
264
        UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);
M
mvandervoord 已提交
265
        UnityPrint("PASS");
M
mvandervoord 已提交
266
        UNITY_PRINT_EOL;
G
greg-williams 已提交
267 268 269 270 271 272 273 274 275 276
    }
    else
    {
        Unity.TestFailures++;
    }

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

M
mvandervoord 已提交
277
//-----------------------------------------------
M
mvandervoord 已提交
278 279 280 281 282 283 284 285 286
void UnityAddMsgIfSpecified(const char* msg)
{
    if (msg)
    {
        UnityPrint(UnityStrSpacer);
        UnityPrint(msg);
    }
}

287 288 289
//-----------------------------------------------
void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual)
{
M
mvandervoord 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    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);          
    }
312 313
}

314 315 316 317
//-----------------------------------------------
// Assertion & Control Helpers
//-----------------------------------------------

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
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;
}
345

M
mvandervoord 已提交
346 347 348 349
//-----------------------------------------------
// Assertion Functions
//-----------------------------------------------

M
mvandervoord 已提交
350 351 352
void UnityAssertBits(const _U_SINT mask,
                     const _U_SINT expected,
                     const _U_SINT actual,
353
                     const char* msg,
M
mvandervoord 已提交
354
                     const UNITY_LINE_TYPE lineNumber)
G
greg-williams 已提交
355
{
356 357
    UNITY_SKIP_EXECUTION;
  
G
greg-williams 已提交
358 359
    if ((mask & expected) != (mask & actual))
    {
360
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
361
        UnityPrint(UnityStrExpected);
G
greg-williams 已提交
362
        UnityPrintMask(mask, expected);
M
mvandervoord 已提交
363
        UnityPrint(UnityStrWas);
G
greg-williams 已提交
364
        UnityPrintMask(mask, actual);
M
mvandervoord 已提交
365 366
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
367 368 369
    }
}

M
mvandervoord 已提交
370
//-----------------------------------------------
M
mvandervoord 已提交
371 372
void UnityAssertEqualNumber(const _U_SINT expected,
                            const _U_SINT actual,
M
mvandervoord 已提交
373 374 375
                            const char* msg,
                            const UNITY_LINE_TYPE lineNumber,
                            const UNITY_DISPLAY_STYLE_T style)
376
{
377 378
    UNITY_SKIP_EXECUTION;

379 380
    if (expected != actual)
    {
381
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
382
        UnityPrint(UnityStrExpected);
383
        UnityPrintNumberByStyle(expected, style);
M
mvandervoord 已提交
384
        UnityPrint(UnityStrWas);
385
        UnityPrintNumberByStyle(actual, style);
M
mvandervoord 已提交
386 387
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
388 389 390
    }
}

M
mvandervoord 已提交
391
//-----------------------------------------------
M
mvandervoord 已提交
392 393
void UnityAssertEqualIntArray(const _U_SINT* expected,
                              const _U_SINT* actual,
394
                              const _UU32 num_elements,
395
                              const char* msg,
M
mvandervoord 已提交
396
                              const UNITY_LINE_TYPE lineNumber,
397 398
                              const UNITY_DISPLAY_STYLE_T style)
{
399
    _UU32 elements = num_elements;
M
mvandervoord 已提交
400 401
    const _US8* ptr_exp = (_US8*)expected;
    const _US8* ptr_act = (_US8*)actual;
402

403 404
    UNITY_SKIP_EXECUTION;
  
405 406 407 408 409 410 411
    if (elements == 0)
    {
        UnityTestResultsFailBegin(lineNumber);
        UnityPrint(UnityStrPointless);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }
412 413 414
    
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;
415 416 417 418

    switch(style)
    {
        case UNITY_DISPLAY_STYLE_HEX8:
M
mvandervoord 已提交
419 420
        case UNITY_DISPLAY_STYLE_INT8:
        case UNITY_DISPLAY_STYLE_UINT8:
421 422
            while (elements--)
            {
M
mvandervoord 已提交
423
                if (*ptr_exp != *ptr_act)
424 425 426 427 428
                {
                    UnityTestResultsFailBegin(lineNumber);
                    UnityPrint(UnityStrElement);
                    UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
                    UnityPrint(UnityStrExpected);
M
mvandervoord 已提交
429
                    UnityPrintNumberByStyle(*ptr_exp, style);
430
                    UnityPrint(UnityStrWas);
M
mvandervoord 已提交
431
                    UnityPrintNumberByStyle(*ptr_act, style);
432 433 434
                    UnityAddMsgIfSpecified(msg);
                    UNITY_FAIL_AND_BAIL;
                }
M
mvandervoord 已提交
435 436
                ptr_exp += 1;
                ptr_act += 1;
437 438 439
            }
            break;
        case UNITY_DISPLAY_STYLE_HEX16:
M
mvandervoord 已提交
440 441
        case UNITY_DISPLAY_STYLE_INT16:
        case UNITY_DISPLAY_STYLE_UINT16:
442 443
            while (elements--)
            {
M
mvandervoord 已提交
444
                if (*(_US16*)ptr_exp != *(_US16*)ptr_act)
445 446 447 448 449
                {
                    UnityTestResultsFailBegin(lineNumber);
                    UnityPrint(UnityStrElement);
                    UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
                    UnityPrint(UnityStrExpected);
M
mvandervoord 已提交
450
                    UnityPrintNumberByStyle(*(_US16*)ptr_exp, style);
451
                    UnityPrint(UnityStrWas);
M
mvandervoord 已提交
452
                    UnityPrintNumberByStyle(*(_US16*)ptr_act, style);
453 454 455
                    UnityAddMsgIfSpecified(msg);
                    UNITY_FAIL_AND_BAIL;
                }
M
mvandervoord 已提交
456 457
                ptr_exp += 2;
                ptr_act += 2;
458 459
            }
            break;
M
mvandervoord 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
#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
483 484 485
        default:
            while (elements--)
            {
M
mvandervoord 已提交
486
                if (*(_US32*)ptr_exp != *(_US32*)ptr_act)
487 488 489 490 491
                {
                    UnityTestResultsFailBegin(lineNumber);
                    UnityPrint(UnityStrElement);
                    UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
                    UnityPrint(UnityStrExpected);
M
mvandervoord 已提交
492
                    UnityPrintNumberByStyle(*(_US32*)ptr_exp, style);
493
                    UnityPrint(UnityStrWas);
M
mvandervoord 已提交
494
                    UnityPrintNumberByStyle(*(_US32*)ptr_act, style);
495 496 497
                    UnityAddMsgIfSpecified(msg);
                    UNITY_FAIL_AND_BAIL;
                }
M
mvandervoord 已提交
498 499
                ptr_exp += 4;
                ptr_act += 4;
500 501 502 503 504
            }
            break;
    }
}

M
mvandervoord 已提交
505
//-----------------------------------------------
506
#ifndef UNITY_EXCLUDE_FLOAT
507 508
void UnityAssertEqualFloatArray(const _UF* expected,
                                const _UF* actual,
509
                                const _UU32 num_elements,
510 511 512
                                const char* msg,
                                const UNITY_LINE_TYPE lineNumber)
{
513
    _UU32 elements = num_elements;
514 515
    const _UF* ptr_expected = expected;
    const _UF* ptr_actual = actual;
M
mvandervoord 已提交
516
    _UF diff, tol;
517

518 519
    UNITY_SKIP_EXECUTION;
  
520 521
    if (elements == 0)
    {
522
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
523 524 525
        UnityPrint(UnityStrPointless);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
526
    }
527 528 529
    
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;
530 531 532

    while (elements--)
    {
533 534 535
        diff = *ptr_expected - *ptr_actual;
        if (diff < 0.0)
          diff = 0.0 - diff;
M
mvandervoord 已提交
536 537 538 539
        tol = UNITY_FLOAT_PRECISION * *ptr_expected;
        if (tol < 0.0)
            tol = 0.0 - tol;
        if (diff > tol)
540
        {
541
            UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
542
            UnityPrint(UnityStrElement);
543
            UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
544 545 546 547 548 549
#ifdef UNITY_FLOAT_VERBOSE
            UnityPrint(UnityStrExpected);
            UnityPrintFloat(*ptr_expected);
            UnityPrint(UnityStrWas);
            UnityPrintFloat(*ptr_actual);
#else
550
            UnityPrint(UnityStrDelta);
551
#endif
M
mvandervoord 已提交
552 553
            UnityAddMsgIfSpecified(msg);
            UNITY_FAIL_AND_BAIL;
M
mvandervoord 已提交
554
        }
555 556
        ptr_expected++;
        ptr_actual++;
M
mvandervoord 已提交
557 558 559
    }
}

M
mvandervoord 已提交
560
//-----------------------------------------------
561 562 563
void UnityAssertFloatsWithin(const _UF delta,
                             const _UF expected,
                             const _UF actual,
564
                             const char* msg,
M
mvandervoord 已提交
565
                             const UNITY_LINE_TYPE lineNumber)
G
greg-williams 已提交
566
{
567 568
    _UF diff = actual - expected;
    _UF pos_delta = delta;
569

570 571
    UNITY_SKIP_EXECUTION;
  
G
greg-williams 已提交
572 573
    if (diff < 0)
    {
574
        diff = 0.0f - diff;
G
greg-williams 已提交
575
    }
576 577 578 579
    if (pos_delta < 0)
    {
        pos_delta = 0.0f - pos_delta;
    }
580

581
    if (pos_delta < diff)
G
greg-williams 已提交
582
    {
583
        UnityTestResultsFailBegin(lineNumber);
584 585 586 587 588 589
#ifdef UNITY_FLOAT_VERBOSE
        UnityPrint(UnityStrExpected);
        UnityPrintFloat(expected);
        UnityPrint(UnityStrWas);
        UnityPrintFloat(actual);
#else
M
mvandervoord 已提交
590
        UnityPrint(UnityStrDelta);
591
#endif
M
mvandervoord 已提交
592 593
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
594 595
    }
}
M
mvandervoord 已提交
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644

#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;
        if (diff > tol)
        {
            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);
645
#endif
M
mvandervoord 已提交
646 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 685 686 687 688 689 690 691
            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;
  
    if (diff < 0)
    {
        diff = 0.0f - diff;
    }
    if (pos_delta < 0)
    {
        pos_delta = 0.0f - pos_delta;
    }

    if (pos_delta < diff)
    {
        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;
    }
}

#endif // not UNITY_EXCLUDE_DOUBLE
G
greg-williams 已提交
692

M
mvandervoord 已提交
693
//-----------------------------------------------
M
mvandervoord 已提交
694 695 696
void UnityAssertNumbersWithin( const _U_SINT delta,
                               const _U_SINT expected,
                               const _U_SINT actual,
M
mvandervoord 已提交
697 698 699
                               const char* msg,
                               const UNITY_LINE_TYPE lineNumber,
                               const UNITY_DISPLAY_STYLE_T style)
G
greg-williams 已提交
700
{
701
    UNITY_SKIP_EXECUTION;
M
mvandervoord 已提交
702 703
    
    if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
G
greg-williams 已提交
704
    {
M
mvandervoord 已提交
705 706 707 708
        if (actual > expected)
          Unity.CurrentTestFailed = ((actual - expected) > delta);
        else
          Unity.CurrentTestFailed = ((expected - actual) > delta);
G
greg-williams 已提交
709
    }
M
mvandervoord 已提交
710
    else
G
greg-williams 已提交
711
    {
M
mvandervoord 已提交
712 713
        if ((_U_UINT)actual > (_U_UINT)expected)
            Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > (_U_UINT)delta);
M
mvandervoord 已提交
714
        else
M
mvandervoord 已提交
715
            Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > (_U_UINT)delta);
716 717
    }

M
mvandervoord 已提交
718
    if (Unity.CurrentTestFailed)
719
    {
720
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
721 722 723 724 725 726 727 728
        UnityPrint(UnityStrDelta);
        UnityPrintNumberByStyle(delta, style);
        UnityPrint(UnityStrExpected);
        UnityPrintNumberByStyle(expected, style);
        UnityPrint(UnityStrWas);
        UnityPrintNumberByStyle(actual, style);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
729 730 731
    }
}

M
mvandervoord 已提交
732
//-----------------------------------------------
733 734 735
void UnityAssertEqualString(const char* expected,
                            const char* actual,
                            const char* msg,
M
mvandervoord 已提交
736
                            const UNITY_LINE_TYPE lineNumber)
G
greg-williams 已提交
737
{
738
    _UU32 i;
G
greg-williams 已提交
739

740 741
    UNITY_SKIP_EXECUTION;
  
G
greg-williams 已提交
742 743 744 745 746 747 748 749
    // 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;
750
                break;
G
greg-williams 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763
            }
        }
    }
    else
    { // handle case of one pointers being null (if both null, test should pass)
        if (expected != actual)
        {
            Unity.CurrentTestFailed = 1;
        }
    }

    if (Unity.CurrentTestFailed)
    {
764
      UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
765
      UnityPrintExpectedAndActualStrings(expected, actual);
766 767
      UnityAddMsgIfSpecified(msg);
      UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
768 769 770
    }
}

M
mvandervoord 已提交
771 772 773 774 775 776 777 778 779
//-----------------------------------------------
void UnityAssertEqualStringArray( const char** expected,
                                  const char** actual,
                                  const _UU32 num_elements,
                                  const char* msg,
                                  const UNITY_LINE_TYPE lineNumber)
{
    _UU32 i, j = 0;
    
780 781
    UNITY_SKIP_EXECUTION;
  
M
mvandervoord 已提交
782 783 784 785 786 787 788 789 790
    // if no elements, it's an error
    if (num_elements == 0)
    {
        UnityTestResultsFailBegin(lineNumber);
        UnityPrint(UnityStrPointless);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }

791 792
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;
M
mvandervoord 已提交
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
    
    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 已提交
824
            UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j]));
M
mvandervoord 已提交
825 826 827 828 829 830 831
            UnityAddMsgIfSpecified(msg);
            UNITY_FAIL_AND_BAIL;
        } 
    } while (++j < num_elements);
}

//-----------------------------------------------
M
mvandervoord 已提交
832 833
void UnityAssertEqualMemory( const void* expected,
                             const void* actual,
834 835
                             const _UU32 length,
                             const _UU32 num_elements,
M
mvandervoord 已提交
836 837
                             const char* msg,
                             const UNITY_LINE_TYPE lineNumber)
838
{
839 840
    unsigned char* ptr_exp = (unsigned char*)expected;
    unsigned char* ptr_act = (unsigned char*)actual;
841
    _UU32 elements = num_elements;
842
    _UU32 bytes;
843 844 845

    UNITY_SKIP_EXECUTION;
  
846 847
    if ((elements == 0) || (length == 0))
    {
848
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
849 850 851
        UnityPrint(UnityStrPointless);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
852
    }
853

854 855 856 857
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;
        
    while (elements--)
858
    {
859 860 861
        /////////////////////////////////////
        bytes = length;
        while (bytes--)
862
        {
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
            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;
883
        }
884 885
        /////////////////////////////////////
        
886 887 888
    }
}

M
mvandervoord 已提交
889 890 891 892 893
//-----------------------------------------------
// Control Functions
//-----------------------------------------------

void UnityFail(const char* msg, const UNITY_LINE_TYPE line)
G
greg-williams 已提交
894
{
895 896
    UNITY_SKIP_EXECUTION;

M
mvandervoord 已提交
897
    UnityTestResultsBegin(Unity.TestFile, line);
898
    UnityPrintFail();
M
mvandervoord 已提交
899
    if (msg != NULL)
900 901
    {
      UNITY_OUTPUT_CHAR(':');
902 903
      if (msg[0] != ' ')
      {
M
mvandervoord 已提交
904 905
        UNITY_OUTPUT_CHAR(' ');  
      }
M
mvandervoord 已提交
906
      UnityPrint(msg);
907
    }
M
mvandervoord 已提交
908
    UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
909 910
}

M
mvandervoord 已提交
911
//-----------------------------------------------
M
mvandervoord 已提交
912
void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
G
greg-williams 已提交
913
{
914 915
    UNITY_SKIP_EXECUTION;

M
mvandervoord 已提交
916
    UnityTestResultsBegin(Unity.TestFile, line);
917
    UnityPrint("IGNORE");
M
mvandervoord 已提交
918
    if (msg != NULL)
919 920
    {
      UNITY_OUTPUT_CHAR(':');
921
      UNITY_OUTPUT_CHAR(' ');
M
mvandervoord 已提交
922
      UnityPrint(msg);
923
    }
M
mvandervoord 已提交
924
    UNITY_IGNORE_AND_BAIL;
G
greg-williams 已提交
925 926
}

927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
//-----------------------------------------------
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 已提交
947
//-----------------------------------------------
948
void UnityBegin(void)
G
greg-williams 已提交
949 950
{
    Unity.NumberOfTests = 0;
951 952 953 954
    Unity.TestFailures = 0;
    Unity.TestIgnores = 0;
    Unity.CurrentTestFailed = 0;
    Unity.CurrentTestIgnored = 0;
G
greg-williams 已提交
955 956
}

M
mvandervoord 已提交
957
//-----------------------------------------------
958
int UnityEnd(void)
G
greg-williams 已提交
959
{
M
mvandervoord 已提交
960
    UnityPrint("-----------------------");
M
mvandervoord 已提交
961
    UNITY_PRINT_EOL;
G
greg-williams 已提交
962 963 964 965 966
    UnityPrintNumber(Unity.NumberOfTests);
    UnityPrint(" Tests ");
    UnityPrintNumber(Unity.TestFailures);
    UnityPrint(" Failures ");
    UnityPrintNumber(Unity.TestIgnores);
M
mvandervoord 已提交
967
    UnityPrint(" Ignored");
M
mvandervoord 已提交
968
    UNITY_PRINT_EOL;
G
greg-williams 已提交
969 970
    if (Unity.TestFailures == 0U)
    {
971
        UnityPrintOk();
G
greg-williams 已提交
972 973 974
    }
    else
    {
975
        UnityPrintFail();
G
greg-williams 已提交
976
    }
M
mvandervoord 已提交
977
    UNITY_PRINT_EOL;
978
    return Unity.TestFailures;
G
greg-williams 已提交
979
}