unity.c 48 KB

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