unity.c 45 KB

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