28 #include "Assertion.h" 32 using namespace internal;
43 template <
typename A,
typename B>
44 void printAssertionMessage(Print* printer,
bool ok,
const char* file,
45 uint16_t line,
const A& lhs,
const char* opName,
const B& rhs) {
53 printer->print(
"Assertion ");
54 printer->print(ok ?
"passed" :
"failed");
55 printer->print(
": (");
58 printer->print(opName);
63 printer->print(
", file ");
65 printer->print(
", line ");
67 printer->println(
'.');
73 void printAssertionMessage(Print* printer,
bool ok,
const char* file,
74 uint16_t line,
bool lhs,
const char* opName,
bool rhs) {
77 printer->print(
"Assertion ");
78 printer->print(ok ?
"passed" :
"failed");
79 printer->print(
": (");
80 printer->print(lhs ?
"true" :
"false");
82 printer->print(opName);
84 printer->print(rhs ?
"true" :
"false");
86 printer->print(
", file ");
88 printer->print(
", line ");
90 printer->println(
'.');
97 void printAssertionBoolMessage(Print* printer,
bool ok,
const char* file,
98 uint16_t line,
bool arg,
bool value) {
101 printer->print(
"Assertion ");
102 printer->print(ok ?
"passed" :
"failed");
103 printer->print(
": (");
104 printer->print(arg ?
"true" :
"false");
105 printer->print(
") is ");
106 printer->print(value ?
"true" :
"false");
107 printer->print(
", file ");
108 printer->print(file);
109 printer->print(
", line ");
110 printer->print(line);
111 printer->println(
'.');
114 template <
typename A>
115 void printAssertionNearMessage(Print* printer,
bool ok,
const char* file,
116 uint16_t line,
const A& lhs,
const A& rhs,
const char* opName,
118 printer->print(
"Assertion ");
119 printer->print(ok ?
"passed" :
"failed");
120 printer->print(
": |(");
122 printer->print(
") - (");
124 printer->print(
")| ");
125 printer->print(opName);
126 printer->print(
" (");
127 printer->print(error);
129 printer->print(
", file ");
130 printer->print(file);
131 printer->print(
", line ");
132 printer->print(line);
133 printer->println(
'.');
143 bool Assertion::assertionBool(
const char* file, uint16_t line,
bool arg,
145 if (isDone())
return false;
146 bool ok = (arg == value);
147 if (isOutputEnabled(ok)) {
155 bool Assertion::assertion(
const char* file, uint16_t line,
bool lhs,
156 const char* opName,
bool (*op)(
bool lhs,
bool rhs),
158 if (isDone())
return false;
159 bool ok = op(lhs, rhs);
160 if (isOutputEnabled(ok)) {
168 bool Assertion::assertion(
const char* file, uint16_t line,
char lhs,
169 const char* opName,
bool (*op)(
char lhs,
char rhs),
171 if (isDone())
return false;
172 bool ok = op(lhs, rhs);
173 if (isOutputEnabled(ok)) {
181 bool Assertion::assertion(
const char* file, uint16_t line,
int lhs,
182 const char* opName,
bool (*op)(
int lhs,
int rhs),
184 if (isDone())
return false;
185 bool ok = op(lhs, rhs);
186 if (isOutputEnabled(ok)) {
194 bool Assertion::assertion(
const char* file, uint16_t line,
unsigned int lhs,
195 const char* opName,
bool (*op)(
unsigned int lhs,
unsigned int rhs),
197 if (isDone())
return false;
198 bool ok = op(lhs, rhs);
199 if (isOutputEnabled(ok)) {
207 bool Assertion::assertion(
const char* file, uint16_t line,
long lhs,
208 const char* opName,
bool (*op)(
long lhs,
long rhs),
210 if (isDone())
return false;
211 bool ok = op(lhs, rhs);
212 if (isOutputEnabled(ok)) {
220 bool Assertion::assertion(
const char* file, uint16_t line,
unsigned long lhs,
221 const char* opName,
bool (*op)(
unsigned long lhs,
unsigned long rhs),
223 if (isDone())
return false;
224 bool ok = op(lhs, rhs);
225 if (isOutputEnabled(ok)) {
233 bool Assertion::assertion(
const char* file, uint16_t line,
double lhs,
234 const char* opName,
bool (*op)(
double lhs,
double rhs),
236 if (isDone())
return false;
237 bool ok = op(lhs, rhs);
238 if (isOutputEnabled(ok)) {
246 bool Assertion::assertion(
const char* file, uint16_t line,
const char* lhs,
247 const char* opName,
bool (*op)(
const char* lhs,
const char* rhs),
249 if (isDone())
return false;
250 bool ok = op(lhs, rhs);
251 if (isOutputEnabled(ok)) {
259 bool Assertion::assertion(
const char* file, uint16_t line,
const char* lhs,
260 const char* opName,
bool (*op)(
const char* lhs,
const String& rhs),
262 if (isDone())
return false;
263 bool ok = op(lhs, rhs);
264 if (isOutputEnabled(ok)) {
272 bool Assertion::assertion(
const char* file, uint16_t line,
const char* lhs,
274 bool (*op)(
const char* lhs,
const __FlashStringHelper* rhs),
275 const __FlashStringHelper* rhs) {
276 if (isDone())
return false;
277 bool ok = op(lhs, rhs);
278 if (isOutputEnabled(ok)) {
286 bool Assertion::assertion(
const char* file, uint16_t line,
const String& lhs,
287 const char* opName,
bool (*op)(
const String& lhs,
const char* rhs),
289 if (isDone())
return false;
290 bool ok = op(lhs, rhs);
291 if (isOutputEnabled(ok)) {
299 bool Assertion::assertion(
const char* file, uint16_t line,
const String& lhs,
300 const char* opName,
bool (*op)(
const String& lhs,
const String& rhs),
302 if (isDone())
return false;
303 bool ok = op(lhs, rhs);
304 if (isOutputEnabled(ok)) {
312 bool Assertion::assertion(
const char* file, uint16_t line,
const String& lhs,
314 bool (*op)(
const String& lhs,
const __FlashStringHelper* rhs),
315 const __FlashStringHelper* rhs) {
316 if (isDone())
return false;
317 bool ok = op(lhs, rhs);
318 if (isOutputEnabled(ok)) {
326 bool Assertion::assertion(
const char* file, uint16_t line,
327 const __FlashStringHelper* lhs,
const char* opName,
328 bool (*op)(
const __FlashStringHelper* lhs,
const char* rhs),
330 if (isDone())
return false;
331 bool ok = op(lhs, rhs);
332 if (isOutputEnabled(ok)) {
340 bool Assertion::assertion(
const char* file, uint16_t line,
341 const __FlashStringHelper* lhs,
const char* opName,
342 bool (*op)(
const __FlashStringHelper* lhs,
const String& rhs),
344 if (isDone())
return false;
345 bool ok = op(lhs, rhs);
346 if (isOutputEnabled(ok)) {
354 bool Assertion::assertion(
const char* file, uint16_t line,
355 const __FlashStringHelper* lhs,
const char* opName,
356 bool (*op)(
const __FlashStringHelper* lhs,
const __FlashStringHelper* rhs),
357 const __FlashStringHelper* rhs) {
358 if (isDone())
return false;
359 bool ok = op(lhs, rhs);
360 if (isOutputEnabled(ok)) {
368 bool Assertion::assertionNear(
const char* file, uint16_t line,
369 int lhs,
int rhs,
int error,
const char* opName,
370 bool (*opNear)(
int lhs,
int rhs,
int error)) {
371 if (isDone())
return false;
372 bool ok = opNear(lhs, rhs, error);
373 if (isOutputEnabled(ok)) {
375 lhs, rhs, opName, error);
381 bool Assertion::assertionNear(
const char* file, uint16_t line,
382 unsigned int lhs,
unsigned int rhs,
unsigned int error,
const char* opName,
383 bool (*opNear)(
unsigned int lhs,
unsigned int rhs,
unsigned int error)) {
384 if (isDone())
return false;
385 bool ok = opNear(lhs, rhs, error);
386 if (isOutputEnabled(ok)) {
388 lhs, rhs, opName, error);
394 bool Assertion::assertionNear(
const char* file, uint16_t line,
395 long lhs,
long rhs,
long error,
const char* opName,
396 bool (*opNear)(
long lhs,
long rhs,
long error)) {
397 if (isDone())
return false;
398 bool ok = opNear(lhs, rhs, error);
399 if (isOutputEnabled(ok)) {
401 lhs, rhs, opName, error);
407 bool Assertion::assertionNear(
const char* file, uint16_t line,
408 unsigned long lhs,
unsigned long rhs,
unsigned long error,
410 bool (*opNear)(
unsigned long lhs,
unsigned long rhs,
unsigned long error)) {
411 if (isDone())
return false;
412 bool ok = opNear(lhs, rhs, error);
413 if (isOutputEnabled(ok)) {
415 lhs, rhs, opName, error);
421 bool Assertion::assertionNear(
const char* file, uint16_t line,
422 double lhs,
double rhs,
double error,
const char* opName,
423 bool (*opNear)(
double lhs,
double rhs,
double error)) {
424 if (isDone())
return false;
425 bool ok = opNear(lhs, rhs, error);
426 if (isOutputEnabled(ok)) {
428 lhs, rhs, opName, error);
444 template <
typename A,
typename B>
445 void printAssertionMessageVerbose(Print* printer,
bool ok,
const char* file,
446 uint16_t line,
const A& lhs,
const __FlashStringHelper* lhsString,
447 const char* opName,
const B& rhs,
const __FlashStringHelper* rhsString) {
454 printer->print(
"Assertion ");
455 printer->print(ok ?
"passed" :
"failed");
456 printer->print(
": (");
457 printer->print(lhsString);
460 printer->print(
") ");
461 printer->print(opName);
462 printer->print(
" (");
463 printer->print(rhsString);
468 printer->print(
", file ");
469 printer->print(file);
470 printer->print(
", line ");
471 printer->print(line);
472 printer->println(
'.');
478 void printAssertionMessageVerbose(Print* printer,
bool ok,
const char* file,
479 uint16_t line,
bool lhs,
const __FlashStringHelper* lhsString,
480 const char* opName,
bool rhs,
const __FlashStringHelper* rhsString) {
483 printer->print(
"Assertion ");
484 printer->print(ok ?
"passed" :
"failed");
485 printer->print(
": (");
486 printer->print(lhsString);
488 printer->print(lhs ?
"true" :
"false");
489 printer->print(
") ");
490 printer->print(opName);
491 printer->print(
" (");
492 printer->print(rhsString);
494 printer->print(rhs ?
"true" :
"false");
496 printer->print(
", file ");
497 printer->print(file);
498 printer->print(
", line ");
499 printer->print(line);
500 printer->println(
'.');
507 void printAssertionBoolMessageVerbose(Print* printer,
bool ok,
const char* file,
508 uint16_t line,
bool arg,
const __FlashStringHelper* argString,
bool value) {
511 printer->print(
"Assertion ");
512 printer->print(ok ?
"passed" :
"failed");
513 printer->print(
": (");
514 printer->print(argString);
516 printer->print(arg ?
"true" :
"false");
517 printer->print(
") is ");
518 printer->print(value ?
"true" :
"false");
519 printer->print(
", file ");
520 printer->print(file);
521 printer->print(
", line ");
522 printer->print(line);
523 printer->println(
'.');
526 template <
typename A>
527 void printAssertionNearMessageVerbose(Print* printer,
bool ok,
const char* file,
528 uint16_t line,
const A& lhs,
const __FlashStringHelper* lhsString,
529 const A& rhs,
const __FlashStringHelper* rhsString,
531 const A& error,
const __FlashStringHelper* errorString) {
532 printer->print(
"Assertion ");
533 printer->print(ok ?
"passed" :
"failed");
534 printer->print(
": |(");
535 printer->print(lhsString);
538 printer->print(
") - (");
539 printer->print(rhsString);
542 printer->print(
")| ");
543 printer->print(opName);
544 printer->print(
" (");
545 printer->print(errorString);
547 printer->print(error);
549 printer->print(
", file ");
550 printer->print(file);
551 printer->print(
", line ");
552 printer->print(line);
553 printer->println(
'.');
558 bool Assertion::assertionBoolVerbose(
const char* file, uint16_t line,
bool arg,
559 const __FlashStringHelper* argString,
bool value) {
560 if (isDone())
return false;
561 bool ok = (arg == value);
562 if (isOutputEnabled(ok)) {
564 arg, argString, value);
570 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
bool lhs,
571 const __FlashStringHelper* lhsString,
const char* opName,
572 bool (*op)(
bool lhs,
bool rhs),
bool rhs,
573 const __FlashStringHelper* rhsString) {
574 if (isDone())
return false;
575 bool ok = op(lhs, rhs);
576 if (isOutputEnabled(ok)) {
578 lhs, lhsString, opName, rhs, rhsString);
584 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
char lhs,
585 const __FlashStringHelper* lhsString,
const char* opName,
586 bool (*op)(
char lhs,
char rhs),
char rhs,
587 const __FlashStringHelper* rhsString) {
588 if (isDone())
return false;
589 bool ok = op(lhs, rhs);
590 if (isOutputEnabled(ok)) {
592 lhs, lhsString, opName, rhs, rhsString);
598 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
int lhs,
599 const __FlashStringHelper* lhsString,
const char* opName,
600 bool (*op)(
int lhs,
int rhs),
int rhs,
601 const __FlashStringHelper* rhsString) {
602 if (isDone())
return false;
603 bool ok = op(lhs, rhs);
604 if (isOutputEnabled(ok)) {
606 lhs, lhsString, opName, rhs, rhsString);
612 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
613 unsigned int lhs,
const __FlashStringHelper* lhsString,
const char* opName,
614 bool (*op)(
unsigned int lhs,
unsigned int rhs),
615 unsigned int rhs,
const __FlashStringHelper* rhsString) {
616 if (isDone())
return false;
617 bool ok = op(lhs, rhs);
618 if (isOutputEnabled(ok)) {
620 lhs, lhsString, opName, rhs, rhsString);
626 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
long lhs,
627 const __FlashStringHelper* lhsString,
const char* opName,
628 bool (*op)(
long lhs,
long rhs),
long rhs,
629 const __FlashStringHelper* rhsString) {
630 if (isDone())
return false;
631 bool ok = op(lhs, rhs);
632 if (isOutputEnabled(ok)) {
634 lhs, lhsString, opName, rhs, rhsString);
640 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
641 unsigned long lhs,
const __FlashStringHelper* lhsString,
const char* opName,
642 bool (*op)(
unsigned long lhs,
unsigned long rhs),
643 unsigned long rhs,
const __FlashStringHelper* rhsString) {
644 if (isDone())
return false;
645 bool ok = op(lhs, rhs);
646 if (isOutputEnabled(ok)) {
648 lhs, lhsString, opName, rhs, rhsString);
654 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
double lhs,
655 const __FlashStringHelper* lhsString,
const char* opName,
656 bool (*op)(
double lhs,
double rhs),
double rhs,
657 const __FlashStringHelper* rhsString) {
658 if (isDone())
return false;
659 bool ok = op(lhs, rhs);
660 if (isOutputEnabled(ok)) {
662 lhs, lhsString, opName, rhs, rhsString);
668 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
669 const char* lhs,
const __FlashStringHelper* lhsString,
const char* opName,
670 bool (*op)(
const char* lhs,
const char* rhs),
671 const char* rhs,
const __FlashStringHelper* rhsString) {
672 if (isDone())
return false;
673 bool ok = op(lhs, rhs);
674 if (isOutputEnabled(ok)) {
676 lhs, lhsString, opName, rhs, rhsString);
682 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
683 const char* lhs,
const __FlashStringHelper* lhsString,
684 const char* opName,
bool (*op)(
const char* lhs,
const String& rhs),
685 const String& rhs,
const __FlashStringHelper* rhsString) {
686 if (isDone())
return false;
687 bool ok = op(lhs, rhs);
688 if (isOutputEnabled(ok)) {
690 lhs, lhsString, opName, rhs, rhsString);
696 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
697 const char* lhs,
const __FlashStringHelper* lhsString,
const char* opName,
698 bool (*op)(
const char* lhs,
const __FlashStringHelper* rhs),
699 const __FlashStringHelper* rhs,
const __FlashStringHelper* rhsString) {
700 if (isDone())
return false;
701 bool ok = op(lhs, rhs);
702 if (isOutputEnabled(ok)) {
704 lhs, lhsString, opName, rhs, rhsString);
710 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
711 const String& lhs,
const __FlashStringHelper* lhsString,
const char* opName,
712 bool (*op)(
const String& lhs,
const char* rhs),
713 const char* rhs,
const __FlashStringHelper* rhsString) {
714 if (isDone())
return false;
715 bool ok = op(lhs, rhs);
716 if (isOutputEnabled(ok)) {
718 lhs, lhsString, opName, rhs, rhsString);
724 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
725 const String& lhs,
const __FlashStringHelper* lhsString,
const char* opName,
726 bool (*op)(
const String& lhs,
const String& rhs),
727 const String& rhs,
const __FlashStringHelper* rhsString) {
728 if (isDone())
return false;
729 bool ok = op(lhs, rhs);
730 if (isOutputEnabled(ok)) {
732 lhs, lhsString, opName, rhs, rhsString);
738 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
739 const String& lhs,
const __FlashStringHelper* lhsString,
const char* opName,
740 bool (*op)(
const String& lhs,
const __FlashStringHelper* rhs),
741 const __FlashStringHelper* rhs,
const __FlashStringHelper* rhsString) {
742 if (isDone())
return false;
743 bool ok = op(lhs, rhs);
744 if (isOutputEnabled(ok)) {
746 lhs, lhsString, opName, rhs, rhsString);
752 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
753 const __FlashStringHelper* lhs,
const __FlashStringHelper* lhsString,
755 bool (*op)(
const __FlashStringHelper* lhs,
const char* rhs),
756 const char* rhs,
const __FlashStringHelper* rhsString) {
757 if (isDone())
return false;
758 bool ok = op(lhs, rhs);
759 if (isOutputEnabled(ok)) {
761 lhs, lhsString, opName, rhs, rhsString);
767 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
768 const __FlashStringHelper* lhs,
const __FlashStringHelper* lhsString,
770 bool (*op)(
const __FlashStringHelper* lhs,
const String& rhs),
771 const String& rhs,
const __FlashStringHelper* rhsString) {
772 if (isDone())
return false;
773 bool ok = op(lhs, rhs);
774 if (isOutputEnabled(ok)) {
776 lhs, lhsString, opName, rhs, rhsString);
782 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
783 const __FlashStringHelper* lhs,
const __FlashStringHelper* lhsString,
785 bool (*op)(
const __FlashStringHelper* lhs,
const __FlashStringHelper* rhs),
786 const __FlashStringHelper* rhs,
const __FlashStringHelper* rhsString) {
787 if (isDone())
return false;
788 bool ok = op(lhs, rhs);
789 if (isOutputEnabled(ok)) {
791 lhs, lhsString, opName, rhs, rhsString);
797 bool Assertion::assertionNearVerbose(
const char* file, uint16_t line,
798 int lhs,
const __FlashStringHelper* lhsString,
799 int rhs,
const __FlashStringHelper* rhsString,
800 int error,
const __FlashStringHelper* errorString,
802 bool (*opNear)(
int lhs,
int rhs,
int error)) {
803 if (isDone())
return false;
804 bool ok = opNear(lhs, rhs, error);
805 if (isOutputEnabled(ok)) {
807 lhs, lhsString, rhs, rhsString, opName, error, errorString);
813 bool Assertion::assertionNearVerbose(
const char* file, uint16_t line,
814 unsigned int lhs,
const __FlashStringHelper* lhsString,
815 unsigned int rhs,
const __FlashStringHelper* rhsString,
816 unsigned int error,
const __FlashStringHelper* errorString,
818 bool (*opNear)(
unsigned int lhs,
unsigned int rhs,
unsigned int error)) {
819 if (isDone())
return false;
820 bool ok = opNear(lhs, rhs, error);
821 if (isOutputEnabled(ok)) {
823 lhs, lhsString, rhs, rhsString, opName, error, errorString);
829 bool Assertion::assertionNearVerbose(
const char* file, uint16_t line,
830 long lhs,
const __FlashStringHelper* lhsString,
831 long rhs,
const __FlashStringHelper* rhsString,
832 long error,
const __FlashStringHelper* errorString,
834 bool (*opNear)(
long lhs,
long rhs,
long error)) {
835 if (isDone())
return false;
836 bool ok = opNear(lhs, rhs, error);
837 if (isOutputEnabled(ok)) {
839 lhs, lhsString, rhs, rhsString, opName, error, errorString);
845 bool Assertion::assertionNearVerbose(
const char* file, uint16_t line,
846 unsigned long lhs,
const __FlashStringHelper* lhsString,
847 unsigned long rhs,
const __FlashStringHelper* rhsString,
848 unsigned long error,
const __FlashStringHelper* errorString,
850 bool (*opNear)(
unsigned long lhs,
unsigned long rhs,
unsigned long error)) {
851 if (isDone())
return false;
852 bool ok = opNear(lhs, rhs, error);
853 if (isOutputEnabled(ok)) {
855 lhs, lhsString, rhs, rhsString, opName, error, errorString);
861 bool Assertion::assertionNearVerbose(
const char* file, uint16_t line,
862 double lhs,
const __FlashStringHelper* lhsString,
863 double rhs,
const __FlashStringHelper* rhsString,
864 double error,
const __FlashStringHelper* errorString,
866 bool (*opNear)(
double lhs,
double rhs,
double error)) {
867 if (isDone())
return false;
868 bool ok = opNear(lhs, rhs, error);
869 if (isOutputEnabled(ok)) {
871 lhs, lhsString, rhs, rhsString, opName, error, errorString);
bool isOutputEnabled(bool ok)
Returns true if an assertion message should be printed.
static const uint8_t kAssertionPassed
Print assertXxx() passed message.
static Print * getPrinter()
Get the output printer used by the various assertion() methods and the TestRunner.
static const uint8_t kAssertionFailed
Print assertXxx() failed message.
Various macros to smooth over the differences among the various platforms with regards to their suppo...