unity.c 13.9 KB
Newer Older
G
greg-williams 已提交
1 2
#include "unity.h"
#include <stdio.h>
M
mvandervoord 已提交
3
#include <string.h>
G
greg-williams 已提交
4

5 6 7 8 9 10 11 12 13 14 15 16
struct _Unity Unity =
{
    NULL,
    NULL,
    0,
    0,
    0,
    0,
    0,
    1e-4f,
    {0},
};
G
greg-williams 已提交
17

18
void UnityPrintChar(const char ch)
G
greg-williams 已提交
19 20 21 22
{
    putchar(ch);
}

23
void UnityPrint(const char* string)
G
greg-williams 已提交
24
{
25
    unsigned char* pch = (unsigned char*)string;
G
greg-williams 已提交
26 27 28 29 30 31 32 33 34 35 36

    if (pch != NULL)
    {
        while (*pch)
        {
            UnityPrintChar(*pch);
            pch++;
        }
    }
}

37
void UnityPrintNumberByStyle(const long number, const UNITY_DISPLAY_STYLE_T style)
G
greg-williams 已提交
38 39 40
{
    switch (style)
    {
M
mvandervoord 已提交
41 42 43 44 45
    case UNITY_DISPLAY_STYLE_HEX8:    UnityPrintNumberHex((unsigned long)number, 2);           break;
    case UNITY_DISPLAY_STYLE_HEX16:   UnityPrintNumberHex((unsigned long)number, 4);           break;
    case UNITY_DISPLAY_STYLE_HEX32:   UnityPrintNumberHex((unsigned long)number, 8);           break;
    case UNITY_DISPLAY_STYLE_UINT:    UnityPrintNumberUnsigned((unsigned long)number);         break;
    default:                          UnityPrintNumber(number);                                break;
G
greg-williams 已提交
46 47 48 49
    }
}

/// basically do an itoa using as little ram as possible
50
void UnityPrintNumber(const long number_to_print)
G
greg-williams 已提交
51
{
M
mvandervoord 已提交
52 53
    long divisor = 1;
    long next_divisor;
54
    long number = number_to_print;
G
greg-williams 已提交
55 56 57 58 59 60 61 62

    if (number < 0)
    {
        UnityPrintChar('-');
        number = -number;
    }

    // figure out initial divisor
M
mvandervoord 已提交
63
    while (number / divisor > 9)
G
greg-williams 已提交
64
    {
M
mvandervoord 已提交
65 66 67 68 69
        next_divisor = divisor * 10;
        if (next_divisor > divisor)
            divisor = next_divisor;
        else
            break;
G
greg-williams 已提交
70 71
    }

M
mvandervoord 已提交
72
    // now mod and print, then divide divisor
G
greg-williams 已提交
73 74 75
    do
    {
        UnityPrintChar((char)('0' + (number / divisor % 10)));
M
mvandervoord 已提交
76
        divisor /= 10;
G
greg-williams 已提交
77
    }
M
mvandervoord 已提交
78
    while (divisor > 0);
G
greg-williams 已提交
79 80 81
}

/// basically do an itoa using as little ram as possible
82
void UnityPrintNumberUnsigned(const unsigned long number)
G
greg-williams 已提交
83
{
M
mvandervoord 已提交
84 85
    unsigned long divisor = 1;
    unsigned long next_divisor;
G
greg-williams 已提交
86 87

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

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

106
void UnityPrintNumberHex(const unsigned long number, const char nibbles_to_print)
G
greg-williams 已提交
107 108
{
    unsigned long nibble;
109
	char nibbles = nibbles_to_print;
G
greg-williams 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    UnityPrint("0x");

    while (nibbles > 0)
    {
        nibble = (number >> (--nibbles << 2)) & 0x0000000F;
        if (nibble <= 9)
        {
            UnityPrintChar((char)('0' + nibble));
        }
        else
        {
            UnityPrintChar((char)('A' - 10 + nibble));
        }
    }
}

126
void UnityPrintMask(const unsigned long mask, const unsigned long number)
G
greg-williams 已提交
127 128
{
    unsigned long bit = 0x00000001;
M
mvandervoord 已提交
129
    long i;
G
greg-williams 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

    for (i = 0; i < 32; i++)
    {
        if (bit & mask)
        {
            if (bit & number)
            {
                UnityPrintChar('1');
            }
            else
            {
                UnityPrintChar('0');
            }
        }
        else
        {
            UnityPrintChar('X');
        }
        bit = bit << 1;
    }
}

M
mvandervoord 已提交
152
void UnityTestResultsBegin(const long line)
G
greg-williams 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
{
    UnityPrint(Unity.TestFile);
    UnityPrintChar(':');
    UnityPrintNumber(line);
    UnityPrintChar(':');
    UnityPrint(Unity.CurrentTestName);
    UnityPrintChar(':');
}

void UnityConcludeTest()
{
    if (Unity.CurrentTestIgnored)
    {
        Unity.TestIgnores++;
    }
    else if (!Unity.CurrentTestFailed)
    {
        UnityPrint(Unity.CurrentTestName);
        UnityPrint("::: PASS\n");
    }
    else
    {
        Unity.TestFailures++;
    }

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

M
mvandervoord 已提交
182 183 184
void UnityAssertBits(const long mask,
                     const long expected,
                     const long actual,
185
                     const char* msg,
186
                     const unsigned short lineNumber)
G
greg-williams 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
{
    if ((mask & expected) != (mask & actual))
    {
        Unity.CurrentTestFailed = 1;

        UnityTestResultsBegin(lineNumber);
        UnityPrint("Expected ");
        UnityPrintMask(mask, expected);
        UnityPrint(" was ");
        UnityPrintMask(mask, actual);
        UnityPrintChar('.');
        if (msg)
        {
            UnityPrintChar(' ');
            UnityPrint(msg);
        }
        UnityPrintChar('\n');
    }
}

207
void UnityAssertEqualNumber(const long expected,
M
mvandervoord 已提交
208
                         const long actual,
209
                         const char* msg,
210 211
                         const unsigned short lineNumber,
                         const UNITY_DISPLAY_STYLE_T style)
G
greg-williams 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
{
    if (expected != actual)
    {
        Unity.CurrentTestFailed = 1;

        UnityTestResultsBegin(lineNumber);
        UnityPrint("Expected ");
        UnityPrintNumberByStyle(expected, style);
        UnityPrint(" was ");
        UnityPrintNumberByStyle(actual, style);
        UnityPrintChar('.');
        if (msg)
        {
            UnityPrintChar(' ');
            UnityPrint(msg);
        }
        UnityPrintChar('\n');
    }
}

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 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 306 307 308 309
void UnityAssertEqualNumberUnsigned(const unsigned long expected,
                         const unsigned long actual,
                         const char* msg,
                         const unsigned short lineNumber,
                         const UNITY_DISPLAY_STYLE_T style)
{
    if (expected != actual)
    {
        Unity.CurrentTestFailed = 1;

        UnityTestResultsBegin(lineNumber);
        UnityPrint("Expected ");
        UnityPrintNumberByStyle(expected, style);
        UnityPrint(" was ");
        UnityPrintNumberByStyle(actual, style);
        UnityPrintChar('.');
        if (msg)
        {
            UnityPrintChar(' ');
            UnityPrint(msg);
        }
        UnityPrintChar('\n');
    }
}

void UnityAssertEqualIntArray(const int* expected,
                              const int* actual,
                              const unsigned long num_elements,
                              const char* msg,
                              const unsigned short lineNumber,
                              const UNITY_DISPLAY_STYLE_T style)
{
    unsigned long elements = num_elements;
    const int* ptr_expected = expected;
    const int* ptr_actual = actual;

    if (elements == 0)
    {
        Unity.CurrentTestFailed = 1;

        UnityTestResultsBegin(lineNumber);
        UnityPrint("You asked me to compare 0 elements of an array, which was pointless.");
        if (msg)
        {
            UnityPrintChar(' ');
            UnityPrint(msg);
        }
        UnityPrintChar('\n');
        return;
    }

    while (elements--)
    {
        if (*ptr_expected++ != *ptr_actual++)
        {
            Unity.CurrentTestFailed = 1;

            UnityTestResultsBegin(lineNumber);
            UnityPrint("Element ");
            UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
            UnityPrint(" Expected ");
            UnityPrintNumberByStyle(*--ptr_expected, style);
            UnityPrint(" was ");
            UnityPrintNumberByStyle(*--ptr_actual, style);
            UnityPrintChar('.');
            if (msg)
            {
                UnityPrintChar(' ');
                UnityPrint(msg);
            }
            UnityPrintChar('\n');
            return;
        }
    }
}

void UnityAssertEqualUnsignedIntArray(const unsigned int* expected,
                              const unsigned int* actual,
M
mvandervoord 已提交
310 311 312 313 314 315
                              const unsigned long num_elements,
                              const char* msg,
                              const unsigned short lineNumber,
                              const UNITY_DISPLAY_STYLE_T style)
{
    unsigned long elements = num_elements;
316 317
    const unsigned int* ptr_expected = expected;
    const unsigned int* ptr_actual = actual;
M
mvandervoord 已提交
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 345 346 347 348 349 350 351 352 353 354 355 356 357 358

    if (elements == 0)
    {
        Unity.CurrentTestFailed = 1;

        UnityTestResultsBegin(lineNumber);
        UnityPrint("You asked me to compare 0 elements of an array, which was pointless.");
        if (msg)
        {
            UnityPrintChar(' ');
            UnityPrint(msg);
        }
        UnityPrintChar('\n');
        return;
    }

    while (elements--)
    {
        if (*ptr_expected++ != *ptr_actual++)
        {
            Unity.CurrentTestFailed = 1;

            UnityTestResultsBegin(lineNumber);
            UnityPrint("Element ");
            UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
            UnityPrint(" Expected ");
            UnityPrintNumberByStyle(*--ptr_expected, style);
            UnityPrint(" was ");
            UnityPrintNumberByStyle(*--ptr_actual, style);
            UnityPrintChar('.');
            if (msg)
            {
                UnityPrintChar(' ');
                UnityPrint(msg);
            }
            UnityPrintChar('\n');
            return;
        }
    }
}

359 360 361
void UnityAssertFloatsWithin(const float delta,
                             const float expected,
                             const float actual,
362
                             const char* msg,
363
                             const unsigned short lineNumber)
G
greg-williams 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
{
    float diff = actual - expected;

    if (diff < 0)
    {
        diff = -diff;
    }

    if (delta < diff)
    {
        Unity.CurrentTestFailed = 1;
        UnityTestResultsBegin(lineNumber);
        UnityPrint("Floats not within delta.");
        if (msg)
        {
            UnityPrintChar(' ');
            UnityPrint(msg);
        }
        UnityPrintChar('\n');
    }
}

386
void UnityAssertNumbersWithin(const long delta,
M
mvandervoord 已提交
387 388
                           const long expected,
                           const long actual,
389
                           const char* msg,
390
                           const unsigned short lineNumber)
G
greg-williams 已提交
391
{
392
    int diff = actual - expected;
G
greg-williams 已提交
393 394 395 396 397 398 399 400 401 402

    if (diff < 0)
    {
        diff = -diff;
    }

    if (delta < diff)
    {
        Unity.CurrentTestFailed = 1;
        UnityTestResultsBegin(lineNumber);
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
        UnityPrint("Values not within delta.");
        if (msg)
        {
            UnityPrintChar(' ');
            UnityPrint(msg);
        }
        UnityPrintChar('\n');
    }
}

void UnityAssertNumbersUnsignedWithin(const unsigned long delta,
                           const unsigned long expected,
                           const unsigned long actual,
                           const char* msg,
                           const unsigned short lineNumber)
{
    unsigned int diff = actual - expected;

    if (delta < diff)
    {
        Unity.CurrentTestFailed = 1;
        UnityTestResultsBegin(lineNumber);
        UnityPrint("Values not within delta.");
G
greg-williams 已提交
426 427 428 429 430 431 432 433 434
        if (msg)
        {
            UnityPrintChar(' ');
            UnityPrint(msg);
        }
        UnityPrintChar('\n');
    }
}

435 436 437
void UnityAssertEqualString(const char* expected,
                            const char* actual,
                            const char* msg,
438
                            const unsigned short lineNumber)
G
greg-williams 已提交
439
{
M
mvandervoord 已提交
440
    unsigned long i;
G
greg-williams 已提交
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470

    // 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;
            }
        }
    }
    else
    { // handle case of one pointers being null (if both null, test should pass)
        if (expected != actual)
        {
            Unity.CurrentTestFailed = 1;
        }
    }

    if (Unity.CurrentTestFailed)
    {
        UnityTestResultsBegin(lineNumber);
        UnityPrint("Expected '");
        UnityPrint(expected);
        UnityPrint("' was '");
        UnityPrint(actual);
        UnityPrintChar('\'');
        UnityPrintChar('.');
        if (msg)
471 472 473 474 475 476 477 478 479
        {
            UnityPrintChar(' ');
            UnityPrint(msg);
        }
        UnityPrintChar('\n');
    }
}


M
mvandervoord 已提交
480 481 482
void UnityAssertEqualMemory(const void* expected,
                            const void* actual,
                            unsigned long length,
483
                            const char* msg,
484
                            const unsigned short lineNumber)
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
{
    if (length == 0)
        return;

    // if both pointers not null compare the memory
    if (expected && actual)
    {
        if (memcmp(expected, actual, length) != 0)
        {
            Unity.CurrentTestFailed = 1;
        }
    }
    else
    { // handle case of one pointers being null (if both null, test should pass)
        if (expected != actual)
        {
            Unity.CurrentTestFailed = 1;
        }
    }

    if (Unity.CurrentTestFailed)
    {
        UnityTestResultsBegin(lineNumber);
508
        UnityPrint("Memory Mismatch.");
509
        if (msg)
G
greg-williams 已提交
510 511 512 513 514 515 516 517
        {
            UnityPrintChar(' ');
            UnityPrint(msg);
        }
        UnityPrintChar('\n');
    }
}

M
mvandervoord 已提交
518
void UnityFail(const char* message, const long line)
G
greg-williams 已提交
519 520 521 522 523 524 525
{
    Unity.CurrentTestFailed = 1;
    UnityTestResultsBegin(line);
    UnityPrint(message);
    UnityPrintChar('\n');
}

M
mvandervoord 已提交
526
void UnityIgnore(const char* message, const long line)
G
greg-williams 已提交
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
{
    Unity.CurrentTestIgnored = 1;
    UnityTestResultsBegin(line);
    UnityPrint(message);
    UnityPrint(" IGNORED\n");
}

void UnityBegin()
{
    Unity.NumberOfTests = 0;
}

void UnityEnd(void)
{
    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");
    }
}