unity.c 48 KB

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