1 //- Copyright (c) 2010 James Grenning and Contributed to Unity Project
2 /* ==========================================
3 Unity Project - A Test Framework for C
4 Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
5 [Released under MIT License. Please refer to license.txt for details]
6 ========================================== */
10 #include "unity_fixture.h"
11 #include "unity_internals.h"
13 UNITY_FIXTURE_T UnityFixture;
15 //If you decide to use the function pointer approach.
16 int (*outputChar)(int) = putchar;
21 static void announceTestRun(unsigned int runNumber)
23 UnityPrint("Unity test run ");
24 UnityPrintNumber(runNumber+1);
26 UnityPrintNumber(UnityFixture.RepeatCount);
27 UNITY_OUTPUT_CHAR('\n');
30 int UnityMain(int argc, const char* argv[], void (*runAllTests)(void))
32 int result = UnityGetCommandLineOptions(argc, argv);
37 for (r = 0; r < UnityFixture.RepeatCount; r++)
42 UNITY_OUTPUT_CHAR('\n');
46 return UnityFailureCount();
49 static int selected(const char * filter, const char * name)
53 return strstr(name, filter) ? 1 : 0;
56 static int testSelected(const char* test)
58 return selected(UnityFixture.NameFilter, test);
61 static int groupSelected(const char* group)
63 return selected(UnityFixture.GroupFilter, group);
66 static void runTestCase(void)
71 void UnityTestRunner(unityfunction* setup,
72 unityfunction* testBody,
73 unityfunction* teardown,
74 const char * printableName,
77 const char * file, int line)
79 if (testSelected(name) && groupSelected(group))
81 Unity.CurrentTestFailed = 0;
82 Unity.TestFile = file;
83 Unity.CurrentTestName = printableName;
84 Unity.CurrentTestLineNumber = line;
85 if (!UnityFixture.Verbose)
86 UNITY_OUTPUT_CHAR('.');
88 UnityPrint(printableName);
90 Unity.NumberOfTests++;
91 UnityMalloc_StartTest();
106 UnityPointer_UndoAllSets();
107 if (!Unity.CurrentTestFailed)
108 UnityMalloc_EndTest();
110 UnityConcludeFixtureTest();
114 void UnityIgnoreTest(const char * printableName)
116 Unity.NumberOfTests++;
117 Unity.CurrentTestIgnored = 1;
118 if (!UnityFixture.Verbose)
119 UNITY_OUTPUT_CHAR('!');
121 UnityPrint(printableName);
122 UnityConcludeFixtureTest();
126 //-------------------------------------------------
127 //Malloc and free stuff
129 #define MALLOC_DONT_FAIL -1
130 static int malloc_count;
131 static int malloc_fail_countdown = MALLOC_DONT_FAIL;
133 void UnityMalloc_StartTest(void)
136 malloc_fail_countdown = MALLOC_DONT_FAIL;
139 void UnityMalloc_EndTest(void)
141 malloc_fail_countdown = MALLOC_DONT_FAIL;
142 if (malloc_count != 0)
144 TEST_FAIL_MESSAGE("This test leaks!");
148 void UnityMalloc_MakeMallocFailAfterCount(int countdown)
150 malloc_fail_countdown = countdown;
172 typedef struct GuardBytes
175 char guard[sizeof(size_t)];
179 static const char * end = "END";
181 void * unity_malloc(size_t size)
186 if (malloc_fail_countdown != MALLOC_DONT_FAIL)
188 if (malloc_fail_countdown == 0)
190 malloc_fail_countdown--;
195 guard = (Guard*)malloc(size + sizeof(Guard) + 4);
197 mem = (char*)&(guard[1]);
198 memcpy(&mem[size], end, strlen(end) + 1);
203 static int isOverrun(void * mem)
205 Guard* guard = (Guard*)mem;
206 char* memAsChar = (char*)mem;
209 return strcmp(&memAsChar[guard->size], end) != 0;
212 static void release_memory(void * mem)
214 Guard* guard = (Guard*)mem;
221 void unity_free(void * mem)
223 int overrun = isOverrun(mem);//strcmp(&memAsChar[guard->size], end) != 0;
227 TEST_FAIL_MESSAGE("Buffer overrun detected during free()");
231 void* unity_calloc(size_t num, size_t size)
233 void* mem = unity_malloc(num * size);
234 memset(mem, 0, num*size);
238 void* unity_realloc(void * oldMem, size_t size)
240 Guard* guard = (Guard*)oldMem;
241 // char* memAsChar = (char*)oldMem;
245 return unity_malloc(size);
248 if (isOverrun(oldMem))
250 release_memory(oldMem);
251 TEST_FAIL_MESSAGE("Buffer overrun detected during realloc()");
256 release_memory(oldMem);
260 if (guard->size >= size)
263 newMem = unity_malloc(size);
264 memcpy(newMem, oldMem, guard->size);
270 //--------------------------------------------------------
271 //Automatic pointer restoration functions
272 typedef struct _PointerPair
274 struct _PointerPair * next;
279 enum {MAX_POINTERS=50};
280 static PointerPair pointer_store[MAX_POINTERS];
281 static int pointer_index = 0;
283 void UnityPointer_Init(void)
288 void UnityPointer_Set(void ** pointer, void * newValue)
290 if (pointer_index >= MAX_POINTERS)
291 TEST_FAIL_MESSAGE("Too many pointers set");
293 pointer_store[pointer_index].pointer = pointer;
294 pointer_store[pointer_index].old_value = *pointer;
299 void UnityPointer_UndoAllSets(void)
301 while (pointer_index > 0)
304 *(pointer_store[pointer_index].pointer) =
305 pointer_store[pointer_index].old_value;
310 int UnityFailureCount(void)
312 return Unity.TestFailures;
315 int UnityGetCommandLineOptions(int argc, const char* argv[])
318 UnityFixture.Verbose = 0;
319 UnityFixture.GroupFilter = 0;
320 UnityFixture.NameFilter = 0;
321 UnityFixture.RepeatCount = 1;
326 for (i = 1; i < argc; )
328 if (strcmp(argv[i], "-v") == 0)
330 UnityFixture.Verbose = 1;
333 else if (strcmp(argv[i], "-g") == 0)
338 UnityFixture.GroupFilter = argv[i];
341 else if (strcmp(argv[i], "-n") == 0)
346 UnityFixture.NameFilter = argv[i];
349 else if (strcmp(argv[i], "-r") == 0)
351 UnityFixture.RepeatCount = 2;
355 if (*(argv[i]) >= '0' && *(argv[i]) <= '9')
357 UnityFixture.RepeatCount = atoi(argv[i]);
362 // ignore unknown parameter
369 void UnityConcludeFixtureTest(void)
371 if (Unity.CurrentTestIgnored)
373 if (UnityFixture.Verbose)
375 UNITY_OUTPUT_CHAR('\n');
379 else if (!Unity.CurrentTestFailed)
381 if (UnityFixture.Verbose)
384 UNITY_OUTPUT_CHAR('\n');
387 else if (Unity.CurrentTestFailed)
389 Unity.TestFailures++;
392 Unity.CurrentTestFailed = 0;
393 Unity.CurrentTestIgnored = 0;