unity_fixture.c 10.3 KB
Newer Older
J
jsalling 已提交
1 2 3 4 5 6
/* Copyright (c) 2010 James Grenning and Contributed to Unity Project
 * ==========================================
 *  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]
 * ========================================== */
7 8 9

#include "unity_fixture.h"
#include "unity_internals.h"
10
#include <string.h>
11

12
struct UNITY_FIXTURE_T UnityFixture;
13

J
jsalling 已提交
14 15 16
/* If you decide to use the function pointer approach.
 * Build with -D UNITY_OUTPUT_CHAR=outputChar and include <stdio.h>
 * int (*outputChar)(int) = putchar; */
17

18
#if !defined(UNITY_WEAK_ATTRIBUTE) && !defined(UNITY_WEAK_PRAGMA)
19 20
void setUp(void)    { /*does nothing*/ }
void tearDown(void) { /*does nothing*/ }
21
#endif
22

23
static void announceTestRun(unsigned int runNumber)
24 25
{
    UnityPrint("Unity test run ");
26
    UnityPrintNumberUnsigned(runNumber+1);
27
    UnityPrint(" of ");
28
    UnityPrintNumberUnsigned(UnityFixture.RepeatCount);
29
    UNITY_PRINT_EOL();
30 31
}

32
int UnityMain(int argc, const char* argv[], void (*runAllTests)(void))
33 34
{
    int result = UnityGetCommandLineOptions(argc, argv);
35
    unsigned int r;
36 37 38 39 40
    if (result != 0)
        return result;

    for (r = 0; r < UnityFixture.RepeatCount; r++)
    {
41
        UnityBegin(argv[0]);
42
        announceTestRun(r);
43
        runAllTests();
44
        if (!UnityFixture.Verbose) UNITY_PRINT_EOL();
45 46 47
        UnityEnd();
    }

48
    return (int)Unity.TestFailures;
49 50
}

51
static int selected(const char* filter, const char* name)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
{
    if (filter == 0)
        return 1;
    return strstr(name, filter) ? 1 : 0;
}

static int testSelected(const char* test)
{
    return selected(UnityFixture.NameFilter, test);
}

static int groupSelected(const char* group)
{
    return selected(UnityFixture.GroupFilter, group);
}

void UnityTestRunner(unityfunction* setup,
69 70 71 72 73
                     unityfunction* testBody,
                     unityfunction* teardown,
                     const char* printableName,
                     const char* group,
                     const char* name,
74 75
                     const char* file,
                     unsigned int line)
76 77 78 79 80 81 82 83 84
{
    if (testSelected(name) && groupSelected(group))
    {
        Unity.TestFile = file;
        Unity.CurrentTestName = printableName;
        Unity.CurrentTestLineNumber = line;
        if (!UnityFixture.Verbose)
            UNITY_OUTPUT_CHAR('.');
        else
85
        {
86
            UnityPrint(printableName);
87 88 89 90
        #ifndef UNITY_REPEAT_TEST_NAME
            Unity.CurrentTestName = NULL;
        #endif
        }
91 92 93 94 95

        Unity.NumberOfTests++;
        UnityMalloc_StartTest();
        UnityPointer_Init();

96 97
        UNITY_EXEC_TIME_START();

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        if (TEST_PROTECT())
        {
            setup();
            testBody();
        }
        if (TEST_PROTECT())
        {
            teardown();
        }
        if (TEST_PROTECT())
        {
            UnityPointer_UndoAllSets();
            if (!Unity.CurrentTestFailed)
                UnityMalloc_EndTest();
        }
113
        UnityConcludeFixtureTest();
114 115 116
    }
}

117
void UnityIgnoreTest(const char* printableName, const char* group, const char* name)
118
{
119
    if (testSelected(name) && groupSelected(group))
120 121
    {
        Unity.NumberOfTests++;
122
        Unity.TestIgnores++;
123 124 125
        if (!UnityFixture.Verbose)
            UNITY_OUTPUT_CHAR('!');
        else
126
        {
127
            UnityPrint(printableName);
128 129
            UNITY_PRINT_EOL();
        }
130
    }
131 132
}

133

J
jsalling 已提交
134 135
/*------------------------------------------------- */
/* Malloc and free stuff */
136 137 138 139
#define MALLOC_DONT_FAIL -1
static int malloc_count;
static int malloc_fail_countdown = MALLOC_DONT_FAIL;

140
void UnityMalloc_StartTest(void)
141 142 143 144 145
{
    malloc_count = 0;
    malloc_fail_countdown = MALLOC_DONT_FAIL;
}

146
void UnityMalloc_EndTest(void)
147 148 149 150
{
    malloc_fail_countdown = MALLOC_DONT_FAIL;
    if (malloc_count != 0)
    {
151
        UNITY_TEST_FAIL(Unity.CurrentTestLineNumber, "This test leaks!");
152 153 154 155 156 157 158 159
    }
}

void UnityMalloc_MakeMallocFailAfterCount(int countdown)
{
    malloc_fail_countdown = countdown;
}

J
jsalling 已提交
160 161
/* These definitions are always included from unity_fixture_malloc_overrides.h */
/* We undef to use them or avoid conflict with <stdlib.h> per the C standard */
162 163
#undef malloc
#undef free
@
@gageas 已提交
164 165 166
#undef calloc
#undef realloc

167 168
#ifdef UNITY_EXCLUDE_STDLIB_MALLOC
static unsigned char unity_heap[UNITY_INTERNAL_HEAP_SIZE_BYTES];
169
static size_t heap_index;
170
#else
171
#include <stdlib.h>
172
#endif
173 174 175

typedef struct GuardBytes
{
176
    size_t size;
177
    size_t guard_space;
178 179 180
} Guard;


181
static const char end[] = "END";
182

183
void* unity_malloc(size_t size)
184 185 186
{
    char* mem;
    Guard* guard;
187
    size_t total_size = size + sizeof(Guard) + sizeof(end);
188 189 190 191

    if (malloc_fail_countdown != MALLOC_DONT_FAIL)
    {
        if (malloc_fail_countdown == 0)
192
            return NULL;
193 194 195
        malloc_fail_countdown--;
    }

196
    if (size == 0) return NULL;
197 198 199 200 201 202 203
#ifdef UNITY_EXCLUDE_STDLIB_MALLOC
    if (heap_index + total_size > UNITY_INTERNAL_HEAP_SIZE_BYTES)
    {
        guard = NULL;
    }
    else
    {
204
        guard = (Guard*)&unity_heap[heap_index];
205 206 207 208 209
        heap_index += total_size;
    }
#else
    guard = (Guard*)UNITY_FIXTURE_MALLOC(total_size);
#endif
210
    if (guard == NULL) return NULL;
211
    malloc_count++;
212
    guard->size = size;
213
    guard->guard_space = 0;
214
    mem = (char*)&(guard[1]);
215
    memcpy(&mem[size], end, sizeof(end));
216 217 218 219

    return (void*)mem;
}

220
static int isOverrun(void* mem)
221 222 223 224
{
    Guard* guard = (Guard*)mem;
    char* memAsChar = (char*)mem;
    guard--;
225

226
    return guard->guard_space != 0 || strcmp(&memAsChar[guard->size], end) != 0;
227 228
}

229
static void release_memory(void* mem)
230 231 232 233 234
{
    Guard* guard = (Guard*)mem;
    guard--;

    malloc_count--;
235 236 237 238 239 240
#ifdef UNITY_EXCLUDE_STDLIB_MALLOC
    if (mem == unity_heap + heap_index - guard->size - sizeof(end))
    {
        heap_index -= (guard->size + sizeof(Guard) + sizeof(end));
    }
#else
241
    UNITY_FIXTURE_FREE(guard);
242
#endif
243 244
}

245
void unity_free(void* mem)
246
{
247 248 249 250 251 252 253
    int overrun;

    if (mem == NULL)
    {
        return;
    }

254
    overrun = isOverrun(mem);
255 256
    release_memory(mem);
    if (overrun)
257
    {
258
        UNITY_TEST_FAIL(Unity.CurrentTestLineNumber, "Buffer overrun detected during free()");
259 260 261 262 263 264
    }
}

void* unity_calloc(size_t num, size_t size)
{
    void* mem = unity_malloc(num * size);
265
    if (mem == NULL) return NULL;
266
    memset(mem, 0, num * size);
267 268 269
    return mem;
}

270
void* unity_realloc(void* oldMem, size_t size)
271 272 273 274
{
    Guard* guard = (Guard*)oldMem;
    void* newMem;

275
    if (oldMem == NULL) return unity_malloc(size);
276 277

    guard--;
278
    if (isOverrun(oldMem))
279
    {
280
        release_memory(oldMem);
281
        UNITY_TEST_FAIL(Unity.CurrentTestLineNumber, "Buffer overrun detected during realloc()");
282 283 284 285
    }

    if (size == 0)
    {
286
        release_memory(oldMem);
287
        return NULL;
288 289
    }

290
    if (guard->size >= size) return oldMem;
291

J
jsalling 已提交
292
#ifdef UNITY_EXCLUDE_STDLIB_MALLOC /* Optimization if memory is expandable */
293 294 295
    if (oldMem == unity_heap + heap_index - guard->size - sizeof(end) &&
        heap_index + size - guard->size <= UNITY_INTERNAL_HEAP_SIZE_BYTES)
    {
296
        release_memory(oldMem);    /* Not thread-safe, like unity_heap generally */
J
jsalling 已提交
297
        return unity_malloc(size); /* No memcpy since data is in place */
298 299
    }
#endif
300
    newMem = unity_malloc(size);
J
jsalling 已提交
301
    if (newMem == NULL) return NULL; /* Do not release old memory */
302
    memcpy(newMem, oldMem, guard->size);
303
    release_memory(oldMem);
304 305 306 307
    return newMem;
}


J
jsalling 已提交
308 309
/*-------------------------------------------------------- */
/*Automatic pointer restoration functions */
310
struct PointerPair
311
{
312 313
    void** pointer;
    void* old_value;
314
};
315

316
static struct PointerPair pointer_store[UNITY_MAX_POINTERS];
317 318
static int pointer_index = 0;

319
void UnityPointer_Init(void)
320 321 322 323
{
    pointer_index = 0;
}

324
void UnityPointer_Set(void** pointer, void* newValue, UNITY_LINE_TYPE line)
325
{
326
    if (pointer_index >= UNITY_MAX_POINTERS)
327
    {
328
        UNITY_TEST_FAIL(line, "Too many pointers set");
329 330 331 332 333 334 335 336
    }
    else
    {
        pointer_store[pointer_index].pointer = pointer;
        pointer_store[pointer_index].old_value = *pointer;
        *pointer = newValue;
        pointer_index++;
    }
337 338
}

339
void UnityPointer_UndoAllSets(void)
340 341 342 343 344
{
    while (pointer_index > 0)
    {
        pointer_index--;
        *(pointer_store[pointer_index].pointer) =
345
            pointer_store[pointer_index].old_value;
346 347 348
    }
}

349
int UnityGetCommandLineOptions(int argc, const char* argv[])
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
{
    int i;
    UnityFixture.Verbose = 0;
    UnityFixture.GroupFilter = 0;
    UnityFixture.NameFilter = 0;
    UnityFixture.RepeatCount = 1;

    if (argc == 1)
        return 0;

    for (i = 1; i < argc; )
    {
        if (strcmp(argv[i], "-v") == 0)
        {
            UnityFixture.Verbose = 1;
            i++;
        }
        else if (strcmp(argv[i], "-g") == 0)
        {
            i++;
            if (i >= argc)
                return 1;
            UnityFixture.GroupFilter = argv[i];
            i++;
        }
        else if (strcmp(argv[i], "-n") == 0)
        {
            i++;
            if (i >= argc)
                return 1;
            UnityFixture.NameFilter = argv[i];
            i++;
        }
        else if (strcmp(argv[i], "-r") == 0)
        {
            UnityFixture.RepeatCount = 2;
            i++;
            if (i < argc)
            {
                if (*(argv[i]) >= '0' && *(argv[i]) <= '9')
                {
391 392 393 394 395 396 397
                    unsigned int digit = 0;
                    UnityFixture.RepeatCount = 0;
                    while (argv[i][digit] >= '0' && argv[i][digit] <= '9')
                    {
                        UnityFixture.RepeatCount *= 10;
                        UnityFixture.RepeatCount += (unsigned int)argv[i][digit++] - '0';
                    }
398 399 400
                    i++;
                }
            }
401 402 403
        }
        else
        {
J
jsalling 已提交
404
            /* ignore unknown parameter */
405
            i++;
406 407 408 409 410
        }
    }
    return 0;
}

411
void UnityConcludeFixtureTest(void)
412 413 414 415
{
    if (Unity.CurrentTestIgnored)
    {
        Unity.TestIgnores++;
416
        UNITY_PRINT_EOL();
417 418 419 420 421
    }
    else if (!Unity.CurrentTestFailed)
    {
        if (UnityFixture.Verbose)
        {
422 423
            UnityPrint(" ");
            UnityPrint(UnityStrPass);
424 425
            UNITY_EXEC_TIME_STOP();
            UNITY_PRINT_EXEC_TIME();
426
            UNITY_PRINT_EOL();
427 428
        }
    }
J
jsalling 已提交
429
    else /* Unity.CurrentTestFailed */
430 431
    {
        Unity.TestFailures++;
432
        UNITY_PRINT_EOL();
433 434 435 436 437
    }

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