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;
22 void setUp(void) { /*does nothing*/ }
23 void tearDown(void) { /*does nothing*/ }
25 static void announceTestRun(unsigned int runNumber)
27 UnityPrint("Unity test run ");
28 UnityPrintNumber(runNumber+1);
30 UnityPrintNumber(UnityFixture.RepeatCount);
31 UNITY_OUTPUT_CHAR('\n');
34 int UnityMain(int argc, const char* argv[], void (*runAllTests)(void))
36 int result = UnityGetCommandLineOptions(argc, argv);
41 for (r = 0; r < UnityFixture.RepeatCount; r++)
46 UNITY_OUTPUT_CHAR('\n');
50 return UnityFailureCount();
53 static int selected(const char * filter, const char * name)
57 return strstr(name, filter) ? 1 : 0;
60 static int testSelected(const char* test)
62 return selected(UnityFixture.NameFilter, test);
65 static int groupSelected(const char* group)
67 return selected(UnityFixture.GroupFilter, group);
70 static void runTestCase(void)
75 void UnityTestRunner(unityfunction* setup,
76 unityfunction* testBody,
77 unityfunction* teardown,
78 const char * printableName,
81 const char * file, int line)
83 if (testSelected(name) && groupSelected(group))
85 Unity.CurrentTestFailed = 0;
86 Unity.TestFile = file;
87 Unity.CurrentTestName = printableName;
88 Unity.CurrentTestLineNumber = line;
89 if (!UnityFixture.Verbose)
90 UNITY_OUTPUT_CHAR('.');
92 UnityPrint(printableName);
94 Unity.NumberOfTests++;
95 UnityMalloc_StartTest();
110 UnityPointer_UndoAllSets();
111 if (!Unity.CurrentTestFailed)
112 UnityMalloc_EndTest();
114 UnityConcludeFixtureTest();
118 void UnityIgnoreTest(const char * printableName)
120 Unity.NumberOfTests++;
121 Unity.CurrentTestIgnored = 1;
122 if (!UnityFixture.Verbose)
123 UNITY_OUTPUT_CHAR('!');
125 UnityPrint(printableName);
126 UnityConcludeFixtureTest();
130 //-------------------------------------------------
131 //Malloc and free stuff
133 #define MALLOC_DONT_FAIL -1
134 static int malloc_count;
135 static int malloc_fail_countdown = MALLOC_DONT_FAIL;
137 void UnityMalloc_StartTest(void)
140 malloc_fail_countdown = MALLOC_DONT_FAIL;
143 void UnityMalloc_EndTest(void)
145 malloc_fail_countdown = MALLOC_DONT_FAIL;
146 if (malloc_count != 0)
148 TEST_FAIL_MESSAGE("This test leaks!");
152 void UnityMalloc_MakeMallocFailAfterCount(int countdown)
154 malloc_fail_countdown = countdown;
176 typedef struct GuardBytes
179 char guard[sizeof(size_t)];
183 static const char * end = "END";
185 void * unity_malloc(size_t size)
190 if (malloc_fail_countdown != MALLOC_DONT_FAIL)
192 if (malloc_fail_countdown == 0)
194 malloc_fail_countdown--;
199 guard = (Guard*)malloc(size + sizeof(Guard) + 4);
201 mem = (char*)&(guard[1]);
202 memcpy(&mem[size], end, strlen(end) + 1);
207 static int isOverrun(void * mem)
209 Guard* guard = (Guard*)mem;
210 char* memAsChar = (char*)mem;
213 return strcmp(&memAsChar[guard->size], end) != 0;
216 static void release_memory(void * mem)
218 Guard* guard = (Guard*)mem;
225 void unity_free(void * mem)
227 int overrun = isOverrun(mem);//strcmp(&memAsChar[guard->size], end) != 0;
231 TEST_FAIL_MESSAGE("Buffer overrun detected during free()");
235 void* unity_calloc(size_t num, size_t size)
237 void* mem = unity_malloc(num * size);
238 memset(mem, 0, num*size);
242 void* unity_realloc(void * oldMem, size_t size)
244 Guard* guard = (Guard*)oldMem;
245 // char* memAsChar = (char*)oldMem;
249 return unity_malloc(size);
252 if (isOverrun(oldMem))
254 release_memory(oldMem);
255 TEST_FAIL_MESSAGE("Buffer overrun detected during realloc()");
260 release_memory(oldMem);
264 if (guard->size >= size)
267 newMem = unity_malloc(size);
268 memcpy(newMem, oldMem, guard->size);
274 //--------------------------------------------------------
275 //Automatic pointer restoration functions
276 typedef struct _PointerPair
278 struct _PointerPair * next;
283 enum {MAX_POINTERS=50};
284 static PointerPair pointer_store[MAX_POINTERS];
285 static int pointer_index = 0;
287 void UnityPointer_Init(void)
292 void UnityPointer_Set(void ** pointer, void * newValue)
294 if (pointer_index >= MAX_POINTERS)
295 TEST_FAIL_MESSAGE("Too many pointers set");
297 pointer_store[pointer_index].pointer = pointer;
298 pointer_store[pointer_index].old_value = *pointer;
303 void UnityPointer_UndoAllSets(void)
305 while (pointer_index > 0)
308 *(pointer_store[pointer_index].pointer) =
309 pointer_store[pointer_index].old_value;
314 int UnityFailureCount(void)
316 return Unity.TestFailures;
319 int UnityGetCommandLineOptions(int argc, const char* argv[])
322 UnityFixture.Verbose = 0;
323 UnityFixture.GroupFilter = 0;
324 UnityFixture.NameFilter = 0;
325 UnityFixture.RepeatCount = 1;
330 for (i = 1; i < argc; )
332 if (strcmp(argv[i], "-v") == 0)
334 UnityFixture.Verbose = 1;
337 else if (strcmp(argv[i], "-g") == 0)
342 UnityFixture.GroupFilter = argv[i];
345 else if (strcmp(argv[i], "-n") == 0)
350 UnityFixture.NameFilter = argv[i];
353 else if (strcmp(argv[i], "-r") == 0)
355 UnityFixture.RepeatCount = 2;
359 if (*(argv[i]) >= '0' && *(argv[i]) <= '9')
361 UnityFixture.RepeatCount = atoi(argv[i]);
366 // ignore unknown parameter
373 void UnityConcludeFixtureTest(void)
375 if (Unity.CurrentTestIgnored)
377 if (UnityFixture.Verbose)
379 UNITY_OUTPUT_CHAR('\n');
383 else if (!Unity.CurrentTestFailed)
385 if (UnityFixture.Verbose)
388 UNITY_OUTPUT_CHAR('\n');
391 else if (Unity.CurrentTestFailed)
393 Unity.TestFailures++;
396 Unity.CurrentTestFailed = 0;
397 Unity.CurrentTestIgnored = 0;