leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers unity_fixture.c Source File

unity_fixture.c

00001 //- Copyright (c) 2010 James Grenning and Contributed to Unity Project
00002 /* ==========================================
00003     Unity Project - A Test Framework for C
00004     Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
00005     [Released under MIT License. Please refer to license.txt for details]
00006 ========================================== */
00007 
00008 #include <string.h>
00009 #include "unity_fixture.h"
00010 #include "unity_internals.h"
00011 
00012 struct _UnityFixture UnityFixture;
00013 
00014 //If you decide to use the function pointer approach.
00015 //Build with -D UNITY_OUTPUT_CHAR=outputChar and include <stdio.h>
00016 //int (*outputChar)(int) = putchar;
00017 
00018 #if !defined(UNITY_WEAK_ATTRIBUTE) && !defined(UNITY_WEAK_PRAGMA)
00019 void setUp(void)    { /*does nothing*/ }
00020 void tearDown(void) { /*does nothing*/ }
00021 #endif
00022 
00023 static void announceTestRun(unsigned int runNumber)
00024 {
00025     UnityPrint("Unity test run ");
00026     UnityPrintNumberUnsigned(runNumber+1);
00027     UnityPrint(" of ");
00028     UnityPrintNumberUnsigned(UnityFixture.RepeatCount);
00029     UNITY_PRINT_EOL();
00030 }
00031 
00032 int UnityMain(int argc, const char* argv[], void (*runAllTests)(void))
00033 {
00034     int result = UnityGetCommandLineOptions(argc, argv);
00035     unsigned int r;
00036     if (result != 0)
00037         return result;
00038 
00039     for (r = 0; r < UnityFixture.RepeatCount; r++)
00040     {
00041         UnityBegin(argv[0]);
00042         announceTestRun(r);
00043         runAllTests();
00044         UNITY_PRINT_EOL();
00045         UnityEnd();
00046     }
00047 
00048     return (int)Unity.TestFailures;
00049 }
00050 
00051 static int selected(const char* filter, const char* name)
00052 {
00053     if (filter == 0)
00054         return 1;
00055     return strstr(name, filter) ? 1 : 0;
00056 }
00057 
00058 static int testSelected(const char* test)
00059 {
00060     return selected(UnityFixture.NameFilter, test);
00061 }
00062 
00063 static int groupSelected(const char* group)
00064 {
00065     return selected(UnityFixture.GroupFilter, group);
00066 }
00067 
00068 void UnityTestRunner(unityfunction* setup,
00069                      unityfunction* testBody,
00070                      unityfunction* teardown,
00071                      const char* printableName,
00072                      const char* group,
00073                      const char* name,
00074                      const char* file, unsigned int line)
00075 {
00076     if (testSelected(name) && groupSelected(group))
00077     {
00078         Unity.TestFile = file;
00079         Unity.CurrentTestName = printableName;
00080         Unity.CurrentTestLineNumber = line;
00081         if (!UnityFixture.Verbose)
00082             UNITY_OUTPUT_CHAR('.');
00083         else
00084         {
00085             /* SA_PATCH: Output results using easy to parse tags. */
00086             UnityPrint(UNITY_RESULTS_TAGS_TEST_START);
00087             UnityPrint(printableName);
00088             /* SA_PATCH: Output results using easy to parse tags. */
00089             UnityPrint(UNITY_RESULTS_TAGS_TEST_END);
00090             UNITY_PRINT_EOL();
00091            //UnityPrint(printableName);
00092         }
00093 
00094         Unity.NumberOfTests++;
00095         UnityMalloc_StartTest();
00096         UnityPointer_Init();
00097 
00098         if (TEST_PROTECT())
00099         {
00100             setup();
00101             testBody();
00102         }
00103         if (TEST_PROTECT())
00104         {
00105             teardown();
00106         }
00107         if (TEST_PROTECT())
00108         {
00109             UnityPointer_UndoAllSets();
00110             if (!Unity.CurrentTestFailed)
00111                 UnityMalloc_EndTest();
00112         }
00113         UnityConcludeFixtureTest();
00114     }
00115 }
00116 
00117 void UnityIgnoreTest(const char* printableName, const char* group, const char* name)
00118 {
00119     if (testSelected(name) && groupSelected(group))
00120     {
00121         Unity.NumberOfTests++;
00122         Unity.TestIgnores++;
00123         if (!UnityFixture.Verbose)
00124             UNITY_OUTPUT_CHAR('!');
00125         else
00126         {
00127             UnityPrint(printableName);
00128             UNITY_PRINT_EOL();
00129         }
00130     }
00131 }
00132 
00133 
00134 //-------------------------------------------------
00135 //Malloc and free stuff
00136 //
00137 #define MALLOC_DONT_FAIL -1
00138 static int malloc_count;
00139 static int malloc_fail_countdown = MALLOC_DONT_FAIL;
00140 
00141 void UnityMalloc_StartTest(void)
00142 {
00143     malloc_count = 0;
00144     malloc_fail_countdown = MALLOC_DONT_FAIL;
00145 }
00146 
00147 void UnityMalloc_EndTest(void)
00148 {
00149     malloc_fail_countdown = MALLOC_DONT_FAIL;
00150     if (malloc_count != 0)
00151     {
00152         UNITY_TEST_FAIL(Unity.CurrentTestLineNumber, "This test leaks!");
00153     }
00154 }
00155 
00156 void UnityMalloc_MakeMallocFailAfterCount(int countdown)
00157 {
00158     malloc_fail_countdown = countdown;
00159 }
00160 
00161 // These definitions are always included from unity_fixture_malloc_overrides.h
00162 // We undef to use them or avoid conflict with <stdlib.h> per the C standard
00163 #undef malloc
00164 #undef free
00165 #undef calloc
00166 #undef realloc
00167 
00168 #ifdef UNITY_EXCLUDE_STDLIB_MALLOC
00169 static unsigned char unity_heap[UNITY_INTERNAL_HEAP_SIZE_BYTES];
00170 static size_t heap_index;
00171 #else
00172 #include <stdlib.h>
00173 #endif
00174 
00175 typedef struct GuardBytes
00176 {
00177     size_t size;
00178     size_t guard_space;
00179 } Guard;
00180 
00181 
00182 static const char end[] = "END";
00183 
00184 void* unity_malloc(size_t size)
00185 {
00186     char* mem;
00187     Guard* guard;
00188     size_t total_size = size + sizeof(Guard) + sizeof(end);
00189 
00190     if (malloc_fail_countdown != MALLOC_DONT_FAIL)
00191     {
00192         if (malloc_fail_countdown == 0)
00193             return NULL;
00194         malloc_fail_countdown--;
00195     }
00196 
00197     if (size == 0) return NULL;
00198 #ifdef UNITY_EXCLUDE_STDLIB_MALLOC
00199     if (heap_index + total_size > UNITY_INTERNAL_HEAP_SIZE_BYTES)
00200     {
00201         guard = NULL;
00202     }
00203     else
00204     {
00205         guard = (Guard*) &unity_heap[heap_index];
00206         heap_index += total_size;
00207     }
00208 #else
00209     guard = (Guard*)UNITY_FIXTURE_MALLOC(total_size);
00210 #endif
00211     if (guard == NULL) return NULL;
00212     malloc_count++;
00213     guard->size = size;
00214     guard->guard_space = 0;
00215     mem = (char*)&(guard[1]);
00216     memcpy(&mem[size], end, sizeof(end));
00217 
00218     return (void*)mem;
00219 }
00220 
00221 static int isOverrun(void* mem)
00222 {
00223     Guard* guard = (Guard*)mem;
00224     char* memAsChar = (char*)mem;
00225     guard--;
00226 
00227     return guard->guard_space != 0 || strcmp(&memAsChar[guard->size], end) != 0;
00228 }
00229 
00230 static void release_memory(void* mem)
00231 {
00232     Guard* guard = (Guard*)mem;
00233     guard--;
00234 
00235     malloc_count--;
00236 #ifdef UNITY_EXCLUDE_STDLIB_MALLOC
00237     if (mem == unity_heap + heap_index - guard->size - sizeof(end))
00238     {
00239         heap_index -= (guard->size + sizeof(Guard) + sizeof(end));
00240     }
00241 #else
00242     UNITY_FIXTURE_FREE(guard);
00243 #endif
00244 }
00245 
00246 void unity_free(void* mem)
00247 {
00248     int overrun;
00249 
00250     if (mem == NULL)
00251     {
00252         return;
00253     }
00254 
00255     overrun = isOverrun(mem);
00256     release_memory(mem);
00257     if (overrun)
00258     {
00259         UNITY_TEST_FAIL(Unity.CurrentTestLineNumber, "Buffer overrun detected during free()");
00260     }
00261 }
00262 
00263 void* unity_calloc(size_t num, size_t size)
00264 {
00265     void* mem = unity_malloc(num * size);
00266     if (mem == NULL) return NULL;
00267     memset(mem, 0, num * size);
00268     return mem;
00269 }
00270 
00271 void* unity_realloc(void* oldMem, size_t size)
00272 {
00273     Guard* guard = (Guard*)oldMem;
00274     void* newMem;
00275 
00276     if (oldMem == NULL) return unity_malloc(size);
00277 
00278     guard--;
00279     if (isOverrun(oldMem))
00280     {
00281         release_memory(oldMem);
00282         UNITY_TEST_FAIL(Unity.CurrentTestLineNumber, "Buffer overrun detected during realloc()");
00283     }
00284 
00285     if (size == 0)
00286     {
00287         release_memory(oldMem);
00288         return NULL;
00289     }
00290 
00291     if (guard->size >= size) return oldMem;
00292 
00293 #ifdef UNITY_EXCLUDE_STDLIB_MALLOC // Optimization if memory is expandable
00294     if (oldMem == unity_heap + heap_index - guard->size - sizeof(end) &&
00295         heap_index + size - guard->size <= UNITY_INTERNAL_HEAP_SIZE_BYTES)
00296     {
00297         release_memory(oldMem); // Not thread-safe, like unity_heap generally
00298         return unity_malloc(size); // No memcpy since data is in place
00299     }
00300 #endif
00301     newMem = unity_malloc(size);
00302     if (newMem == NULL) return NULL; // Do not release old memory
00303     memcpy(newMem, oldMem, guard->size);
00304     release_memory(oldMem);
00305     return newMem;
00306 }
00307 
00308 
00309 //--------------------------------------------------------
00310 //Automatic pointer restoration functions
00311 struct PointerPair
00312 {
00313     void** pointer;
00314     void* old_value;
00315 };
00316 
00317 enum { MAX_POINTERS = 50 };
00318 static struct PointerPair pointer_store[MAX_POINTERS];
00319 static int pointer_index = 0;
00320 
00321 void UnityPointer_Init(void)
00322 {
00323     pointer_index = 0;
00324 }
00325 
00326 void UnityPointer_Set(void** pointer, void* newValue, UNITY_LINE_TYPE line)
00327 {
00328     if (pointer_index >= MAX_POINTERS)
00329     {
00330         UNITY_TEST_FAIL(line, "Too many pointers set");
00331     }
00332     else
00333     {
00334         pointer_store[pointer_index].pointer = pointer;
00335         pointer_store[pointer_index].old_value = *pointer;
00336         *pointer = newValue;
00337         pointer_index++;
00338     }
00339 }
00340 
00341 void UnityPointer_UndoAllSets(void)
00342 {
00343     while (pointer_index > 0)
00344     {
00345         pointer_index--;
00346         *(pointer_store[pointer_index].pointer) =
00347             pointer_store[pointer_index].old_value;
00348     }
00349 }
00350 
00351 int UnityGetCommandLineOptions(int argc, const char* argv[])
00352 {
00353     int i;
00354     UnityFixture.Verbose = 0;
00355     UnityFixture.GroupFilter = 0;
00356     UnityFixture.NameFilter = 0;
00357     UnityFixture.RepeatCount = 1;
00358 
00359     if (argc == 1)
00360         return 0;
00361 
00362     for (i = 1; i < argc; )
00363     {
00364         if (strcmp(argv[i], "-v") == 0)
00365         {
00366             UnityFixture.Verbose = 1;
00367             i++;
00368         }
00369         else if (strcmp(argv[i], "-g") == 0)
00370         {
00371             i++;
00372             if (i >= argc)
00373                 return 1;
00374             UnityFixture.GroupFilter = argv[i];
00375             i++;
00376         }
00377         else if (strcmp(argv[i], "-n") == 0)
00378         {
00379             i++;
00380             if (i >= argc)
00381                 return 1;
00382             UnityFixture.NameFilter = argv[i];
00383             i++;
00384         }
00385         else if (strcmp(argv[i], "-r") == 0)
00386         {
00387             UnityFixture.RepeatCount = 2;
00388             i++;
00389             if (i < argc)
00390             {
00391                 if (*(argv[i]) >= '0' && *(argv[i]) <= '9')
00392                 {
00393                     unsigned int digit = 0;
00394                     UnityFixture.RepeatCount = 0;
00395                     while (argv[i][digit] >= '0' && argv[i][digit] <= '9')
00396                     {
00397                         UnityFixture.RepeatCount *= 10;
00398                         UnityFixture.RepeatCount += (unsigned int)argv[i][digit++] - '0';
00399                     }
00400                     i++;
00401                 }
00402             }
00403         } else {
00404             // ignore unknown parameter
00405             i++;
00406         }
00407     }
00408     return 0;
00409 }
00410 
00411 void UnityConcludeFixtureTest(void)
00412 {
00413     if (Unity.CurrentTestIgnored)
00414     {
00415         Unity.TestIgnores++;
00416         UNITY_PRINT_EOL();
00417     }
00418     else if (!Unity.CurrentTestFailed)
00419     {
00420         if (UnityFixture.Verbose)
00421         {
00422             UnityPrint(" ");
00423             /* SA_PATCH: Output results using easy to parse tags. */
00424             UnityPrint(UNITY_RESULTS_TAGS_RESULT_START);
00425             UnityPrint("PASS");
00426             /* SA_PATCH: Output results using easy to parse tags. */
00427             UnityPrint(UNITY_RESULTS_TAGS_RESULT_END);
00428             UNITY_OUTPUT_CHAR('\n');
00429         }
00430 //        {
00431 //            UnityPrint(" PASS");
00432 //            UNITY_PRINT_EOL();
00433 //        }
00434     }
00435     else // Unity.CurrentTestFailed
00436     {
00437         Unity.TestFailures++;
00438         UNITY_PRINT_EOL();
00439     }
00440 
00441     Unity.CurrentTestFailed = 0;
00442     Unity.CurrentTestIgnored = 0;
00443 }