unity.c 64 KB

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