unity.c 22.8 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
#define UNITY_SKIP_EXECUTION  { if (UnityCheckSkipConditions()) {return;} }
M
mvandervoord 已提交
14

15
struct _Unity Unity = { 0 };
G
greg-williams 已提交
16

17
const char* UnityStrNull     = "NULL";
18
const char* UnityStrSpacer   = ". ";
M
mvandervoord 已提交
19 20 21 22
const char* UnityStrExpected = " Expected ";
const char* UnityStrWas      = " Was ";
const char* UnityStrTo       = " To ";
const char* UnityStrElement  = " Element ";
23
const char* UnityStrMemory   = " Memory Mismatch";
M
mvandervoord 已提交
24
const char* UnityStrDelta    = " Values Not Within Delta ";
25
const char* UnityStrPointless= " You Asked Me To Compare Nothing, Which Was Pointless.";
26 27
const char* UnityStrNullPointerForExpected= " Expected pointer to be NULL";
const char* UnityStrNullPointerForActual  = " Actual pointer was NULL";
M
mvandervoord 已提交
28 29

//-----------------------------------------------
30
// Pretty Printers & Test Result Output Handlers
M
mvandervoord 已提交
31 32
//-----------------------------------------------

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

    if (pch != NULL)
    {
        while (*pch)
        {
M
mvandervoord 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
            // printable characters plus CR & LF are printed
            if ( ((*pch <= 126) && (*pch >= 32)) || (*pch == 13) || (*pch == 10) )
            {
                UNITY_OUTPUT_CHAR(*pch);
            }
            // unprintable characters are shown as codes
            else
            {
                UNITY_OUTPUT_CHAR('{');
                UNITY_OUTPUT_CHAR('\\');
                UnityPrintNumber((_US32)*pch);
                UNITY_OUTPUT_CHAR('}');
            }
G
greg-williams 已提交
54 55 56 57 58
            pch++;
        }
    }
}

M
mvandervoord 已提交
59
//-----------------------------------------------
60
void UnityPrintNumberByStyle(const _US32 number, const UNITY_DISPLAY_STYLE_T style)
G
greg-williams 已提交
61 62 63
{
    switch (style)
    {
64 65 66 67
        case UNITY_DISPLAY_STYLE_HEX8:    UnityPrintNumberHex((_UU32)number, 2);           break;
        case UNITY_DISPLAY_STYLE_HEX16:   UnityPrintNumberHex((_UU32)number, 4);           break;
        case UNITY_DISPLAY_STYLE_HEX32:   UnityPrintNumberHex((_UU32)number, 8);           break;
        case UNITY_DISPLAY_STYLE_UINT:    UnityPrintNumberUnsigned((_UU32)number);         break;
68
        default:                          UnityPrintNumber(number);                        break;
G
greg-williams 已提交
69 70 71
    }
}

M
mvandervoord 已提交
72
//-----------------------------------------------
G
greg-williams 已提交
73
/// basically do an itoa using as little ram as possible
74
void UnityPrintNumber(const _US32 number_to_print)
G
greg-williams 已提交
75
{
76 77 78
    _US32 divisor = 1;
    _US32 next_divisor;
    _US32 number = number_to_print;
G
greg-williams 已提交
79 80 81

    if (number < 0)
    {
82
        UNITY_OUTPUT_CHAR('-');
G
greg-williams 已提交
83 84 85 86
        number = -number;
    }

    // figure out initial divisor
M
mvandervoord 已提交
87
    while (number / divisor > 9)
G
greg-williams 已提交
88
    {
M
mvandervoord 已提交
89 90 91 92 93
        next_divisor = divisor * 10;
        if (next_divisor > divisor)
            divisor = next_divisor;
        else
            break;
G
greg-williams 已提交
94 95
    }

M
mvandervoord 已提交
96
    // now mod and print, then divide divisor
G
greg-williams 已提交
97 98
    do
    {
99
        UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
M
mvandervoord 已提交
100
        divisor /= 10;
G
greg-williams 已提交
101
    }
M
mvandervoord 已提交
102
    while (divisor > 0);
G
greg-williams 已提交
103 104
}

M
mvandervoord 已提交
105
//-----------------------------------------------
G
greg-williams 已提交
106
/// basically do an itoa using as little ram as possible
107
void UnityPrintNumberUnsigned(const _UU32 number)
G
greg-williams 已提交
108
{
109 110
    _UU32 divisor = 1;
    _UU32 next_divisor;
G
greg-williams 已提交
111 112

    // figure out initial divisor
M
mvandervoord 已提交
113
    while (number / divisor > 9)
G
greg-williams 已提交
114
    {
M
mvandervoord 已提交
115 116 117 118 119
        next_divisor = divisor * 10;
        if (next_divisor > divisor)
            divisor = next_divisor;
        else
            break;
G
greg-williams 已提交
120 121
    }

M
mvandervoord 已提交
122
    // now mod and print, then divide divisor
G
greg-williams 已提交
123 124
    do
    {
125
        UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
M
mvandervoord 已提交
126
        divisor /= 10;
G
greg-williams 已提交
127
    }
M
mvandervoord 已提交
128
    while (divisor > 0);
G
greg-williams 已提交
129 130
}

M
mvandervoord 已提交
131
//-----------------------------------------------
132
void UnityPrintNumberHex(const _UU32 number, const char nibbles_to_print)
G
greg-williams 已提交
133
{
134
    _UU32 nibble;
M
mvandervoord 已提交
135
    char nibbles = nibbles_to_print;
G
greg-williams 已提交
136 137 138 139 140 141 142
    UnityPrint("0x");

    while (nibbles > 0)
    {
        nibble = (number >> (--nibbles << 2)) & 0x0000000F;
        if (nibble <= 9)
        {
143
            UNITY_OUTPUT_CHAR((char)('0' + nibble));
G
greg-williams 已提交
144 145 146
        }
        else
        {
147
            UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble));
G
greg-williams 已提交
148 149 150 151
        }
    }
}

M
mvandervoord 已提交
152
//-----------------------------------------------
153
void UnityPrintMask(const _UU32 mask, const _UU32 number)
G
greg-williams 已提交
154
{
155 156
    _UU32 bit = 0x80000000;
    _US32 i;
G
greg-williams 已提交
157 158 159 160 161 162 163

    for (i = 0; i < 32; i++)
    {
        if (bit & mask)
        {
            if (bit & number)
            {
164
                UNITY_OUTPUT_CHAR('1');
G
greg-williams 已提交
165 166 167
            }
            else
            {
168
                UNITY_OUTPUT_CHAR('0');
G
greg-williams 已提交
169 170 171 172
            }
        }
        else
        {
173
            UNITY_OUTPUT_CHAR('X');
G
greg-williams 已提交
174
        }
M
mvandervoord 已提交
175
        bit = bit >> 1;
G
greg-williams 已提交
176 177 178
    }
}

M
mvandervoord 已提交
179
//-----------------------------------------------
180 181 182 183 184 185 186 187 188
#ifdef UNITY_FLOAT_VERBOSE
void UnityPrintFloat(_UF number)
{
    char TempBuffer[32];
    sprintf(TempBuffer, "%.6f", number);
    UnityPrint(TempBuffer);
}
#endif

M
mvandervoord 已提交
189
//-----------------------------------------------
M
mvandervoord 已提交
190
void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line)
G
greg-williams 已提交
191
{
192
    UnityPrint(file);
193
    UNITY_OUTPUT_CHAR(':');
G
greg-williams 已提交
194
    UnityPrintNumber(line);
195
    UNITY_OUTPUT_CHAR(':');
G
greg-williams 已提交
196
    UnityPrint(Unity.CurrentTestName);
197
    UNITY_OUTPUT_CHAR(':');
G
greg-williams 已提交
198 199
}

M
mvandervoord 已提交
200
//-----------------------------------------------
M
mvandervoord 已提交
201
void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)
202
{
M
mvandervoord 已提交
203
    UnityTestResultsBegin(Unity.TestFile, line);
204 205 206
    UnityPrint("FAIL:");
}

M
mvandervoord 已提交
207
//-----------------------------------------------
208
void UnityConcludeTest(void)
G
greg-williams 已提交
209 210 211 212 213 214 215
{
    if (Unity.CurrentTestIgnored)
    {
        Unity.TestIgnores++;
    }
    else if (!Unity.CurrentTestFailed)
    {
216 217
        UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);
        UnityPrint("PASS\n");
G
greg-williams 已提交
218 219 220 221 222 223 224 225 226 227
    }
    else
    {
        Unity.TestFailures++;
    }

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

M
mvandervoord 已提交
228
//-----------------------------------------------
M
mvandervoord 已提交
229 230 231 232 233 234 235 236 237
void UnityAddMsgIfSpecified(const char* msg)
{
    if (msg)
    {
        UnityPrint(UnityStrSpacer);
        UnityPrint(msg);
    }
}

238 239 240
//-----------------------------------------------
void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual)
{
M
mvandervoord 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    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);          
    }
263 264
}

265 266 267 268 269 270 271 272 273 274 275 276 277
//-----------------------------------------------
// Assertion & Control Helpers
//-----------------------------------------------

int UnityCheckSkipConditions(void)
{
    // are we already in failure or ignore state?
    if ((Unity.CurrentTestFailed != 0) || (Unity.CurrentTestIgnored != 0))
        return 1;

    return 0;
}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
//-----------------------------------------------
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;
}
306

M
mvandervoord 已提交
307 308 309 310
//-----------------------------------------------
// Assertion Functions
//-----------------------------------------------

311 312 313
void UnityAssertBits(const _US32 mask,
                     const _US32 expected,
                     const _US32 actual,
314
                     const char* msg,
M
mvandervoord 已提交
315
                     const UNITY_LINE_TYPE lineNumber)
G
greg-williams 已提交
316
{
317 318
    UNITY_SKIP_EXECUTION;
  
G
greg-williams 已提交
319 320
    if ((mask & expected) != (mask & actual))
    {
321
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
322
        UnityPrint(UnityStrExpected);
G
greg-williams 已提交
323
        UnityPrintMask(mask, expected);
M
mvandervoord 已提交
324
        UnityPrint(UnityStrWas);
G
greg-williams 已提交
325
        UnityPrintMask(mask, actual);
M
mvandervoord 已提交
326 327
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
328 329 330
    }
}

M
mvandervoord 已提交
331
//-----------------------------------------------
332 333
void UnityAssertEqualNumber(const _US32 expected,
                            const _US32 actual,
M
mvandervoord 已提交
334 335 336
                            const char* msg,
                            const UNITY_LINE_TYPE lineNumber,
                            const UNITY_DISPLAY_STYLE_T style)
337
{
338 339
    UNITY_SKIP_EXECUTION;

340 341
    if (expected != actual)
    {
342
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
343
        UnityPrint(UnityStrExpected);
344
        UnityPrintNumberByStyle(expected, style);
M
mvandervoord 已提交
345
        UnityPrint(UnityStrWas);
346
        UnityPrintNumberByStyle(actual, style);
M
mvandervoord 已提交
347 348
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
349 350 351
    }
}

M
mvandervoord 已提交
352
//-----------------------------------------------
353 354
void UnityAssertEqualIntArray(const int* expected,
                              const int* actual,
355
                              const _UU32 num_elements,
356
                              const char* msg,
M
mvandervoord 已提交
357
                              const UNITY_LINE_TYPE lineNumber,
358 359
                              const UNITY_DISPLAY_STYLE_T style)
{
360
    _UU32 elements = num_elements;
361 362 363 364 365 366 367
    const _US32* ptr_exp32 = (_US32*)expected;
    const _US16* ptr_exp16 = (_US16*)expected;
    const _US8*  ptr_exp8  = (_US8*)expected;
    const _US32* ptr_act32 = (_US32*)actual;
    const _US16* ptr_act16 = (_US16*)actual;
    const _US8*  ptr_act8  = (_US8*)actual;

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

    switch(style)
    {
        case UNITY_DISPLAY_STYLE_HEX8:
M
mvandervoord 已提交
384 385
        case UNITY_DISPLAY_STYLE_INT8:
        case UNITY_DISPLAY_STYLE_UINT8:
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
            while (elements--)
            {
                if (*ptr_exp8++ != *ptr_act8++)
                {
                    UnityTestResultsFailBegin(lineNumber);
                    UnityPrint(UnityStrElement);
                    UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
                    UnityPrint(UnityStrExpected);
                    UnityPrintNumberByStyle(*--ptr_exp8, style);
                    UnityPrint(UnityStrWas);
                    UnityPrintNumberByStyle(*--ptr_act8, style);
                    UnityAddMsgIfSpecified(msg);
                    UNITY_FAIL_AND_BAIL;
                }
            }
            break;
        case UNITY_DISPLAY_STYLE_HEX16:
M
mvandervoord 已提交
403 404
        case UNITY_DISPLAY_STYLE_INT16:
        case UNITY_DISPLAY_STYLE_UINT16:
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
            while (elements--)
            {
                if (*ptr_exp16++ != *ptr_act16++)
                {
                    UnityTestResultsFailBegin(lineNumber);
                    UnityPrint(UnityStrElement);
                    UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
                    UnityPrint(UnityStrExpected);
                    UnityPrintNumberByStyle(*--ptr_exp16, style);
                    UnityPrint(UnityStrWas);
                    UnityPrintNumberByStyle(*--ptr_act16, style);
                    UnityAddMsgIfSpecified(msg);
                    UNITY_FAIL_AND_BAIL;
                }
            }
            break;
        default:
            while (elements--)
            {
                if (*ptr_exp32++ != *ptr_act32++)
                {
                    UnityTestResultsFailBegin(lineNumber);
                    UnityPrint(UnityStrElement);
                    UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
                    UnityPrint(UnityStrExpected);
                    UnityPrintNumberByStyle(*--ptr_exp32, style);
                    UnityPrint(UnityStrWas);
                    UnityPrintNumberByStyle(*--ptr_act32, style);
                    UnityAddMsgIfSpecified(msg);
                    UNITY_FAIL_AND_BAIL;
                }
            }
            break;
    }
}

M
mvandervoord 已提交
441
//-----------------------------------------------
442
#ifndef UNITY_EXCLUDE_FLOAT
443 444
void UnityAssertEqualFloatArray(const _UF* expected,
                                const _UF* actual,
445
                                const _UU32 num_elements,
446 447 448
                                const char* msg,
                                const UNITY_LINE_TYPE lineNumber)
{
449
    _UU32 elements = num_elements;
450 451
    const _UF* ptr_expected = expected;
    const _UF* ptr_actual = actual;
M
mvandervoord 已提交
452
    _UF diff, tol;
453

454 455
    UNITY_SKIP_EXECUTION;
  
456 457
    if (elements == 0)
    {
458
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
459 460 461
        UnityPrint(UnityStrPointless);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
462
    }
463 464 465
    
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;
466 467 468

    while (elements--)
    {
469 470 471
        diff = *ptr_expected - *ptr_actual;
        if (diff < 0.0)
          diff = 0.0 - diff;
M
mvandervoord 已提交
472 473 474 475
        tol = UNITY_FLOAT_PRECISION * *ptr_expected;
        if (tol < 0.0)
            tol = 0.0 - tol;
        if (diff > tol)
476
        {
477
            UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
478
            UnityPrint(UnityStrElement);
479
            UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
480 481 482 483 484 485
#ifdef UNITY_FLOAT_VERBOSE
            UnityPrint(UnityStrExpected);
            UnityPrintFloat(*ptr_expected);
            UnityPrint(UnityStrWas);
            UnityPrintFloat(*ptr_actual);
#else
486
            UnityPrint(UnityStrDelta);
487
#endif
M
mvandervoord 已提交
488 489
            UnityAddMsgIfSpecified(msg);
            UNITY_FAIL_AND_BAIL;
M
mvandervoord 已提交
490
        }
491 492
        ptr_expected++;
        ptr_actual++;
M
mvandervoord 已提交
493 494 495
    }
}

M
mvandervoord 已提交
496
//-----------------------------------------------
497 498 499
void UnityAssertFloatsWithin(const _UF delta,
                             const _UF expected,
                             const _UF actual,
500
                             const char* msg,
M
mvandervoord 已提交
501
                             const UNITY_LINE_TYPE lineNumber)
G
greg-williams 已提交
502
{
503 504
    _UF diff = actual - expected;
    _UF pos_delta = delta;
505

506 507
    UNITY_SKIP_EXECUTION;
  
G
greg-williams 已提交
508 509
    if (diff < 0)
    {
510
        diff = 0.0f - diff;
G
greg-williams 已提交
511
    }
512 513 514 515
    if (pos_delta < 0)
    {
        pos_delta = 0.0f - pos_delta;
    }
516

517
    if (pos_delta < diff)
G
greg-williams 已提交
518
    {
519
        UnityTestResultsFailBegin(lineNumber);
520 521 522 523 524 525
#ifdef UNITY_FLOAT_VERBOSE
        UnityPrint(UnityStrExpected);
        UnityPrintFloat(expected);
        UnityPrint(UnityStrWas);
        UnityPrintFloat(actual);
#else
M
mvandervoord 已提交
526
        UnityPrint(UnityStrDelta);
527
#endif
M
mvandervoord 已提交
528 529
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
530 531
    }
}
532
#endif
G
greg-williams 已提交
533

M
mvandervoord 已提交
534
//-----------------------------------------------
535 536 537
void UnityAssertNumbersWithin( const _US32 delta,
                               const _US32 expected,
                               const _US32 actual,
M
mvandervoord 已提交
538 539 540
                               const char* msg,
                               const UNITY_LINE_TYPE lineNumber,
                               const UNITY_DISPLAY_STYLE_T style)
G
greg-williams 已提交
541
{
542 543
    UNITY_SKIP_EXECUTION;

M
mvandervoord 已提交
544
    if (style == UNITY_DISPLAY_STYLE_INT)
G
greg-williams 已提交
545
    {
M
mvandervoord 已提交
546 547 548 549
        if (actual > expected)
          Unity.CurrentTestFailed = ((actual - expected) > delta);
        else
          Unity.CurrentTestFailed = ((expected - actual) > delta);
G
greg-williams 已提交
550
    }
M
mvandervoord 已提交
551
    else
G
greg-williams 已提交
552
    {
553 554
        if ((_UU32)actual > (_UU32)expected)
            Unity.CurrentTestFailed = ((_UU32)(actual - expected) > (_UU32)delta);
M
mvandervoord 已提交
555
        else
556
            Unity.CurrentTestFailed = ((_UU32)(expected - actual) > (_UU32)delta);
557 558
    }

M
mvandervoord 已提交
559
    if (Unity.CurrentTestFailed)
560
    {
561
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
562 563 564 565 566 567 568 569
        UnityPrint(UnityStrDelta);
        UnityPrintNumberByStyle(delta, style);
        UnityPrint(UnityStrExpected);
        UnityPrintNumberByStyle(expected, style);
        UnityPrint(UnityStrWas);
        UnityPrintNumberByStyle(actual, style);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
570 571 572
    }
}

M
mvandervoord 已提交
573
//-----------------------------------------------
574 575 576
void UnityAssertEqualString(const char* expected,
                            const char* actual,
                            const char* msg,
M
mvandervoord 已提交
577
                            const UNITY_LINE_TYPE lineNumber)
G
greg-williams 已提交
578
{
579
    _UU32 i;
G
greg-williams 已提交
580

581 582
    UNITY_SKIP_EXECUTION;
  
G
greg-williams 已提交
583 584 585 586 587 588 589 590
    // 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;
591
                break;
G
greg-williams 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604
            }
        }
    }
    else
    { // handle case of one pointers being null (if both null, test should pass)
        if (expected != actual)
        {
            Unity.CurrentTestFailed = 1;
        }
    }

    if (Unity.CurrentTestFailed)
    {
605
      UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
606
      UnityPrintExpectedAndActualStrings(expected, actual);
607 608
      UnityAddMsgIfSpecified(msg);
      UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
609 610 611
    }
}

M
mvandervoord 已提交
612 613 614 615 616 617 618 619 620
//-----------------------------------------------
void UnityAssertEqualStringArray( const char** expected,
                                  const char** actual,
                                  const _UU32 num_elements,
                                  const char* msg,
                                  const UNITY_LINE_TYPE lineNumber)
{
    _UU32 i, j = 0;
    
621 622
    UNITY_SKIP_EXECUTION;
  
M
mvandervoord 已提交
623 624 625 626 627 628 629 630 631
    // if no elements, it's an error
    if (num_elements == 0)
    {
        UnityTestResultsFailBegin(lineNumber);
        UnityPrint(UnityStrPointless);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
    }

632 633
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;
M
mvandervoord 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
    
    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 已提交
665
            UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j]));
M
mvandervoord 已提交
666 667 668 669 670 671 672
            UnityAddMsgIfSpecified(msg);
            UNITY_FAIL_AND_BAIL;
        } 
    } while (++j < num_elements);
}

//-----------------------------------------------
M
mvandervoord 已提交
673 674
void UnityAssertEqualMemory( const void* expected,
                             const void* actual,
675 676
                             _UU32 length,
                             _UU32 num_elements,
M
mvandervoord 已提交
677 678
                             const char* msg,
                             const UNITY_LINE_TYPE lineNumber)
679
{
M
mvandervoord 已提交
680 681
    unsigned char* expected_ptr = (unsigned char*)expected;
    unsigned char* actual_ptr = (unsigned char*)actual;
682
    _UU32 elements = num_elements;
683 684 685

    UNITY_SKIP_EXECUTION;
  
686 687
    if ((elements == 0) || (length == 0))
    {
688
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
689 690 691
        UnityPrint(UnityStrPointless);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
692
    }
693

694 695 696 697
    if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
        return;
        
    while (elements--)
698
    {
699
        if (memcmp((const void*)expected_ptr, (const void*)actual_ptr, length) != 0)
700 701
        {
            Unity.CurrentTestFailed = 1;
702
            break;
703
        }
704 705
        expected_ptr += length;
        actual_ptr += length;
706 707 708 709
    }

    if (Unity.CurrentTestFailed)
    {
710
        UnityTestResultsFailBegin(lineNumber);
M
mvandervoord 已提交
711
        if (num_elements > 1)
712
        {
M
mvandervoord 已提交
713 714
            UnityPrint(UnityStrElement);
            UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
715
        }
M
mvandervoord 已提交
716 717 718
        UnityPrint(UnityStrMemory);
        UnityAddMsgIfSpecified(msg);
        UNITY_FAIL_AND_BAIL;
719 720 721
    }
}

M
mvandervoord 已提交
722 723 724 725 726
//-----------------------------------------------
// Control Functions
//-----------------------------------------------

void UnityFail(const char* msg, const UNITY_LINE_TYPE line)
G
greg-williams 已提交
727
{
728 729
    UNITY_SKIP_EXECUTION;

M
mvandervoord 已提交
730
    UnityTestResultsBegin(Unity.TestFile, line);
731
    UnityPrint("FAIL");
M
mvandervoord 已提交
732
    if (msg != NULL)
733 734
    {
      UNITY_OUTPUT_CHAR(':');
735 736
      if (msg[0] != ' ')
      {
M
mvandervoord 已提交
737 738
        UNITY_OUTPUT_CHAR(' ');  
      }
M
mvandervoord 已提交
739
      UnityPrint(msg);
740
    }
M
mvandervoord 已提交
741
    UNITY_FAIL_AND_BAIL;
G
greg-williams 已提交
742 743
}

M
mvandervoord 已提交
744
//-----------------------------------------------
M
mvandervoord 已提交
745
void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
G
greg-williams 已提交
746
{
747 748
    UNITY_SKIP_EXECUTION;

M
mvandervoord 已提交
749
    UnityTestResultsBegin(Unity.TestFile, line);
750
    UnityPrint("IGNORE");
M
mvandervoord 已提交
751
    if (msg != NULL)
752 753
    {
      UNITY_OUTPUT_CHAR(':');
754
      UNITY_OUTPUT_CHAR(' ');
M
mvandervoord 已提交
755
      UnityPrint(msg);
756
    }
M
mvandervoord 已提交
757
    UNITY_IGNORE_AND_BAIL;
G
greg-williams 已提交
758 759
}

M
mvandervoord 已提交
760
//-----------------------------------------------
761
void UnityBegin(void)
G
greg-williams 已提交
762 763 764 765
{
    Unity.NumberOfTests = 0;
}

M
mvandervoord 已提交
766
//-----------------------------------------------
G
greg-williams 已提交
767 768
void UnityEnd(void)
{
M
mvandervoord 已提交
769
    UnityPrint("-----------------------\n");
G
greg-williams 已提交
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
    UnityPrintNumber(Unity.NumberOfTests);
    UnityPrint(" Tests ");
    UnityPrintNumber(Unity.TestFailures);
    UnityPrint(" Failures ");
    UnityPrintNumber(Unity.TestIgnores);
    UnityPrint(" Ignored\n");
    if (Unity.TestFailures == 0U)
    {
        UnityPrint("OK\n");
    }
    else
    {
        UnityPrint("FAIL\n");
    }
}