unity.c 45 KB

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