unity_fixture.c 8.6 KB
Newer Older
1 2 3 4 5 6 7
//- 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]
========================================== */

8
#include <string.h>
K
kotofos 已提交
9
#include <stdio.h>
10 11 12 13 14 15 16 17 18 19
#include "unity_fixture.h"
#include "unity_internals.h"

UNITY_FIXTURE_T UnityFixture;

//If you decide to use the function pointer approach.
int (*outputChar)(int) = putchar;

int verbose = 0;

20 21
void setUp(void);
void tearDown(void);
22 23 24
void setUp(void)    { /*does nothing*/ }
void tearDown(void) { /*does nothing*/ }

25
static void announceTestRun(unsigned int runNumber)
26 27 28 29 30 31 32 33
{
    UnityPrint("Unity test run ");
    UnityPrintNumber(runNumber+1);
    UnityPrint(" of ");
    UnityPrintNumber(UnityFixture.RepeatCount);
    UNITY_OUTPUT_CHAR('\n');
}

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

    for (r = 0; r < UnityFixture.RepeatCount; r++)
    {
43
        UnityBegin(argv[0]);
44
        announceTestRun(r);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        runAllTests();
        UNITY_OUTPUT_CHAR('\n');
        UnityEnd();
    }

    return UnityFailureCount();
}

static int selected(const char * filter, const char * name)
{
    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);
}

70
static void runTestCase(void)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
{

}

void UnityTestRunner(unityfunction* setup,
        unityfunction* testBody,
        unityfunction* teardown,
        const char * printableName,
        const char * group,
        const char * name,
        const char * file, int line)
{
    if (testSelected(name) && groupSelected(group))
    {
        Unity.CurrentTestFailed = 0;
        Unity.TestFile = file;
        Unity.CurrentTestName = printableName;
        Unity.CurrentTestLineNumber = line;
        if (!UnityFixture.Verbose)
            UNITY_OUTPUT_CHAR('.');
        else
            UnityPrint(printableName);

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

        runTestCase();
        if (TEST_PROTECT())
        {
            setup();
            testBody();
        }
        if (TEST_PROTECT())
        {
            teardown();
        }
        if (TEST_PROTECT())
        {
            UnityPointer_UndoAllSets();
            if (!Unity.CurrentTestFailed)
                UnityMalloc_EndTest();
        }
114
        UnityConcludeFixtureTest();
115 116 117
    }
}

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

132 133 134 135 136 137 138 139

//-------------------------------------------------
//Malloc and free stuff
//
#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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
{
    malloc_fail_countdown = MALLOC_DONT_FAIL;
    if (malloc_count != 0)
    {
        TEST_FAIL_MESSAGE("This test leaks!");
    }
}

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

#ifdef malloc
#undef malloc
#endif

#ifdef free
#undef free
#endif

@
@gageas 已提交
168 169 170 171 172 173 174 175
#ifdef calloc
#undef calloc
#endif

#ifdef realloc
#undef realloc
#endif

176 177 178 179 180
#include <stdlib.h>
#include <string.h>

typedef struct GuardBytes
{
181 182
    size_t size;
    char guard[sizeof(size_t)];
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
} Guard;


static const char * end = "END";

void * unity_malloc(size_t size)
{
    char* mem;
    Guard* guard;

    if (malloc_fail_countdown != MALLOC_DONT_FAIL)
    {
        if (malloc_fail_countdown == 0)
            return 0;
        malloc_fail_countdown--;
    }

    malloc_count++;

202
    guard = (Guard*)UNITY_FIXTURE_MALLOC(size + sizeof(Guard) + 4);
203 204 205 206 207 208 209
    guard->size = size;
    mem = (char*)&(guard[1]);
    memcpy(&mem[size], end, strlen(end) + 1);

    return (void*)mem;
}

210
static int isOverrun(void * mem)
211 212 213 214
{
    Guard* guard = (Guard*)mem;
    char* memAsChar = (char*)mem;
    guard--;
215 216 217 218 219 220 221 222 223 224

    return strcmp(&memAsChar[guard->size], end) != 0;
}

static void release_memory(void * mem)
{
    Guard* guard = (Guard*)mem;
    guard--;

    malloc_count--;
225
    UNITY_FIXTURE_FREE(guard);
226 227 228 229
}

void unity_free(void * mem)
{
230 231 232 233 234 235 236 237
    int overrun;

    if (mem == NULL)
    {
        return;
    }

    overrun = isOverrun(mem);//strcmp(&memAsChar[guard->size], end) != 0;
238 239
    release_memory(mem);
    if (overrun)
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    {
        TEST_FAIL_MESSAGE("Buffer overrun detected during free()");
    }
}

void* unity_calloc(size_t num, size_t size)
{
    void* mem = unity_malloc(num * size);
    memset(mem, 0, num*size);
    return mem;
}

void* unity_realloc(void * oldMem, size_t size)
{
    Guard* guard = (Guard*)oldMem;
255
//    char* memAsChar = (char*)oldMem;
256 257 258 259 260 261
    void* newMem;

    if (oldMem == 0)
        return unity_malloc(size);

    guard--;
262
    if (isOverrun(oldMem))
263
    {
264
        release_memory(oldMem);
265 266 267 268 269
        TEST_FAIL_MESSAGE("Buffer overrun detected during realloc()");
    }

    if (size == 0)
    {
270
        release_memory(oldMem);
271 272 273 274 275 276 277
        return 0;
    }

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

    newMem = unity_malloc(size);
278
    memcpy(newMem, oldMem, guard->size);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    unity_free(oldMem);
    return newMem;
}


//--------------------------------------------------------
//Automatic pointer restoration functions
typedef struct _PointerPair
{
    struct _PointerPair * next;
    void ** pointer;
    void * old_value;
} PointerPair;

enum {MAX_POINTERS=50};
static PointerPair pointer_store[MAX_POINTERS];
static int pointer_index = 0;

297
void UnityPointer_Init(void)
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
{
    pointer_index = 0;
}

void UnityPointer_Set(void ** pointer, void * newValue)
{
    if (pointer_index >= MAX_POINTERS)
        TEST_FAIL_MESSAGE("Too many pointers set");

    pointer_store[pointer_index].pointer = pointer;
    pointer_store[pointer_index].old_value = *pointer;
    *pointer = newValue;
    pointer_index++;
}

313
void UnityPointer_UndoAllSets(void)
314 315 316 317 318 319 320 321 322 323
{
    while (pointer_index > 0)
    {
        pointer_index--;
        *(pointer_store[pointer_index].pointer) =
        pointer_store[pointer_index].old_value;

    }
}

324
UNITY_COUNTER_TYPE UnityFailureCount(void)
325 326 327 328
{
    return Unity.TestFailures;
}

329 330 331 332 333 334 335 336 337 338
UNITY_COUNTER_TYPE UnityIgnoreCount(void)
{
    return Unity.TestIgnores;
}

UNITY_COUNTER_TYPE UnityTestsCount(void)
{
    return Unity.NumberOfTests;
}

339
int UnityGetCommandLineOptions(int argc, const char* argv[])
340 341 342 343 344 345 346 347 348 349 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
{
    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')
                {
                    UnityFixture.RepeatCount = atoi(argv[i]);
                    i++;
                }
            }
385 386 387
        } else {
            // ignore unknown parameter
            i++;
388 389 390 391 392
        }
    }
    return 0;
}

393
void UnityConcludeFixtureTest(void)
394 395 396
{
    if (Unity.CurrentTestIgnored)
    {
397 398
        //if (UnityFixture.Verbose)
        //{
399
            UNITY_OUTPUT_CHAR('\n');
400
        //}
401 402 403 404 405 406 407 408 409 410 411 412 413
        Unity.TestIgnores++;
    }
    else if (!Unity.CurrentTestFailed)
    {
        if (UnityFixture.Verbose)
        {
            UnityPrint(" PASS");
            UNITY_OUTPUT_CHAR('\n');
        }
    }
    else if (Unity.CurrentTestFailed)
    {
        Unity.TestFailures++;
414
        UNITY_OUTPUT_CHAR('\n');
415 416 417 418 419
    }

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