unity.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119
  1. /* =========================================================================
  2. Unity Project - A Test Framework for C
  3. Copyright (c) 2007-21 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. #ifdef AVR
  9. #include <avr/pgmspace.h>
  10. #else
  11. #define PROGMEM
  12. #endif
  13. /* If omitted from header, declare overrideable prototypes here so they're ready for use */
  14. #ifdef UNITY_OMIT_OUTPUT_CHAR_HEADER_DECLARATION
  15. void UNITY_OUTPUT_CHAR(int);
  16. #endif
  17. /* Helpful macros for us to use here in Assert functions */
  18. #define UNITY_FAIL_AND_BAIL do { Unity.CurrentTestFailed = 1; UNITY_OUTPUT_FLUSH(); TEST_ABORT(); } while (0)
  19. #define UNITY_IGNORE_AND_BAIL do { Unity.CurrentTestIgnored = 1; UNITY_OUTPUT_FLUSH(); TEST_ABORT(); } while (0)
  20. #define RETURN_IF_FAIL_OR_IGNORE do { if (Unity.CurrentTestFailed || Unity.CurrentTestIgnored) { TEST_ABORT(); } } while (0)
  21. struct UNITY_STORAGE_T Unity;
  22. #ifdef UNITY_OUTPUT_COLOR
  23. const char PROGMEM UnityStrOk[] = "\033[42mOK\033[00m";
  24. const char PROGMEM UnityStrPass[] = "\033[42mPASS\033[00m";
  25. const char PROGMEM UnityStrFail[] = "\033[41mFAIL\033[00m";
  26. const char PROGMEM UnityStrIgnore[] = "\033[43mIGNORE\033[00m";
  27. #else
  28. const char PROGMEM UnityStrOk[] = "OK";
  29. const char PROGMEM UnityStrPass[] = "PASS";
  30. const char PROGMEM UnityStrFail[] = "FAIL";
  31. const char PROGMEM UnityStrIgnore[] = "IGNORE";
  32. #endif
  33. static const char PROGMEM UnityStrNull[] = "NULL";
  34. static const char PROGMEM UnityStrSpacer[] = ". ";
  35. static const char PROGMEM UnityStrExpected[] = " Expected ";
  36. static const char PROGMEM UnityStrWas[] = " Was ";
  37. static const char PROGMEM UnityStrGt[] = " to be greater than ";
  38. static const char PROGMEM UnityStrLt[] = " to be less than ";
  39. static const char PROGMEM UnityStrOrEqual[] = "or equal to ";
  40. static const char PROGMEM UnityStrNotEqual[] = " to be not equal to ";
  41. static const char PROGMEM UnityStrElement[] = " Element ";
  42. static const char PROGMEM UnityStrByte[] = " Byte ";
  43. static const char PROGMEM UnityStrMemory[] = " Memory Mismatch.";
  44. static const char PROGMEM UnityStrDelta[] = " Values Not Within Delta ";
  45. static const char PROGMEM UnityStrPointless[] = " You Asked Me To Compare Nothing, Which Was Pointless.";
  46. static const char PROGMEM UnityStrNullPointerForExpected[] = " Expected pointer to be NULL";
  47. static const char PROGMEM UnityStrNullPointerForActual[] = " Actual pointer was NULL";
  48. #ifndef UNITY_EXCLUDE_FLOAT
  49. static const char PROGMEM UnityStrNot[] = "Not ";
  50. static const char PROGMEM UnityStrInf[] = "Infinity";
  51. static const char PROGMEM UnityStrNegInf[] = "Negative Infinity";
  52. static const char PROGMEM UnityStrNaN[] = "NaN";
  53. static const char PROGMEM UnityStrDet[] = "Determinate";
  54. static const char PROGMEM UnityStrInvalidFloatTrait[] = "Invalid Float Trait";
  55. #endif
  56. const char PROGMEM UnityStrErrShorthand[] = "Unity Shorthand Support Disabled";
  57. const char PROGMEM UnityStrErrFloat[] = "Unity Floating Point Disabled";
  58. const char PROGMEM UnityStrErrDouble[] = "Unity Double Precision Disabled";
  59. const char PROGMEM UnityStrErr64[] = "Unity 64-bit Support Disabled";
  60. static const char PROGMEM UnityStrBreaker[] = "-----------------------";
  61. static const char PROGMEM UnityStrResultsTests[] = " Tests ";
  62. static const char PROGMEM UnityStrResultsFailures[] = " Failures ";
  63. static const char PROGMEM UnityStrResultsIgnored[] = " Ignored ";
  64. #ifndef UNITY_EXCLUDE_DETAILS
  65. static const char PROGMEM UnityStrDetail1Name[] = UNITY_DETAIL1_NAME " ";
  66. static const char PROGMEM UnityStrDetail2Name[] = " " UNITY_DETAIL2_NAME " ";
  67. #endif
  68. /*-----------------------------------------------
  69. * Pretty Printers & Test Result Output Handlers
  70. *-----------------------------------------------*/
  71. /*-----------------------------------------------*/
  72. /* Local helper function to print characters. */
  73. static void UnityPrintChar(const char* pch)
  74. {
  75. /* printable characters plus CR & LF are printed */
  76. if ((*pch <= 126) && (*pch >= 32))
  77. {
  78. UNITY_OUTPUT_CHAR(*pch);
  79. }
  80. /* write escaped carriage returns */
  81. else if (*pch == 13)
  82. {
  83. UNITY_OUTPUT_CHAR('\\');
  84. UNITY_OUTPUT_CHAR('r');
  85. }
  86. /* write escaped line feeds */
  87. else if (*pch == 10)
  88. {
  89. UNITY_OUTPUT_CHAR('\\');
  90. UNITY_OUTPUT_CHAR('n');
  91. }
  92. /* unprintable characters are shown as codes */
  93. else
  94. {
  95. UNITY_OUTPUT_CHAR('\\');
  96. UNITY_OUTPUT_CHAR('x');
  97. UnityPrintNumberHex((UNITY_UINT)*pch, 2);
  98. }
  99. }
  100. /*-----------------------------------------------*/
  101. /* Local helper function to print ANSI escape strings e.g. "\033[42m". */
  102. #ifdef UNITY_OUTPUT_COLOR
  103. static UNITY_UINT UnityPrintAnsiEscapeString(const char* string)
  104. {
  105. const char* pch = string;
  106. UNITY_UINT count = 0;
  107. while (*pch && (*pch != 'm'))
  108. {
  109. UNITY_OUTPUT_CHAR(*pch);
  110. pch++;
  111. count++;
  112. }
  113. UNITY_OUTPUT_CHAR('m');
  114. count++;
  115. return count;
  116. }
  117. #endif
  118. /*-----------------------------------------------*/
  119. void UnityPrint(const char* string)
  120. {
  121. const char* pch = string;
  122. if (pch != NULL)
  123. {
  124. while (*pch)
  125. {
  126. #ifdef UNITY_OUTPUT_COLOR
  127. /* print ANSI escape code */
  128. if ((*pch == 27) && (*(pch + 1) == '['))
  129. {
  130. pch += UnityPrintAnsiEscapeString(pch);
  131. continue;
  132. }
  133. #endif
  134. UnityPrintChar(pch);
  135. pch++;
  136. }
  137. }
  138. }
  139. /*-----------------------------------------------*/
  140. void UnityPrintLen(const char* string, const UNITY_UINT32 length)
  141. {
  142. const char* pch = string;
  143. if (pch != NULL)
  144. {
  145. while (*pch && ((UNITY_UINT32)(pch - string) < length))
  146. {
  147. /* printable characters plus CR & LF are printed */
  148. if ((*pch <= 126) && (*pch >= 32))
  149. {
  150. UNITY_OUTPUT_CHAR(*pch);
  151. }
  152. /* write escaped carriage returns */
  153. else if (*pch == 13)
  154. {
  155. UNITY_OUTPUT_CHAR('\\');
  156. UNITY_OUTPUT_CHAR('r');
  157. }
  158. /* write escaped line feeds */
  159. else if (*pch == 10)
  160. {
  161. UNITY_OUTPUT_CHAR('\\');
  162. UNITY_OUTPUT_CHAR('n');
  163. }
  164. /* unprintable characters are shown as codes */
  165. else
  166. {
  167. UNITY_OUTPUT_CHAR('\\');
  168. UNITY_OUTPUT_CHAR('x');
  169. UnityPrintNumberHex((UNITY_UINT)*pch, 2);
  170. }
  171. pch++;
  172. }
  173. }
  174. }
  175. /*-----------------------------------------------*/
  176. void UnityPrintNumberByStyle(const UNITY_INT number, const UNITY_DISPLAY_STYLE_T style)
  177. {
  178. if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
  179. {
  180. if (style == UNITY_DISPLAY_STYLE_CHAR)
  181. {
  182. /* printable characters plus CR & LF are printed */
  183. UNITY_OUTPUT_CHAR('\'');
  184. if ((number <= 126) && (number >= 32))
  185. {
  186. UNITY_OUTPUT_CHAR((int)number);
  187. }
  188. /* write escaped carriage returns */
  189. else if (number == 13)
  190. {
  191. UNITY_OUTPUT_CHAR('\\');
  192. UNITY_OUTPUT_CHAR('r');
  193. }
  194. /* write escaped line feeds */
  195. else if (number == 10)
  196. {
  197. UNITY_OUTPUT_CHAR('\\');
  198. UNITY_OUTPUT_CHAR('n');
  199. }
  200. /* unprintable characters are shown as codes */
  201. else
  202. {
  203. UNITY_OUTPUT_CHAR('\\');
  204. UNITY_OUTPUT_CHAR('x');
  205. UnityPrintNumberHex((UNITY_UINT)number, 2);
  206. }
  207. UNITY_OUTPUT_CHAR('\'');
  208. }
  209. else
  210. {
  211. UnityPrintNumber(number);
  212. }
  213. }
  214. else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
  215. {
  216. UnityPrintNumberUnsigned((UNITY_UINT)number);
  217. }
  218. else
  219. {
  220. UNITY_OUTPUT_CHAR('0');
  221. UNITY_OUTPUT_CHAR('x');
  222. UnityPrintNumberHex((UNITY_UINT)number, (char)((style & 0xF) * 2));
  223. }
  224. }
  225. /*-----------------------------------------------*/
  226. void UnityPrintNumber(const UNITY_INT number_to_print)
  227. {
  228. UNITY_UINT number = (UNITY_UINT)number_to_print;
  229. if (number_to_print < 0)
  230. {
  231. /* A negative number, including MIN negative */
  232. UNITY_OUTPUT_CHAR('-');
  233. number = (~number) + 1;
  234. }
  235. UnityPrintNumberUnsigned(number);
  236. }
  237. /*-----------------------------------------------
  238. * basically do an itoa using as little ram as possible */
  239. void UnityPrintNumberUnsigned(const UNITY_UINT number)
  240. {
  241. UNITY_UINT divisor = 1;
  242. /* figure out initial divisor */
  243. while (number / divisor > 9)
  244. {
  245. divisor *= 10;
  246. }
  247. /* now mod and print, then divide divisor */
  248. do
  249. {
  250. UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
  251. divisor /= 10;
  252. } while (divisor > 0);
  253. }
  254. /*-----------------------------------------------*/
  255. void UnityPrintNumberHex(const UNITY_UINT number, const char nibbles_to_print)
  256. {
  257. int nibble;
  258. char nibbles = nibbles_to_print;
  259. if ((unsigned)nibbles > UNITY_MAX_NIBBLES)
  260. {
  261. nibbles = UNITY_MAX_NIBBLES;
  262. }
  263. while (nibbles > 0)
  264. {
  265. nibbles--;
  266. nibble = (int)(number >> (nibbles * 4)) & 0x0F;
  267. if (nibble <= 9)
  268. {
  269. UNITY_OUTPUT_CHAR((char)('0' + nibble));
  270. }
  271. else
  272. {
  273. UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble));
  274. }
  275. }
  276. }
  277. /*-----------------------------------------------*/
  278. void UnityPrintMask(const UNITY_UINT mask, const UNITY_UINT number)
  279. {
  280. UNITY_UINT current_bit = (UNITY_UINT)1 << (UNITY_INT_WIDTH - 1);
  281. UNITY_INT32 i;
  282. for (i = 0; i < UNITY_INT_WIDTH; i++)
  283. {
  284. if (current_bit & mask)
  285. {
  286. if (current_bit & number)
  287. {
  288. UNITY_OUTPUT_CHAR('1');
  289. }
  290. else
  291. {
  292. UNITY_OUTPUT_CHAR('0');
  293. }
  294. }
  295. else
  296. {
  297. UNITY_OUTPUT_CHAR('X');
  298. }
  299. current_bit = current_bit >> 1;
  300. }
  301. }
  302. /*-----------------------------------------------*/
  303. #ifndef UNITY_EXCLUDE_FLOAT_PRINT
  304. /*
  305. * This function prints a floating-point value in a format similar to
  306. * printf("%.7g") on a single-precision machine or printf("%.9g") on a
  307. * double-precision machine. The 7th digit won't always be totally correct
  308. * in single-precision operation (for that level of accuracy, a more
  309. * complicated algorithm would be needed).
  310. */
  311. void UnityPrintFloat(const UNITY_DOUBLE input_number)
  312. {
  313. #ifdef UNITY_INCLUDE_DOUBLE
  314. static const int sig_digits = 9;
  315. static const UNITY_INT32 min_scaled = 100000000;
  316. static const UNITY_INT32 max_scaled = 1000000000;
  317. #else
  318. static const int sig_digits = 7;
  319. static const UNITY_INT32 min_scaled = 1000000;
  320. static const UNITY_INT32 max_scaled = 10000000;
  321. #endif
  322. UNITY_DOUBLE number = input_number;
  323. /* print minus sign (does not handle negative zero) */
  324. if (number < 0.0f)
  325. {
  326. UNITY_OUTPUT_CHAR('-');
  327. number = -number;
  328. }
  329. /* handle zero, NaN, and +/- infinity */
  330. if (number == 0.0f)
  331. {
  332. UnityPrint("0");
  333. }
  334. else if (isnan(number))
  335. {
  336. UnityPrint("nan");
  337. }
  338. else if (isinf(number))
  339. {
  340. UnityPrint("inf");
  341. }
  342. else
  343. {
  344. UNITY_INT32 n_int = 0;
  345. UNITY_INT32 n;
  346. int exponent = 0;
  347. int decimals;
  348. int digits;
  349. char buf[16] = {0};
  350. /*
  351. * Scale up or down by powers of 10. To minimize rounding error,
  352. * start with a factor/divisor of 10^10, which is the largest
  353. * power of 10 that can be represented exactly. Finally, compute
  354. * (exactly) the remaining power of 10 and perform one more
  355. * multiplication or division.
  356. */
  357. if (number < 1.0f)
  358. {
  359. UNITY_DOUBLE factor = 1.0f;
  360. while (number < (UNITY_DOUBLE)max_scaled / 1e10f) { number *= 1e10f; exponent -= 10; }
  361. while (number * factor < (UNITY_DOUBLE)min_scaled) { factor *= 10.0f; exponent--; }
  362. number *= factor;
  363. }
  364. else if (number > (UNITY_DOUBLE)max_scaled)
  365. {
  366. UNITY_DOUBLE divisor = 1.0f;
  367. while (number > (UNITY_DOUBLE)min_scaled * 1e10f) { number /= 1e10f; exponent += 10; }
  368. while (number / divisor > (UNITY_DOUBLE)max_scaled) { divisor *= 10.0f; exponent++; }
  369. number /= divisor;
  370. }
  371. else
  372. {
  373. /*
  374. * In this range, we can split off the integer part before
  375. * doing any multiplications. This reduces rounding error by
  376. * freeing up significant bits in the fractional part.
  377. */
  378. UNITY_DOUBLE factor = 1.0f;
  379. n_int = (UNITY_INT32)number;
  380. number -= (UNITY_DOUBLE)n_int;
  381. while (n_int < min_scaled) { n_int *= 10; factor *= 10.0f; exponent--; }
  382. number *= factor;
  383. }
  384. /* round to nearest integer */
  385. n = ((UNITY_INT32)(number + number) + 1) / 2;
  386. #ifndef UNITY_ROUND_TIES_AWAY_FROM_ZERO
  387. /* round to even if exactly between two integers */
  388. if ((n & 1) && (((UNITY_DOUBLE)n - number) == 0.5f))
  389. n--;
  390. #endif
  391. n += n_int;
  392. if (n >= max_scaled)
  393. {
  394. n = min_scaled;
  395. exponent++;
  396. }
  397. /* determine where to place decimal point */
  398. decimals = ((exponent <= 0) && (exponent >= -(sig_digits + 3))) ? (-exponent) : (sig_digits - 1);
  399. exponent += decimals;
  400. /* truncate trailing zeroes after decimal point */
  401. while ((decimals > 0) && ((n % 10) == 0))
  402. {
  403. n /= 10;
  404. decimals--;
  405. }
  406. /* build up buffer in reverse order */
  407. digits = 0;
  408. while ((n != 0) || (digits <= decimals))
  409. {
  410. buf[digits++] = (char)('0' + n % 10);
  411. n /= 10;
  412. }
  413. /* print out buffer (backwards) */
  414. while (digits > 0)
  415. {
  416. if (digits == decimals)
  417. {
  418. UNITY_OUTPUT_CHAR('.');
  419. }
  420. UNITY_OUTPUT_CHAR(buf[--digits]);
  421. }
  422. /* print exponent if needed */
  423. if (exponent != 0)
  424. {
  425. UNITY_OUTPUT_CHAR('e');
  426. if (exponent < 0)
  427. {
  428. UNITY_OUTPUT_CHAR('-');
  429. exponent = -exponent;
  430. }
  431. else
  432. {
  433. UNITY_OUTPUT_CHAR('+');
  434. }
  435. digits = 0;
  436. while ((exponent != 0) || (digits < 2))
  437. {
  438. buf[digits++] = (char)('0' + exponent % 10);
  439. exponent /= 10;
  440. }
  441. while (digits > 0)
  442. {
  443. UNITY_OUTPUT_CHAR(buf[--digits]);
  444. }
  445. }
  446. }
  447. }
  448. #endif /* ! UNITY_EXCLUDE_FLOAT_PRINT */
  449. /*-----------------------------------------------*/
  450. static void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line)
  451. {
  452. #ifdef UNITY_OUTPUT_FOR_ECLIPSE
  453. UNITY_OUTPUT_CHAR('(');
  454. UnityPrint(file);
  455. UNITY_OUTPUT_CHAR(':');
  456. UnityPrintNumber((UNITY_INT)line);
  457. UNITY_OUTPUT_CHAR(')');
  458. UNITY_OUTPUT_CHAR(' ');
  459. UnityPrint(Unity.CurrentTestName);
  460. UNITY_OUTPUT_CHAR(':');
  461. #else
  462. #ifdef UNITY_OUTPUT_FOR_IAR_WORKBENCH
  463. UnityPrint("<SRCREF line=");
  464. UnityPrintNumber((UNITY_INT)line);
  465. UnityPrint(" file=\"");
  466. UnityPrint(file);
  467. UNITY_OUTPUT_CHAR('"');
  468. UNITY_OUTPUT_CHAR('>');
  469. UnityPrint(Unity.CurrentTestName);
  470. UnityPrint("</SRCREF> ");
  471. #else
  472. #ifdef UNITY_OUTPUT_FOR_QT_CREATOR
  473. UnityPrint("file://");
  474. UnityPrint(file);
  475. UNITY_OUTPUT_CHAR(':');
  476. UnityPrintNumber((UNITY_INT)line);
  477. UNITY_OUTPUT_CHAR(' ');
  478. UnityPrint(Unity.CurrentTestName);
  479. UNITY_OUTPUT_CHAR(':');
  480. #else
  481. UnityPrint(file);
  482. UNITY_OUTPUT_CHAR(':');
  483. UnityPrintNumber((UNITY_INT)line);
  484. UNITY_OUTPUT_CHAR(':');
  485. UnityPrint(Unity.CurrentTestName);
  486. UNITY_OUTPUT_CHAR(':');
  487. #endif
  488. #endif
  489. #endif
  490. }
  491. /*-----------------------------------------------*/
  492. static void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)
  493. {
  494. UnityTestResultsBegin(Unity.TestFile, line);
  495. UnityPrint(UnityStrFail);
  496. UNITY_OUTPUT_CHAR(':');
  497. }
  498. /*-----------------------------------------------*/
  499. void UnityConcludeTest(void)
  500. {
  501. if (Unity.CurrentTestIgnored)
  502. {
  503. Unity.TestIgnores++;
  504. }
  505. else if (!Unity.CurrentTestFailed)
  506. {
  507. UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);
  508. UnityPrint(UnityStrPass);
  509. }
  510. else
  511. {
  512. Unity.TestFailures++;
  513. }
  514. Unity.CurrentTestFailed = 0;
  515. Unity.CurrentTestIgnored = 0;
  516. UNITY_PRINT_EXEC_TIME();
  517. UNITY_PRINT_EOL();
  518. UNITY_FLUSH_CALL();
  519. }
  520. /*-----------------------------------------------*/
  521. static void UnityAddMsgIfSpecified(const char* msg)
  522. {
  523. if (msg)
  524. {
  525. UnityPrint(UnityStrSpacer);
  526. #ifdef UNITY_PRINT_TEST_CONTEXT
  527. UNITY_PRINT_TEST_CONTEXT();
  528. #endif
  529. #ifndef UNITY_EXCLUDE_DETAILS
  530. if (Unity.CurrentDetail1)
  531. {
  532. UnityPrint(UnityStrDetail1Name);
  533. UnityPrint(Unity.CurrentDetail1);
  534. if (Unity.CurrentDetail2)
  535. {
  536. UnityPrint(UnityStrDetail2Name);
  537. UnityPrint(Unity.CurrentDetail2);
  538. }
  539. UnityPrint(UnityStrSpacer);
  540. }
  541. #endif
  542. UnityPrint(msg);
  543. }
  544. }
  545. /*-----------------------------------------------*/
  546. static void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual)
  547. {
  548. UnityPrint(UnityStrExpected);
  549. if (expected != NULL)
  550. {
  551. UNITY_OUTPUT_CHAR('\'');
  552. UnityPrint(expected);
  553. UNITY_OUTPUT_CHAR('\'');
  554. }
  555. else
  556. {
  557. UnityPrint(UnityStrNull);
  558. }
  559. UnityPrint(UnityStrWas);
  560. if (actual != NULL)
  561. {
  562. UNITY_OUTPUT_CHAR('\'');
  563. UnityPrint(actual);
  564. UNITY_OUTPUT_CHAR('\'');
  565. }
  566. else
  567. {
  568. UnityPrint(UnityStrNull);
  569. }
  570. }
  571. /*-----------------------------------------------*/
  572. static void UnityPrintExpectedAndActualStringsLen(const char* expected,
  573. const char* actual,
  574. const UNITY_UINT32 length)
  575. {
  576. UnityPrint(UnityStrExpected);
  577. if (expected != NULL)
  578. {
  579. UNITY_OUTPUT_CHAR('\'');
  580. UnityPrintLen(expected, length);
  581. UNITY_OUTPUT_CHAR('\'');
  582. }
  583. else
  584. {
  585. UnityPrint(UnityStrNull);
  586. }
  587. UnityPrint(UnityStrWas);
  588. if (actual != NULL)
  589. {
  590. UNITY_OUTPUT_CHAR('\'');
  591. UnityPrintLen(actual, length);
  592. UNITY_OUTPUT_CHAR('\'');
  593. }
  594. else
  595. {
  596. UnityPrint(UnityStrNull);
  597. }
  598. }
  599. /*-----------------------------------------------
  600. * Assertion & Control Helpers
  601. *-----------------------------------------------*/
  602. /*-----------------------------------------------*/
  603. static int UnityIsOneArrayNull(UNITY_INTERNAL_PTR expected,
  604. UNITY_INTERNAL_PTR actual,
  605. const UNITY_LINE_TYPE lineNumber,
  606. const char* msg)
  607. {
  608. /* Both are NULL or same pointer */
  609. if (expected == actual) { return 0; }
  610. /* print and return true if just expected is NULL */
  611. if (expected == NULL)
  612. {
  613. UnityTestResultsFailBegin(lineNumber);
  614. UnityPrint(UnityStrNullPointerForExpected);
  615. UnityAddMsgIfSpecified(msg);
  616. return 1;
  617. }
  618. /* print and return true if just actual is NULL */
  619. if (actual == NULL)
  620. {
  621. UnityTestResultsFailBegin(lineNumber);
  622. UnityPrint(UnityStrNullPointerForActual);
  623. UnityAddMsgIfSpecified(msg);
  624. return 1;
  625. }
  626. return 0; /* return false if neither is NULL */
  627. }
  628. /*-----------------------------------------------
  629. * Assertion Functions
  630. *-----------------------------------------------*/
  631. /*-----------------------------------------------*/
  632. void UnityAssertBits(const UNITY_INT mask,
  633. const UNITY_INT expected,
  634. const UNITY_INT actual,
  635. const char* msg,
  636. const UNITY_LINE_TYPE lineNumber)
  637. {
  638. RETURN_IF_FAIL_OR_IGNORE;
  639. if ((mask & expected) != (mask & actual))
  640. {
  641. UnityTestResultsFailBegin(lineNumber);
  642. UnityPrint(UnityStrExpected);
  643. UnityPrintMask((UNITY_UINT)mask, (UNITY_UINT)expected);
  644. UnityPrint(UnityStrWas);
  645. UnityPrintMask((UNITY_UINT)mask, (UNITY_UINT)actual);
  646. UnityAddMsgIfSpecified(msg);
  647. UNITY_FAIL_AND_BAIL;
  648. }
  649. }
  650. /*-----------------------------------------------*/
  651. void UnityAssertEqualNumber(const UNITY_INT expected,
  652. const UNITY_INT actual,
  653. const char* msg,
  654. const UNITY_LINE_TYPE lineNumber,
  655. const UNITY_DISPLAY_STYLE_T style)
  656. {
  657. RETURN_IF_FAIL_OR_IGNORE;
  658. if (expected != actual)
  659. {
  660. UnityTestResultsFailBegin(lineNumber);
  661. UnityPrint(UnityStrExpected);
  662. UnityPrintNumberByStyle(expected, style);
  663. UnityPrint(UnityStrWas);
  664. UnityPrintNumberByStyle(actual, style);
  665. UnityAddMsgIfSpecified(msg);
  666. UNITY_FAIL_AND_BAIL;
  667. }
  668. }
  669. /*-----------------------------------------------*/
  670. void UnityAssertGreaterOrLessOrEqualNumber(const UNITY_INT threshold,
  671. const UNITY_INT actual,
  672. const UNITY_COMPARISON_T compare,
  673. const char *msg,
  674. const UNITY_LINE_TYPE lineNumber,
  675. const UNITY_DISPLAY_STYLE_T style)
  676. {
  677. int failed = 0;
  678. RETURN_IF_FAIL_OR_IGNORE;
  679. if ((threshold == actual) && (compare & UNITY_EQUAL_TO)) { return; }
  680. if ((threshold == actual)) { failed = 1; }
  681. if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
  682. {
  683. if ((actual > threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
  684. if ((actual < threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }
  685. }
  686. else /* UINT or HEX */
  687. {
  688. if (((UNITY_UINT)actual > (UNITY_UINT)threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
  689. if (((UNITY_UINT)actual < (UNITY_UINT)threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }
  690. }
  691. if (failed)
  692. {
  693. UnityTestResultsFailBegin(lineNumber);
  694. UnityPrint(UnityStrExpected);
  695. UnityPrintNumberByStyle(actual, style);
  696. if (compare & UNITY_GREATER_THAN) { UnityPrint(UnityStrGt); }
  697. if (compare & UNITY_SMALLER_THAN) { UnityPrint(UnityStrLt); }
  698. if (compare & UNITY_EQUAL_TO) { UnityPrint(UnityStrOrEqual); }
  699. if (compare == UNITY_NOT_EQUAL) { UnityPrint(UnityStrNotEqual); }
  700. UnityPrintNumberByStyle(threshold, style);
  701. UnityAddMsgIfSpecified(msg);
  702. UNITY_FAIL_AND_BAIL;
  703. }
  704. }
  705. #define UnityPrintPointlessAndBail() \
  706. do { \
  707. UnityTestResultsFailBegin(lineNumber); \
  708. UnityPrint(UnityStrPointless); \
  709. UnityAddMsgIfSpecified(msg); \
  710. UNITY_FAIL_AND_BAIL; \
  711. } while (0)
  712. /*-----------------------------------------------*/
  713. void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
  714. UNITY_INTERNAL_PTR actual,
  715. const UNITY_UINT32 num_elements,
  716. const char* msg,
  717. const UNITY_LINE_TYPE lineNumber,
  718. const UNITY_DISPLAY_STYLE_T style,
  719. const UNITY_FLAGS_T flags)
  720. {
  721. UNITY_UINT32 elements = num_elements;
  722. unsigned int length = style & 0xF;
  723. unsigned int increment = 0;
  724. RETURN_IF_FAIL_OR_IGNORE;
  725. if (num_elements == 0)
  726. {
  727. UnityPrintPointlessAndBail();
  728. }
  729. if (expected == actual)
  730. {
  731. return; /* Both are NULL or same pointer */
  732. }
  733. if (UnityIsOneArrayNull(expected, actual, lineNumber, msg))
  734. {
  735. UNITY_FAIL_AND_BAIL;
  736. }
  737. while ((elements > 0) && (elements--))
  738. {
  739. UNITY_INT expect_val;
  740. UNITY_INT actual_val;
  741. switch (length)
  742. {
  743. case 1:
  744. expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT8*)expected;
  745. actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT8*)actual;
  746. increment = sizeof(UNITY_INT8);
  747. break;
  748. case 2:
  749. expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT16*)expected;
  750. actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT16*)actual;
  751. increment = sizeof(UNITY_INT16);
  752. break;
  753. #ifdef UNITY_SUPPORT_64
  754. case 8:
  755. expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT64*)expected;
  756. actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT64*)actual;
  757. increment = sizeof(UNITY_INT64);
  758. break;
  759. #endif
  760. default: /* default is length 4 bytes */
  761. case 4:
  762. expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT32*)expected;
  763. actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT32*)actual;
  764. increment = sizeof(UNITY_INT32);
  765. length = 4;
  766. break;
  767. }
  768. if (expect_val != actual_val)
  769. {
  770. if ((style & UNITY_DISPLAY_RANGE_UINT) && (length < (UNITY_INT_WIDTH / 8)))
  771. { /* For UINT, remove sign extension (padding 1's) from signed type casts above */
  772. UNITY_INT mask = 1;
  773. mask = (mask << 8 * length) - 1;
  774. expect_val &= mask;
  775. actual_val &= mask;
  776. }
  777. UnityTestResultsFailBegin(lineNumber);
  778. UnityPrint(UnityStrElement);
  779. UnityPrintNumberUnsigned(num_elements - elements - 1);
  780. UnityPrint(UnityStrExpected);
  781. UnityPrintNumberByStyle(expect_val, style);
  782. UnityPrint(UnityStrWas);
  783. UnityPrintNumberByStyle(actual_val, style);
  784. UnityAddMsgIfSpecified(msg);
  785. UNITY_FAIL_AND_BAIL;
  786. }
  787. /* Walk through array by incrementing the pointers */
  788. if (flags == UNITY_ARRAY_TO_ARRAY)
  789. {
  790. expected = (UNITY_INTERNAL_PTR)((const char*)expected + increment);
  791. }
  792. actual = (UNITY_INTERNAL_PTR)((const char*)actual + increment);
  793. }
  794. }
  795. /*-----------------------------------------------*/
  796. #ifndef UNITY_EXCLUDE_FLOAT
  797. /* Wrap this define in a function with variable types as float or double */
  798. #define UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff) \
  799. if (isinf(expected) && isinf(actual) && (((expected) < 0) == ((actual) < 0))) return 1; \
  800. if (UNITY_NAN_CHECK) return 1; \
  801. (diff) = (actual) - (expected); \
  802. if ((diff) < 0) (diff) = -(diff); \
  803. if ((delta) < 0) (delta) = -(delta); \
  804. return !(isnan(diff) || isinf(diff) || ((diff) > (delta)))
  805. /* This first part of this condition will catch any NaN or Infinite values */
  806. #ifndef UNITY_NAN_NOT_EQUAL_NAN
  807. #define UNITY_NAN_CHECK isnan(expected) && isnan(actual)
  808. #else
  809. #define UNITY_NAN_CHECK 0
  810. #endif
  811. #ifndef UNITY_EXCLUDE_FLOAT_PRINT
  812. #define UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(expected, actual) \
  813. do { \
  814. UnityPrint(UnityStrExpected); \
  815. UnityPrintFloat(expected); \
  816. UnityPrint(UnityStrWas); \
  817. UnityPrintFloat(actual); \
  818. } while (0)
  819. #else
  820. #define UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(expected, actual) \
  821. UnityPrint(UnityStrDelta)
  822. #endif /* UNITY_EXCLUDE_FLOAT_PRINT */
  823. /*-----------------------------------------------*/
  824. static int UnityFloatsWithin(UNITY_FLOAT delta, UNITY_FLOAT expected, UNITY_FLOAT actual)
  825. {
  826. UNITY_FLOAT diff;
  827. UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);
  828. }
  829. /*-----------------------------------------------*/
  830. void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected,
  831. UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* actual,
  832. const UNITY_UINT32 num_elements,
  833. const char* msg,
  834. const UNITY_LINE_TYPE lineNumber,
  835. const UNITY_FLAGS_T flags)
  836. {
  837. UNITY_UINT32 elements = num_elements;
  838. UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* ptr_expected = expected;
  839. UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* ptr_actual = actual;
  840. RETURN_IF_FAIL_OR_IGNORE;
  841. if (elements == 0)
  842. {
  843. UnityPrintPointlessAndBail();
  844. }
  845. if (expected == actual)
  846. {
  847. return; /* Both are NULL or same pointer */
  848. }
  849. if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg))
  850. {
  851. UNITY_FAIL_AND_BAIL;
  852. }
  853. while (elements--)
  854. {
  855. if (!UnityFloatsWithin(*ptr_expected * UNITY_FLOAT_PRECISION, *ptr_expected, *ptr_actual))
  856. {
  857. UnityTestResultsFailBegin(lineNumber);
  858. UnityPrint(UnityStrElement);
  859. UnityPrintNumberUnsigned(num_elements - elements - 1);
  860. UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT((UNITY_DOUBLE)*ptr_expected, (UNITY_DOUBLE)*ptr_actual);
  861. UnityAddMsgIfSpecified(msg);
  862. UNITY_FAIL_AND_BAIL;
  863. }
  864. if (flags == UNITY_ARRAY_TO_ARRAY)
  865. {
  866. ptr_expected++;
  867. }
  868. ptr_actual++;
  869. }
  870. }
  871. /*-----------------------------------------------*/
  872. void UnityAssertFloatsWithin(const UNITY_FLOAT delta,
  873. const UNITY_FLOAT expected,
  874. const UNITY_FLOAT actual,
  875. const char* msg,
  876. const UNITY_LINE_TYPE lineNumber)
  877. {
  878. RETURN_IF_FAIL_OR_IGNORE;
  879. if (!UnityFloatsWithin(delta, expected, actual))
  880. {
  881. UnityTestResultsFailBegin(lineNumber);
  882. UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT((UNITY_DOUBLE)expected, (UNITY_DOUBLE)actual);
  883. UnityAddMsgIfSpecified(msg);
  884. UNITY_FAIL_AND_BAIL;
  885. }
  886. }
  887. /*-----------------------------------------------*/
  888. void UnityAssertFloatSpecial(const UNITY_FLOAT actual,
  889. const char* msg,
  890. const UNITY_LINE_TYPE lineNumber,
  891. const UNITY_FLOAT_TRAIT_T style)
  892. {
  893. const char* trait_names[] = {UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet};
  894. UNITY_INT should_be_trait = ((UNITY_INT)style & 1);
  895. UNITY_INT is_trait = !should_be_trait;
  896. UNITY_INT trait_index = (UNITY_INT)(style >> 1);
  897. RETURN_IF_FAIL_OR_IGNORE;
  898. switch (style)
  899. {
  900. case UNITY_FLOAT_IS_INF:
  901. case UNITY_FLOAT_IS_NOT_INF:
  902. is_trait = isinf(actual) && (actual > 0);
  903. break;
  904. case UNITY_FLOAT_IS_NEG_INF:
  905. case UNITY_FLOAT_IS_NOT_NEG_INF:
  906. is_trait = isinf(actual) && (actual < 0);
  907. break;
  908. case UNITY_FLOAT_IS_NAN:
  909. case UNITY_FLOAT_IS_NOT_NAN:
  910. is_trait = isnan(actual) ? 1 : 0;
  911. break;
  912. case UNITY_FLOAT_IS_DET: /* A determinate number is non infinite and not NaN. */
  913. case UNITY_FLOAT_IS_NOT_DET:
  914. is_trait = !isinf(actual) && !isnan(actual);
  915. break;
  916. default: /* including UNITY_FLOAT_INVALID_TRAIT */
  917. trait_index = 0;
  918. trait_names[0] = UnityStrInvalidFloatTrait;
  919. break;
  920. }
  921. if (is_trait != should_be_trait)
  922. {
  923. UnityTestResultsFailBegin(lineNumber);
  924. UnityPrint(UnityStrExpected);
  925. if (!should_be_trait)
  926. {
  927. UnityPrint(UnityStrNot);
  928. }
  929. UnityPrint(trait_names[trait_index]);
  930. UnityPrint(UnityStrWas);
  931. #ifndef UNITY_EXCLUDE_FLOAT_PRINT
  932. UnityPrintFloat((UNITY_DOUBLE)actual);
  933. #else
  934. if (should_be_trait)
  935. {
  936. UnityPrint(UnityStrNot);
  937. }
  938. UnityPrint(trait_names[trait_index]);
  939. #endif
  940. UnityAddMsgIfSpecified(msg);
  941. UNITY_FAIL_AND_BAIL;
  942. }
  943. }
  944. #endif /* not UNITY_EXCLUDE_FLOAT */
  945. /*-----------------------------------------------*/
  946. #ifndef UNITY_EXCLUDE_DOUBLE
  947. static int UnityDoublesWithin(UNITY_DOUBLE delta, UNITY_DOUBLE expected, UNITY_DOUBLE actual)
  948. {
  949. UNITY_DOUBLE diff;
  950. UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);
  951. }
  952. /*-----------------------------------------------*/
  953. void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expected,
  954. UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* actual,
  955. const UNITY_UINT32 num_elements,
  956. const char* msg,
  957. const UNITY_LINE_TYPE lineNumber,
  958. const UNITY_FLAGS_T flags)
  959. {
  960. UNITY_UINT32 elements = num_elements;
  961. UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* ptr_expected = expected;
  962. UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* ptr_actual = actual;
  963. RETURN_IF_FAIL_OR_IGNORE;
  964. if (elements == 0)
  965. {
  966. UnityPrintPointlessAndBail();
  967. }
  968. if (expected == actual)
  969. {
  970. return; /* Both are NULL or same pointer */
  971. }
  972. if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg))
  973. {
  974. UNITY_FAIL_AND_BAIL;
  975. }
  976. while (elements--)
  977. {
  978. if (!UnityDoublesWithin(*ptr_expected * UNITY_DOUBLE_PRECISION, *ptr_expected, *ptr_actual))
  979. {
  980. UnityTestResultsFailBegin(lineNumber);
  981. UnityPrint(UnityStrElement);
  982. UnityPrintNumberUnsigned(num_elements - elements - 1);
  983. UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(*ptr_expected, *ptr_actual);
  984. UnityAddMsgIfSpecified(msg);
  985. UNITY_FAIL_AND_BAIL;
  986. }
  987. if (flags == UNITY_ARRAY_TO_ARRAY)
  988. {
  989. ptr_expected++;
  990. }
  991. ptr_actual++;
  992. }
  993. }
  994. /*-----------------------------------------------*/
  995. void UnityAssertDoublesWithin(const UNITY_DOUBLE delta,
  996. const UNITY_DOUBLE expected,
  997. const UNITY_DOUBLE actual,
  998. const char* msg,
  999. const UNITY_LINE_TYPE lineNumber)
  1000. {
  1001. RETURN_IF_FAIL_OR_IGNORE;
  1002. if (!UnityDoublesWithin(delta, expected, actual))
  1003. {
  1004. UnityTestResultsFailBegin(lineNumber);
  1005. UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(expected, actual);
  1006. UnityAddMsgIfSpecified(msg);
  1007. UNITY_FAIL_AND_BAIL;
  1008. }
  1009. }
  1010. /*-----------------------------------------------*/
  1011. void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual,
  1012. const char* msg,
  1013. const UNITY_LINE_TYPE lineNumber,
  1014. const UNITY_FLOAT_TRAIT_T style)
  1015. {
  1016. const char* trait_names[] = {UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet};
  1017. UNITY_INT should_be_trait = ((UNITY_INT)style & 1);
  1018. UNITY_INT is_trait = !should_be_trait;
  1019. UNITY_INT trait_index = (UNITY_INT)(style >> 1);
  1020. RETURN_IF_FAIL_OR_IGNORE;
  1021. switch (style)
  1022. {
  1023. case UNITY_FLOAT_IS_INF:
  1024. case UNITY_FLOAT_IS_NOT_INF:
  1025. is_trait = isinf(actual) && (actual > 0);
  1026. break;
  1027. case UNITY_FLOAT_IS_NEG_INF:
  1028. case UNITY_FLOAT_IS_NOT_NEG_INF:
  1029. is_trait = isinf(actual) && (actual < 0);
  1030. break;
  1031. case UNITY_FLOAT_IS_NAN:
  1032. case UNITY_FLOAT_IS_NOT_NAN:
  1033. is_trait = isnan(actual) ? 1 : 0;
  1034. break;
  1035. case UNITY_FLOAT_IS_DET: /* A determinate number is non infinite and not NaN. */
  1036. case UNITY_FLOAT_IS_NOT_DET:
  1037. is_trait = !isinf(actual) && !isnan(actual);
  1038. break;
  1039. default: /* including UNITY_FLOAT_INVALID_TRAIT */
  1040. trait_index = 0;
  1041. trait_names[0] = UnityStrInvalidFloatTrait;
  1042. break;
  1043. }
  1044. if (is_trait != should_be_trait)
  1045. {
  1046. UnityTestResultsFailBegin(lineNumber);
  1047. UnityPrint(UnityStrExpected);
  1048. if (!should_be_trait)
  1049. {
  1050. UnityPrint(UnityStrNot);
  1051. }
  1052. UnityPrint(trait_names[trait_index]);
  1053. UnityPrint(UnityStrWas);
  1054. #ifndef UNITY_EXCLUDE_FLOAT_PRINT
  1055. UnityPrintFloat(actual);
  1056. #else
  1057. if (should_be_trait)
  1058. {
  1059. UnityPrint(UnityStrNot);
  1060. }
  1061. UnityPrint(trait_names[trait_index]);
  1062. #endif
  1063. UnityAddMsgIfSpecified(msg);
  1064. UNITY_FAIL_AND_BAIL;
  1065. }
  1066. }
  1067. #endif /* not UNITY_EXCLUDE_DOUBLE */
  1068. /*-----------------------------------------------*/
  1069. void UnityAssertNumbersWithin(const UNITY_UINT delta,
  1070. const UNITY_INT expected,
  1071. const UNITY_INT actual,
  1072. const char* msg,
  1073. const UNITY_LINE_TYPE lineNumber,
  1074. const UNITY_DISPLAY_STYLE_T style)
  1075. {
  1076. RETURN_IF_FAIL_OR_IGNORE;
  1077. if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
  1078. {
  1079. if (actual > expected)
  1080. {
  1081. Unity.CurrentTestFailed = (((UNITY_UINT)actual - (UNITY_UINT)expected) > delta);
  1082. }
  1083. else
  1084. {
  1085. Unity.CurrentTestFailed = (((UNITY_UINT)expected - (UNITY_UINT)actual) > delta);
  1086. }
  1087. }
  1088. else
  1089. {
  1090. if ((UNITY_UINT)actual > (UNITY_UINT)expected)
  1091. {
  1092. Unity.CurrentTestFailed = (((UNITY_UINT)actual - (UNITY_UINT)expected) > delta);
  1093. }
  1094. else
  1095. {
  1096. Unity.CurrentTestFailed = (((UNITY_UINT)expected - (UNITY_UINT)actual) > delta);
  1097. }
  1098. }
  1099. if (Unity.CurrentTestFailed)
  1100. {
  1101. UnityTestResultsFailBegin(lineNumber);
  1102. UnityPrint(UnityStrDelta);
  1103. UnityPrintNumberByStyle((UNITY_INT)delta, style);
  1104. UnityPrint(UnityStrExpected);
  1105. UnityPrintNumberByStyle(expected, style);
  1106. UnityPrint(UnityStrWas);
  1107. UnityPrintNumberByStyle(actual, style);
  1108. UnityAddMsgIfSpecified(msg);
  1109. UNITY_FAIL_AND_BAIL;
  1110. }
  1111. }
  1112. /*-----------------------------------------------*/
  1113. void UnityAssertNumbersArrayWithin(const UNITY_UINT delta,
  1114. UNITY_INTERNAL_PTR expected,
  1115. UNITY_INTERNAL_PTR actual,
  1116. const UNITY_UINT32 num_elements,
  1117. const char* msg,
  1118. const UNITY_LINE_TYPE lineNumber,
  1119. const UNITY_DISPLAY_STYLE_T style,
  1120. const UNITY_FLAGS_T flags)
  1121. {
  1122. UNITY_UINT32 elements = num_elements;
  1123. unsigned int length = style & 0xF;
  1124. unsigned int increment = 0;
  1125. RETURN_IF_FAIL_OR_IGNORE;
  1126. if (num_elements == 0)
  1127. {
  1128. UnityPrintPointlessAndBail();
  1129. }
  1130. if (expected == actual)
  1131. {
  1132. return; /* Both are NULL or same pointer */
  1133. }
  1134. if (UnityIsOneArrayNull(expected, actual, lineNumber, msg))
  1135. {
  1136. UNITY_FAIL_AND_BAIL;
  1137. }
  1138. while ((elements > 0) && (elements--))
  1139. {
  1140. UNITY_INT expect_val;
  1141. UNITY_INT actual_val;
  1142. switch (length)
  1143. {
  1144. case 1:
  1145. expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT8*)expected;
  1146. actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT8*)actual;
  1147. increment = sizeof(UNITY_INT8);
  1148. break;
  1149. case 2:
  1150. expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT16*)expected;
  1151. actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT16*)actual;
  1152. increment = sizeof(UNITY_INT16);
  1153. break;
  1154. #ifdef UNITY_SUPPORT_64
  1155. case 8:
  1156. expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT64*)expected;
  1157. actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT64*)actual;
  1158. increment = sizeof(UNITY_INT64);
  1159. break;
  1160. #endif
  1161. default: /* default is length 4 bytes */
  1162. case 4:
  1163. expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT32*)expected;
  1164. actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT32*)actual;
  1165. increment = sizeof(UNITY_INT32);
  1166. length = 4;
  1167. break;
  1168. }
  1169. if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
  1170. {
  1171. if (actual_val > expect_val)
  1172. {
  1173. Unity.CurrentTestFailed = (((UNITY_UINT)actual_val - (UNITY_UINT)expect_val) > delta);
  1174. }
  1175. else
  1176. {
  1177. Unity.CurrentTestFailed = (((UNITY_UINT)expect_val - (UNITY_UINT)actual_val) > delta);
  1178. }
  1179. }
  1180. else
  1181. {
  1182. if ((UNITY_UINT)actual_val > (UNITY_UINT)expect_val)
  1183. {
  1184. Unity.CurrentTestFailed = (((UNITY_UINT)actual_val - (UNITY_UINT)expect_val) > delta);
  1185. }
  1186. else
  1187. {
  1188. Unity.CurrentTestFailed = (((UNITY_UINT)expect_val - (UNITY_UINT)actual_val) > delta);
  1189. }
  1190. }
  1191. if (Unity.CurrentTestFailed)
  1192. {
  1193. if ((style & UNITY_DISPLAY_RANGE_UINT) && (length < (UNITY_INT_WIDTH / 8)))
  1194. { /* For UINT, remove sign extension (padding 1's) from signed type casts above */
  1195. UNITY_INT mask = 1;
  1196. mask = (mask << 8 * length) - 1;
  1197. expect_val &= mask;
  1198. actual_val &= mask;
  1199. }
  1200. UnityTestResultsFailBegin(lineNumber);
  1201. UnityPrint(UnityStrDelta);
  1202. UnityPrintNumberByStyle((UNITY_INT)delta, style);
  1203. UnityPrint(UnityStrElement);
  1204. UnityPrintNumberUnsigned(num_elements - elements - 1);
  1205. UnityPrint(UnityStrExpected);
  1206. UnityPrintNumberByStyle(expect_val, style);
  1207. UnityPrint(UnityStrWas);
  1208. UnityPrintNumberByStyle(actual_val, style);
  1209. UnityAddMsgIfSpecified(msg);
  1210. UNITY_FAIL_AND_BAIL;
  1211. }
  1212. /* Walk through array by incrementing the pointers */
  1213. if (flags == UNITY_ARRAY_TO_ARRAY)
  1214. {
  1215. expected = (UNITY_INTERNAL_PTR)((const char*)expected + increment);
  1216. }
  1217. actual = (UNITY_INTERNAL_PTR)((const char*)actual + increment);
  1218. }
  1219. }
  1220. /*-----------------------------------------------*/
  1221. void UnityAssertEqualString(const char* expected,
  1222. const char* actual,
  1223. const char* msg,
  1224. const UNITY_LINE_TYPE lineNumber)
  1225. {
  1226. UNITY_UINT32 i;
  1227. RETURN_IF_FAIL_OR_IGNORE;
  1228. /* if both pointers not null compare the strings */
  1229. if (expected && actual)
  1230. {
  1231. for (i = 0; expected[i] || actual[i]; i++)
  1232. {
  1233. if (expected[i] != actual[i])
  1234. {
  1235. Unity.CurrentTestFailed = 1;
  1236. break;
  1237. }
  1238. }
  1239. }
  1240. else
  1241. { /* handle case of one pointers being null (if both null, test should pass) */
  1242. if (expected != actual)
  1243. {
  1244. Unity.CurrentTestFailed = 1;
  1245. }
  1246. }
  1247. if (Unity.CurrentTestFailed)
  1248. {
  1249. UnityTestResultsFailBegin(lineNumber);
  1250. UnityPrintExpectedAndActualStrings(expected, actual);
  1251. UnityAddMsgIfSpecified(msg);
  1252. UNITY_FAIL_AND_BAIL;
  1253. }
  1254. }
  1255. /*-----------------------------------------------*/
  1256. void UnityAssertEqualStringLen(const char* expected,
  1257. const char* actual,
  1258. const UNITY_UINT32 length,
  1259. const char* msg,
  1260. const UNITY_LINE_TYPE lineNumber)
  1261. {
  1262. UNITY_UINT32 i;
  1263. RETURN_IF_FAIL_OR_IGNORE;
  1264. /* if both pointers not null compare the strings */
  1265. if (expected && actual)
  1266. {
  1267. for (i = 0; (i < length) && (expected[i] || actual[i]); i++)
  1268. {
  1269. if (expected[i] != actual[i])
  1270. {
  1271. Unity.CurrentTestFailed = 1;
  1272. break;
  1273. }
  1274. }
  1275. }
  1276. else
  1277. { /* handle case of one pointers being null (if both null, test should pass) */
  1278. if (expected != actual)
  1279. {
  1280. Unity.CurrentTestFailed = 1;
  1281. }
  1282. }
  1283. if (Unity.CurrentTestFailed)
  1284. {
  1285. UnityTestResultsFailBegin(lineNumber);
  1286. UnityPrintExpectedAndActualStringsLen(expected, actual, length);
  1287. UnityAddMsgIfSpecified(msg);
  1288. UNITY_FAIL_AND_BAIL;
  1289. }
  1290. }
  1291. /*-----------------------------------------------*/
  1292. void UnityAssertEqualStringArray(UNITY_INTERNAL_PTR expected,
  1293. const char** actual,
  1294. const UNITY_UINT32 num_elements,
  1295. const char* msg,
  1296. const UNITY_LINE_TYPE lineNumber,
  1297. const UNITY_FLAGS_T flags)
  1298. {
  1299. UNITY_UINT32 i = 0;
  1300. UNITY_UINT32 j = 0;
  1301. const char* expd = NULL;
  1302. const char* act = NULL;
  1303. RETURN_IF_FAIL_OR_IGNORE;
  1304. /* if no elements, it's an error */
  1305. if (num_elements == 0)
  1306. {
  1307. UnityPrintPointlessAndBail();
  1308. }
  1309. if ((const void*)expected == (const void*)actual)
  1310. {
  1311. return; /* Both are NULL or same pointer */
  1312. }
  1313. if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg))
  1314. {
  1315. UNITY_FAIL_AND_BAIL;
  1316. }
  1317. if (flags != UNITY_ARRAY_TO_ARRAY)
  1318. {
  1319. expd = (const char*)expected;
  1320. }
  1321. do
  1322. {
  1323. act = actual[j];
  1324. if (flags == UNITY_ARRAY_TO_ARRAY)
  1325. {
  1326. expd = ((const char* const*)expected)[j];
  1327. }
  1328. /* if both pointers not null compare the strings */
  1329. if (expd && act)
  1330. {
  1331. for (i = 0; expd[i] || act[i]; i++)
  1332. {
  1333. if (expd[i] != act[i])
  1334. {
  1335. Unity.CurrentTestFailed = 1;
  1336. break;
  1337. }
  1338. }
  1339. }
  1340. else
  1341. { /* handle case of one pointers being null (if both null, test should pass) */
  1342. if (expd != act)
  1343. {
  1344. Unity.CurrentTestFailed = 1;
  1345. }
  1346. }
  1347. if (Unity.CurrentTestFailed)
  1348. {
  1349. UnityTestResultsFailBegin(lineNumber);
  1350. if (num_elements > 1)
  1351. {
  1352. UnityPrint(UnityStrElement);
  1353. UnityPrintNumberUnsigned(j);
  1354. }
  1355. UnityPrintExpectedAndActualStrings(expd, act);
  1356. UnityAddMsgIfSpecified(msg);
  1357. UNITY_FAIL_AND_BAIL;
  1358. }
  1359. } while (++j < num_elements);
  1360. }
  1361. /*-----------------------------------------------*/
  1362. void UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected,
  1363. UNITY_INTERNAL_PTR actual,
  1364. const UNITY_UINT32 length,
  1365. const UNITY_UINT32 num_elements,
  1366. const char* msg,
  1367. const UNITY_LINE_TYPE lineNumber,
  1368. const UNITY_FLAGS_T flags)
  1369. {
  1370. UNITY_PTR_ATTRIBUTE const unsigned char* ptr_exp = (UNITY_PTR_ATTRIBUTE const unsigned char*)expected;
  1371. UNITY_PTR_ATTRIBUTE const unsigned char* ptr_act = (UNITY_PTR_ATTRIBUTE const unsigned char*)actual;
  1372. UNITY_UINT32 elements = num_elements;
  1373. UNITY_UINT32 bytes;
  1374. RETURN_IF_FAIL_OR_IGNORE;
  1375. if ((elements == 0) || (length == 0))
  1376. {
  1377. UnityPrintPointlessAndBail();
  1378. }
  1379. if (expected == actual)
  1380. {
  1381. return; /* Both are NULL or same pointer */
  1382. }
  1383. if (UnityIsOneArrayNull(expected, actual, lineNumber, msg))
  1384. {
  1385. UNITY_FAIL_AND_BAIL;
  1386. }
  1387. while (elements--)
  1388. {
  1389. bytes = length;
  1390. while (bytes--)
  1391. {
  1392. if (*ptr_exp != *ptr_act)
  1393. {
  1394. UnityTestResultsFailBegin(lineNumber);
  1395. UnityPrint(UnityStrMemory);
  1396. if (num_elements > 1)
  1397. {
  1398. UnityPrint(UnityStrElement);
  1399. UnityPrintNumberUnsigned(num_elements - elements - 1);
  1400. }
  1401. UnityPrint(UnityStrByte);
  1402. UnityPrintNumberUnsigned(length - bytes - 1);
  1403. UnityPrint(UnityStrExpected);
  1404. UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
  1405. UnityPrint(UnityStrWas);
  1406. UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
  1407. UnityAddMsgIfSpecified(msg);
  1408. UNITY_FAIL_AND_BAIL;
  1409. }
  1410. ptr_exp++;
  1411. ptr_act++;
  1412. }
  1413. if (flags == UNITY_ARRAY_TO_VAL)
  1414. {
  1415. ptr_exp = (UNITY_PTR_ATTRIBUTE const unsigned char*)expected;
  1416. }
  1417. }
  1418. }
  1419. /*-----------------------------------------------*/
  1420. static union
  1421. {
  1422. UNITY_INT8 i8;
  1423. UNITY_INT16 i16;
  1424. UNITY_INT32 i32;
  1425. #ifdef UNITY_SUPPORT_64
  1426. UNITY_INT64 i64;
  1427. #endif
  1428. #ifndef UNITY_EXCLUDE_FLOAT
  1429. float f;
  1430. #endif
  1431. #ifndef UNITY_EXCLUDE_DOUBLE
  1432. double d;
  1433. #endif
  1434. } UnityQuickCompare;
  1435. UNITY_INTERNAL_PTR UnityNumToPtr(const UNITY_INT num, const UNITY_UINT8 size)
  1436. {
  1437. switch(size)
  1438. {
  1439. case 1:
  1440. UnityQuickCompare.i8 = (UNITY_INT8)num;
  1441. return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.i8);
  1442. case 2:
  1443. UnityQuickCompare.i16 = (UNITY_INT16)num;
  1444. return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.i16);
  1445. #ifdef UNITY_SUPPORT_64
  1446. case 8:
  1447. UnityQuickCompare.i64 = (UNITY_INT64)num;
  1448. return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.i64);
  1449. #endif
  1450. default: /* 4 bytes */
  1451. UnityQuickCompare.i32 = (UNITY_INT32)num;
  1452. return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.i32);
  1453. }
  1454. }
  1455. #ifndef UNITY_EXCLUDE_FLOAT
  1456. /*-----------------------------------------------*/
  1457. UNITY_INTERNAL_PTR UnityFloatToPtr(const float num)
  1458. {
  1459. UnityQuickCompare.f = num;
  1460. return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.f);
  1461. }
  1462. #endif
  1463. #ifndef UNITY_EXCLUDE_DOUBLE
  1464. /*-----------------------------------------------*/
  1465. UNITY_INTERNAL_PTR UnityDoubleToPtr(const double num)
  1466. {
  1467. UnityQuickCompare.d = num;
  1468. return (UNITY_INTERNAL_PTR)(&UnityQuickCompare.d);
  1469. }
  1470. #endif
  1471. /*-----------------------------------------------
  1472. * printf helper function
  1473. *-----------------------------------------------*/
  1474. #ifdef UNITY_INCLUDE_PRINT_FORMATTED
  1475. static void UnityPrintFVA(const char* format, va_list va)
  1476. {
  1477. const char* pch = format;
  1478. if (pch != NULL)
  1479. {
  1480. while (*pch)
  1481. {
  1482. /* format identification character */
  1483. if (*pch == '%')
  1484. {
  1485. pch++;
  1486. if (pch != NULL)
  1487. {
  1488. switch (*pch)
  1489. {
  1490. case 'd':
  1491. case 'i':
  1492. {
  1493. const int number = va_arg(va, int);
  1494. UnityPrintNumber((UNITY_INT)number);
  1495. break;
  1496. }
  1497. #ifndef UNITY_EXCLUDE_FLOAT_PRINT
  1498. case 'f':
  1499. case 'g':
  1500. {
  1501. const double number = va_arg(va, double);
  1502. UnityPrintFloat((UNITY_DOUBLE)number);
  1503. break;
  1504. }
  1505. #endif
  1506. case 'u':
  1507. {
  1508. const unsigned int number = va_arg(va, unsigned int);
  1509. UnityPrintNumberUnsigned((UNITY_UINT)number);
  1510. break;
  1511. }
  1512. case 'b':
  1513. {
  1514. const unsigned int number = va_arg(va, unsigned int);
  1515. const UNITY_UINT mask = (UNITY_UINT)0 - (UNITY_UINT)1;
  1516. UNITY_OUTPUT_CHAR('0');
  1517. UNITY_OUTPUT_CHAR('b');
  1518. UnityPrintMask(mask, (UNITY_UINT)number);
  1519. break;
  1520. }
  1521. case 'x':
  1522. case 'X':
  1523. case 'p':
  1524. {
  1525. const unsigned int number = va_arg(va, unsigned int);
  1526. UNITY_OUTPUT_CHAR('0');
  1527. UNITY_OUTPUT_CHAR('x');
  1528. UnityPrintNumberHex((UNITY_UINT)number, 8);
  1529. break;
  1530. }
  1531. case 'c':
  1532. {
  1533. const int ch = va_arg(va, int);
  1534. UnityPrintChar((const char *)&ch);
  1535. break;
  1536. }
  1537. case 's':
  1538. {
  1539. const char * string = va_arg(va, const char *);
  1540. UnityPrint(string);
  1541. break;
  1542. }
  1543. case '%':
  1544. {
  1545. UnityPrintChar(pch);
  1546. break;
  1547. }
  1548. default:
  1549. {
  1550. /* print the unknown format character */
  1551. UNITY_OUTPUT_CHAR('%');
  1552. UnityPrintChar(pch);
  1553. break;
  1554. }
  1555. }
  1556. }
  1557. }
  1558. #ifdef UNITY_OUTPUT_COLOR
  1559. /* print ANSI escape code */
  1560. else if ((*pch == 27) && (*(pch + 1) == '['))
  1561. {
  1562. pch += UnityPrintAnsiEscapeString(pch);
  1563. continue;
  1564. }
  1565. #endif
  1566. else if (*pch == '\n')
  1567. {
  1568. UNITY_PRINT_EOL();
  1569. }
  1570. else
  1571. {
  1572. UnityPrintChar(pch);
  1573. }
  1574. pch++;
  1575. }
  1576. }
  1577. }
  1578. void UnityPrintF(const UNITY_LINE_TYPE line, const char* format, ...)
  1579. {
  1580. UnityTestResultsBegin(Unity.TestFile, line);
  1581. UnityPrint("INFO");
  1582. if(format != NULL)
  1583. {
  1584. UnityPrint(": ");
  1585. va_list va;
  1586. va_start(va, format);
  1587. UnityPrintFVA(format, va);
  1588. va_end(va);
  1589. }
  1590. UNITY_PRINT_EOL();
  1591. }
  1592. #endif /* ! UNITY_INCLUDE_PRINT_FORMATTED */
  1593. /*-----------------------------------------------
  1594. * Control Functions
  1595. *-----------------------------------------------*/
  1596. /*-----------------------------------------------*/
  1597. void UnityFail(const char* msg, const UNITY_LINE_TYPE line)
  1598. {
  1599. RETURN_IF_FAIL_OR_IGNORE;
  1600. UnityTestResultsBegin(Unity.TestFile, line);
  1601. UnityPrint(UnityStrFail);
  1602. if (msg != NULL)
  1603. {
  1604. UNITY_OUTPUT_CHAR(':');
  1605. #ifdef UNITY_PRINT_TEST_CONTEXT
  1606. UNITY_PRINT_TEST_CONTEXT();
  1607. #endif
  1608. #ifndef UNITY_EXCLUDE_DETAILS
  1609. if (Unity.CurrentDetail1)
  1610. {
  1611. UnityPrint(UnityStrDetail1Name);
  1612. UnityPrint(Unity.CurrentDetail1);
  1613. if (Unity.CurrentDetail2)
  1614. {
  1615. UnityPrint(UnityStrDetail2Name);
  1616. UnityPrint(Unity.CurrentDetail2);
  1617. }
  1618. UnityPrint(UnityStrSpacer);
  1619. }
  1620. #endif
  1621. if (msg[0] != ' ')
  1622. {
  1623. UNITY_OUTPUT_CHAR(' ');
  1624. }
  1625. UnityPrint(msg);
  1626. }
  1627. UNITY_FAIL_AND_BAIL;
  1628. }
  1629. /*-----------------------------------------------*/
  1630. void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
  1631. {
  1632. RETURN_IF_FAIL_OR_IGNORE;
  1633. UnityTestResultsBegin(Unity.TestFile, line);
  1634. UnityPrint(UnityStrIgnore);
  1635. if (msg != NULL)
  1636. {
  1637. UNITY_OUTPUT_CHAR(':');
  1638. UNITY_OUTPUT_CHAR(' ');
  1639. UnityPrint(msg);
  1640. }
  1641. UNITY_IGNORE_AND_BAIL;
  1642. }
  1643. /*-----------------------------------------------*/
  1644. void UnityMessage(const char* msg, const UNITY_LINE_TYPE line)
  1645. {
  1646. UnityTestResultsBegin(Unity.TestFile, line);
  1647. UnityPrint("INFO");
  1648. if (msg != NULL)
  1649. {
  1650. UNITY_OUTPUT_CHAR(':');
  1651. UNITY_OUTPUT_CHAR(' ');
  1652. UnityPrint(msg);
  1653. }
  1654. UNITY_PRINT_EOL();
  1655. }
  1656. /*-----------------------------------------------*/
  1657. /* If we have not defined our own test runner, then include our default test runner to make life easier */
  1658. #ifndef UNITY_SKIP_DEFAULT_RUNNER
  1659. void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
  1660. {
  1661. Unity.CurrentTestName = FuncName;
  1662. Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum;
  1663. Unity.NumberOfTests++;
  1664. UNITY_CLR_DETAILS();
  1665. UNITY_EXEC_TIME_START();
  1666. if (TEST_PROTECT())
  1667. {
  1668. setUp();
  1669. Func();
  1670. }
  1671. if (TEST_PROTECT())
  1672. {
  1673. tearDown();
  1674. }
  1675. UNITY_EXEC_TIME_STOP();
  1676. UnityConcludeTest();
  1677. }
  1678. #endif
  1679. /*-----------------------------------------------*/
  1680. void UnitySetTestFile(const char* filename)
  1681. {
  1682. Unity.TestFile = filename;
  1683. }
  1684. /*-----------------------------------------------*/
  1685. void UnityBegin(const char* filename)
  1686. {
  1687. Unity.TestFile = filename;
  1688. Unity.CurrentTestName = NULL;
  1689. Unity.CurrentTestLineNumber = 0;
  1690. Unity.NumberOfTests = 0;
  1691. Unity.TestFailures = 0;
  1692. Unity.TestIgnores = 0;
  1693. Unity.CurrentTestFailed = 0;
  1694. Unity.CurrentTestIgnored = 0;
  1695. UNITY_CLR_DETAILS();
  1696. UNITY_OUTPUT_START();
  1697. }
  1698. /*-----------------------------------------------*/
  1699. int UnityEnd(void)
  1700. {
  1701. UNITY_PRINT_EOL();
  1702. UnityPrint(UnityStrBreaker);
  1703. UNITY_PRINT_EOL();
  1704. UnityPrintNumber((UNITY_INT)(Unity.NumberOfTests));
  1705. UnityPrint(UnityStrResultsTests);
  1706. UnityPrintNumber((UNITY_INT)(Unity.TestFailures));
  1707. UnityPrint(UnityStrResultsFailures);
  1708. UnityPrintNumber((UNITY_INT)(Unity.TestIgnores));
  1709. UnityPrint(UnityStrResultsIgnored);
  1710. UNITY_PRINT_EOL();
  1711. if (Unity.TestFailures == 0U)
  1712. {
  1713. UnityPrint(UnityStrOk);
  1714. }
  1715. else
  1716. {
  1717. UnityPrint(UnityStrFail);
  1718. #ifdef UNITY_DIFFERENTIATE_FINAL_FAIL
  1719. UNITY_OUTPUT_CHAR('E'); UNITY_OUTPUT_CHAR('D');
  1720. #endif
  1721. }
  1722. UNITY_PRINT_EOL();
  1723. UNITY_FLUSH_CALL();
  1724. UNITY_OUTPUT_COMPLETE();
  1725. return (int)(Unity.TestFailures);
  1726. }
  1727. /*-----------------------------------------------
  1728. * Command Line Argument Support
  1729. *-----------------------------------------------*/
  1730. #ifdef UNITY_USE_COMMAND_LINE_ARGS
  1731. char* UnityOptionIncludeNamed = NULL;
  1732. char* UnityOptionExcludeNamed = NULL;
  1733. int UnityVerbosity = 1;
  1734. /*-----------------------------------------------*/
  1735. int UnityParseOptions(int argc, char** argv)
  1736. {
  1737. int i;
  1738. UnityOptionIncludeNamed = NULL;
  1739. UnityOptionExcludeNamed = NULL;
  1740. for (i = 1; i < argc; i++)
  1741. {
  1742. if (argv[i][0] == '-')
  1743. {
  1744. switch (argv[i][1])
  1745. {
  1746. case 'l': /* list tests */
  1747. return -1;
  1748. case 'n': /* include tests with name including this string */
  1749. case 'f': /* an alias for -n */
  1750. if (argv[i][2] == '=')
  1751. {
  1752. UnityOptionIncludeNamed = &argv[i][3];
  1753. }
  1754. else if (++i < argc)
  1755. {
  1756. UnityOptionIncludeNamed = argv[i];
  1757. }
  1758. else
  1759. {
  1760. UnityPrint("ERROR: No Test String to Include Matches For");
  1761. UNITY_PRINT_EOL();
  1762. return 1;
  1763. }
  1764. break;
  1765. case 'q': /* quiet */
  1766. UnityVerbosity = 0;
  1767. break;
  1768. case 'v': /* verbose */
  1769. UnityVerbosity = 2;
  1770. break;
  1771. case 'x': /* exclude tests with name including this string */
  1772. if (argv[i][2] == '=')
  1773. {
  1774. UnityOptionExcludeNamed = &argv[i][3];
  1775. }
  1776. else if (++i < argc)
  1777. {
  1778. UnityOptionExcludeNamed = argv[i];
  1779. }
  1780. else
  1781. {
  1782. UnityPrint("ERROR: No Test String to Exclude Matches For");
  1783. UNITY_PRINT_EOL();
  1784. return 1;
  1785. }
  1786. break;
  1787. default:
  1788. UnityPrint("ERROR: Unknown Option ");
  1789. UNITY_OUTPUT_CHAR(argv[i][1]);
  1790. UNITY_PRINT_EOL();
  1791. return 1;
  1792. }
  1793. }
  1794. }
  1795. return 0;
  1796. }
  1797. /*-----------------------------------------------*/
  1798. int IsStringInBiggerString(const char* longstring, const char* shortstring)
  1799. {
  1800. const char* lptr = longstring;
  1801. const char* sptr = shortstring;
  1802. const char* lnext = lptr;
  1803. if (*sptr == '*')
  1804. {
  1805. return 1;
  1806. }
  1807. while (*lptr)
  1808. {
  1809. lnext = lptr + 1;
  1810. /* If they current bytes match, go on to the next bytes */
  1811. while (*lptr && *sptr && (*lptr == *sptr))
  1812. {
  1813. lptr++;
  1814. sptr++;
  1815. /* We're done if we match the entire string or up to a wildcard */
  1816. if (*sptr == '*')
  1817. return 1;
  1818. if (*sptr == ',')
  1819. return 1;
  1820. if (*sptr == '"')
  1821. return 1;
  1822. if (*sptr == '\'')
  1823. return 1;
  1824. if (*sptr == ':')
  1825. return 2;
  1826. if (*sptr == 0)
  1827. return 1;
  1828. }
  1829. /* Otherwise we start in the long pointer 1 character further and try again */
  1830. lptr = lnext;
  1831. sptr = shortstring;
  1832. }
  1833. return 0;
  1834. }
  1835. /*-----------------------------------------------*/
  1836. int UnityStringArgumentMatches(const char* str)
  1837. {
  1838. int retval;
  1839. const char* ptr1;
  1840. const char* ptr2;
  1841. const char* ptrf;
  1842. /* Go through the options and get the substrings for matching one at a time */
  1843. ptr1 = str;
  1844. while (ptr1[0] != 0)
  1845. {
  1846. if ((ptr1[0] == '"') || (ptr1[0] == '\''))
  1847. {
  1848. ptr1++;
  1849. }
  1850. /* look for the start of the next partial */
  1851. ptr2 = ptr1;
  1852. ptrf = 0;
  1853. do
  1854. {
  1855. ptr2++;
  1856. if ((ptr2[0] == ':') && (ptr2[1] != 0) && (ptr2[0] != '\'') && (ptr2[0] != '"') && (ptr2[0] != ','))
  1857. {
  1858. ptrf = &ptr2[1];
  1859. }
  1860. } while ((ptr2[0] != 0) && (ptr2[0] != '\'') && (ptr2[0] != '"') && (ptr2[0] != ','));
  1861. while ((ptr2[0] != 0) && ((ptr2[0] == ':') || (ptr2[0] == '\'') || (ptr2[0] == '"') || (ptr2[0] == ',')))
  1862. {
  1863. ptr2++;
  1864. }
  1865. /* done if complete filename match */
  1866. retval = IsStringInBiggerString(Unity.TestFile, ptr1);
  1867. if (retval == 1)
  1868. {
  1869. return retval;
  1870. }
  1871. /* done if testname match after filename partial match */
  1872. if ((retval == 2) && (ptrf != 0))
  1873. {
  1874. if (IsStringInBiggerString(Unity.CurrentTestName, ptrf))
  1875. {
  1876. return 1;
  1877. }
  1878. }
  1879. /* done if complete testname match */
  1880. if (IsStringInBiggerString(Unity.CurrentTestName, ptr1) == 1)
  1881. {
  1882. return 1;
  1883. }
  1884. ptr1 = ptr2;
  1885. }
  1886. /* we couldn't find a match for any substrings */
  1887. return 0;
  1888. }
  1889. /*-----------------------------------------------*/
  1890. int UnityTestMatches(void)
  1891. {
  1892. /* Check if this test name matches the included test pattern */
  1893. int retval;
  1894. if (UnityOptionIncludeNamed)
  1895. {
  1896. retval = UnityStringArgumentMatches(UnityOptionIncludeNamed);
  1897. }
  1898. else
  1899. {
  1900. retval = 1;
  1901. }
  1902. /* Check if this test name matches the excluded test pattern */
  1903. if (UnityOptionExcludeNamed)
  1904. {
  1905. if (UnityStringArgumentMatches(UnityOptionExcludeNamed))
  1906. {
  1907. retval = 0;
  1908. }
  1909. }
  1910. return retval;
  1911. }
  1912. #endif /* UNITY_USE_COMMAND_LINE_ARGS */
  1913. /*-----------------------------------------------*/