28 #include "Assertion.h" 41 template <
typename A,
typename B>
42 void printAssertionMessage(
bool ok,
const char* file, uint16_t line,
43 const A& lhs,
const char *opName,
const B& rhs) {
52 printer->print(
"Assertion ");
53 printer->print(ok ?
"passed" :
"failed");
54 printer->print(
": (");
57 printer->print(opName);
62 printer->print(
", file ");
64 printer->print(
", line ");
66 printer->println(
'.');
72 void printAssertionMessage(
bool ok,
const char* file, uint16_t line,
73 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(
bool ok,
const char* file, uint16_t line,
98 bool arg,
bool value) {
102 printer->print(
"Assertion ");
103 printer->print(ok ?
"passed" :
"failed");
104 printer->print(
": (");
105 printer->print(arg ?
"true" :
"false");
106 printer->print(
") is ");
107 printer->print(value ?
"true" :
"false");
108 printer->print(
", file ");
109 printer->print(file);
110 printer->print(
", line ");
111 printer->print(line);
112 printer->println(
'.');
122 bool Assertion::assertionBool(
const char* file, uint16_t line,
bool arg,
124 if (
isDone())
return false;
125 bool ok = (arg == value);
127 printAssertionBoolMessage(ok, file, line, arg, value);
133 bool Assertion::assertion(
const char* file, uint16_t line,
bool lhs,
134 const char* opName,
bool (*op)(
bool lhs,
bool rhs),
136 if (
isDone())
return false;
137 bool ok = op(lhs, rhs);
139 printAssertionMessage(ok, file, line, lhs, opName, rhs);
145 bool Assertion::assertion(
const char* file, uint16_t line,
char lhs,
146 const char* opName,
bool (*op)(
char lhs,
char rhs),
148 if (
isDone())
return false;
149 bool ok = op(lhs, rhs);
151 printAssertionMessage(ok, file, line, lhs, opName, rhs);
157 bool Assertion::assertion(
const char* file, uint16_t line,
int lhs,
158 const char* opName,
bool (*op)(
int lhs,
int rhs),
160 if (
isDone())
return false;
161 bool ok = op(lhs, rhs);
163 printAssertionMessage(ok, file, line, lhs, opName, rhs);
169 bool Assertion::assertion(
const char* file, uint16_t line,
unsigned int lhs,
170 const char* opName,
bool (*op)(
unsigned int lhs,
unsigned int rhs),
172 if (
isDone())
return false;
173 bool ok = op(lhs, rhs);
175 printAssertionMessage(ok, file, line, lhs, opName, rhs);
181 bool Assertion::assertion(
const char* file, uint16_t line,
long lhs,
182 const char* opName,
bool (*op)(
long lhs,
long rhs),
184 if (
isDone())
return false;
185 bool ok = op(lhs, rhs);
187 printAssertionMessage(ok, file, line, lhs, opName, rhs);
193 bool Assertion::assertion(
const char* file, uint16_t line,
unsigned long lhs,
194 const char* opName,
bool (*op)(
unsigned long lhs,
unsigned long rhs),
196 if (
isDone())
return false;
197 bool ok = op(lhs, rhs);
199 printAssertionMessage(ok, file, line, lhs, opName, rhs);
205 bool Assertion::assertion(
const char* file, uint16_t line,
double lhs,
206 const char* opName,
bool (*op)(
double lhs,
double rhs),
208 if (
isDone())
return false;
209 bool ok = op(lhs, rhs);
211 printAssertionMessage(ok, file, line, lhs, opName, rhs);
217 bool Assertion::assertion(
const char* file, uint16_t line,
const char* lhs,
218 const char* opName,
bool (*op)(
const char* lhs,
const char* rhs),
220 if (
isDone())
return false;
221 bool ok = op(lhs, rhs);
223 printAssertionMessage(ok, file, line, lhs, opName, rhs);
229 bool Assertion::assertion(
const char* file, uint16_t line,
const char* lhs,
230 const char *opName,
bool (*op)(
const char* lhs,
const String& rhs),
232 if (
isDone())
return false;
233 bool ok = op(lhs, rhs);
235 printAssertionMessage(ok, file, line, lhs, opName, rhs);
241 bool Assertion::assertion(
const char* file, uint16_t line,
const char* lhs,
243 bool (*op)(
const char* lhs,
const __FlashStringHelper* rhs),
244 const __FlashStringHelper* rhs) {
245 if (
isDone())
return false;
246 bool ok = op(lhs, rhs);
248 printAssertionMessage(ok, file, line, lhs, opName, rhs);
254 bool Assertion::assertion(
const char* file, uint16_t line,
const String& lhs,
255 const char *opName,
bool (*op)(
const String& lhs,
const char* rhs),
257 if (
isDone())
return false;
258 bool ok = op(lhs, rhs);
260 printAssertionMessage(ok, file, line, lhs, opName, rhs);
266 bool Assertion::assertion(
const char* file, uint16_t line,
const String& lhs,
267 const char *opName,
bool (*op)(
const String& lhs,
const String& rhs),
269 if (
isDone())
return false;
270 bool ok = op(lhs, rhs);
272 printAssertionMessage(ok, file, line, lhs, opName, rhs);
278 bool Assertion::assertion(
const char* file, uint16_t line,
const String& lhs,
280 bool (*op)(
const String& lhs,
const __FlashStringHelper* rhs),
281 const __FlashStringHelper* rhs) {
282 if (
isDone())
return false;
283 bool ok = op(lhs, rhs);
285 printAssertionMessage(ok, file, line, lhs, opName, rhs);
291 bool Assertion::assertion(
const char* file, uint16_t line,
292 const __FlashStringHelper* lhs,
const char *opName,
293 bool (*op)(
const __FlashStringHelper* lhs,
const char* rhs),
295 if (
isDone())
return false;
296 bool ok = op(lhs, rhs);
298 printAssertionMessage(ok, file, line, lhs, opName, rhs);
304 bool Assertion::assertion(
const char* file, uint16_t line,
305 const __FlashStringHelper* lhs,
const char *opName,
306 bool (*op)(
const __FlashStringHelper* lhs,
const String& rhs),
308 if (
isDone())
return false;
309 bool ok = op(lhs, rhs);
311 printAssertionMessage(ok, file, line, lhs, opName, rhs);
317 bool Assertion::assertion(
const char* file, uint16_t line,
318 const __FlashStringHelper* lhs,
const char *opName,
319 bool (*op)(
const __FlashStringHelper* lhs,
const __FlashStringHelper* rhs),
320 const __FlashStringHelper* rhs) {
321 if (
isDone())
return false;
322 bool ok = op(lhs, rhs);
324 printAssertionMessage(ok, file, line, lhs, opName, rhs);
338 template <
typename A,
typename B>
339 void printAssertionMessageVerbose(
bool ok,
const char* file,
340 uint16_t line,
const A& lhs,
const __FlashStringHelper* lhsString,
341 const char *opName,
const B& rhs,
const __FlashStringHelper* rhsString) {
349 printer->print(
"Assertion ");
350 printer->print(ok ?
"passed" :
"failed");
351 printer->print(
": (");
352 printer->print(lhsString);
355 printer->print(
") ");
356 printer->print(opName);
357 printer->print(
" (");
358 printer->print(rhsString);
363 printer->print(
", file ");
364 printer->print(file);
365 printer->print(
", line ");
366 printer->print(line);
367 printer->println(
'.');
373 void printAssertionMessageVerbose(
bool ok,
const char* file,
374 uint16_t line,
bool lhs,
const __FlashStringHelper* lhsString,
375 const char *opName,
bool rhs,
const __FlashStringHelper* rhsString) {
379 printer->print(
"Assertion ");
380 printer->print(ok ?
"passed" :
"failed");
381 printer->print(
": (");
382 printer->print(lhsString);
384 printer->print(lhs ?
"true" :
"false");
385 printer->print(
") ");
386 printer->print(opName);
387 printer->print(
" (");
388 printer->print(rhsString);
390 printer->print(rhs ?
"true" :
"false");
392 printer->print(
", file ");
393 printer->print(file);
394 printer->print(
", line ");
395 printer->print(line);
396 printer->println(
'.');
403 void printAssertionBoolMessageVerbose(
bool ok,
const char* file,
404 uint16_t line,
bool arg,
const __FlashStringHelper* argString,
bool value) {
408 printer->print(
"Assertion ");
409 printer->print(ok ?
"passed" :
"failed");
410 printer->print(
": (");
411 printer->print(argString);
413 printer->print(arg ?
"true" :
"false");
414 printer->print(
") is ");
415 printer->print(value ?
"true" :
"false");
416 printer->print(
", file ");
417 printer->print(file);
418 printer->print(
", line ");
419 printer->print(line);
420 printer->println(
'.');
425 bool Assertion::assertionBoolVerbose(
const char* file, uint16_t line,
bool arg,
426 const __FlashStringHelper* argString,
bool value) {
427 if (
isDone())
return false;
428 bool ok = (arg == value);
430 printAssertionBoolMessageVerbose(ok, file, line, arg, argString, value);
436 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
bool lhs,
437 const __FlashStringHelper* lhsString,
const char* opName,
438 bool (*op)(
bool lhs,
bool rhs),
bool rhs,
439 const __FlashStringHelper* rhsString) {
440 if (
isDone())
return false;
441 bool ok = op(lhs, rhs);
443 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
450 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
char lhs,
451 const __FlashStringHelper* lhsString,
const char* opName,
452 bool (*op)(
char lhs,
char rhs),
char rhs,
453 const __FlashStringHelper* rhsString) {
454 if (
isDone())
return false;
455 bool ok = op(lhs, rhs);
457 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
464 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
int lhs,
465 const __FlashStringHelper* lhsString,
const char* opName,
466 bool (*op)(
int lhs,
int rhs),
int rhs,
467 const __FlashStringHelper* rhsString) {
468 if (
isDone())
return false;
469 bool ok = op(lhs, rhs);
471 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
478 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
479 unsigned int lhs,
const __FlashStringHelper* lhsString,
const char* opName,
480 bool (*op)(
unsigned int lhs,
unsigned int rhs),
481 unsigned int rhs,
const __FlashStringHelper* rhsString) {
482 if (
isDone())
return false;
483 bool ok = op(lhs, rhs);
485 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
492 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
long lhs,
493 const __FlashStringHelper* lhsString,
const char* opName,
494 bool (*op)(
long lhs,
long rhs),
long rhs,
495 const __FlashStringHelper* rhsString) {
496 if (
isDone())
return false;
497 bool ok = op(lhs, rhs);
499 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
506 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
507 unsigned long lhs,
const __FlashStringHelper* lhsString,
const char* opName,
508 bool (*op)(
unsigned long lhs,
unsigned long rhs),
509 unsigned long rhs,
const __FlashStringHelper* rhsString) {
510 if (
isDone())
return false;
511 bool ok = op(lhs, rhs);
513 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
520 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
double lhs,
521 const __FlashStringHelper* lhsString,
const char* opName,
522 bool (*op)(
double lhs,
double rhs),
double rhs,
523 const __FlashStringHelper* rhsString) {
524 if (
isDone())
return false;
525 bool ok = op(lhs, rhs);
527 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
534 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
535 const char* lhs,
const __FlashStringHelper* lhsString,
const char* opName,
536 bool (*op)(
const char* lhs,
const char* rhs),
537 const char* rhs,
const __FlashStringHelper* rhsString) {
538 if (
isDone())
return false;
539 bool ok = op(lhs, rhs);
541 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
548 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
549 const char* lhs,
const __FlashStringHelper* lhsString,
550 const char *opName,
bool (*op)(
const char* lhs,
const String& rhs),
551 const String& rhs,
const __FlashStringHelper* rhsString) {
552 if (
isDone())
return false;
553 bool ok = op(lhs, rhs);
555 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
562 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
563 const char* lhs,
const __FlashStringHelper* lhsString,
const char *opName,
564 bool (*op)(
const char* lhs,
const __FlashStringHelper* rhs),
565 const __FlashStringHelper* rhs,
const __FlashStringHelper* rhsString) {
566 if (
isDone())
return false;
567 bool ok = op(lhs, rhs);
569 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
576 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
577 const String& lhs,
const __FlashStringHelper* lhsString,
const char *opName,
578 bool (*op)(
const String& lhs,
const char* rhs),
579 const char* rhs,
const __FlashStringHelper* rhsString) {
580 if (
isDone())
return false;
581 bool ok = op(lhs, rhs);
583 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
590 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
591 const String& lhs,
const __FlashStringHelper* lhsString,
const char *opName,
592 bool (*op)(
const String& lhs,
const String& rhs),
593 const String& rhs,
const __FlashStringHelper* rhsString) {
594 if (
isDone())
return false;
595 bool ok = op(lhs, rhs);
597 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
604 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
605 const String& lhs,
const __FlashStringHelper* lhsString,
const char *opName,
606 bool (*op)(
const String& lhs,
const __FlashStringHelper* rhs),
607 const __FlashStringHelper* rhs,
const __FlashStringHelper* rhsString) {
608 if (
isDone())
return false;
609 bool ok = op(lhs, rhs);
611 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
618 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
619 const __FlashStringHelper* lhs,
const __FlashStringHelper* lhsString,
621 bool (*op)(
const __FlashStringHelper* lhs,
const char* rhs),
622 const char* rhs,
const __FlashStringHelper* rhsString) {
623 if (
isDone())
return false;
624 bool ok = op(lhs, rhs);
626 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
633 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
634 const __FlashStringHelper* lhs,
const __FlashStringHelper* lhsString,
636 bool (*op)(
const __FlashStringHelper* lhs,
const String& rhs),
637 const String& rhs,
const __FlashStringHelper* rhsString) {
638 if (
isDone())
return false;
639 bool ok = op(lhs, rhs);
641 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
648 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
649 const __FlashStringHelper* lhs,
const __FlashStringHelper* lhsString,
651 bool (*op)(
const __FlashStringHelper* lhs,
const __FlashStringHelper* rhs),
652 const __FlashStringHelper* rhs,
const __FlashStringHelper* rhsString) {
653 if (
isDone())
return false;
654 bool ok = op(lhs, rhs);
656 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
void setPassOrFail(bool ok)
Set the status to Passed or Failed depending on ok.
bool isOutputEnabled(bool ok)
Returns true if an assertion message should be printed.
bool isVerbosity(uint8_t verbosity)
Determine if any of the given verbosity is enabled.
static const uint8_t kAssertionPassed
Print assertXxx() passed message.
bool isDone()
Return true if test has been asserted.
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...