PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/extras/fixture/src/unity_fixture.c

https://github.com/bjones1/Unity
C | 384 lines | 311 code | 60 blank | 13 comment | 55 complexity | 329bb98defc1571a3041c2db6c058a8d MD5 | raw file
  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. ========================================== */
  7. #include <string.h>
  8. #include "unity_fixture.h"
  9. #include "unity_internals.h"
  10. UNITY_FIXTURE_T UnityFixture;
  11. //If you decide to use the function pointer approach.
  12. int (*outputChar)(int) = putchar;
  13. int verbose = 0;
  14. void setUp(void) { /*does nothing*/ }
  15. void tearDown(void) { /*does nothing*/ }
  16. void announceTestRun(unsigned int runNumber)
  17. {
  18. UnityPrint("Unity test run ");
  19. UnityPrintNumber(runNumber+1);
  20. UnityPrint(" of ");
  21. UnityPrintNumber(UnityFixture.RepeatCount);
  22. UNITY_OUTPUT_CHAR('\n');
  23. }
  24. int UnityMain(int argc, char* argv[], void (*runAllTests)())
  25. {
  26. int result = UnityGetCommandLineOptions(argc, argv);
  27. unsigned int r;
  28. if (result != 0)
  29. return result;
  30. for (r = 0; r < UnityFixture.RepeatCount; r++)
  31. {
  32. announceTestRun(r);
  33. UnityBegin();
  34. runAllTests();
  35. UNITY_OUTPUT_CHAR('\n');
  36. UnityEnd();
  37. }
  38. return UnityFailureCount();
  39. }
  40. static int selected(const char * filter, const char * name)
  41. {
  42. if (filter == 0)
  43. return 1;
  44. return strstr(name, filter) ? 1 : 0;
  45. }
  46. static int testSelected(const char* test)
  47. {
  48. return selected(UnityFixture.NameFilter, test);
  49. }
  50. static int groupSelected(const char* group)
  51. {
  52. return selected(UnityFixture.GroupFilter, group);
  53. }
  54. static void runTestCase()
  55. {
  56. }
  57. void UnityTestRunner(unityfunction* setup,
  58. unityfunction* testBody,
  59. unityfunction* teardown,
  60. const char * printableName,
  61. const char * group,
  62. const char * name,
  63. const char * file, int line)
  64. {
  65. if (testSelected(name) && groupSelected(group))
  66. {
  67. Unity.CurrentTestFailed = 0;
  68. Unity.TestFile = file;
  69. Unity.CurrentTestName = printableName;
  70. Unity.CurrentTestLineNumber = line;
  71. if (!UnityFixture.Verbose)
  72. UNITY_OUTPUT_CHAR('.');
  73. else
  74. UnityPrint(printableName);
  75. Unity.NumberOfTests++;
  76. UnityMalloc_StartTest();
  77. UnityPointer_Init();
  78. runTestCase();
  79. if (TEST_PROTECT())
  80. {
  81. setup();
  82. testBody();
  83. }
  84. if (TEST_PROTECT())
  85. {
  86. teardown();
  87. }
  88. if (TEST_PROTECT())
  89. {
  90. UnityPointer_UndoAllSets();
  91. if (!Unity.CurrentTestFailed)
  92. UnityMalloc_EndTest();
  93. }
  94. UnityConcludeFixtureTest();
  95. }
  96. }
  97. void UnityIgnoreTest(const char * printableName)
  98. {
  99. Unity.NumberOfTests++;
  100. Unity.CurrentTestIgnored = 1;
  101. if (!UnityFixture.Verbose)
  102. UNITY_OUTPUT_CHAR('!');
  103. else
  104. UnityPrint(printableName);
  105. UnityConcludeFixtureTest();
  106. }
  107. //-------------------------------------------------
  108. //Malloc and free stuff
  109. //
  110. #define MALLOC_DONT_FAIL -1
  111. static int malloc_count;
  112. static int malloc_fail_countdown = MALLOC_DONT_FAIL;
  113. void UnityMalloc_StartTest()
  114. {
  115. malloc_count = 0;
  116. malloc_fail_countdown = MALLOC_DONT_FAIL;
  117. }
  118. void UnityMalloc_EndTest()
  119. {
  120. malloc_fail_countdown = MALLOC_DONT_FAIL;
  121. if (malloc_count != 0)
  122. {
  123. TEST_FAIL_MESSAGE("This test leaks!");
  124. }
  125. }
  126. void UnityMalloc_MakeMallocFailAfterCount(int countdown)
  127. {
  128. malloc_fail_countdown = countdown;
  129. }
  130. #ifdef malloc
  131. #undef malloc
  132. #endif
  133. #ifdef free
  134. #undef free
  135. #endif
  136. #include <stdlib.h>
  137. #include <string.h>
  138. typedef struct GuardBytes
  139. {
  140. size_t size;
  141. char guard[sizeof(size_t)];
  142. } Guard;
  143. static const char * end = "END";
  144. void * unity_malloc(size_t size)
  145. {
  146. char* mem;
  147. Guard* guard;
  148. if (malloc_fail_countdown != MALLOC_DONT_FAIL)
  149. {
  150. if (malloc_fail_countdown == 0)
  151. return 0;
  152. malloc_fail_countdown--;
  153. }
  154. malloc_count++;
  155. guard = (Guard*)malloc(size + sizeof(Guard) + 4);
  156. guard->size = size;
  157. mem = (char*)&(guard[1]);
  158. memcpy(&mem[size], end, strlen(end) + 1);
  159. return (void*)mem;
  160. }
  161. static int isOverrun(void * mem)
  162. {
  163. Guard* guard = (Guard*)mem;
  164. char* memAsChar = (char*)mem;
  165. guard--;
  166. return strcmp(&memAsChar[guard->size], end) != 0;
  167. }
  168. static void release_memory(void * mem)
  169. {
  170. Guard* guard = (Guard*)mem;
  171. guard--;
  172. malloc_count--;
  173. free(guard);
  174. }
  175. void unity_free(void * mem)
  176. {
  177. int overrun = isOverrun(mem);//strcmp(&memAsChar[guard->size], end) != 0;
  178. release_memory(mem);
  179. if (overrun)
  180. {
  181. TEST_FAIL_MESSAGE("Buffer overrun detected during free()");
  182. }
  183. }
  184. void* unity_calloc(size_t num, size_t size)
  185. {
  186. void* mem = unity_malloc(num * size);
  187. memset(mem, 0, num*size);
  188. return mem;
  189. }
  190. void* unity_realloc(void * oldMem, size_t size)
  191. {
  192. Guard* guard = (Guard*)oldMem;
  193. // char* memAsChar = (char*)oldMem;
  194. void* newMem;
  195. if (oldMem == 0)
  196. return unity_malloc(size);
  197. guard--;
  198. if (isOverrun(oldMem))
  199. {
  200. release_memory(oldMem);
  201. TEST_FAIL_MESSAGE("Buffer overrun detected during realloc()");
  202. }
  203. if (size == 0)
  204. {
  205. release_memory(oldMem);
  206. return 0;
  207. }
  208. if (guard->size >= size)
  209. return oldMem;
  210. newMem = unity_malloc(size);
  211. memcpy(newMem, oldMem, guard->size);
  212. unity_free(oldMem);
  213. return newMem;
  214. }
  215. //--------------------------------------------------------
  216. //Automatic pointer restoration functions
  217. typedef struct _PointerPair
  218. {
  219. struct _PointerPair * next;
  220. void ** pointer;
  221. void * old_value;
  222. } PointerPair;
  223. enum {MAX_POINTERS=50};
  224. static PointerPair pointer_store[MAX_POINTERS];
  225. static int pointer_index = 0;
  226. void UnityPointer_Init()
  227. {
  228. pointer_index = 0;
  229. }
  230. void UnityPointer_Set(void ** pointer, void * newValue)
  231. {
  232. if (pointer_index >= MAX_POINTERS)
  233. TEST_FAIL_MESSAGE("Too many pointers set");
  234. pointer_store[pointer_index].pointer = pointer;
  235. pointer_store[pointer_index].old_value = *pointer;
  236. *pointer = newValue;
  237. pointer_index++;
  238. }
  239. void UnityPointer_UndoAllSets()
  240. {
  241. while (pointer_index > 0)
  242. {
  243. pointer_index--;
  244. *(pointer_store[pointer_index].pointer) =
  245. pointer_store[pointer_index].old_value;
  246. }
  247. }
  248. int UnityFailureCount()
  249. {
  250. return Unity.TestFailures;
  251. }
  252. int UnityGetCommandLineOptions(int argc, char* argv[])
  253. {
  254. int i;
  255. UnityFixture.Verbose = 0;
  256. UnityFixture.GroupFilter = 0;
  257. UnityFixture.NameFilter = 0;
  258. UnityFixture.RepeatCount = 1;
  259. if (argc == 1)
  260. return 0;
  261. for (i = 1; i < argc; )
  262. {
  263. if (strcmp(argv[i], "-v") == 0)
  264. {
  265. UnityFixture.Verbose = 1;
  266. i++;
  267. }
  268. else if (strcmp(argv[i], "-g") == 0)
  269. {
  270. i++;
  271. if (i >= argc)
  272. return 1;
  273. UnityFixture.GroupFilter = argv[i];
  274. i++;
  275. }
  276. else if (strcmp(argv[i], "-n") == 0)
  277. {
  278. i++;
  279. if (i >= argc)
  280. return 1;
  281. UnityFixture.NameFilter = argv[i];
  282. i++;
  283. }
  284. else if (strcmp(argv[i], "-r") == 0)
  285. {
  286. UnityFixture.RepeatCount = 2;
  287. i++;
  288. if (i < argc)
  289. {
  290. if (*(argv[i]) >= '0' && *(argv[i]) <= '9')
  291. {
  292. UnityFixture.RepeatCount = atoi(argv[i]);
  293. i++;
  294. }
  295. }
  296. }
  297. }
  298. return 0;
  299. }
  300. void UnityConcludeFixtureTest()
  301. {
  302. if (Unity.CurrentTestIgnored)
  303. {
  304. if (UnityFixture.Verbose)
  305. {
  306. UNITY_OUTPUT_CHAR('\n');
  307. }
  308. Unity.TestIgnores++;
  309. }
  310. else if (!Unity.CurrentTestFailed)
  311. {
  312. if (UnityFixture.Verbose)
  313. {
  314. UnityPrint(" PASS");
  315. UNITY_OUTPUT_CHAR('\n');
  316. }
  317. }
  318. else if (Unity.CurrentTestFailed)
  319. {
  320. Unity.TestFailures++;
  321. }
  322. Unity.CurrentTestFailed = 0;
  323. Unity.CurrentTestIgnored = 0;
  324. }