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


5
struct _Unity Unity =
G
greg-williams 已提交
6
{
7
    NULL,
G
greg-williams 已提交
8 9 10 11 12 13 14
    NULL,
    0,
    0,
    0,
    0,
    0,
    1e-4f,
15
    0,
G
greg-williams 已提交
16 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 41 42 43 44 45 46 47 48 49
{
    switch (style)
    {
    case UNITY_DISPLAY_STYLE_HEX8:    UnityPrintNumberHex(number,2);            break;
    case UNITY_DISPLAY_STYLE_HEX16:   UnityPrintNumberHex(number,4);            break;
    case UNITY_DISPLAY_STYLE_HEX32:   UnityPrintNumberHex(number,8);            break;
    case UNITY_DISPLAY_STYLE_UINT:    UnityPrintNumberUnsigned(number);         break;
    default:                          UnityPrintNumber(number);                 break;
    }
}

/// basically do an itoa using as little ram as possible
50
void UnityPrintNumber(const long number_to_print)
G
greg-williams 已提交
51 52
{
    unsigned long divisor = 10;
53
    long number = number_to_print;
G
greg-williams 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

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

    // figure out initial divisor
    while (number / divisor)
    {
        divisor *= 10;
    }

    // now divide number by divisor, mod and print, then divide divisor
    do
    {
        divisor /= 10;
        UnityPrintChar((char)('0' + (number / divisor % 10)));
    }
    while (divisor > 1U);
}

/// basically do an itoa using as little ram as possible
77
void UnityPrintNumberUnsigned(const unsigned long number)
G
greg-williams 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
{
    unsigned long divisor = 10;

    // figure out initial divisor
    while (number / divisor)
    {
        divisor *= 10;
    }

    // now divide number by divisor, mod and print, then divide divisor
    do
    {
        divisor /= 10;
        UnityPrintChar((char)('0' + (number / divisor % 10)));
    }
    while (divisor > 1U);
}

96
void UnityPrintNumberHex(const unsigned long number, const char nibbles_to_print)
G
greg-williams 已提交
97 98
{
    unsigned long nibble;
99
	char nibbles = nibbles_to_print;
G
greg-williams 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    UnityPrint("0x");

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

116
void UnityPrintMask(const unsigned long mask, const unsigned long number)
G
greg-williams 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
{
    unsigned long bit = 0x00000001;
    int i;

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

142
void UnityTestResultsBegin(const int line)
G
greg-williams 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
{
    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;
}

172 173 174
void UnityAssertBits(const int mask,
                     const int expected,
                     const int actual,
175
                     const char* msg,
176
                     const unsigned short lineNumber)
G
greg-williams 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
{
    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');
    }
}

197 198
void UnityAssertEqualInt(const int expected,
                         const int actual,
199
                         const char* msg,
200 201
                         const unsigned short lineNumber,
                         const UNITY_DISPLAY_STYLE_T style)
G
greg-williams 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
{
    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');
    }
}

222 223 224
void UnityAssertFloatsWithin(const float delta,
                             const float expected,
                             const float actual,
225
                             const char* msg,
226
                             const unsigned short lineNumber)
G
greg-williams 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
{
    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');
    }
}

249 250 251
void UnityAssertIntsWithin(const int delta,
                           const int expected,
                           const int actual,
252
                           const char* msg,
253
                           const unsigned short lineNumber)
G
greg-williams 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
{
    int diff = actual - expected;

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

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

276 277 278
void UnityAssertEqualString(const char* expected,
                            const char* actual,
                            const char* msg,
279
                            unsigned short lineNumber)
G
greg-williams 已提交
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 310 311
{
    unsigned int i;

    // 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)
312 313 314 315 316 317 318 319 320
        {
            UnityPrintChar(' ');
            UnityPrint(msg);
        }
        UnityPrintChar('\n');
    }
}


321 322
void UnityAssertEqualMemory(void* expected,
                            void* actual,
323
                            unsigned int length,
324
                            const char* msg,
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
                            unsigned short lineNumber)
{
    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);
        UnityPrint("Expected '");
        UnityPrint(expected);
        UnityPrint("' was '");
        UnityPrint(actual);
        UnityPrintChar('\'');
        UnityPrintChar('.');
        if (msg)
G
greg-williams 已提交
356 357 358 359 360 361 362 363
        {
            UnityPrintChar(' ');
            UnityPrint(msg);
        }
        UnityPrintChar('\n');
    }
}

364
void UnityFail(const char* message, const int line)
G
greg-williams 已提交
365 366 367 368 369 370 371
{
    Unity.CurrentTestFailed = 1;
    UnityTestResultsBegin(line);
    UnityPrint(message);
    UnityPrintChar('\n');
}

372
void UnityIgnore(const char* message, const int line)
G
greg-williams 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
{
    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");
    }
}

403 404 405 406
int UnityGetNumFailures(void)
{
    return Unity.TestFailures;
}