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