xref: /freebsd/contrib/ntp/sntp/unity/unity_fixture.c (revision f391d6bc)
1*276da39aSCy Schubert //- Copyright (c) 2010 James Grenning and Contributed to Unity Project
2*276da39aSCy Schubert /* ==========================================
3*276da39aSCy Schubert     Unity Project - A Test Framework for C
4*276da39aSCy Schubert     Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
5*276da39aSCy Schubert     [Released under MIT License. Please refer to license.txt for details]
6*276da39aSCy Schubert ========================================== */
7*276da39aSCy Schubert 
8*276da39aSCy Schubert #include <string.h>
9*276da39aSCy Schubert #include <stdio.h>
10*276da39aSCy Schubert #include "unity_fixture.h"
11*276da39aSCy Schubert #include "unity_internals.h"
12*276da39aSCy Schubert 
13*276da39aSCy Schubert UNITY_FIXTURE_T UnityFixture;
14*276da39aSCy Schubert 
15*276da39aSCy Schubert //If you decide to use the function pointer approach.
16*276da39aSCy Schubert int (*outputChar)(int) = putchar;
17*276da39aSCy Schubert 
18*276da39aSCy Schubert int verbose = 0;
19*276da39aSCy Schubert 
20*276da39aSCy Schubert 
announceTestRun(unsigned int runNumber)21*276da39aSCy Schubert static void announceTestRun(unsigned int runNumber)
22*276da39aSCy Schubert {
23*276da39aSCy Schubert     UnityPrint("Unity test run ");
24*276da39aSCy Schubert     UnityPrintNumber(runNumber+1);
25*276da39aSCy Schubert     UnityPrint(" of ");
26*276da39aSCy Schubert     UnityPrintNumber(UnityFixture.RepeatCount);
27*276da39aSCy Schubert     UNITY_OUTPUT_CHAR('\n');
28*276da39aSCy Schubert }
29*276da39aSCy Schubert 
UnityMain(int argc,const char * argv[],void (* runAllTests)(void))30*276da39aSCy Schubert int UnityMain(int argc, const char* argv[], void (*runAllTests)(void))
31*276da39aSCy Schubert {
32*276da39aSCy Schubert     int result = UnityGetCommandLineOptions(argc, argv);
33*276da39aSCy Schubert     unsigned int r;
34*276da39aSCy Schubert     if (result != 0)
35*276da39aSCy Schubert         return result;
36*276da39aSCy Schubert 
37*276da39aSCy Schubert     for (r = 0; r < UnityFixture.RepeatCount; r++)
38*276da39aSCy Schubert     {
39*276da39aSCy Schubert         UnityBegin(argv[0]);
40*276da39aSCy Schubert         announceTestRun(r);
41*276da39aSCy Schubert         runAllTests();
42*276da39aSCy Schubert         UNITY_OUTPUT_CHAR('\n');
43*276da39aSCy Schubert         UnityEnd();
44*276da39aSCy Schubert     }
45*276da39aSCy Schubert 
46*276da39aSCy Schubert     return UnityFailureCount();
47*276da39aSCy Schubert }
48*276da39aSCy Schubert 
selected(const char * filter,const char * name)49*276da39aSCy Schubert static int selected(const char * filter, const char * name)
50*276da39aSCy Schubert {
51*276da39aSCy Schubert     if (filter == 0)
52*276da39aSCy Schubert         return 1;
53*276da39aSCy Schubert     return strstr(name, filter) ? 1 : 0;
54*276da39aSCy Schubert }
55*276da39aSCy Schubert 
testSelected(const char * test)56*276da39aSCy Schubert static int testSelected(const char* test)
57*276da39aSCy Schubert {
58*276da39aSCy Schubert     return selected(UnityFixture.NameFilter, test);
59*276da39aSCy Schubert }
60*276da39aSCy Schubert 
groupSelected(const char * group)61*276da39aSCy Schubert static int groupSelected(const char* group)
62*276da39aSCy Schubert {
63*276da39aSCy Schubert     return selected(UnityFixture.GroupFilter, group);
64*276da39aSCy Schubert }
65*276da39aSCy Schubert 
runTestCase(void)66*276da39aSCy Schubert static void runTestCase(void)
67*276da39aSCy Schubert {
68*276da39aSCy Schubert 
69*276da39aSCy Schubert }
70*276da39aSCy Schubert 
UnityTestRunner(unityfunction * setup,unityfunction * testBody,unityfunction * teardown,const char * printableName,const char * group,const char * name,const char * file,int line)71*276da39aSCy Schubert void UnityTestRunner(unityfunction* setup,
72*276da39aSCy Schubert         unityfunction* testBody,
73*276da39aSCy Schubert         unityfunction* teardown,
74*276da39aSCy Schubert         const char * printableName,
75*276da39aSCy Schubert         const char * group,
76*276da39aSCy Schubert         const char * name,
77*276da39aSCy Schubert         const char * file, int line)
78*276da39aSCy Schubert {
79*276da39aSCy Schubert     if (testSelected(name) && groupSelected(group))
80*276da39aSCy Schubert     {
81*276da39aSCy Schubert         Unity.CurrentTestFailed = 0;
82*276da39aSCy Schubert         Unity.TestFile = file;
83*276da39aSCy Schubert         Unity.CurrentTestName = printableName;
84*276da39aSCy Schubert         Unity.CurrentTestLineNumber = line;
85*276da39aSCy Schubert         if (!UnityFixture.Verbose)
86*276da39aSCy Schubert             UNITY_OUTPUT_CHAR('.');
87*276da39aSCy Schubert         else
88*276da39aSCy Schubert             UnityPrint(printableName);
89*276da39aSCy Schubert 
90*276da39aSCy Schubert         Unity.NumberOfTests++;
91*276da39aSCy Schubert         UnityMalloc_StartTest();
92*276da39aSCy Schubert         UnityPointer_Init();
93*276da39aSCy Schubert 
94*276da39aSCy Schubert         runTestCase();
95*276da39aSCy Schubert         if (TEST_PROTECT())
96*276da39aSCy Schubert         {
97*276da39aSCy Schubert             setup();
98*276da39aSCy Schubert             testBody();
99*276da39aSCy Schubert         }
100*276da39aSCy Schubert         if (TEST_PROTECT())
101*276da39aSCy Schubert         {
102*276da39aSCy Schubert             teardown();
103*276da39aSCy Schubert         }
104*276da39aSCy Schubert         if (TEST_PROTECT())
105*276da39aSCy Schubert         {
106*276da39aSCy Schubert             UnityPointer_UndoAllSets();
107*276da39aSCy Schubert             if (!Unity.CurrentTestFailed)
108*276da39aSCy Schubert                 UnityMalloc_EndTest();
109*276da39aSCy Schubert         }
110*276da39aSCy Schubert         UnityConcludeFixtureTest();
111*276da39aSCy Schubert     }
112*276da39aSCy Schubert }
113*276da39aSCy Schubert 
UnityIgnoreTest(const char * printableName)114*276da39aSCy Schubert void UnityIgnoreTest(const char * printableName)
115*276da39aSCy Schubert {
116*276da39aSCy Schubert     Unity.NumberOfTests++;
117*276da39aSCy Schubert     Unity.CurrentTestIgnored = 1;
118*276da39aSCy Schubert     if (!UnityFixture.Verbose)
119*276da39aSCy Schubert         UNITY_OUTPUT_CHAR('!');
120*276da39aSCy Schubert     else
121*276da39aSCy Schubert         UnityPrint(printableName);
122*276da39aSCy Schubert     UnityConcludeFixtureTest();
123*276da39aSCy Schubert }
124*276da39aSCy Schubert 
125*276da39aSCy Schubert 
126*276da39aSCy Schubert //-------------------------------------------------
127*276da39aSCy Schubert //Malloc and free stuff
128*276da39aSCy Schubert //
129*276da39aSCy Schubert #define MALLOC_DONT_FAIL -1
130*276da39aSCy Schubert static int malloc_count;
131*276da39aSCy Schubert static int malloc_fail_countdown = MALLOC_DONT_FAIL;
132*276da39aSCy Schubert 
UnityMalloc_StartTest(void)133*276da39aSCy Schubert void UnityMalloc_StartTest(void)
134*276da39aSCy Schubert {
135*276da39aSCy Schubert     malloc_count = 0;
136*276da39aSCy Schubert     malloc_fail_countdown = MALLOC_DONT_FAIL;
137*276da39aSCy Schubert }
138*276da39aSCy Schubert 
UnityMalloc_EndTest(void)139*276da39aSCy Schubert void UnityMalloc_EndTest(void)
140*276da39aSCy Schubert {
141*276da39aSCy Schubert     malloc_fail_countdown = MALLOC_DONT_FAIL;
142*276da39aSCy Schubert     if (malloc_count != 0)
143*276da39aSCy Schubert     {
144*276da39aSCy Schubert         TEST_FAIL_MESSAGE("This test leaks!");
145*276da39aSCy Schubert     }
146*276da39aSCy Schubert }
147*276da39aSCy Schubert 
UnityMalloc_MakeMallocFailAfterCount(int countdown)148*276da39aSCy Schubert void UnityMalloc_MakeMallocFailAfterCount(int countdown)
149*276da39aSCy Schubert {
150*276da39aSCy Schubert     malloc_fail_countdown = countdown;
151*276da39aSCy Schubert }
152*276da39aSCy Schubert 
153*276da39aSCy Schubert #ifdef malloc
154*276da39aSCy Schubert #undef malloc
155*276da39aSCy Schubert #endif
156*276da39aSCy Schubert 
157*276da39aSCy Schubert #ifdef free
158*276da39aSCy Schubert #undef free
159*276da39aSCy Schubert #endif
160*276da39aSCy Schubert 
161*276da39aSCy Schubert #ifdef calloc
162*276da39aSCy Schubert #undef calloc
163*276da39aSCy Schubert #endif
164*276da39aSCy Schubert 
165*276da39aSCy Schubert #ifdef realloc
166*276da39aSCy Schubert #undef realloc
167*276da39aSCy Schubert #endif
168*276da39aSCy Schubert 
169*276da39aSCy Schubert #include <stdlib.h>
170*276da39aSCy Schubert #include <string.h>
171*276da39aSCy Schubert 
172*276da39aSCy Schubert typedef struct GuardBytes
173*276da39aSCy Schubert {
174*276da39aSCy Schubert     size_t size;
175*276da39aSCy Schubert     char guard[sizeof(size_t)];
176*276da39aSCy Schubert } Guard;
177*276da39aSCy Schubert 
178*276da39aSCy Schubert 
179*276da39aSCy Schubert static const char * end = "END";
180*276da39aSCy Schubert 
unity_malloc(size_t size)181*276da39aSCy Schubert void * unity_malloc(size_t size)
182*276da39aSCy Schubert {
183*276da39aSCy Schubert     char* mem;
184*276da39aSCy Schubert     Guard* guard;
185*276da39aSCy Schubert 
186*276da39aSCy Schubert     if (malloc_fail_countdown != MALLOC_DONT_FAIL)
187*276da39aSCy Schubert     {
188*276da39aSCy Schubert         if (malloc_fail_countdown == 0)
189*276da39aSCy Schubert             return 0;
190*276da39aSCy Schubert         malloc_fail_countdown--;
191*276da39aSCy Schubert     }
192*276da39aSCy Schubert 
193*276da39aSCy Schubert     malloc_count++;
194*276da39aSCy Schubert 
195*276da39aSCy Schubert     guard = (Guard*)malloc(size + sizeof(Guard) + 4);
196*276da39aSCy Schubert     guard->size = size;
197*276da39aSCy Schubert     mem = (char*)&(guard[1]);
198*276da39aSCy Schubert     memcpy(&mem[size], end, strlen(end) + 1);
199*276da39aSCy Schubert 
200*276da39aSCy Schubert     return (void*)mem;
201*276da39aSCy Schubert }
202*276da39aSCy Schubert 
isOverrun(void * mem)203*276da39aSCy Schubert static int isOverrun(void * mem)
204*276da39aSCy Schubert {
205*276da39aSCy Schubert     Guard* guard = (Guard*)mem;
206*276da39aSCy Schubert     char* memAsChar = (char*)mem;
207*276da39aSCy Schubert     guard--;
208*276da39aSCy Schubert 
209*276da39aSCy Schubert     return strcmp(&memAsChar[guard->size], end) != 0;
210*276da39aSCy Schubert }
211*276da39aSCy Schubert 
release_memory(void * mem)212*276da39aSCy Schubert static void release_memory(void * mem)
213*276da39aSCy Schubert {
214*276da39aSCy Schubert     Guard* guard = (Guard*)mem;
215*276da39aSCy Schubert     guard--;
216*276da39aSCy Schubert 
217*276da39aSCy Schubert     malloc_count--;
218*276da39aSCy Schubert     free(guard);
219*276da39aSCy Schubert }
220*276da39aSCy Schubert 
unity_free(void * mem)221*276da39aSCy Schubert void unity_free(void * mem)
222*276da39aSCy Schubert {
223*276da39aSCy Schubert     int overrun = isOverrun(mem);//strcmp(&memAsChar[guard->size], end) != 0;
224*276da39aSCy Schubert     release_memory(mem);
225*276da39aSCy Schubert     if (overrun)
226*276da39aSCy Schubert     {
227*276da39aSCy Schubert         TEST_FAIL_MESSAGE("Buffer overrun detected during free()");
228*276da39aSCy Schubert     }
229*276da39aSCy Schubert }
230*276da39aSCy Schubert 
unity_calloc(size_t num,size_t size)231*276da39aSCy Schubert void* unity_calloc(size_t num, size_t size)
232*276da39aSCy Schubert {
233*276da39aSCy Schubert     void* mem = unity_malloc(num * size);
234*276da39aSCy Schubert     memset(mem, 0, num*size);
235*276da39aSCy Schubert     return mem;
236*276da39aSCy Schubert }
237*276da39aSCy Schubert 
unity_realloc(void * oldMem,size_t size)238*276da39aSCy Schubert void* unity_realloc(void * oldMem, size_t size)
239*276da39aSCy Schubert {
240*276da39aSCy Schubert     Guard* guard = (Guard*)oldMem;
241*276da39aSCy Schubert //    char* memAsChar = (char*)oldMem;
242*276da39aSCy Schubert     void* newMem;
243*276da39aSCy Schubert 
244*276da39aSCy Schubert     if (oldMem == 0)
245*276da39aSCy Schubert         return unity_malloc(size);
246*276da39aSCy Schubert 
247*276da39aSCy Schubert     guard--;
248*276da39aSCy Schubert     if (isOverrun(oldMem))
249*276da39aSCy Schubert     {
250*276da39aSCy Schubert         release_memory(oldMem);
251*276da39aSCy Schubert         TEST_FAIL_MESSAGE("Buffer overrun detected during realloc()");
252*276da39aSCy Schubert     }
253*276da39aSCy Schubert 
254*276da39aSCy Schubert     if (size == 0)
255*276da39aSCy Schubert     {
256*276da39aSCy Schubert         release_memory(oldMem);
257*276da39aSCy Schubert         return 0;
258*276da39aSCy Schubert     }
259*276da39aSCy Schubert 
260*276da39aSCy Schubert     if (guard->size >= size)
261*276da39aSCy Schubert         return oldMem;
262*276da39aSCy Schubert 
263*276da39aSCy Schubert     newMem = unity_malloc(size);
264*276da39aSCy Schubert     memcpy(newMem, oldMem, guard->size);
265*276da39aSCy Schubert     unity_free(oldMem);
266*276da39aSCy Schubert     return newMem;
267*276da39aSCy Schubert }
268*276da39aSCy Schubert 
269*276da39aSCy Schubert 
270*276da39aSCy Schubert //--------------------------------------------------------
271*276da39aSCy Schubert //Automatic pointer restoration functions
272*276da39aSCy Schubert typedef struct _PointerPair
273*276da39aSCy Schubert {
274*276da39aSCy Schubert     struct _PointerPair * next;
275*276da39aSCy Schubert     void ** pointer;
276*276da39aSCy Schubert     void * old_value;
277*276da39aSCy Schubert } PointerPair;
278*276da39aSCy Schubert 
279*276da39aSCy Schubert enum {MAX_POINTERS=50};
280*276da39aSCy Schubert static PointerPair pointer_store[MAX_POINTERS];
281*276da39aSCy Schubert static int pointer_index = 0;
282*276da39aSCy Schubert 
UnityPointer_Init(void)283*276da39aSCy Schubert void UnityPointer_Init(void)
284*276da39aSCy Schubert {
285*276da39aSCy Schubert     pointer_index = 0;
286*276da39aSCy Schubert }
287*276da39aSCy Schubert 
UnityPointer_Set(void ** pointer,void * newValue)288*276da39aSCy Schubert void UnityPointer_Set(void ** pointer, void * newValue)
289*276da39aSCy Schubert {
290*276da39aSCy Schubert     if (pointer_index >= MAX_POINTERS)
291*276da39aSCy Schubert         TEST_FAIL_MESSAGE("Too many pointers set");
292*276da39aSCy Schubert 
293*276da39aSCy Schubert     pointer_store[pointer_index].pointer = pointer;
294*276da39aSCy Schubert     pointer_store[pointer_index].old_value = *pointer;
295*276da39aSCy Schubert     *pointer = newValue;
296*276da39aSCy Schubert     pointer_index++;
297*276da39aSCy Schubert }
298*276da39aSCy Schubert 
UnityPointer_UndoAllSets(void)299*276da39aSCy Schubert void UnityPointer_UndoAllSets(void)
300*276da39aSCy Schubert {
301*276da39aSCy Schubert     while (pointer_index > 0)
302*276da39aSCy Schubert     {
303*276da39aSCy Schubert         pointer_index--;
304*276da39aSCy Schubert         *(pointer_store[pointer_index].pointer) =
305*276da39aSCy Schubert         pointer_store[pointer_index].old_value;
306*276da39aSCy Schubert 
307*276da39aSCy Schubert     }
308*276da39aSCy Schubert }
309*276da39aSCy Schubert 
UnityFailureCount(void)310*276da39aSCy Schubert int UnityFailureCount(void)
311*276da39aSCy Schubert {
312*276da39aSCy Schubert     return Unity.TestFailures;
313*276da39aSCy Schubert }
314*276da39aSCy Schubert 
UnityGetCommandLineOptions(int argc,const char * argv[])315*276da39aSCy Schubert int UnityGetCommandLineOptions(int argc, const char* argv[])
316*276da39aSCy Schubert {
317*276da39aSCy Schubert     int i;
318*276da39aSCy Schubert     UnityFixture.Verbose = 0;
319*276da39aSCy Schubert     UnityFixture.GroupFilter = 0;
320*276da39aSCy Schubert     UnityFixture.NameFilter = 0;
321*276da39aSCy Schubert     UnityFixture.RepeatCount = 1;
322*276da39aSCy Schubert 
323*276da39aSCy Schubert     if (argc == 1)
324*276da39aSCy Schubert         return 0;
325*276da39aSCy Schubert 
326*276da39aSCy Schubert     for (i = 1; i < argc; )
327*276da39aSCy Schubert     {
328*276da39aSCy Schubert         if (strcmp(argv[i], "-v") == 0)
329*276da39aSCy Schubert         {
330*276da39aSCy Schubert             UnityFixture.Verbose = 1;
331*276da39aSCy Schubert             i++;
332*276da39aSCy Schubert         }
333*276da39aSCy Schubert         else if (strcmp(argv[i], "-g") == 0)
334*276da39aSCy Schubert         {
335*276da39aSCy Schubert             i++;
336*276da39aSCy Schubert             if (i >= argc)
337*276da39aSCy Schubert                 return 1;
338*276da39aSCy Schubert             UnityFixture.GroupFilter = argv[i];
339*276da39aSCy Schubert             i++;
340*276da39aSCy Schubert         }
341*276da39aSCy Schubert         else if (strcmp(argv[i], "-n") == 0)
342*276da39aSCy Schubert         {
343*276da39aSCy Schubert             i++;
344*276da39aSCy Schubert             if (i >= argc)
345*276da39aSCy Schubert                 return 1;
346*276da39aSCy Schubert             UnityFixture.NameFilter = argv[i];
347*276da39aSCy Schubert             i++;
348*276da39aSCy Schubert         }
349*276da39aSCy Schubert         else if (strcmp(argv[i], "-r") == 0)
350*276da39aSCy Schubert         {
351*276da39aSCy Schubert             UnityFixture.RepeatCount = 2;
352*276da39aSCy Schubert             i++;
353*276da39aSCy Schubert             if (i < argc)
354*276da39aSCy Schubert             {
355*276da39aSCy Schubert                 if (*(argv[i]) >= '0' && *(argv[i]) <= '9')
356*276da39aSCy Schubert                 {
357*276da39aSCy Schubert                     UnityFixture.RepeatCount = atoi(argv[i]);
358*276da39aSCy Schubert                     i++;
359*276da39aSCy Schubert                 }
360*276da39aSCy Schubert             }
361*276da39aSCy Schubert         } else {
362*276da39aSCy Schubert             // ignore unknown parameter
363*276da39aSCy Schubert             i++;
364*276da39aSCy Schubert         }
365*276da39aSCy Schubert     }
366*276da39aSCy Schubert     return 0;
367*276da39aSCy Schubert }
368*276da39aSCy Schubert 
UnityConcludeFixtureTest(void)369*276da39aSCy Schubert void UnityConcludeFixtureTest(void)
370*276da39aSCy Schubert {
371*276da39aSCy Schubert     if (Unity.CurrentTestIgnored)
372*276da39aSCy Schubert     {
373*276da39aSCy Schubert         if (UnityFixture.Verbose)
374*276da39aSCy Schubert         {
375*276da39aSCy Schubert             UNITY_OUTPUT_CHAR('\n');
376*276da39aSCy Schubert         }
377*276da39aSCy Schubert         Unity.TestIgnores++;
378*276da39aSCy Schubert     }
379*276da39aSCy Schubert     else if (!Unity.CurrentTestFailed)
380*276da39aSCy Schubert     {
381*276da39aSCy Schubert         if (UnityFixture.Verbose)
382*276da39aSCy Schubert         {
383*276da39aSCy Schubert             UnityPrint(" PASS");
384*276da39aSCy Schubert             UNITY_OUTPUT_CHAR('\n');
385*276da39aSCy Schubert         }
386*276da39aSCy Schubert     }
387*276da39aSCy Schubert     else if (Unity.CurrentTestFailed)
388*276da39aSCy Schubert     {
389*276da39aSCy Schubert         Unity.TestFailures++;
390*276da39aSCy Schubert     }
391*276da39aSCy Schubert 
392*276da39aSCy Schubert     Unity.CurrentTestFailed = 0;
393*276da39aSCy Schubert     Unity.CurrentTestIgnored = 0;
394*276da39aSCy Schubert }
395