unity.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. /* =========================================================================
  2. Unity Project - A Test Framework for C
  3. Copyright (c) 2007-14 Mike Karlesky, Mark VanderVoord, Greg Williams
  4. [Released under MIT License. Please refer to license.txt for details]
  5. ============================================================================ */
  6. #include "unity.h"
  7. #include <stddef.h>
  8. /* If omitted from header, declare overrideable prototypes here so they're ready for use */
  9. #ifdef UNITY_OMIT_OUTPUT_CHAR_HEADER_DECLARATION
  10. int UNITY_OUTPUT_CHAR(int);
  11. #endif
  12. #ifdef UNITY_OMIT_OUTPUT_FLUSH_HEADER_DECLARATION
  13. int UNITY_OUTPUT_FLUSH(void);
  14. #endif
  15. /* Helpful macros for us to use here */
  16. #define UNITY_FAIL_AND_BAIL { Unity.CurrentTestFailed = 1; longjmp(Unity.AbortFrame, 1); }
  17. #define UNITY_IGNORE_AND_BAIL { Unity.CurrentTestIgnored = 1; longjmp(Unity.AbortFrame, 1); }
  18. /* return prematurely if we are already in failure or ignore state */
  19. #define UNITY_SKIP_EXECUTION { if ((Unity.CurrentTestFailed != 0) || (Unity.CurrentTestIgnored != 0)) {return;} }
  20. struct _Unity Unity;
  21. static const char UnityStrOk[] = "OK";
  22. static const char UnityStrPass[] = "PASS";
  23. static const char UnityStrFail[] = "FAIL";
  24. static const char UnityStrIgnore[] = "IGNORE";
  25. static const char UnityStrNull[] = "NULL";
  26. static const char UnityStrSpacer[] = ". ";
  27. static const char UnityStrExpected[] = " Expected ";
  28. static const char UnityStrWas[] = " Was ";
  29. static const char UnityStrElement[] = " Element ";
  30. static const char UnityStrByte[] = " Byte ";
  31. static const char UnityStrMemory[] = " Memory Mismatch.";
  32. static const char UnityStrDelta[] = " Values Not Within Delta ";
  33. static const char UnityStrPointless[] = " You Asked Me To Compare Nothing, Which Was Pointless.";
  34. static const char UnityStrNullPointerForExpected[] = " Expected pointer to be NULL";
  35. static const char UnityStrNullPointerForActual[] = " Actual pointer was NULL";
  36. static const char UnityStrNot[] = "Not ";
  37. static const char UnityStrInf[] = "Infinity";
  38. static const char UnityStrNegInf[] = "Negative Infinity";
  39. static const char UnityStrNaN[] = "NaN";
  40. static const char UnityStrDet[] = "Determinate";
  41. static const char UnityStrInvalidFloatTrait[] = "Invalid Float Trait";
  42. const char UnityStrErrFloat[] = "Unity Floating Point Disabled";
  43. const char UnityStrErrDouble[] = "Unity Double Precision Disabled";
  44. const char UnityStrErr64[] = "Unity 64-bit Support Disabled";
  45. static const char UnityStrBreaker[] = "-----------------------";
  46. static const char UnityStrResultsTests[] = " Tests ";
  47. static const char UnityStrResultsFailures[] = " Failures ";
  48. static const char UnityStrResultsIgnored[] = " Ignored ";
  49. static const char UnityStrDetail1Name[] = UNITY_DETAIL1_NAME " ";
  50. static const char UnityStrDetail2Name[] = " " UNITY_DETAIL2_NAME " ";
  51. #ifdef UNITY_FLOAT_NEEDS_ZERO
  52. /* Dividing by these constants produces +/- infinity.
  53. * The rationale is given in UnityAssertFloatIsInf's body. */
  54. static const _UF f_zero = 0.0f;
  55. #endif
  56. /* compiler-generic print formatting masks */
  57. static const _U_UINT UnitySizeMask[] =
  58. {
  59. 255u, /* 0xFF */
  60. 65535u, /* 0xFFFF */
  61. 65535u,
  62. 4294967295u, /* 0xFFFFFFFF */
  63. 4294967295u,
  64. 4294967295u,
  65. 4294967295u
  66. #ifdef UNITY_SUPPORT_64
  67. ,0xFFFFFFFFFFFFFFFF
  68. #endif
  69. };
  70. /*-----------------------------------------------
  71. * Pretty Printers & Test Result Output Handlers
  72. *-----------------------------------------------*/
  73. void UnityPrint(const char* string)
  74. {
  75. const char* pch = string;
  76. if (pch != NULL)
  77. {
  78. while (*pch)
  79. {
  80. /* printable characters plus CR & LF are printed */
  81. if ((*pch <= 126) && (*pch >= 32))
  82. {
  83. UNITY_OUTPUT_CHAR(*pch);
  84. }
  85. /* write escaped carriage returns */
  86. else if (*pch == 13)
  87. {
  88. UNITY_OUTPUT_CHAR('\\');
  89. UNITY_OUTPUT_CHAR('r');
  90. }
  91. /* write escaped line feeds */
  92. else if (*pch == 10)
  93. {
  94. UNITY_OUTPUT_CHAR('\\');
  95. UNITY_OUTPUT_CHAR('n');
  96. }
  97. /* unprintable characters are shown as codes */
  98. else
  99. {
  100. UNITY_OUTPUT_CHAR('\\');
  101. UnityPrintNumberHex((_U_UINT)*pch, 2);
  102. }
  103. pch++;
  104. }
  105. }
  106. }
  107. void UnityPrintLen(const char* string, const _UU32 length);
  108. void UnityPrintLen(const char* string, const _UU32 length)
  109. {
  110. const char* pch = string;
  111. if (pch != NULL)
  112. {
  113. while (*pch && (_UU32)(pch - string) < length)
  114. {
  115. /* printable characters plus CR & LF are printed */
  116. if ((*pch <= 126) && (*pch >= 32))
  117. {
  118. UNITY_OUTPUT_CHAR(*pch);
  119. }
  120. /* write escaped carriage returns */
  121. else if (*pch == 13)
  122. {
  123. UNITY_OUTPUT_CHAR('\\');
  124. UNITY_OUTPUT_CHAR('r');
  125. }
  126. /* write escaped line feeds */
  127. else if (*pch == 10)
  128. {
  129. UNITY_OUTPUT_CHAR('\\');
  130. UNITY_OUTPUT_CHAR('n');
  131. }
  132. /* unprintable characters are shown as codes */
  133. else
  134. {
  135. UNITY_OUTPUT_CHAR('\\');
  136. UnityPrintNumberHex((_U_UINT)*pch, 2);
  137. }
  138. pch++;
  139. }
  140. }
  141. }
  142. /*-----------------------------------------------*/
  143. void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style)
  144. {
  145. if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
  146. {
  147. UnityPrintNumber(number);
  148. }
  149. else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
  150. {
  151. UnityPrintNumberUnsigned( (_U_UINT)number & UnitySizeMask[((_U_UINT)style & (_U_UINT)0x0F) - 1] );
  152. }
  153. else
  154. {
  155. UnityPrintNumberHex((_U_UINT)number, (char)((style & 0x000F) << 1));
  156. }
  157. }
  158. /*-----------------------------------------------*/
  159. void UnityPrintNumber(const _U_SINT number_to_print)
  160. {
  161. _U_UINT number = (_U_UINT)number_to_print;
  162. if (number_to_print < 0)
  163. {
  164. /* A negative number, including MIN negative */
  165. UNITY_OUTPUT_CHAR('-');
  166. number = (_U_UINT)(-number_to_print);
  167. }
  168. UnityPrintNumberUnsigned(number);
  169. }
  170. /*-----------------------------------------------
  171. * basically do an itoa using as little ram as possible */
  172. void UnityPrintNumberUnsigned(const _U_UINT number)
  173. {
  174. _U_UINT divisor = 1;
  175. /* figure out initial divisor */
  176. while (number / divisor > 9)
  177. {
  178. divisor *= 10;
  179. }
  180. /* now mod and print, then divide divisor */
  181. do
  182. {
  183. UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
  184. divisor /= 10;
  185. }
  186. while (divisor > 0);
  187. }
  188. /*-----------------------------------------------*/
  189. void UnityPrintNumberHex(const _U_UINT number, const char nibbles_to_print)
  190. {
  191. _U_UINT nibble;
  192. char nibbles = nibbles_to_print;
  193. UNITY_OUTPUT_CHAR('0');
  194. UNITY_OUTPUT_CHAR('x');
  195. while (nibbles > 0)
  196. {
  197. nibble = (number >> (--nibbles << 2)) & 0x0000000F;
  198. if (nibble <= 9)
  199. {
  200. UNITY_OUTPUT_CHAR((char)('0' + nibble));
  201. }
  202. else
  203. {
  204. UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble));
  205. }
  206. }
  207. }
  208. /*-----------------------------------------------*/
  209. void UnityPrintMask(const _U_UINT mask, const _U_UINT number)
  210. {
  211. _U_UINT current_bit = (_U_UINT)1 << (UNITY_INT_WIDTH - 1);
  212. _US32 i;
  213. for (i = 0; i < UNITY_INT_WIDTH; i++)
  214. {
  215. if (current_bit & mask)
  216. {
  217. if (current_bit & number)
  218. {
  219. UNITY_OUTPUT_CHAR('1');
  220. }
  221. else
  222. {
  223. UNITY_OUTPUT_CHAR('0');
  224. }
  225. }
  226. else
  227. {
  228. UNITY_OUTPUT_CHAR('X');
  229. }
  230. current_bit = current_bit >> 1;
  231. }
  232. }
  233. /*-----------------------------------------------*/
  234. #ifdef UNITY_FLOAT_VERBOSE
  235. #include <stdio.h>
  236. #ifndef UNITY_VERBOSE_NUMBER_MAX_LENGTH
  237. # ifdef UNITY_DOUBLE_VERBOSE
  238. # define UNITY_VERBOSE_NUMBER_MAX_LENGTH 317
  239. # else
  240. # define UNITY_VERBOSE_NUMBER_MAX_LENGTH 47
  241. # endif
  242. #endif
  243. void UnityPrintFloat(_UF number)
  244. {
  245. char TempBuffer[UNITY_VERBOSE_NUMBER_MAX_LENGTH + 1];
  246. snprintf(TempBuffer, sizeof(TempBuffer), "%.6f", number);
  247. UnityPrint(TempBuffer);
  248. }
  249. #endif
  250. /*-----------------------------------------------*/
  251. void UnityPrintFail(void);
  252. void UnityPrintFail(void)
  253. {
  254. UnityPrint(UnityStrFail);
  255. }
  256. void UnityPrintOk(void);
  257. void UnityPrintOk(void)
  258. {
  259. UnityPrint(UnityStrOk);
  260. }
  261. /*-----------------------------------------------*/
  262. static void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line);
  263. static void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line)
  264. {
  265. #ifndef UNITY_FIXTURES
  266. UnityPrint(file);
  267. UNITY_OUTPUT_CHAR(':');
  268. UnityPrintNumber((_U_SINT)line);
  269. UNITY_OUTPUT_CHAR(':');
  270. UnityPrint(Unity.CurrentTestName);
  271. UNITY_OUTPUT_CHAR(':');
  272. #else
  273. UNITY_UNUSED(file);
  274. UNITY_UNUSED(line);
  275. #endif
  276. }
  277. /*-----------------------------------------------*/
  278. static void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line);
  279. static void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)
  280. {
  281. #ifndef UNITY_FIXTURES
  282. UnityTestResultsBegin(Unity.TestFile, line);
  283. #else
  284. UNITY_UNUSED(line);
  285. #endif
  286. UnityPrint(UnityStrFail);
  287. UNITY_OUTPUT_CHAR(':');
  288. }
  289. /*-----------------------------------------------*/
  290. void UnityConcludeTest(void)
  291. {
  292. if (Unity.CurrentTestIgnored)
  293. {
  294. Unity.TestIgnores++;
  295. }
  296. else if (!Unity.CurrentTestFailed)
  297. {
  298. UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);
  299. UnityPrint(UnityStrPass);
  300. }
  301. else
  302. {
  303. Unity.TestFailures++;
  304. }
  305. Unity.CurrentTestFailed = 0;
  306. Unity.CurrentTestIgnored = 0;
  307. UNITY_PRINT_EOL();
  308. UNITY_OUTPUT_FLUSH();
  309. }
  310. /*-----------------------------------------------*/
  311. static void UnityAddMsgIfSpecified(const char* msg);
  312. static void UnityAddMsgIfSpecified(const char* msg)
  313. {
  314. if (msg)
  315. {
  316. UnityPrint(UnityStrSpacer);
  317. #ifndef UNITY_EXCLUDE_DETAILS
  318. if (Unity.CurrentDetail1)
  319. {
  320. UnityPrint(UnityStrDetail1Name);
  321. UnityPrint(Unity.CurrentDetail1);
  322. if (Unity.CurrentDetail2)
  323. {
  324. UnityPrint(UnityStrDetail2Name);
  325. UnityPrint(Unity.CurrentDetail2);
  326. }
  327. UnityPrint(UnityStrSpacer);
  328. }
  329. #endif
  330. UnityPrint(msg);
  331. }
  332. }
  333. /*-----------------------------------------------*/
  334. static void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual);
  335. static void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual)
  336. {
  337. UnityPrint(UnityStrExpected);
  338. if (expected != NULL)
  339. {
  340. UNITY_OUTPUT_CHAR('\'');
  341. UnityPrint(expected);
  342. UNITY_OUTPUT_CHAR('\'');
  343. }
  344. else
  345. {
  346. UnityPrint(UnityStrNull);
  347. }
  348. UnityPrint(UnityStrWas);
  349. if (actual != NULL)
  350. {
  351. UNITY_OUTPUT_CHAR('\'');
  352. UnityPrint(actual);
  353. UNITY_OUTPUT_CHAR('\'');
  354. }
  355. else
  356. {
  357. UnityPrint(UnityStrNull);
  358. }
  359. }
  360. /*-----------------------------------------------*/
  361. static void UnityPrintExpectedAndActualStringsLen(const char* expected, const char* actual, const _UU32 length)
  362. {
  363. UnityPrint(UnityStrExpected);
  364. if (expected != NULL)
  365. {
  366. UNITY_OUTPUT_CHAR('\'');
  367. UnityPrintLen(expected, length);
  368. UNITY_OUTPUT_CHAR('\'');
  369. }
  370. else
  371. {
  372. UnityPrint(UnityStrNull);
  373. }
  374. UnityPrint(UnityStrWas);
  375. if (actual != NULL)
  376. {
  377. UNITY_OUTPUT_CHAR('\'');
  378. UnityPrintLen(actual, length);
  379. UNITY_OUTPUT_CHAR('\'');
  380. }
  381. else
  382. {
  383. UnityPrint(UnityStrNull);
  384. }
  385. }
  386. /*-----------------------------------------------
  387. * Assertion & Control Helpers
  388. *-----------------------------------------------*/
  389. static int UnityCheckArraysForNull(UNITY_INTERNAL_PTR expected, UNITY_INTERNAL_PTR actual, const UNITY_LINE_TYPE lineNumber, const char* msg)
  390. {
  391. /* return true if they are both NULL */
  392. if ((expected == NULL) && (actual == NULL))
  393. return 1;
  394. /* throw error if just expected is NULL */
  395. if (expected == NULL)
  396. {
  397. UnityTestResultsFailBegin(lineNumber);
  398. UnityPrint(UnityStrNullPointerForExpected);
  399. UnityAddMsgIfSpecified(msg);
  400. UNITY_FAIL_AND_BAIL;
  401. }
  402. /* throw error if just actual is NULL */
  403. if (actual == NULL)
  404. {
  405. UnityTestResultsFailBegin(lineNumber);
  406. UnityPrint(UnityStrNullPointerForActual);
  407. UnityAddMsgIfSpecified(msg);
  408. UNITY_FAIL_AND_BAIL;
  409. }
  410. /* return false if neither is NULL */
  411. return 0;
  412. }
  413. /*-----------------------------------------------
  414. * Assertion Functions
  415. *-----------------------------------------------*/
  416. void UnityAssertBits(const _U_SINT mask,
  417. const _U_SINT expected,
  418. const _U_SINT actual,
  419. const char* msg,
  420. const UNITY_LINE_TYPE lineNumber)
  421. {
  422. UNITY_SKIP_EXECUTION;
  423. if ((mask & expected) != (mask & actual))
  424. {
  425. UnityTestResultsFailBegin(lineNumber);
  426. UnityPrint(UnityStrExpected);
  427. UnityPrintMask((_U_UINT)mask, (_U_UINT)expected);
  428. UnityPrint(UnityStrWas);
  429. UnityPrintMask((_U_UINT)mask, (_U_UINT)actual);
  430. UnityAddMsgIfSpecified(msg);
  431. UNITY_FAIL_AND_BAIL;
  432. }
  433. }
  434. /*-----------------------------------------------*/
  435. void UnityAssertEqualNumber(const _U_SINT expected,
  436. const _U_SINT actual,
  437. const char* msg,
  438. const UNITY_LINE_TYPE lineNumber,
  439. const UNITY_DISPLAY_STYLE_T style)
  440. {
  441. UNITY_SKIP_EXECUTION;
  442. if (expected != actual)
  443. {
  444. UnityTestResultsFailBegin(lineNumber);
  445. UnityPrint(UnityStrExpected);
  446. UnityPrintNumberByStyle(expected, style);
  447. UnityPrint(UnityStrWas);
  448. UnityPrintNumberByStyle(actual, style);
  449. UnityAddMsgIfSpecified(msg);
  450. UNITY_FAIL_AND_BAIL;
  451. }
  452. }
  453. #define UnityPrintPointlessAndBail() \
  454. { \
  455. UnityTestResultsFailBegin(lineNumber); \
  456. UnityPrint(UnityStrPointless); \
  457. UnityAddMsgIfSpecified(msg); \
  458. UNITY_FAIL_AND_BAIL; }
  459. /*-----------------------------------------------*/
  460. void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
  461. UNITY_INTERNAL_PTR actual,
  462. const _UU32 num_elements,
  463. const char* msg,
  464. const UNITY_LINE_TYPE lineNumber,
  465. const UNITY_DISPLAY_STYLE_T style)
  466. {
  467. _UU32 elements = num_elements;
  468. UNITY_INTERNAL_PTR ptr_exp = (UNITY_INTERNAL_PTR)expected;
  469. UNITY_INTERNAL_PTR ptr_act = (UNITY_INTERNAL_PTR)actual;
  470. UNITY_SKIP_EXECUTION;
  471. if (elements == 0)
  472. {
  473. UnityPrintPointlessAndBail();
  474. }
  475. if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1)
  476. return;
  477. /* If style is UNITY_DISPLAY_STYLE_INT, we'll fall into the default case rather than the INT16 or INT32 (etc) case
  478. * as UNITY_DISPLAY_STYLE_INT includes a flag for UNITY_DISPLAY_RANGE_AUTO, which the width-specific
  479. * variants do not. Therefore remove this flag. */
  480. switch(style & (UNITY_DISPLAY_STYLE_T)(~UNITY_DISPLAY_RANGE_AUTO))
  481. {
  482. case UNITY_DISPLAY_STYLE_HEX8:
  483. case UNITY_DISPLAY_STYLE_INT8:
  484. case UNITY_DISPLAY_STYLE_UINT8:
  485. while (elements--)
  486. {
  487. if (*(UNITY_PTR_ATTRIBUTE const _US8*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US8*)ptr_act)
  488. {
  489. UnityTestResultsFailBegin(lineNumber);
  490. UnityPrint(UnityStrElement);
  491. UnityPrintNumberUnsigned(num_elements - elements - 1);
  492. UnityPrint(UnityStrExpected);
  493. UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US8*)ptr_exp, style);
  494. UnityPrint(UnityStrWas);
  495. UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US8*)ptr_act, style);
  496. UnityAddMsgIfSpecified(msg);
  497. UNITY_FAIL_AND_BAIL;
  498. }
  499. ptr_exp = (UNITY_INTERNAL_PTR)((_UP)ptr_exp + 1);
  500. ptr_act = (UNITY_INTERNAL_PTR)((_UP)ptr_act + 1);
  501. }
  502. break;
  503. case UNITY_DISPLAY_STYLE_HEX16:
  504. case UNITY_DISPLAY_STYLE_INT16:
  505. case UNITY_DISPLAY_STYLE_UINT16:
  506. while (elements--)
  507. {
  508. if (*(UNITY_PTR_ATTRIBUTE const _US16*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US16*)ptr_act)
  509. {
  510. UnityTestResultsFailBegin(lineNumber);
  511. UnityPrint(UnityStrElement);
  512. UnityPrintNumberUnsigned(num_elements - elements - 1);
  513. UnityPrint(UnityStrExpected);
  514. UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US16*)ptr_exp, style);
  515. UnityPrint(UnityStrWas);
  516. UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US16*)ptr_act, style);
  517. UnityAddMsgIfSpecified(msg);
  518. UNITY_FAIL_AND_BAIL;
  519. }
  520. ptr_exp = (UNITY_INTERNAL_PTR)((_UP)ptr_exp + 2);
  521. ptr_act = (UNITY_INTERNAL_PTR)((_UP)ptr_act + 2);
  522. }
  523. break;
  524. #ifdef UNITY_SUPPORT_64
  525. case UNITY_DISPLAY_STYLE_HEX64:
  526. case UNITY_DISPLAY_STYLE_INT64:
  527. case UNITY_DISPLAY_STYLE_UINT64:
  528. while (elements--)
  529. {
  530. if (*(UNITY_PTR_ATTRIBUTE const _US64*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US64*)ptr_act)
  531. {
  532. UnityTestResultsFailBegin(lineNumber);
  533. UnityPrint(UnityStrElement);
  534. UnityPrintNumberUnsigned(num_elements - elements - 1);
  535. UnityPrint(UnityStrExpected);
  536. UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US64*)ptr_exp, style);
  537. UnityPrint(UnityStrWas);
  538. UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US64*)ptr_act, style);
  539. UnityAddMsgIfSpecified(msg);
  540. UNITY_FAIL_AND_BAIL;
  541. }
  542. ptr_exp = (UNITY_INTERNAL_PTR)((_UP)ptr_exp + 8);
  543. ptr_act = (UNITY_INTERNAL_PTR)((_UP)ptr_act + 8);
  544. }
  545. break;
  546. #endif
  547. default:
  548. while (elements--)
  549. {
  550. if (*(UNITY_PTR_ATTRIBUTE const _US32*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US32*)ptr_act)
  551. {
  552. UnityTestResultsFailBegin(lineNumber);
  553. UnityPrint(UnityStrElement);
  554. UnityPrintNumberUnsigned(num_elements - elements - 1);
  555. UnityPrint(UnityStrExpected);
  556. UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US32*)ptr_exp, style);
  557. UnityPrint(UnityStrWas);
  558. UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US32*)ptr_act, style);
  559. UnityAddMsgIfSpecified(msg);
  560. UNITY_FAIL_AND_BAIL;
  561. }
  562. ptr_exp = (UNITY_INTERNAL_PTR)((_UP)ptr_exp + 4);
  563. ptr_act = (UNITY_INTERNAL_PTR)((_UP)ptr_act + 4);
  564. }
  565. break;
  566. }
  567. }
  568. /*-----------------------------------------------*/
  569. #ifndef UNITY_EXCLUDE_FLOAT
  570. void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const _UF* expected,
  571. UNITY_PTR_ATTRIBUTE const _UF* actual,
  572. const _UU32 num_elements,
  573. const char* msg,
  574. const UNITY_LINE_TYPE lineNumber)
  575. {
  576. _UU32 elements = num_elements;
  577. UNITY_PTR_ATTRIBUTE const _UF* ptr_expected = expected;
  578. UNITY_PTR_ATTRIBUTE const _UF* ptr_actual = actual;
  579. _UF diff, tol;
  580. UNITY_SKIP_EXECUTION;
  581. if (elements == 0)
  582. {
  583. UnityPrintPointlessAndBail();
  584. }
  585. if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1)
  586. return;
  587. while (elements--)
  588. {
  589. diff = *ptr_expected - *ptr_actual;
  590. if (diff < 0.0f)
  591. diff = 0.0f - diff;
  592. tol = UNITY_FLOAT_PRECISION * *ptr_expected;
  593. if (tol < 0.0f)
  594. tol = 0.0f - tol;
  595. /* This first part of this condition will catch any NaN or Infinite values */
  596. if (isnan(diff) || isinf(diff) || (diff > tol))
  597. {
  598. UnityTestResultsFailBegin(lineNumber);
  599. UnityPrint(UnityStrElement);
  600. UnityPrintNumberUnsigned(num_elements - elements - 1);
  601. #ifdef UNITY_FLOAT_VERBOSE
  602. UnityPrint(UnityStrExpected);
  603. UnityPrintFloat(*ptr_expected);
  604. UnityPrint(UnityStrWas);
  605. UnityPrintFloat(*ptr_actual);
  606. #else
  607. UnityPrint(UnityStrDelta);
  608. #endif
  609. UnityAddMsgIfSpecified(msg);
  610. UNITY_FAIL_AND_BAIL;
  611. }
  612. ptr_expected++;
  613. ptr_actual++;
  614. }
  615. }
  616. /*-----------------------------------------------*/
  617. void UnityAssertFloatsWithin(const _UF delta,
  618. const _UF expected,
  619. const _UF actual,
  620. const char* msg,
  621. const UNITY_LINE_TYPE lineNumber)
  622. {
  623. _UF diff = actual - expected;
  624. _UF pos_delta = delta;
  625. UNITY_SKIP_EXECUTION;
  626. if (diff < 0.0f)
  627. {
  628. diff = 0.0f - diff;
  629. }
  630. if (pos_delta < 0.0f)
  631. {
  632. pos_delta = 0.0f - pos_delta;
  633. }
  634. /* This first part of this condition will catch any NaN or Infinite values */
  635. if (isnan(diff) || isinf(diff) || (pos_delta < diff))
  636. {
  637. UnityTestResultsFailBegin(lineNumber);
  638. #ifdef UNITY_FLOAT_VERBOSE
  639. UnityPrint(UnityStrExpected);
  640. UnityPrintFloat(expected);
  641. UnityPrint(UnityStrWas);
  642. UnityPrintFloat(actual);
  643. #else
  644. UnityPrint(UnityStrDelta);
  645. #endif
  646. UnityAddMsgIfSpecified(msg);
  647. UNITY_FAIL_AND_BAIL;
  648. }
  649. }
  650. /*-----------------------------------------------*/
  651. void UnityAssertFloatSpecial(const _UF actual,
  652. const char* msg,
  653. const UNITY_LINE_TYPE lineNumber,
  654. const UNITY_FLOAT_TRAIT_T style)
  655. {
  656. const char* trait_names[] = { UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet };
  657. _U_SINT should_be_trait = ((_U_SINT)style & 1);
  658. _U_SINT is_trait = !should_be_trait;
  659. _U_SINT trait_index = (_U_SINT)(style >> 1);
  660. UNITY_SKIP_EXECUTION;
  661. switch(style)
  662. {
  663. /* To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly
  664. * We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise */
  665. case UNITY_FLOAT_IS_INF:
  666. case UNITY_FLOAT_IS_NOT_INF:
  667. is_trait = isinf(actual) & ispos(actual);
  668. break;
  669. case UNITY_FLOAT_IS_NEG_INF:
  670. case UNITY_FLOAT_IS_NOT_NEG_INF:
  671. is_trait = isinf(actual) & isneg(actual);
  672. break;
  673. /* NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN. */
  674. case UNITY_FLOAT_IS_NAN:
  675. case UNITY_FLOAT_IS_NOT_NAN:
  676. is_trait = isnan(actual);
  677. break;
  678. /* A determinate number is non infinite and not NaN. (therefore the opposite of the two above) */
  679. case UNITY_FLOAT_IS_DET:
  680. case UNITY_FLOAT_IS_NOT_DET:
  681. if (isinf(actual) | isnan(actual))
  682. is_trait = 0;
  683. else
  684. is_trait = 1;
  685. break;
  686. default:
  687. trait_index = 0;
  688. trait_names[0] = UnityStrInvalidFloatTrait;
  689. break;
  690. }
  691. if (is_trait != should_be_trait)
  692. {
  693. UnityTestResultsFailBegin(lineNumber);
  694. UnityPrint(UnityStrExpected);
  695. if (!should_be_trait)
  696. UnityPrint(UnityStrNot);
  697. UnityPrint(trait_names[trait_index]);
  698. UnityPrint(UnityStrWas);
  699. #ifdef UNITY_FLOAT_VERBOSE
  700. UnityPrintFloat(actual);
  701. #else
  702. if (should_be_trait)
  703. UnityPrint(UnityStrNot);
  704. UnityPrint(trait_names[trait_index]);
  705. #endif
  706. UnityAddMsgIfSpecified(msg);
  707. UNITY_FAIL_AND_BAIL;
  708. }
  709. }
  710. #endif /* not UNITY_EXCLUDE_FLOAT */
  711. /*-----------------------------------------------*/
  712. #ifndef UNITY_EXCLUDE_DOUBLE
  713. void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected,
  714. UNITY_PTR_ATTRIBUTE const _UD* actual,
  715. const _UU32 num_elements,
  716. const char* msg,
  717. const UNITY_LINE_TYPE lineNumber)
  718. {
  719. _UU32 elements = num_elements;
  720. UNITY_PTR_ATTRIBUTE const _UD* ptr_expected = expected;
  721. UNITY_PTR_ATTRIBUTE const _UD* ptr_actual = actual;
  722. _UD diff, tol;
  723. UNITY_SKIP_EXECUTION;
  724. if (elements == 0)
  725. {
  726. UnityPrintPointlessAndBail();
  727. }
  728. if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1)
  729. return;
  730. while (elements--)
  731. {
  732. diff = *ptr_expected - *ptr_actual;
  733. if (diff < 0.0)
  734. diff = 0.0 - diff;
  735. tol = UNITY_DOUBLE_PRECISION * *ptr_expected;
  736. if (tol < 0.0)
  737. tol = 0.0 - tol;
  738. /* This first part of this condition will catch any NaN or Infinite values */
  739. if (isnan(diff) || isinf(diff) || (diff > tol))
  740. {
  741. UnityTestResultsFailBegin(lineNumber);
  742. UnityPrint(UnityStrElement);
  743. UnityPrintNumberUnsigned(num_elements - elements - 1);
  744. #ifdef UNITY_DOUBLE_VERBOSE
  745. UnityPrint(UnityStrExpected);
  746. UnityPrintFloat((float)(*ptr_expected));
  747. UnityPrint(UnityStrWas);
  748. UnityPrintFloat((float)(*ptr_actual));
  749. #else
  750. UnityPrint(UnityStrDelta);
  751. #endif
  752. UnityAddMsgIfSpecified(msg);
  753. UNITY_FAIL_AND_BAIL;
  754. }
  755. ptr_expected++;
  756. ptr_actual++;
  757. }
  758. }
  759. /*-----------------------------------------------*/
  760. void UnityAssertDoublesWithin(const _UD delta,
  761. const _UD expected,
  762. const _UD actual,
  763. const char* msg,
  764. const UNITY_LINE_TYPE lineNumber)
  765. {
  766. _UD diff = actual - expected;
  767. _UD pos_delta = delta;
  768. UNITY_SKIP_EXECUTION;
  769. if (diff < 0.0)
  770. {
  771. diff = 0.0 - diff;
  772. }
  773. if (pos_delta < 0.0)
  774. {
  775. pos_delta = 0.0 - pos_delta;
  776. }
  777. /* This first part of this condition will catch any NaN or Infinite values */
  778. if (isnan(diff) || isinf(diff) || (pos_delta < diff))
  779. {
  780. UnityTestResultsFailBegin(lineNumber);
  781. #ifdef UNITY_DOUBLE_VERBOSE
  782. UnityPrint(UnityStrExpected);
  783. UnityPrintFloat((float)expected);
  784. UnityPrint(UnityStrWas);
  785. UnityPrintFloat((float)actual);
  786. #else
  787. UnityPrint(UnityStrDelta);
  788. #endif
  789. UnityAddMsgIfSpecified(msg);
  790. UNITY_FAIL_AND_BAIL;
  791. }
  792. }
  793. /*-----------------------------------------------*/
  794. void UnityAssertDoubleSpecial(const _UD actual,
  795. const char* msg,
  796. const UNITY_LINE_TYPE lineNumber,
  797. const UNITY_FLOAT_TRAIT_T style)
  798. {
  799. const char* trait_names[] = { UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet };
  800. _U_SINT should_be_trait = ((_U_SINT)style & 1);
  801. _U_SINT is_trait = !should_be_trait;
  802. _U_SINT trait_index = (_U_SINT)(style >> 1);
  803. UNITY_SKIP_EXECUTION;
  804. switch(style)
  805. {
  806. /* To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly
  807. * We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise */
  808. case UNITY_FLOAT_IS_INF:
  809. case UNITY_FLOAT_IS_NOT_INF:
  810. is_trait = isinf(actual) & ispos(actual);
  811. break;
  812. case UNITY_FLOAT_IS_NEG_INF:
  813. case UNITY_FLOAT_IS_NOT_NEG_INF:
  814. is_trait = isinf(actual) & isneg(actual);
  815. break;
  816. /* NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN. */
  817. case UNITY_FLOAT_IS_NAN:
  818. case UNITY_FLOAT_IS_NOT_NAN:
  819. is_trait = isnan(actual);
  820. break;
  821. /* A determinate number is non infinite and not NaN. (therefore the opposite of the two above) */
  822. case UNITY_FLOAT_IS_DET:
  823. case UNITY_FLOAT_IS_NOT_DET:
  824. if (isinf(actual) | isnan(actual))
  825. is_trait = 0;
  826. else
  827. is_trait = 1;
  828. break;
  829. default:
  830. trait_index = 0;
  831. trait_names[0] = UnityStrInvalidFloatTrait;
  832. break;
  833. }
  834. if (is_trait != should_be_trait)
  835. {
  836. UnityTestResultsFailBegin(lineNumber);
  837. UnityPrint(UnityStrExpected);
  838. if (!should_be_trait)
  839. UnityPrint(UnityStrNot);
  840. UnityPrint(trait_names[trait_index]);
  841. UnityPrint(UnityStrWas);
  842. #ifdef UNITY_DOUBLE_VERBOSE
  843. UnityPrintFloat(actual);
  844. #else
  845. if (should_be_trait)
  846. UnityPrint(UnityStrNot);
  847. UnityPrint(trait_names[trait_index]);
  848. #endif
  849. UnityAddMsgIfSpecified(msg);
  850. UNITY_FAIL_AND_BAIL;
  851. }
  852. }
  853. #endif /* not UNITY_EXCLUDE_DOUBLE */
  854. /*-----------------------------------------------*/
  855. void UnityAssertNumbersWithin( const _U_UINT delta,
  856. const _U_SINT expected,
  857. const _U_SINT actual,
  858. const char* msg,
  859. const UNITY_LINE_TYPE lineNumber,
  860. const UNITY_DISPLAY_STYLE_T style)
  861. {
  862. UNITY_SKIP_EXECUTION;
  863. if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
  864. {
  865. if (actual > expected)
  866. Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > delta);
  867. else
  868. Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > delta);
  869. }
  870. else
  871. {
  872. if ((_U_UINT)actual > (_U_UINT)expected)
  873. Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > delta);
  874. else
  875. Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > delta);
  876. }
  877. if (Unity.CurrentTestFailed)
  878. {
  879. UnityTestResultsFailBegin(lineNumber);
  880. UnityPrint(UnityStrDelta);
  881. UnityPrintNumberByStyle((_U_SINT)delta, style);
  882. UnityPrint(UnityStrExpected);
  883. UnityPrintNumberByStyle(expected, style);
  884. UnityPrint(UnityStrWas);
  885. UnityPrintNumberByStyle(actual, style);
  886. UnityAddMsgIfSpecified(msg);
  887. UNITY_FAIL_AND_BAIL;
  888. }
  889. }
  890. /*-----------------------------------------------*/
  891. void UnityAssertEqualString(const char* expected,
  892. const char* actual,
  893. const char* msg,
  894. const UNITY_LINE_TYPE lineNumber)
  895. {
  896. _UU32 i;
  897. UNITY_SKIP_EXECUTION;
  898. /* if both pointers not null compare the strings */
  899. if (expected && actual)
  900. {
  901. for (i = 0; expected[i] || actual[i]; i++)
  902. {
  903. if (expected[i] != actual[i])
  904. {
  905. Unity.CurrentTestFailed = 1;
  906. break;
  907. }
  908. }
  909. }
  910. else
  911. { /* handle case of one pointers being null (if both null, test should pass) */
  912. if (expected != actual)
  913. {
  914. Unity.CurrentTestFailed = 1;
  915. }
  916. }
  917. if (Unity.CurrentTestFailed)
  918. {
  919. UnityTestResultsFailBegin(lineNumber);
  920. UnityPrintExpectedAndActualStrings(expected, actual);
  921. UnityAddMsgIfSpecified(msg);
  922. UNITY_FAIL_AND_BAIL;
  923. }
  924. }
  925. /*-----------------------------------------------*/
  926. void UnityAssertEqualStringLen(const char* expected,
  927. const char* actual,
  928. const _UU32 length,
  929. const char* msg,
  930. const UNITY_LINE_TYPE lineNumber)
  931. {
  932. _UU32 i;
  933. UNITY_SKIP_EXECUTION;
  934. /* if both pointers not null compare the strings */
  935. if (expected && actual)
  936. {
  937. for (i = 0; (expected[i] || actual[i]) && i < length; i++)
  938. {
  939. if (expected[i] != actual[i])
  940. {
  941. Unity.CurrentTestFailed = 1;
  942. break;
  943. }
  944. }
  945. }
  946. else
  947. { /* handle case of one pointers being null (if both null, test should pass) */
  948. if (expected != actual)
  949. {
  950. Unity.CurrentTestFailed = 1;
  951. }
  952. }
  953. if (Unity.CurrentTestFailed)
  954. {
  955. UnityTestResultsFailBegin(lineNumber);
  956. UnityPrintExpectedAndActualStringsLen(expected, actual, length);
  957. UnityAddMsgIfSpecified(msg);
  958. UNITY_FAIL_AND_BAIL;
  959. }
  960. }
  961. /*-----------------------------------------------*/
  962. void UnityAssertEqualStringArray( const char** expected,
  963. const char** actual,
  964. const _UU32 num_elements,
  965. const char* msg,
  966. const UNITY_LINE_TYPE lineNumber)
  967. {
  968. _UU32 i, j = 0;
  969. UNITY_SKIP_EXECUTION;
  970. /* if no elements, it's an error */
  971. if (num_elements == 0)
  972. {
  973. UnityPrintPointlessAndBail();
  974. }
  975. if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1)
  976. return;
  977. do
  978. {
  979. /* if both pointers not null compare the strings */
  980. if (expected[j] && actual[j])
  981. {
  982. for (i = 0; expected[j][i] || actual[j][i]; i++)
  983. {
  984. if (expected[j][i] != actual[j][i])
  985. {
  986. Unity.CurrentTestFailed = 1;
  987. break;
  988. }
  989. }
  990. }
  991. else
  992. { /* handle case of one pointers being null (if both null, test should pass) */
  993. if (expected[j] != actual[j])
  994. {
  995. Unity.CurrentTestFailed = 1;
  996. }
  997. }
  998. if (Unity.CurrentTestFailed)
  999. {
  1000. UnityTestResultsFailBegin(lineNumber);
  1001. if (num_elements > 1)
  1002. {
  1003. UnityPrint(UnityStrElement);
  1004. UnityPrintNumberUnsigned(j);
  1005. }
  1006. UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j]));
  1007. UnityAddMsgIfSpecified(msg);
  1008. UNITY_FAIL_AND_BAIL;
  1009. }
  1010. } while (++j < num_elements);
  1011. }
  1012. /*-----------------------------------------------*/
  1013. void UnityAssertEqualMemory( UNITY_INTERNAL_PTR expected,
  1014. UNITY_INTERNAL_PTR actual,
  1015. const _UU32 length,
  1016. const _UU32 num_elements,
  1017. const char* msg,
  1018. const UNITY_LINE_TYPE lineNumber)
  1019. {
  1020. UNITY_PTR_ATTRIBUTE const unsigned char* ptr_exp = (UNITY_PTR_ATTRIBUTE const unsigned char*)expected;
  1021. UNITY_PTR_ATTRIBUTE const unsigned char* ptr_act = (UNITY_PTR_ATTRIBUTE const unsigned char*)actual;
  1022. _UU32 elements = num_elements;
  1023. _UU32 bytes;
  1024. UNITY_SKIP_EXECUTION;
  1025. if ((elements == 0) || (length == 0))
  1026. {
  1027. UnityPrintPointlessAndBail();
  1028. }
  1029. if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1)
  1030. return;
  1031. while (elements--)
  1032. {
  1033. /* /////////////////////////////////// */
  1034. bytes = length;
  1035. while (bytes--)
  1036. {
  1037. if (*ptr_exp != *ptr_act)
  1038. {
  1039. UnityTestResultsFailBegin(lineNumber);
  1040. UnityPrint(UnityStrMemory);
  1041. if (num_elements > 1)
  1042. {
  1043. UnityPrint(UnityStrElement);
  1044. UnityPrintNumberUnsigned(num_elements - elements - 1);
  1045. }
  1046. UnityPrint(UnityStrByte);
  1047. UnityPrintNumberUnsigned(length - bytes - 1);
  1048. UnityPrint(UnityStrExpected);
  1049. UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
  1050. UnityPrint(UnityStrWas);
  1051. UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
  1052. UnityAddMsgIfSpecified(msg);
  1053. UNITY_FAIL_AND_BAIL;
  1054. }
  1055. ptr_exp = (UNITY_INTERNAL_PTR)((_UP)ptr_exp + 1);
  1056. ptr_act = (UNITY_INTERNAL_PTR)((_UP)ptr_act + 1);
  1057. }
  1058. /* /////////////////////////////////// */
  1059. }
  1060. }
  1061. /*-----------------------------------------------
  1062. * Control Functions
  1063. *-----------------------------------------------*/
  1064. void UnityFail(const char* msg, const UNITY_LINE_TYPE line)
  1065. {
  1066. UNITY_SKIP_EXECUTION;
  1067. UnityTestResultsBegin(Unity.TestFile, line);
  1068. UnityPrintFail();
  1069. if (msg != NULL)
  1070. {
  1071. UNITY_OUTPUT_CHAR(':');
  1072. #ifndef UNITY_EXCLUDE_DETAILS
  1073. if (Unity.CurrentDetail1)
  1074. {
  1075. UnityPrint(UnityStrDetail1Name);
  1076. UnityPrint(Unity.CurrentDetail1);
  1077. if (Unity.CurrentDetail2)
  1078. {
  1079. UnityPrint(UnityStrDetail2Name);
  1080. UnityPrint(Unity.CurrentDetail2);
  1081. }
  1082. UnityPrint(UnityStrSpacer);
  1083. }
  1084. #endif
  1085. if (msg[0] != ' ')
  1086. {
  1087. UNITY_OUTPUT_CHAR(' ');
  1088. }
  1089. UnityPrint(msg);
  1090. }
  1091. UNITY_FAIL_AND_BAIL;
  1092. }
  1093. /*-----------------------------------------------*/
  1094. void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
  1095. {
  1096. UNITY_SKIP_EXECUTION;
  1097. UnityTestResultsBegin(Unity.TestFile, line);
  1098. UnityPrint(UnityStrIgnore);
  1099. if (msg != NULL)
  1100. {
  1101. UNITY_OUTPUT_CHAR(':');
  1102. UNITY_OUTPUT_CHAR(' ');
  1103. UnityPrint(msg);
  1104. }
  1105. UNITY_IGNORE_AND_BAIL;
  1106. }
  1107. /*-----------------------------------------------*/
  1108. #if defined(UNITY_WEAK_ATTRIBUTE)
  1109. UNITY_WEAK_ATTRIBUTE void setUp(void) { }
  1110. UNITY_WEAK_ATTRIBUTE void tearDown(void) { }
  1111. #elif defined(UNITY_WEAK_PRAGMA)
  1112. # pragma weak setUp
  1113. void setUp(void) { }
  1114. # pragma weak tearDown
  1115. void tearDown(void) { }
  1116. #endif
  1117. /*-----------------------------------------------*/
  1118. void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
  1119. {
  1120. Unity.CurrentTestName = FuncName;
  1121. Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum;
  1122. Unity.NumberOfTests++;
  1123. UNITY_CLR_DETAILS();
  1124. if (TEST_PROTECT())
  1125. {
  1126. setUp();
  1127. Func();
  1128. }
  1129. if (TEST_PROTECT() && !(Unity.CurrentTestIgnored))
  1130. {
  1131. tearDown();
  1132. }
  1133. UnityConcludeTest();
  1134. }
  1135. /*-----------------------------------------------*/
  1136. void UnityBegin(const char* filename)
  1137. {
  1138. Unity.TestFile = filename;
  1139. Unity.CurrentTestName = NULL;
  1140. Unity.CurrentTestLineNumber = 0;
  1141. Unity.NumberOfTests = 0;
  1142. Unity.TestFailures = 0;
  1143. Unity.TestIgnores = 0;
  1144. Unity.CurrentTestFailed = 0;
  1145. Unity.CurrentTestIgnored = 0;
  1146. UNITY_CLR_DETAILS();
  1147. UNITY_OUTPUT_START();
  1148. }
  1149. /*-----------------------------------------------*/
  1150. int UnityEnd(void)
  1151. {
  1152. UNITY_PRINT_EOL();
  1153. UnityPrint(UnityStrBreaker);
  1154. UNITY_PRINT_EOL();
  1155. UnityPrintNumber((_U_SINT)(Unity.NumberOfTests));
  1156. UnityPrint(UnityStrResultsTests);
  1157. UnityPrintNumber((_U_SINT)(Unity.TestFailures));
  1158. UnityPrint(UnityStrResultsFailures);
  1159. UnityPrintNumber((_U_SINT)(Unity.TestIgnores));
  1160. UnityPrint(UnityStrResultsIgnored);
  1161. UNITY_PRINT_EOL();
  1162. if (Unity.TestFailures == 0U)
  1163. {
  1164. UnityPrintOk();
  1165. }
  1166. else
  1167. {
  1168. UnityPrintFail();
  1169. #ifdef UNITY_DIFFERENTIATE_FINAL_FAIL
  1170. UNITY_OUTPUT_CHAR('E'); UNITY_OUTPUT_CHAR('D');
  1171. #endif
  1172. }
  1173. UNITY_PRINT_EOL();
  1174. UNITY_OUTPUT_FLUSH();
  1175. UNITY_OUTPUT_COMPLETE();
  1176. return (int)(Unity.TestFailures);
  1177. }
  1178. /*-----------------------------------------------*/