unity.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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_OUTPUT_CHAR('\n');
  401. UNITY_FAIL_AND_BAIL;
  402. }
  403. /* throw error if just actual is NULL */
  404. if (actual == NULL)
  405. {
  406. UnityTestResultsFailBegin(lineNumber);
  407. UnityPrint(UnityStrNullPointerForActual);
  408. UnityAddMsgIfSpecified(msg);
  409. UNITY_OUTPUT_CHAR('\n');
  410. UNITY_FAIL_AND_BAIL;
  411. }
  412. /* return false if neither is NULL */
  413. return 0;
  414. }
  415. /*-----------------------------------------------
  416. * Assertion Functions
  417. *-----------------------------------------------*/
  418. void UnityAssertBits(const _U_SINT mask,
  419. const _U_SINT expected,
  420. const _U_SINT actual,
  421. const char* msg,
  422. const UNITY_LINE_TYPE lineNumber)
  423. {
  424. UNITY_SKIP_EXECUTION;
  425. if ((mask & expected) != (mask & actual))
  426. {
  427. UnityTestResultsFailBegin(lineNumber);
  428. UnityPrint(UnityStrExpected);
  429. UnityPrintMask((_U_UINT)mask, (_U_UINT)expected);
  430. UnityPrint(UnityStrWas);
  431. UnityPrintMask((_U_UINT)mask, (_U_UINT)actual);
  432. UnityAddMsgIfSpecified(msg);
  433. UNITY_OUTPUT_CHAR('\n');
  434. UNITY_FAIL_AND_BAIL;
  435. }
  436. }
  437. /*-----------------------------------------------*/
  438. void UnityAssertEqualNumber(const _U_SINT expected,
  439. const _U_SINT actual,
  440. const char* msg,
  441. const UNITY_LINE_TYPE lineNumber,
  442. const UNITY_DISPLAY_STYLE_T style)
  443. {
  444. UNITY_SKIP_EXECUTION;
  445. if (expected != actual)
  446. {
  447. UnityTestResultsFailBegin(lineNumber);
  448. UnityPrint(UnityStrExpected);
  449. UnityPrintNumberByStyle(expected, style);
  450. UnityPrint(UnityStrWas);
  451. UnityPrintNumberByStyle(actual, style);
  452. UnityAddMsgIfSpecified(msg);
  453. UNITY_OUTPUT_CHAR('\n');
  454. UNITY_FAIL_AND_BAIL;
  455. }
  456. }
  457. #define UnityPrintPointlessAndBail() \
  458. { \
  459. UnityTestResultsFailBegin(lineNumber); \
  460. UnityPrint(UnityStrPointless); \
  461. UnityAddMsgIfSpecified(msg); \
  462. UNITY_OUTPUT_CHAR('\n'); \
  463. UNITY_FAIL_AND_BAIL; }
  464. /*-----------------------------------------------*/
  465. void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
  466. UNITY_INTERNAL_PTR actual,
  467. const _UU32 num_elements,
  468. const char* msg,
  469. const UNITY_LINE_TYPE lineNumber,
  470. const UNITY_DISPLAY_STYLE_T style)
  471. {
  472. _UU32 elements = num_elements;
  473. UNITY_INTERNAL_PTR ptr_exp = (UNITY_INTERNAL_PTR)expected;
  474. UNITY_INTERNAL_PTR ptr_act = (UNITY_INTERNAL_PTR)actual;
  475. UNITY_SKIP_EXECUTION;
  476. if (elements == 0)
  477. {
  478. UnityPrintPointlessAndBail();
  479. }
  480. if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1)
  481. return;
  482. /* If style is UNITY_DISPLAY_STYLE_INT, we'll fall into the default case rather than the INT16 or INT32 (etc) case
  483. * as UNITY_DISPLAY_STYLE_INT includes a flag for UNITY_DISPLAY_RANGE_AUTO, which the width-specific
  484. * variants do not. Therefore remove this flag. */
  485. switch(style & (UNITY_DISPLAY_STYLE_T)(~UNITY_DISPLAY_RANGE_AUTO))
  486. {
  487. case UNITY_DISPLAY_STYLE_HEX8:
  488. case UNITY_DISPLAY_STYLE_INT8:
  489. case UNITY_DISPLAY_STYLE_UINT8:
  490. while (elements--)
  491. {
  492. if (*(UNITY_PTR_ATTRIBUTE const _US8*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US8*)ptr_act)
  493. {
  494. UnityTestResultsFailBegin(lineNumber);
  495. UnityPrint(UnityStrElement);
  496. UnityPrintNumberUnsigned(num_elements - elements - 1);
  497. UnityPrint(UnityStrExpected);
  498. UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US8*)ptr_exp, style);
  499. UnityPrint(UnityStrWas);
  500. UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US8*)ptr_act, style);
  501. UnityAddMsgIfSpecified(msg);
  502. UNITY_OUTPUT_CHAR('\n');
  503. UNITY_FAIL_AND_BAIL;
  504. }
  505. ptr_exp = (UNITY_INTERNAL_PTR)((_UP)ptr_exp + 1);
  506. ptr_act = (UNITY_INTERNAL_PTR)((_UP)ptr_act + 1);
  507. }
  508. break;
  509. case UNITY_DISPLAY_STYLE_HEX16:
  510. case UNITY_DISPLAY_STYLE_INT16:
  511. case UNITY_DISPLAY_STYLE_UINT16:
  512. while (elements--)
  513. {
  514. if (*(UNITY_PTR_ATTRIBUTE const _US16*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US16*)ptr_act)
  515. {
  516. UnityTestResultsFailBegin(lineNumber);
  517. UnityPrint(UnityStrElement);
  518. UnityPrintNumberUnsigned(num_elements - elements - 1);
  519. UnityPrint(UnityStrExpected);
  520. UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US16*)ptr_exp, style);
  521. UnityPrint(UnityStrWas);
  522. UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US16*)ptr_act, style);
  523. UnityAddMsgIfSpecified(msg);
  524. UNITY_OUTPUT_CHAR('\n');
  525. UNITY_FAIL_AND_BAIL;
  526. }
  527. ptr_exp = (UNITY_INTERNAL_PTR)((_UP)ptr_exp + 2);
  528. ptr_act = (UNITY_INTERNAL_PTR)((_UP)ptr_act + 2);
  529. }
  530. break;
  531. #ifdef UNITY_SUPPORT_64
  532. case UNITY_DISPLAY_STYLE_HEX64:
  533. case UNITY_DISPLAY_STYLE_INT64:
  534. case UNITY_DISPLAY_STYLE_UINT64:
  535. while (elements--)
  536. {
  537. if (*(UNITY_PTR_ATTRIBUTE const _US64*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US64*)ptr_act)
  538. {
  539. UnityTestResultsFailBegin(lineNumber);
  540. UnityPrint(UnityStrElement);
  541. UnityPrintNumberUnsigned(num_elements - elements - 1);
  542. UnityPrint(UnityStrExpected);
  543. UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US64*)ptr_exp, style);
  544. UnityPrint(UnityStrWas);
  545. UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US64*)ptr_act, style);
  546. UnityAddMsgIfSpecified(msg);
  547. UNITY_OUTPUT_CHAR('\n');
  548. UNITY_FAIL_AND_BAIL;
  549. }
  550. ptr_exp = (UNITY_INTERNAL_PTR)((_UP)ptr_exp + 8);
  551. ptr_act = (UNITY_INTERNAL_PTR)((_UP)ptr_act + 8);
  552. }
  553. break;
  554. #endif
  555. default:
  556. while (elements--)
  557. {
  558. if (*(UNITY_PTR_ATTRIBUTE const _US32*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const _US32*)ptr_act)
  559. {
  560. UnityTestResultsFailBegin(lineNumber);
  561. UnityPrint(UnityStrElement);
  562. UnityPrintNumberUnsigned(num_elements - elements - 1);
  563. UnityPrint(UnityStrExpected);
  564. UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US32*)ptr_exp, style);
  565. UnityPrint(UnityStrWas);
  566. UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const _US32*)ptr_act, style);
  567. UnityAddMsgIfSpecified(msg);
  568. UNITY_OUTPUT_CHAR('\n');
  569. UNITY_FAIL_AND_BAIL;
  570. }
  571. ptr_exp = (UNITY_INTERNAL_PTR)((_UP)ptr_exp + 4);
  572. ptr_act = (UNITY_INTERNAL_PTR)((_UP)ptr_act + 4);
  573. }
  574. break;
  575. }
  576. }
  577. /*-----------------------------------------------*/
  578. #ifndef UNITY_EXCLUDE_FLOAT
  579. void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const _UF* expected,
  580. UNITY_PTR_ATTRIBUTE const _UF* actual,
  581. const _UU32 num_elements,
  582. const char* msg,
  583. const UNITY_LINE_TYPE lineNumber)
  584. {
  585. _UU32 elements = num_elements;
  586. UNITY_PTR_ATTRIBUTE const _UF* ptr_expected = expected;
  587. UNITY_PTR_ATTRIBUTE const _UF* ptr_actual = actual;
  588. _UF diff, tol;
  589. UNITY_SKIP_EXECUTION;
  590. if (elements == 0)
  591. {
  592. UnityPrintPointlessAndBail();
  593. }
  594. if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1)
  595. return;
  596. while (elements--)
  597. {
  598. diff = *ptr_expected - *ptr_actual;
  599. if (diff < 0.0f)
  600. diff = 0.0f - diff;
  601. tol = UNITY_FLOAT_PRECISION * *ptr_expected;
  602. if (tol < 0.0f)
  603. tol = 0.0f - tol;
  604. /* This first part of this condition will catch any NaN or Infinite values */
  605. if (isnan(diff) || isinf(diff) || (diff > tol))
  606. {
  607. UnityTestResultsFailBegin(lineNumber);
  608. UnityPrint(UnityStrElement);
  609. UnityPrintNumberUnsigned(num_elements - elements - 1);
  610. #ifdef UNITY_FLOAT_VERBOSE
  611. UnityPrint(UnityStrExpected);
  612. UnityPrintFloat(*ptr_expected);
  613. UnityPrint(UnityStrWas);
  614. UnityPrintFloat(*ptr_actual);
  615. #else
  616. UnityPrint(UnityStrDelta);
  617. #endif
  618. UnityAddMsgIfSpecified(msg);
  619. UNITY_OUTPUT_CHAR('\n');
  620. UNITY_FAIL_AND_BAIL;
  621. }
  622. ptr_expected++;
  623. ptr_actual++;
  624. }
  625. }
  626. /*-----------------------------------------------*/
  627. void UnityAssertFloatsWithin(const _UF delta,
  628. const _UF expected,
  629. const _UF actual,
  630. const char* msg,
  631. const UNITY_LINE_TYPE lineNumber)
  632. {
  633. _UF diff = actual - expected;
  634. _UF pos_delta = delta;
  635. UNITY_SKIP_EXECUTION;
  636. if (diff < 0.0f)
  637. {
  638. diff = 0.0f - diff;
  639. }
  640. if (pos_delta < 0.0f)
  641. {
  642. pos_delta = 0.0f - pos_delta;
  643. }
  644. /* This first part of this condition will catch any NaN or Infinite values */
  645. if (isnan(diff) || isinf(diff) || (pos_delta < diff))
  646. {
  647. UnityTestResultsFailBegin(lineNumber);
  648. #ifdef UNITY_FLOAT_VERBOSE
  649. UnityPrint(UnityStrExpected);
  650. UnityPrintFloat(expected);
  651. UnityPrint(UnityStrWas);
  652. UnityPrintFloat(actual);
  653. #else
  654. UnityPrint(UnityStrDelta);
  655. #endif
  656. UnityAddMsgIfSpecified(msg);
  657. UNITY_OUTPUT_CHAR('\n');
  658. UNITY_FAIL_AND_BAIL;
  659. }
  660. }
  661. /*-----------------------------------------------*/
  662. void UnityAssertFloatSpecial(const _UF actual,
  663. const char* msg,
  664. const UNITY_LINE_TYPE lineNumber,
  665. const UNITY_FLOAT_TRAIT_T style)
  666. {
  667. const char* trait_names[] = { UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet };
  668. _U_SINT should_be_trait = ((_U_SINT)style & 1);
  669. _U_SINT is_trait = !should_be_trait;
  670. _U_SINT trait_index = (_U_SINT)(style >> 1);
  671. UNITY_SKIP_EXECUTION;
  672. switch(style)
  673. {
  674. /* To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly
  675. * We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise */
  676. case UNITY_FLOAT_IS_INF:
  677. case UNITY_FLOAT_IS_NOT_INF:
  678. is_trait = isinf(actual) & ispos(actual);
  679. break;
  680. case UNITY_FLOAT_IS_NEG_INF:
  681. case UNITY_FLOAT_IS_NOT_NEG_INF:
  682. is_trait = isinf(actual) & isneg(actual);
  683. break;
  684. /* NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN. */
  685. case UNITY_FLOAT_IS_NAN:
  686. case UNITY_FLOAT_IS_NOT_NAN:
  687. is_trait = isnan(actual);
  688. break;
  689. /* A determinate number is non infinite and not NaN. (therefore the opposite of the two above) */
  690. case UNITY_FLOAT_IS_DET:
  691. case UNITY_FLOAT_IS_NOT_DET:
  692. if (isinf(actual) | isnan(actual))
  693. is_trait = 0;
  694. else
  695. is_trait = 1;
  696. break;
  697. default:
  698. trait_index = 0;
  699. trait_names[0] = UnityStrInvalidFloatTrait;
  700. break;
  701. }
  702. if (is_trait != should_be_trait)
  703. {
  704. UnityTestResultsFailBegin(lineNumber);
  705. UnityPrint(UnityStrExpected);
  706. if (!should_be_trait)
  707. UnityPrint(UnityStrNot);
  708. UnityPrint(trait_names[trait_index]);
  709. UnityPrint(UnityStrWas);
  710. #ifdef UNITY_FLOAT_VERBOSE
  711. UnityPrintFloat(actual);
  712. #else
  713. if (should_be_trait)
  714. UnityPrint(UnityStrNot);
  715. UnityPrint(trait_names[trait_index]);
  716. #endif
  717. UnityAddMsgIfSpecified(msg);
  718. UNITY_OUTPUT_CHAR('\n');
  719. UNITY_FAIL_AND_BAIL;
  720. }
  721. }
  722. #endif /* not UNITY_EXCLUDE_FLOAT */
  723. /*-----------------------------------------------*/
  724. #ifndef UNITY_EXCLUDE_DOUBLE
  725. void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected,
  726. UNITY_PTR_ATTRIBUTE const _UD* actual,
  727. const _UU32 num_elements,
  728. const char* msg,
  729. const UNITY_LINE_TYPE lineNumber)
  730. {
  731. _UU32 elements = num_elements;
  732. UNITY_PTR_ATTRIBUTE const _UD* ptr_expected = expected;
  733. UNITY_PTR_ATTRIBUTE const _UD* ptr_actual = actual;
  734. _UD diff, tol;
  735. UNITY_SKIP_EXECUTION;
  736. if (elements == 0)
  737. {
  738. UnityPrintPointlessAndBail();
  739. }
  740. if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1)
  741. return;
  742. while (elements--)
  743. {
  744. diff = *ptr_expected - *ptr_actual;
  745. if (diff < 0.0)
  746. diff = 0.0 - diff;
  747. tol = UNITY_DOUBLE_PRECISION * *ptr_expected;
  748. if (tol < 0.0)
  749. tol = 0.0 - tol;
  750. /* This first part of this condition will catch any NaN or Infinite values */
  751. if (isnan(diff) || isinf(diff) || (diff > tol))
  752. {
  753. UnityTestResultsFailBegin(lineNumber);
  754. UnityPrint(UnityStrElement);
  755. UnityPrintNumberUnsigned(num_elements - elements - 1);
  756. #ifdef UNITY_DOUBLE_VERBOSE
  757. UnityPrint(UnityStrExpected);
  758. UnityPrintFloat((float)(*ptr_expected));
  759. UnityPrint(UnityStrWas);
  760. UnityPrintFloat((float)(*ptr_actual));
  761. #else
  762. UnityPrint(UnityStrDelta);
  763. #endif
  764. UnityAddMsgIfSpecified(msg);
  765. UNITY_OUTPUT_CHAR('\n');
  766. UNITY_FAIL_AND_BAIL;
  767. }
  768. ptr_expected++;
  769. ptr_actual++;
  770. }
  771. }
  772. /*-----------------------------------------------*/
  773. void UnityAssertDoublesWithin(const _UD delta,
  774. const _UD expected,
  775. const _UD actual,
  776. const char* msg,
  777. const UNITY_LINE_TYPE lineNumber)
  778. {
  779. _UD diff = actual - expected;
  780. _UD pos_delta = delta;
  781. UNITY_SKIP_EXECUTION;
  782. if (diff < 0.0)
  783. {
  784. diff = 0.0 - diff;
  785. }
  786. if (pos_delta < 0.0)
  787. {
  788. pos_delta = 0.0 - pos_delta;
  789. }
  790. /* This first part of this condition will catch any NaN or Infinite values */
  791. if (isnan(diff) || isinf(diff) || (pos_delta < diff))
  792. {
  793. UnityTestResultsFailBegin(lineNumber);
  794. #ifdef UNITY_DOUBLE_VERBOSE
  795. UnityPrint(UnityStrExpected);
  796. UnityPrintFloat((float)expected);
  797. UnityPrint(UnityStrWas);
  798. UnityPrintFloat((float)actual);
  799. #else
  800. UnityPrint(UnityStrDelta);
  801. #endif
  802. UnityAddMsgIfSpecified(msg);
  803. UNITY_OUTPUT_CHAR('\n');
  804. UNITY_FAIL_AND_BAIL;
  805. }
  806. }
  807. /*-----------------------------------------------*/
  808. void UnityAssertDoubleSpecial(const _UD actual,
  809. const char* msg,
  810. const UNITY_LINE_TYPE lineNumber,
  811. const UNITY_FLOAT_TRAIT_T style)
  812. {
  813. const char* trait_names[] = { UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet };
  814. _U_SINT should_be_trait = ((_U_SINT)style & 1);
  815. _U_SINT is_trait = !should_be_trait;
  816. _U_SINT trait_index = (_U_SINT)(style >> 1);
  817. UNITY_SKIP_EXECUTION;
  818. switch(style)
  819. {
  820. /* To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly
  821. * We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise */
  822. case UNITY_FLOAT_IS_INF:
  823. case UNITY_FLOAT_IS_NOT_INF:
  824. is_trait = isinf(actual) & ispos(actual);
  825. break;
  826. case UNITY_FLOAT_IS_NEG_INF:
  827. case UNITY_FLOAT_IS_NOT_NEG_INF:
  828. is_trait = isinf(actual) & isneg(actual);
  829. break;
  830. /* NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN. */
  831. case UNITY_FLOAT_IS_NAN:
  832. case UNITY_FLOAT_IS_NOT_NAN:
  833. is_trait = isnan(actual);
  834. break;
  835. /* A determinate number is non infinite and not NaN. (therefore the opposite of the two above) */
  836. case UNITY_FLOAT_IS_DET:
  837. case UNITY_FLOAT_IS_NOT_DET:
  838. if (isinf(actual) | isnan(actual))
  839. is_trait = 0;
  840. else
  841. is_trait = 1;
  842. break;
  843. default:
  844. trait_index = 0;
  845. trait_names[0] = UnityStrInvalidFloatTrait;
  846. break;
  847. }
  848. if (is_trait != should_be_trait)
  849. {
  850. UnityTestResultsFailBegin(lineNumber);
  851. UnityPrint(UnityStrExpected);
  852. if (!should_be_trait)
  853. UnityPrint(UnityStrNot);
  854. UnityPrint(trait_names[trait_index]);
  855. UnityPrint(UnityStrWas);
  856. #ifdef UNITY_DOUBLE_VERBOSE
  857. UnityPrintFloat(actual);
  858. #else
  859. if (should_be_trait)
  860. UnityPrint(UnityStrNot);
  861. UnityPrint(trait_names[trait_index]);
  862. #endif
  863. UnityAddMsgIfSpecified(msg);
  864. UNITY_OUTPUT_CHAR('\n');
  865. UNITY_FAIL_AND_BAIL;
  866. }
  867. }
  868. #endif /* not UNITY_EXCLUDE_DOUBLE */
  869. /*-----------------------------------------------*/
  870. void UnityAssertNumbersWithin( const _U_UINT delta,
  871. const _U_SINT expected,
  872. const _U_SINT actual,
  873. const char* msg,
  874. const UNITY_LINE_TYPE lineNumber,
  875. const UNITY_DISPLAY_STYLE_T style)
  876. {
  877. UNITY_SKIP_EXECUTION;
  878. if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
  879. {
  880. if (actual > expected)
  881. Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > delta);
  882. else
  883. Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > delta);
  884. }
  885. else
  886. {
  887. if ((_U_UINT)actual > (_U_UINT)expected)
  888. Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > delta);
  889. else
  890. Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > delta);
  891. }
  892. if (Unity.CurrentTestFailed)
  893. {
  894. UnityTestResultsFailBegin(lineNumber);
  895. UnityPrint(UnityStrDelta);
  896. UnityPrintNumberByStyle((_U_SINT)delta, style);
  897. UnityPrint(UnityStrExpected);
  898. UnityPrintNumberByStyle(expected, style);
  899. UnityPrint(UnityStrWas);
  900. UnityPrintNumberByStyle(actual, style);
  901. UnityAddMsgIfSpecified(msg);
  902. UNITY_OUTPUT_CHAR('\n');
  903. UNITY_FAIL_AND_BAIL;
  904. }
  905. }
  906. /*-----------------------------------------------*/
  907. void UnityAssertEqualString(const char* expected,
  908. const char* actual,
  909. const char* msg,
  910. const UNITY_LINE_TYPE lineNumber)
  911. {
  912. _UU32 i;
  913. UNITY_SKIP_EXECUTION;
  914. /* if both pointers not null compare the strings */
  915. if (expected && actual)
  916. {
  917. for (i = 0; expected[i] || actual[i]; i++)
  918. {
  919. if (expected[i] != actual[i])
  920. {
  921. Unity.CurrentTestFailed = 1;
  922. break;
  923. }
  924. }
  925. }
  926. else
  927. { /* handle case of one pointers being null (if both null, test should pass) */
  928. if (expected != actual)
  929. {
  930. Unity.CurrentTestFailed = 1;
  931. }
  932. }
  933. if (Unity.CurrentTestFailed)
  934. {
  935. UnityTestResultsFailBegin(lineNumber);
  936. UnityPrintExpectedAndActualStrings(expected, actual);
  937. UnityAddMsgIfSpecified(msg);
  938. UNITY_OUTPUT_CHAR('\n');
  939. UNITY_FAIL_AND_BAIL;
  940. }
  941. }
  942. /*-----------------------------------------------*/
  943. void UnityAssertEqualStringLen(const char* expected,
  944. const char* actual,
  945. const _UU32 length,
  946. const char* msg,
  947. const UNITY_LINE_TYPE lineNumber)
  948. {
  949. _UU32 i;
  950. UNITY_SKIP_EXECUTION;
  951. /* if both pointers not null compare the strings */
  952. if (expected && actual)
  953. {
  954. for (i = 0; (expected[i] || actual[i]) && i < length; i++)
  955. {
  956. if (expected[i] != actual[i])
  957. {
  958. Unity.CurrentTestFailed = 1;
  959. break;
  960. }
  961. }
  962. }
  963. else
  964. { /* handle case of one pointers being null (if both null, test should pass) */
  965. if (expected != actual)
  966. {
  967. Unity.CurrentTestFailed = 1;
  968. }
  969. }
  970. if (Unity.CurrentTestFailed)
  971. {
  972. UnityTestResultsFailBegin(lineNumber);
  973. UnityPrintExpectedAndActualStringsLen(expected, actual, length);
  974. UnityAddMsgIfSpecified(msg);
  975. UNITY_OUTPUT_CHAR('\n');
  976. UNITY_FAIL_AND_BAIL;
  977. }
  978. }
  979. /*-----------------------------------------------*/
  980. void UnityAssertEqualStringArray( const char** expected,
  981. const char** actual,
  982. const _UU32 num_elements,
  983. const char* msg,
  984. const UNITY_LINE_TYPE lineNumber)
  985. {
  986. _UU32 i, j = 0;
  987. UNITY_SKIP_EXECUTION;
  988. /* if no elements, it's an error */
  989. if (num_elements == 0)
  990. {
  991. UnityPrintPointlessAndBail();
  992. }
  993. if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1)
  994. return;
  995. do
  996. {
  997. /* if both pointers not null compare the strings */
  998. if (expected[j] && actual[j])
  999. {
  1000. for (i = 0; expected[j][i] || actual[j][i]; i++)
  1001. {
  1002. if (expected[j][i] != actual[j][i])
  1003. {
  1004. Unity.CurrentTestFailed = 1;
  1005. break;
  1006. }
  1007. }
  1008. }
  1009. else
  1010. { /* handle case of one pointers being null (if both null, test should pass) */
  1011. if (expected[j] != actual[j])
  1012. {
  1013. Unity.CurrentTestFailed = 1;
  1014. }
  1015. }
  1016. if (Unity.CurrentTestFailed)
  1017. {
  1018. UnityTestResultsFailBegin(lineNumber);
  1019. if (num_elements > 1)
  1020. {
  1021. UnityPrint(UnityStrElement);
  1022. UnityPrintNumberUnsigned(j);
  1023. }
  1024. UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j]));
  1025. UnityAddMsgIfSpecified(msg);
  1026. UNITY_OUTPUT_CHAR('\n');
  1027. UNITY_FAIL_AND_BAIL;
  1028. }
  1029. } while (++j < num_elements);
  1030. }
  1031. /*-----------------------------------------------*/
  1032. void UnityAssertEqualMemory( UNITY_INTERNAL_PTR expected,
  1033. UNITY_INTERNAL_PTR actual,
  1034. const _UU32 length,
  1035. const _UU32 num_elements,
  1036. const char* msg,
  1037. const UNITY_LINE_TYPE lineNumber)
  1038. {
  1039. UNITY_PTR_ATTRIBUTE const unsigned char* ptr_exp = (UNITY_PTR_ATTRIBUTE const unsigned char*)expected;
  1040. UNITY_PTR_ATTRIBUTE const unsigned char* ptr_act = (UNITY_PTR_ATTRIBUTE const unsigned char*)actual;
  1041. _UU32 elements = num_elements;
  1042. _UU32 bytes;
  1043. UNITY_SKIP_EXECUTION;
  1044. if ((elements == 0) || (length == 0))
  1045. {
  1046. UnityPrintPointlessAndBail();
  1047. }
  1048. if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1)
  1049. return;
  1050. while (elements--)
  1051. {
  1052. /* /////////////////////////////////// */
  1053. bytes = length;
  1054. while (bytes--)
  1055. {
  1056. if (*ptr_exp != *ptr_act)
  1057. {
  1058. UnityTestResultsFailBegin(lineNumber);
  1059. UnityPrint(UnityStrMemory);
  1060. if (num_elements > 1)
  1061. {
  1062. UnityPrint(UnityStrElement);
  1063. UnityPrintNumberUnsigned(num_elements - elements - 1);
  1064. }
  1065. UnityPrint(UnityStrByte);
  1066. UnityPrintNumberUnsigned(length - bytes - 1);
  1067. UnityPrint(UnityStrExpected);
  1068. UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
  1069. UnityPrint(UnityStrWas);
  1070. UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
  1071. UnityAddMsgIfSpecified(msg);
  1072. UNITY_OUTPUT_CHAR('\n');
  1073. UNITY_FAIL_AND_BAIL;
  1074. }
  1075. ptr_exp = (UNITY_INTERNAL_PTR)((_UP)ptr_exp + 1);
  1076. ptr_act = (UNITY_INTERNAL_PTR)((_UP)ptr_act + 1);
  1077. }
  1078. /* /////////////////////////////////// */
  1079. }
  1080. }
  1081. /*-----------------------------------------------
  1082. * Control Functions
  1083. *-----------------------------------------------*/
  1084. void UnityFail(const char* msg, const UNITY_LINE_TYPE line)
  1085. {
  1086. UNITY_SKIP_EXECUTION;
  1087. UnityTestResultsBegin(Unity.TestFile, line);
  1088. UnityPrintFail();
  1089. if (msg != NULL)
  1090. {
  1091. UNITY_OUTPUT_CHAR(':');
  1092. #ifndef UNITY_EXCLUDE_DETAILS
  1093. if (Unity.CurrentDetail1)
  1094. {
  1095. UnityPrint(UnityStrDetail1Name);
  1096. UnityPrint(Unity.CurrentDetail1);
  1097. if (Unity.CurrentDetail2)
  1098. {
  1099. UnityPrint(UnityStrDetail2Name);
  1100. UnityPrint(Unity.CurrentDetail2);
  1101. }
  1102. UnityPrint(UnityStrSpacer);
  1103. }
  1104. #endif
  1105. if (msg[0] != ' ')
  1106. {
  1107. UNITY_OUTPUT_CHAR(' ');
  1108. }
  1109. UnityPrint(msg);
  1110. }
  1111. UNITY_OUTPUT_CHAR('\n');
  1112. UNITY_FAIL_AND_BAIL;
  1113. }
  1114. /*-----------------------------------------------*/
  1115. void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
  1116. {
  1117. UNITY_SKIP_EXECUTION;
  1118. UnityTestResultsBegin(Unity.TestFile, line);
  1119. UnityPrint(UnityStrIgnore);
  1120. if (msg != NULL)
  1121. {
  1122. UNITY_OUTPUT_CHAR(':');
  1123. UNITY_OUTPUT_CHAR(' ');
  1124. UnityPrint(msg);
  1125. }
  1126. UNITY_OUTPUT_CHAR('\n');
  1127. UNITY_IGNORE_AND_BAIL;
  1128. }
  1129. /*-----------------------------------------------*/
  1130. #if defined(UNITY_WEAK_ATTRIBUTE)
  1131. UNITY_WEAK_ATTRIBUTE void setUp(void) { }
  1132. UNITY_WEAK_ATTRIBUTE void tearDown(void) { }
  1133. #elif defined(UNITY_WEAK_PRAGMA)
  1134. # pragma weak setUp
  1135. void setUp(void) { }
  1136. # pragma weak tearDown
  1137. void tearDown(void) { }
  1138. #endif
  1139. /*-----------------------------------------------*/
  1140. void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
  1141. {
  1142. Unity.CurrentTestName = FuncName;
  1143. Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum;
  1144. Unity.NumberOfTests++;
  1145. UNITY_CLR_DETAILS();
  1146. if (TEST_PROTECT())
  1147. {
  1148. setUp();
  1149. Func();
  1150. }
  1151. if (TEST_PROTECT() && !(Unity.CurrentTestIgnored))
  1152. {
  1153. tearDown();
  1154. }
  1155. UnityConcludeTest();
  1156. }
  1157. /*-----------------------------------------------*/
  1158. void UnityBegin(const char* filename)
  1159. {
  1160. Unity.TestFile = filename;
  1161. Unity.CurrentTestName = NULL;
  1162. Unity.CurrentTestLineNumber = 0;
  1163. Unity.NumberOfTests = 0;
  1164. Unity.TestFailures = 0;
  1165. Unity.TestIgnores = 0;
  1166. Unity.CurrentTestFailed = 0;
  1167. Unity.CurrentTestIgnored = 0;
  1168. UNITY_CLR_DETAILS();
  1169. UNITY_OUTPUT_START();
  1170. }
  1171. /*-----------------------------------------------*/
  1172. int UnityEnd(void)
  1173. {
  1174. UNITY_PRINT_EOL();
  1175. UnityPrint(UnityStrBreaker);
  1176. UNITY_PRINT_EOL();
  1177. UnityPrintNumber((_U_SINT)(Unity.NumberOfTests));
  1178. UnityPrint(UnityStrResultsTests);
  1179. UnityPrintNumber((_U_SINT)(Unity.TestFailures));
  1180. UnityPrint(UnityStrResultsFailures);
  1181. UnityPrintNumber((_U_SINT)(Unity.TestIgnores));
  1182. UnityPrint(UnityStrResultsIgnored);
  1183. UNITY_PRINT_EOL();
  1184. if (Unity.TestFailures == 0U)
  1185. {
  1186. UnityPrintOk();
  1187. }
  1188. else
  1189. {
  1190. UnityPrintFail();
  1191. #ifdef UNITY_DIFFERENTIATE_FINAL_FAIL
  1192. UNITY_OUTPUT_CHAR('E'); UNITY_OUTPUT_CHAR('D');
  1193. #endif
  1194. }
  1195. UNITY_PRINT_EOL();
  1196. UNITY_OUTPUT_FLUSH();
  1197. UNITY_OUTPUT_COMPLETE();
  1198. return (int)(Unity.TestFailures);
  1199. }
  1200. /*-----------------------------------------------*/