unity_fixture.c 10 KB

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