Line data Source code
1 : /* ==========================================
2 : Unity Project - A Test Framework for C
3 : Copyright (c) 2007 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 <stdio.h>
9 : #include <string.h>
10 :
11 : #define UNITY_FAIL_AND_BAIL { Unity.CurrentTestFailed = 1; UNITY_OUTPUT_CHAR('\n'); longjmp(Unity.AbortFrame, 1); }
12 : #define UNITY_IGNORE_AND_BAIL { Unity.CurrentTestIgnored = 1; UNITY_OUTPUT_CHAR('\n'); longjmp(Unity.AbortFrame, 1); }
13 : /// return prematurely if we are already in failure or ignore state
14 : #define UNITY_SKIP_EXECUTION { if ((Unity.CurrentTestFailed != 0) || (Unity.CurrentTestIgnored != 0)) {return;} }
15 : #define UNITY_PRINT_EOL { UNITY_OUTPUT_CHAR('\n'); }
16 :
17 : struct _Unity Unity = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , { 0 } };
18 :
19 : const char* UnityStrNull = "NULL";
20 : const char* UnityStrSpacer = ". ";
21 : const char* UnityStrExpected = " Expected ";
22 : const char* UnityStrWas = " Was ";
23 : const char* UnityStrTo = " To ";
24 : const char* UnityStrElement = " Element ";
25 : const char* UnityStrByte = " Byte ";
26 : const char* UnityStrMemory = " Memory Mismatch.";
27 : const char* UnityStrDelta = " Values Not Within Delta ";
28 : const char* UnityStrPointless= " You Asked Me To Compare Nothing, Which Was Pointless.";
29 : const char* UnityStrNullPointerForExpected= " Expected pointer to be NULL";
30 : const char* UnityStrNullPointerForActual = " Actual pointer was NULL";
31 :
32 : // compiler-generic print formatting masks
33 : const _U_UINT UnitySizeMask[] =
34 : {
35 : 255u, // 0xFF
36 : 65535u, // 0xFFFF
37 : 65535u,
38 : 4294967295u, // 0xFFFFFFFF
39 : 4294967295u,
40 : 4294967295u,
41 : 4294967295u
42 : #ifdef UNITY_SUPPORT_64
43 : ,0xFFFFFFFFFFFFFFFF
44 : #endif
45 : };
46 :
47 : void UnityPrintFail(void);
48 : void UnityPrintOk(void);
49 :
50 : //-----------------------------------------------
51 : // Pretty Printers & Test Result Output Handlers
52 : //-----------------------------------------------
53 :
54 101 : void UnityPrint(const char* string)
55 : {
56 101 : const char* pch = string;
57 :
58 101 : if (pch != NULL)
59 : {
60 1765 : while (*pch)
61 : {
62 : // printable characters plus CR & LF are printed
63 1563 : if ((*pch <= 126) && (*pch >= 32))
64 : {
65 1563 : UNITY_OUTPUT_CHAR(*pch);
66 : }
67 : //write escaped carriage returns
68 0 : else if (*pch == 13)
69 : {
70 0 : UNITY_OUTPUT_CHAR('\\');
71 0 : UNITY_OUTPUT_CHAR('r');
72 : }
73 : //write escaped line feeds
74 0 : else if (*pch == 10)
75 : {
76 0 : UNITY_OUTPUT_CHAR('\\');
77 0 : UNITY_OUTPUT_CHAR('n');
78 : }
79 : // unprintable characters are shown as codes
80 : else
81 : {
82 0 : UNITY_OUTPUT_CHAR('\\');
83 0 : UnityPrintNumberHex((_U_SINT)*pch, 2);
84 : }
85 1563 : pch++;
86 : }
87 : }
88 101 : }
89 :
90 : //-----------------------------------------------
91 0 : void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style)
92 : {
93 0 : if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
94 : {
95 0 : UnityPrintNumber(number);
96 : }
97 0 : else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
98 : {
99 0 : UnityPrintNumberUnsigned( (_U_UINT)number & UnitySizeMask[((_U_UINT)style & (_U_UINT)0x0F) - 1] );
100 : }
101 : else
102 : {
103 0 : UnityPrintNumberHex((_U_UINT)number, (style & 0x000F) << 1);
104 : }
105 0 : }
106 :
107 : //-----------------------------------------------
108 : /// basically do an itoa using as little ram as possible
109 35 : void UnityPrintNumber(const _U_SINT number_to_print)
110 : {
111 35 : _U_SINT divisor = 1;
112 : _U_SINT next_divisor;
113 35 : _U_SINT number = number_to_print;
114 :
115 35 : if (number < 0)
116 : {
117 0 : UNITY_OUTPUT_CHAR('-');
118 0 : number = -number;
119 : }
120 :
121 : // figure out initial divisor
122 131 : while (number / divisor > 9)
123 : {
124 61 : next_divisor = divisor * 10;
125 61 : if (next_divisor > divisor)
126 61 : divisor = next_divisor;
127 : else
128 0 : break;
129 : }
130 :
131 : // now mod and print, then divide divisor
132 96 : do
133 : {
134 96 : UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
135 96 : divisor /= 10;
136 : }
137 : while (divisor > 0);
138 35 : }
139 :
140 : //-----------------------------------------------
141 : /// basically do an itoa using as little ram as possible
142 0 : void UnityPrintNumberUnsigned(const _U_UINT number)
143 : {
144 0 : _U_UINT divisor = 1;
145 : _U_UINT next_divisor;
146 :
147 : // figure out initial divisor
148 0 : while (number / divisor > 9)
149 : {
150 0 : next_divisor = divisor * 10;
151 0 : if (next_divisor > divisor)
152 0 : divisor = next_divisor;
153 : else
154 0 : break;
155 : }
156 :
157 : // now mod and print, then divide divisor
158 0 : do
159 : {
160 0 : UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
161 0 : divisor /= 10;
162 : }
163 : while (divisor > 0);
164 0 : }
165 :
166 : //-----------------------------------------------
167 0 : void UnityPrintNumberHex(const _U_UINT number, const char nibbles_to_print)
168 : {
169 : _U_UINT nibble;
170 0 : char nibbles = nibbles_to_print;
171 0 : UNITY_OUTPUT_CHAR('0');
172 0 : UNITY_OUTPUT_CHAR('x');
173 :
174 0 : while (nibbles > 0)
175 : {
176 0 : nibble = (number >> (--nibbles << 2)) & 0x0000000F;
177 0 : if (nibble <= 9)
178 : {
179 0 : UNITY_OUTPUT_CHAR((char)('0' + nibble));
180 : }
181 : else
182 : {
183 0 : UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble));
184 : }
185 : }
186 0 : }
187 :
188 : //-----------------------------------------------
189 0 : void UnityPrintMask(const _U_UINT mask, const _U_UINT number)
190 : {
191 0 : _U_UINT current_bit = (_U_UINT)1 << (UNITY_INT_WIDTH - 1);
192 : _US32 i;
193 :
194 0 : for (i = 0; i < UNITY_INT_WIDTH; i++)
195 : {
196 0 : if (current_bit & mask)
197 : {
198 0 : if (current_bit & number)
199 : {
200 0 : UNITY_OUTPUT_CHAR('1');
201 : }
202 : else
203 : {
204 0 : UNITY_OUTPUT_CHAR('0');
205 : }
206 : }
207 : else
208 : {
209 0 : UNITY_OUTPUT_CHAR('X');
210 : }
211 0 : current_bit = current_bit >> 1;
212 : }
213 0 : }
214 :
215 : //-----------------------------------------------
216 : #ifdef UNITY_FLOAT_VERBOSE
217 : void UnityPrintFloat(_UF number)
218 : {
219 : char TempBuffer[32];
220 : sprintf(TempBuffer, "%.6f", number);
221 : UnityPrint(TempBuffer);
222 : }
223 : #endif
224 :
225 : //-----------------------------------------------
226 :
227 0 : void UnityPrintFail(void)
228 : {
229 0 : UnityPrint("FAIL");
230 0 : }
231 :
232 1 : void UnityPrintOk(void)
233 : {
234 1 : UnityPrint("OK");
235 1 : }
236 :
237 : //-----------------------------------------------
238 32 : void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line)
239 : {
240 32 : UnityPrint(file);
241 32 : UNITY_OUTPUT_CHAR(':');
242 32 : UnityPrintNumber(line);
243 32 : UNITY_OUTPUT_CHAR(':');
244 32 : UnityPrint(Unity.CurrentTestName);
245 32 : UNITY_OUTPUT_CHAR(':');
246 32 : }
247 :
248 : //-----------------------------------------------
249 0 : void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)
250 : {
251 0 : UnityTestResultsBegin(Unity.TestFile, line);
252 0 : UnityPrint("FAIL:");
253 0 : }
254 :
255 : //-----------------------------------------------
256 32 : void UnityConcludeTest(void)
257 : {
258 32 : if (Unity.CurrentTestIgnored)
259 : {
260 0 : Unity.TestIgnores++;
261 : }
262 32 : else if (!Unity.CurrentTestFailed)
263 : {
264 32 : UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);
265 32 : UnityPrint("PASS");
266 32 : UNITY_PRINT_EOL;
267 : }
268 : else
269 : {
270 0 : Unity.TestFailures++;
271 : }
272 :
273 32 : Unity.CurrentTestFailed = 0;
274 32 : Unity.CurrentTestIgnored = 0;
275 32 : }
276 :
277 : //-----------------------------------------------
278 0 : void UnityAddMsgIfSpecified(const char* msg)
279 : {
280 0 : if (msg)
281 : {
282 0 : UnityPrint(UnityStrSpacer);
283 0 : UnityPrint(msg);
284 : }
285 0 : }
286 :
287 : //-----------------------------------------------
288 0 : void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual)
289 : {
290 0 : UnityPrint(UnityStrExpected);
291 0 : if (expected != NULL)
292 : {
293 0 : UNITY_OUTPUT_CHAR('\'');
294 0 : UnityPrint(expected);
295 0 : UNITY_OUTPUT_CHAR('\'');
296 : }
297 : else
298 : {
299 0 : UnityPrint(UnityStrNull);
300 : }
301 0 : UnityPrint(UnityStrWas);
302 0 : if (actual != NULL)
303 : {
304 0 : UNITY_OUTPUT_CHAR('\'');
305 0 : UnityPrint(actual);
306 0 : UNITY_OUTPUT_CHAR('\'');
307 : }
308 : else
309 : {
310 0 : UnityPrint(UnityStrNull);
311 : }
312 0 : }
313 :
314 : //-----------------------------------------------
315 : // Assertion & Control Helpers
316 : //-----------------------------------------------
317 :
318 0 : int UnityCheckArraysForNull(const void* expected, const void* actual, const UNITY_LINE_TYPE lineNumber, const char* msg)
319 : {
320 : //return true if they are both NULL
321 0 : if ((expected == NULL) && (actual == NULL))
322 0 : return 1;
323 :
324 : //throw error if just expected is NULL
325 0 : if (expected == NULL)
326 : {
327 0 : UnityTestResultsFailBegin(lineNumber);
328 0 : UnityPrint(UnityStrNullPointerForExpected);
329 0 : UnityAddMsgIfSpecified(msg);
330 0 : UNITY_FAIL_AND_BAIL;
331 : }
332 :
333 : //throw error if just actual is NULL
334 0 : if (actual == NULL)
335 : {
336 0 : UnityTestResultsFailBegin(lineNumber);
337 0 : UnityPrint(UnityStrNullPointerForActual);
338 0 : UnityAddMsgIfSpecified(msg);
339 0 : UNITY_FAIL_AND_BAIL;
340 : }
341 :
342 : //return false if neither is NULL
343 0 : return 0;
344 : }
345 :
346 : //-----------------------------------------------
347 : // Assertion Functions
348 : //-----------------------------------------------
349 :
350 0 : void UnityAssertBits(const _U_SINT mask,
351 : const _U_SINT expected,
352 : const _U_SINT actual,
353 : const char* msg,
354 : const UNITY_LINE_TYPE lineNumber)
355 : {
356 0 : UNITY_SKIP_EXECUTION;
357 :
358 0 : if ((mask & expected) != (mask & actual))
359 : {
360 0 : UnityTestResultsFailBegin(lineNumber);
361 0 : UnityPrint(UnityStrExpected);
362 0 : UnityPrintMask(mask, expected);
363 0 : UnityPrint(UnityStrWas);
364 0 : UnityPrintMask(mask, actual);
365 0 : UnityAddMsgIfSpecified(msg);
366 0 : UNITY_FAIL_AND_BAIL;
367 : }
368 : }
369 :
370 : //-----------------------------------------------
371 113 : void UnityAssertEqualNumber(const _U_SINT expected,
372 : const _U_SINT actual,
373 : const char* msg,
374 : const UNITY_LINE_TYPE lineNumber,
375 : const UNITY_DISPLAY_STYLE_T style)
376 : {
377 226 : UNITY_SKIP_EXECUTION;
378 :
379 113 : if (expected != actual)
380 : {
381 0 : UnityTestResultsFailBegin(lineNumber);
382 0 : UnityPrint(UnityStrExpected);
383 0 : UnityPrintNumberByStyle(expected, style);
384 0 : UnityPrint(UnityStrWas);
385 0 : UnityPrintNumberByStyle(actual, style);
386 0 : UnityAddMsgIfSpecified(msg);
387 0 : UNITY_FAIL_AND_BAIL;
388 : }
389 : }
390 :
391 : //-----------------------------------------------
392 0 : void UnityAssertEqualIntArray(const _U_SINT* expected,
393 : const _U_SINT* actual,
394 : const _UU32 num_elements,
395 : const char* msg,
396 : const UNITY_LINE_TYPE lineNumber,
397 : const UNITY_DISPLAY_STYLE_T style)
398 : {
399 0 : _UU32 elements = num_elements;
400 0 : const _US8* ptr_exp = (_US8*)expected;
401 0 : const _US8* ptr_act = (_US8*)actual;
402 :
403 0 : UNITY_SKIP_EXECUTION;
404 :
405 0 : if (elements == 0)
406 : {
407 0 : UnityTestResultsFailBegin(lineNumber);
408 0 : UnityPrint(UnityStrPointless);
409 0 : UnityAddMsgIfSpecified(msg);
410 0 : UNITY_FAIL_AND_BAIL;
411 : }
412 :
413 0 : if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
414 0 : return;
415 :
416 0 : switch(style)
417 : {
418 : case UNITY_DISPLAY_STYLE_HEX8:
419 : case UNITY_DISPLAY_STYLE_INT8:
420 : case UNITY_DISPLAY_STYLE_UINT8:
421 0 : while (elements--)
422 : {
423 0 : if (*ptr_exp != *ptr_act)
424 : {
425 0 : UnityTestResultsFailBegin(lineNumber);
426 0 : UnityPrint(UnityStrElement);
427 0 : UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
428 0 : UnityPrint(UnityStrExpected);
429 0 : UnityPrintNumberByStyle(*ptr_exp, style);
430 0 : UnityPrint(UnityStrWas);
431 0 : UnityPrintNumberByStyle(*ptr_act, style);
432 0 : UnityAddMsgIfSpecified(msg);
433 0 : UNITY_FAIL_AND_BAIL;
434 : }
435 0 : ptr_exp += 1;
436 0 : ptr_act += 1;
437 : }
438 0 : break;
439 : case UNITY_DISPLAY_STYLE_HEX16:
440 : case UNITY_DISPLAY_STYLE_INT16:
441 : case UNITY_DISPLAY_STYLE_UINT16:
442 0 : while (elements--)
443 : {
444 0 : if (*(_US16*)ptr_exp != *(_US16*)ptr_act)
445 : {
446 0 : UnityTestResultsFailBegin(lineNumber);
447 0 : UnityPrint(UnityStrElement);
448 0 : UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
449 0 : UnityPrint(UnityStrExpected);
450 0 : UnityPrintNumberByStyle(*(_US16*)ptr_exp, style);
451 0 : UnityPrint(UnityStrWas);
452 0 : UnityPrintNumberByStyle(*(_US16*)ptr_act, style);
453 0 : UnityAddMsgIfSpecified(msg);
454 0 : UNITY_FAIL_AND_BAIL;
455 : }
456 0 : ptr_exp += 2;
457 0 : ptr_act += 2;
458 : }
459 0 : break;
460 : #ifdef UNITY_SUPPORT_64
461 : case UNITY_DISPLAY_STYLE_HEX64:
462 : case UNITY_DISPLAY_STYLE_INT64:
463 : case UNITY_DISPLAY_STYLE_UINT64:
464 0 : while (elements--)
465 : {
466 0 : if (*(_US64*)ptr_exp != *(_US64*)ptr_act)
467 : {
468 0 : UnityTestResultsFailBegin(lineNumber);
469 0 : UnityPrint(UnityStrElement);
470 0 : UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
471 0 : UnityPrint(UnityStrExpected);
472 0 : UnityPrintNumberByStyle(*(_US64*)ptr_exp, style);
473 0 : UnityPrint(UnityStrWas);
474 0 : UnityPrintNumberByStyle(*(_US64*)ptr_act, style);
475 0 : UnityAddMsgIfSpecified(msg);
476 0 : UNITY_FAIL_AND_BAIL;
477 : }
478 0 : ptr_exp += 8;
479 0 : ptr_act += 8;
480 : }
481 0 : break;
482 : #endif
483 : default:
484 0 : while (elements--)
485 : {
486 0 : if (*(_US32*)ptr_exp != *(_US32*)ptr_act)
487 : {
488 0 : UnityTestResultsFailBegin(lineNumber);
489 0 : UnityPrint(UnityStrElement);
490 0 : UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
491 0 : UnityPrint(UnityStrExpected);
492 0 : UnityPrintNumberByStyle(*(_US32*)ptr_exp, style);
493 0 : UnityPrint(UnityStrWas);
494 0 : UnityPrintNumberByStyle(*(_US32*)ptr_act, style);
495 0 : UnityAddMsgIfSpecified(msg);
496 0 : UNITY_FAIL_AND_BAIL;
497 : }
498 0 : ptr_exp += 4;
499 0 : ptr_act += 4;
500 : }
501 0 : break;
502 : }
503 : }
504 :
505 : //-----------------------------------------------
506 : #ifndef UNITY_EXCLUDE_FLOAT
507 0 : void UnityAssertEqualFloatArray(const _UF* expected,
508 : const _UF* actual,
509 : const _UU32 num_elements,
510 : const char* msg,
511 : const UNITY_LINE_TYPE lineNumber)
512 : {
513 0 : _UU32 elements = num_elements;
514 0 : const _UF* ptr_expected = expected;
515 0 : const _UF* ptr_actual = actual;
516 : _UF diff, tol;
517 :
518 0 : UNITY_SKIP_EXECUTION;
519 :
520 0 : if (elements == 0)
521 : {
522 0 : UnityTestResultsFailBegin(lineNumber);
523 0 : UnityPrint(UnityStrPointless);
524 0 : UnityAddMsgIfSpecified(msg);
525 0 : UNITY_FAIL_AND_BAIL;
526 : }
527 :
528 0 : if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
529 0 : return;
530 :
531 0 : while (elements--)
532 : {
533 0 : diff = *ptr_expected - *ptr_actual;
534 0 : if (diff < 0.0)
535 0 : diff = 0.0 - diff;
536 0 : tol = UNITY_FLOAT_PRECISION * *ptr_expected;
537 0 : if (tol < 0.0)
538 0 : tol = 0.0 - tol;
539 0 : if (diff > tol)
540 : {
541 0 : UnityTestResultsFailBegin(lineNumber);
542 0 : UnityPrint(UnityStrElement);
543 0 : UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
544 : #ifdef UNITY_FLOAT_VERBOSE
545 : UnityPrint(UnityStrExpected);
546 : UnityPrintFloat(*ptr_expected);
547 : UnityPrint(UnityStrWas);
548 : UnityPrintFloat(*ptr_actual);
549 : #else
550 0 : UnityPrint(UnityStrDelta);
551 : #endif
552 0 : UnityAddMsgIfSpecified(msg);
553 0 : UNITY_FAIL_AND_BAIL;
554 : }
555 0 : ptr_expected++;
556 0 : ptr_actual++;
557 : }
558 : }
559 :
560 : //-----------------------------------------------
561 0 : void UnityAssertFloatsWithin(const _UF delta,
562 : const _UF expected,
563 : const _UF actual,
564 : const char* msg,
565 : const UNITY_LINE_TYPE lineNumber)
566 : {
567 0 : _UF diff = actual - expected;
568 0 : _UF pos_delta = delta;
569 :
570 0 : UNITY_SKIP_EXECUTION;
571 :
572 0 : if (diff < 0)
573 : {
574 0 : diff = 0.0f - diff;
575 : }
576 0 : if (pos_delta < 0)
577 : {
578 0 : pos_delta = 0.0f - pos_delta;
579 : }
580 :
581 : // NOTE: This comparison is deliberately this way round so that NaNs fail.
582 0 : if ( ! (pos_delta >= diff) )
583 : {
584 0 : UnityTestResultsFailBegin(lineNumber);
585 : #ifdef UNITY_FLOAT_VERBOSE
586 : UnityPrint(UnityStrExpected);
587 : UnityPrintFloat(expected);
588 : UnityPrint(UnityStrWas);
589 : UnityPrintFloat(actual);
590 : #else
591 0 : UnityPrint(UnityStrDelta);
592 : #endif
593 0 : UnityAddMsgIfSpecified(msg);
594 0 : UNITY_FAIL_AND_BAIL;
595 : }
596 : }
597 :
598 : #endif //not UNITY_EXCLUDE_FLOAT
599 :
600 : //-----------------------------------------------
601 : #ifndef UNITY_EXCLUDE_DOUBLE
602 : void UnityAssertEqualDoubleArray(const _UD* expected,
603 : const _UD* actual,
604 : const _UU32 num_elements,
605 : const char* msg,
606 : const UNITY_LINE_TYPE lineNumber)
607 : {
608 : _UU32 elements = num_elements;
609 : const _UD* ptr_expected = expected;
610 : const _UD* ptr_actual = actual;
611 : _UD diff, tol;
612 :
613 : UNITY_SKIP_EXECUTION;
614 :
615 : if (elements == 0)
616 : {
617 : UnityTestResultsFailBegin(lineNumber);
618 : UnityPrint(UnityStrPointless);
619 : UnityAddMsgIfSpecified(msg);
620 : UNITY_FAIL_AND_BAIL;
621 : }
622 :
623 : if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
624 : return;
625 :
626 : while (elements--)
627 : {
628 : diff = *ptr_expected - *ptr_actual;
629 : if (diff < 0.0)
630 : diff = 0.0 - diff;
631 : tol = UNITY_DOUBLE_PRECISION * *ptr_expected;
632 : if (tol < 0.0)
633 : tol = 0.0 - tol;
634 : if (diff > tol)
635 : {
636 : UnityTestResultsFailBegin(lineNumber);
637 : UnityPrint(UnityStrElement);
638 : UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
639 : #ifdef UNITY_DOUBLE_VERBOSE
640 : UnityPrint(UnityStrExpected);
641 : UnityPrintFloat((float)(*ptr_expected));
642 : UnityPrint(UnityStrWas);
643 : UnityPrintFloat((float)(*ptr_actual));
644 : #else
645 : UnityPrint(UnityStrDelta);
646 : #endif
647 : UnityAddMsgIfSpecified(msg);
648 : UNITY_FAIL_AND_BAIL;
649 : }
650 : ptr_expected++;
651 : ptr_actual++;
652 : }
653 : }
654 :
655 : //-----------------------------------------------
656 : void UnityAssertDoublesWithin(const _UD delta,
657 : const _UD expected,
658 : const _UD actual,
659 : const char* msg,
660 : const UNITY_LINE_TYPE lineNumber)
661 : {
662 : _UD diff = actual - expected;
663 : _UD pos_delta = delta;
664 :
665 : UNITY_SKIP_EXECUTION;
666 :
667 : if (diff < 0)
668 : {
669 : diff = 0.0f - diff;
670 : }
671 : if (pos_delta < 0)
672 : {
673 : pos_delta = 0.0f - pos_delta;
674 : }
675 :
676 : if (pos_delta < diff)
677 : {
678 : UnityTestResultsFailBegin(lineNumber);
679 : #ifdef UNITY_DOUBLE_VERBOSE
680 : UnityPrint(UnityStrExpected);
681 : UnityPrintFloat((float)expected);
682 : UnityPrint(UnityStrWas);
683 : UnityPrintFloat((float)actual);
684 : #else
685 : UnityPrint(UnityStrDelta);
686 : #endif
687 : UnityAddMsgIfSpecified(msg);
688 : UNITY_FAIL_AND_BAIL;
689 : }
690 : }
691 :
692 : #endif // not UNITY_EXCLUDE_DOUBLE
693 :
694 : //-----------------------------------------------
695 0 : void UnityAssertNumbersWithin( const _U_SINT delta,
696 : const _U_SINT expected,
697 : const _U_SINT actual,
698 : const char* msg,
699 : const UNITY_LINE_TYPE lineNumber,
700 : const UNITY_DISPLAY_STYLE_T style)
701 : {
702 0 : UNITY_SKIP_EXECUTION;
703 :
704 0 : if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
705 : {
706 0 : if (actual > expected)
707 0 : Unity.CurrentTestFailed = ((actual - expected) > delta);
708 : else
709 0 : Unity.CurrentTestFailed = ((expected - actual) > delta);
710 : }
711 : else
712 : {
713 0 : if ((_U_UINT)actual > (_U_UINT)expected)
714 0 : Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > (_U_UINT)delta);
715 : else
716 0 : Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > (_U_UINT)delta);
717 : }
718 :
719 0 : if (Unity.CurrentTestFailed)
720 : {
721 0 : UnityTestResultsFailBegin(lineNumber);
722 0 : UnityPrint(UnityStrDelta);
723 0 : UnityPrintNumberByStyle(delta, style);
724 0 : UnityPrint(UnityStrExpected);
725 0 : UnityPrintNumberByStyle(expected, style);
726 0 : UnityPrint(UnityStrWas);
727 0 : UnityPrintNumberByStyle(actual, style);
728 0 : UnityAddMsgIfSpecified(msg);
729 0 : UNITY_FAIL_AND_BAIL;
730 : }
731 : }
732 :
733 : //-----------------------------------------------
734 9 : void UnityAssertEqualString(const char* expected,
735 : const char* actual,
736 : const char* msg,
737 : const UNITY_LINE_TYPE lineNumber)
738 : {
739 : _UU32 i;
740 :
741 18 : UNITY_SKIP_EXECUTION;
742 :
743 : // if both pointers not null compare the strings
744 9 : if (expected && actual)
745 : {
746 50 : for (i = 0; expected[i] || actual[i]; i++)
747 : {
748 41 : if (expected[i] != actual[i])
749 : {
750 0 : Unity.CurrentTestFailed = 1;
751 0 : break;
752 : }
753 : }
754 : }
755 : else
756 : { // handle case of one pointers being null (if both null, test should pass)
757 0 : if (expected != actual)
758 : {
759 0 : Unity.CurrentTestFailed = 1;
760 : }
761 : }
762 :
763 9 : if (Unity.CurrentTestFailed)
764 : {
765 0 : UnityTestResultsFailBegin(lineNumber);
766 0 : UnityPrintExpectedAndActualStrings(expected, actual);
767 0 : UnityAddMsgIfSpecified(msg);
768 0 : UNITY_FAIL_AND_BAIL;
769 : }
770 : }
771 :
772 : //-----------------------------------------------
773 0 : void UnityAssertEqualStringArray( const char** expected,
774 : const char** actual,
775 : const _UU32 num_elements,
776 : const char* msg,
777 : const UNITY_LINE_TYPE lineNumber)
778 : {
779 0 : _UU32 i, j = 0;
780 :
781 0 : UNITY_SKIP_EXECUTION;
782 :
783 : // if no elements, it's an error
784 0 : if (num_elements == 0)
785 : {
786 0 : UnityTestResultsFailBegin(lineNumber);
787 0 : UnityPrint(UnityStrPointless);
788 0 : UnityAddMsgIfSpecified(msg);
789 0 : UNITY_FAIL_AND_BAIL;
790 : }
791 :
792 0 : if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
793 0 : return;
794 :
795 0 : do
796 : {
797 : // if both pointers not null compare the strings
798 0 : if (expected[j] && actual[j])
799 : {
800 0 : for (i = 0; expected[j][i] || actual[j][i]; i++)
801 : {
802 0 : if (expected[j][i] != actual[j][i])
803 : {
804 0 : Unity.CurrentTestFailed = 1;
805 0 : break;
806 : }
807 : }
808 : }
809 : else
810 : { // handle case of one pointers being null (if both null, test should pass)
811 0 : if (expected[j] != actual[j])
812 : {
813 0 : Unity.CurrentTestFailed = 1;
814 : }
815 : }
816 :
817 0 : if (Unity.CurrentTestFailed)
818 : {
819 0 : UnityTestResultsFailBegin(lineNumber);
820 0 : if (num_elements > 1)
821 : {
822 0 : UnityPrint(UnityStrElement);
823 0 : UnityPrintNumberByStyle((num_elements - j - 1), UNITY_DISPLAY_STYLE_UINT);
824 : }
825 0 : UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j]));
826 0 : UnityAddMsgIfSpecified(msg);
827 0 : UNITY_FAIL_AND_BAIL;
828 : }
829 : } while (++j < num_elements);
830 : }
831 :
832 : //-----------------------------------------------
833 0 : void UnityAssertEqualMemory( const void* expected,
834 : const void* actual,
835 : const _UU32 length,
836 : const _UU32 num_elements,
837 : const char* msg,
838 : const UNITY_LINE_TYPE lineNumber)
839 : {
840 0 : unsigned char* ptr_exp = (unsigned char*)expected;
841 0 : unsigned char* ptr_act = (unsigned char*)actual;
842 0 : _UU32 elements = num_elements;
843 : _UU32 bytes;
844 :
845 0 : UNITY_SKIP_EXECUTION;
846 :
847 0 : if ((elements == 0) || (length == 0))
848 : {
849 0 : UnityTestResultsFailBegin(lineNumber);
850 0 : UnityPrint(UnityStrPointless);
851 0 : UnityAddMsgIfSpecified(msg);
852 0 : UNITY_FAIL_AND_BAIL;
853 : }
854 :
855 0 : if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
856 0 : return;
857 :
858 0 : while (elements--)
859 : {
860 : /////////////////////////////////////
861 0 : bytes = length;
862 0 : while (bytes--)
863 : {
864 0 : if (*ptr_exp != *ptr_act)
865 : {
866 0 : UnityTestResultsFailBegin(lineNumber);
867 0 : UnityPrint(UnityStrMemory);
868 0 : if (num_elements > 1)
869 : {
870 0 : UnityPrint(UnityStrElement);
871 0 : UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
872 : }
873 0 : UnityPrint(UnityStrByte);
874 0 : UnityPrintNumberByStyle((length - bytes - 1), UNITY_DISPLAY_STYLE_UINT);
875 0 : UnityPrint(UnityStrExpected);
876 0 : UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
877 0 : UnityPrint(UnityStrWas);
878 0 : UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
879 0 : UnityAddMsgIfSpecified(msg);
880 0 : UNITY_FAIL_AND_BAIL;
881 : }
882 0 : ptr_exp += 1;
883 0 : ptr_act += 1;
884 : }
885 : /////////////////////////////////////
886 :
887 : }
888 : }
889 :
890 : //-----------------------------------------------
891 : // Control Functions
892 : //-----------------------------------------------
893 :
894 0 : void UnityFail(const char* msg, const UNITY_LINE_TYPE line)
895 : {
896 0 : UNITY_SKIP_EXECUTION;
897 :
898 0 : UnityTestResultsBegin(Unity.TestFile, line);
899 0 : UnityPrintFail();
900 0 : if (msg != NULL)
901 : {
902 0 : UNITY_OUTPUT_CHAR(':');
903 0 : if (msg[0] != ' ')
904 : {
905 0 : UNITY_OUTPUT_CHAR(' ');
906 : }
907 0 : UnityPrint(msg);
908 : }
909 0 : UNITY_FAIL_AND_BAIL;
910 : }
911 :
912 : //-----------------------------------------------
913 0 : void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
914 : {
915 0 : UNITY_SKIP_EXECUTION;
916 :
917 0 : UnityTestResultsBegin(Unity.TestFile, line);
918 0 : UnityPrint("IGNORE");
919 0 : if (msg != NULL)
920 : {
921 0 : UNITY_OUTPUT_CHAR(':');
922 0 : UNITY_OUTPUT_CHAR(' ');
923 0 : UnityPrint(msg);
924 : }
925 0 : UNITY_IGNORE_AND_BAIL;
926 : }
927 :
928 : //-----------------------------------------------
929 : void setUp(void);
930 : void tearDown(void);
931 0 : void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
932 : {
933 0 : Unity.CurrentTestName = FuncName;
934 0 : Unity.CurrentTestLineNumber = FuncLineNum;
935 0 : Unity.NumberOfTests++;
936 0 : if (TEST_PROTECT())
937 : {
938 0 : setUp();
939 0 : Func();
940 : }
941 0 : if (TEST_PROTECT() && !(Unity.CurrentTestIgnored))
942 : {
943 0 : tearDown();
944 : }
945 0 : UnityConcludeTest();
946 0 : }
947 :
948 : //-----------------------------------------------
949 1 : void UnityBegin(void)
950 : {
951 1 : Unity.NumberOfTests = 0;
952 1 : Unity.TestFailures = 0;
953 1 : Unity.TestIgnores = 0;
954 1 : Unity.CurrentTestFailed = 0;
955 1 : Unity.CurrentTestIgnored = 0;
956 1 : }
957 :
958 : //-----------------------------------------------
959 1 : int UnityEnd(void)
960 : {
961 1 : UnityPrint("-----------------------");
962 1 : UNITY_PRINT_EOL;
963 1 : UnityPrintNumber(Unity.NumberOfTests);
964 1 : UnityPrint(" Tests ");
965 1 : UnityPrintNumber(Unity.TestFailures);
966 1 : UnityPrint(" Failures ");
967 1 : UnityPrintNumber(Unity.TestIgnores);
968 1 : UnityPrint(" Ignored");
969 1 : UNITY_PRINT_EOL;
970 1 : if (Unity.TestFailures == 0U)
971 : {
972 1 : UnityPrintOk();
973 : }
974 : else
975 : {
976 0 : UnityPrintFail();
977 : }
978 1 : UNITY_PRINT_EOL;
979 1 : return Unity.TestFailures;
980 : }
|