AUnit  1.2.1
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test.
Assertion.cpp
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #include <Arduino.h> // definition of Print
26 #include "Flash.h"
27 #include "Printer.h"
28 #include "Assertion.h"
29 #include "print64.h"
30 
31 namespace aunit {
32 
33 using namespace internal;
34 
35 namespace internal {
36 
37 // This can be a template function because it is accessed only through the
38 // various assertXxx() methods. Those assertXxx() methods are explicitly
39 // overloaded for the various types that we want to support.
40 //
41 // Prints something like the following:
42 // Assertion failed: (5) == (6), file Test.ino, line 820.
43 // Assertion passed: (6) == (6), file Test.ino, line 820.
44 template <typename A, typename B>
45 void printAssertionMessage(Print* printer, bool ok, const char* file,
46  uint16_t line, const A& lhs, const char* opName, const B& rhs) {
47 
48  // Don't use F() strings here because flash memory strings are not deduped by
49  // the compiler, so each template instantiation of this method causes a
50  // duplication of all the strings below. See
51  // https://github.com/mmurdoch/arduinounit/issues/70
52  // for more info. Normal (const char*) strings will be deduped by the
53  // compiler/linker.
54  printer->print("Assertion ");
55  printer->print(ok ? "passed" : "failed");
56  printer->print(": (");
57  printer->print(lhs);
58  printer->print(") ");
59  printer->print(opName);
60  printer->print(" (");
61  printer->print(rhs);
62  printer->print(')');
63  // reuse string in MataAssertion::printAssertionTestStatusMessage()
64  printer->print(", file ");
65  printer->print(file);
66  printer->print(", line ");
67  printer->print(line);
68  printer->println('.');
69 }
70 
71 // Special version of (bool, bool) because Arduino Print.h converts
72 // bool into int, which prints out "(1) == (0)", which isn't as useful.
73 // This prints "(true) == (false)".
74 void printAssertionMessage(Print* printer, bool ok, const char* file,
75  uint16_t line, bool lhs, const char* opName, bool rhs) {
76 
77  // Don't use F() strings here. Same reason as above.
78  printer->print("Assertion ");
79  printer->print(ok ? "passed" : "failed");
80  printer->print(": (");
81  printer->print(lhs ? "true" : "false");
82  printer->print(") ");
83  printer->print(opName);
84  printer->print(" (");
85  printer->print(rhs ? "true" : "false");
86  printer->print(')');
87  printer->print(", file ");
88  printer->print(file);
89  printer->print(", line ");
90  printer->print(line);
91  printer->println('.');
92 }
93 
94 // Version for (long long, long long) because Print.h does not support int64.
95 void printAssertionMessage(Print* printer, bool ok, const char* file,
96  uint16_t line, long long& lhs, const char* opName, long long& rhs) {
97 
98  // Don't use F() strings here. Same reason as above.
99  printer->print("Assertion ");
100  printer->print(ok ? "passed" : "failed");
101  printer->print(": (");
102  print64(*printer, lhs);
103  printer->print(") ");
104  printer->print(opName);
105  printer->print(" (");
106  print64(*printer, rhs);
107  printer->print(')');
108  printer->print(", file ");
109  printer->print(file);
110  printer->print(", line ");
111  printer->print(line);
112  printer->println('.');
113 }
114 
115 // Version for (unsigned long long, unsigned long long) because Print.h does
116 // not support int64.
117 void printAssertionMessage(Print* printer, bool ok, const char* file,
118  uint16_t line, unsigned long long& lhs, const char* opName,
119  unsigned long long& rhs) {
120 
121  // Don't use F() strings here. Same reason as above.
122  printer->print("Assertion ");
123  printer->print(ok ? "passed" : "failed");
124  printer->print(": (");
125  print64(*printer, lhs);
126  printer->print(") ");
127  printer->print(opName);
128  printer->print(" (");
129  print64(*printer, rhs);
130  printer->print(')');
131  printer->print(", file ");
132  printer->print(file);
133  printer->print(", line ");
134  printer->print(line);
135  printer->println('.');
136 }
137 
138 // Special version for assertTrue(arg) and assertFalse(arg).
139 // Prints:
140 // "Assertion passed/failed: (arg) is true"
141 // "Assertion passed/failed: (arg) is false"
142 void printAssertionBoolMessage(Print* printer, bool ok, const char* file,
143  uint16_t line, bool arg, bool value) {
144 
145  // Don't use F() strings here. Same reason as above.
146  printer->print("Assertion ");
147  printer->print(ok ? "passed" : "failed");
148  printer->print(": (");
149  printer->print(arg ? "true" : "false");
150  printer->print(") is ");
151  printer->print(value ? "true" : "false");
152  printer->print(", file ");
153  printer->print(file);
154  printer->print(", line ");
155  printer->print(line);
156  printer->println('.');
157 }
158 
159 template <typename A>
160 void printAssertionNearMessage(Print* printer, bool ok, const char* file,
161  uint16_t line, const A& lhs, const A& rhs, const char* opName,
162  const A& error) {
163  printer->print("Assertion ");
164  printer->print(ok ? "passed" : "failed");
165  printer->print(": |(");
166  printer->print(lhs);
167  printer->print(") - (");
168  printer->print(rhs);
169  printer->print(")| ");
170  printer->print(opName);
171  printer->print(" (");
172  printer->print(error);
173  printer->print(')');
174  printer->print(", file ");
175  printer->print(file);
176  printer->print(", line ");
177  printer->print(line);
178  printer->println('.');
179 }
180 
181 } // namespace
182 
184  return (ok && isVerbosity(Verbosity::kAssertionPassed)) ||
185  (!ok && isVerbosity(Verbosity::kAssertionFailed));
186 }
187 
188 bool Assertion::assertionBool(const char* file, uint16_t line, bool arg,
189  bool value) {
190  if (isDone()) return false;
191  bool ok = (arg == value);
192  if (isOutputEnabled(ok)) {
193  printAssertionBoolMessage(Printer::getPrinter(), ok, file, line,
194  arg, value);
195  }
196  setPassOrFail(ok);
197  return ok;
198 }
199 
200 bool Assertion::assertion(const char* file, uint16_t line, bool lhs,
201  const char* opName, bool (*op)(bool lhs, bool rhs),
202  bool rhs) {
203  if (isDone()) return false;
204  bool ok = op(lhs, rhs);
205  if (isOutputEnabled(ok)) {
206  printAssertionMessage(Printer::getPrinter(), ok, file, line,
207  lhs, opName, rhs);
208  }
209  setPassOrFail(ok);
210  return ok;
211 }
212 
213 bool Assertion::assertion(const char* file, uint16_t line, char lhs,
214  const char* opName, bool (*op)(char lhs, char rhs),
215  char rhs) {
216  if (isDone()) return false;
217  bool ok = op(lhs, rhs);
218  if (isOutputEnabled(ok)) {
219  printAssertionMessage(Printer::getPrinter(), ok, file, line,
220  lhs, opName, rhs);
221  }
222  setPassOrFail(ok);
223  return ok;
224 }
225 
226 bool Assertion::assertion(const char* file, uint16_t line, int lhs,
227  const char* opName, bool (*op)(int lhs, int rhs),
228  int rhs) {
229  if (isDone()) return false;
230  bool ok = op(lhs, rhs);
231  if (isOutputEnabled(ok)) {
232  printAssertionMessage(Printer::getPrinter(), ok, file, line,
233  lhs, opName, rhs);
234  }
235  setPassOrFail(ok);
236  return ok;
237 }
238 
239 bool Assertion::assertion(const char* file, uint16_t line, unsigned int lhs,
240  const char* opName, bool (*op)(unsigned int lhs, unsigned int rhs),
241  unsigned int rhs) {
242  if (isDone()) return false;
243  bool ok = op(lhs, rhs);
244  if (isOutputEnabled(ok)) {
245  printAssertionMessage(Printer::getPrinter(), ok, file, line,
246  lhs, opName, rhs);
247  }
248  setPassOrFail(ok);
249  return ok;
250 }
251 
252 bool Assertion::assertion(const char* file, uint16_t line, long lhs,
253  const char* opName, bool (*op)(long lhs, long rhs),
254  long rhs) {
255  if (isDone()) return false;
256  bool ok = op(lhs, rhs);
257  if (isOutputEnabled(ok)) {
258  printAssertionMessage(Printer::getPrinter(), ok, file, line,
259  lhs, opName, rhs);
260  }
261  setPassOrFail(ok);
262  return ok;
263 }
264 
265 bool Assertion::assertion(const char* file, uint16_t line, unsigned long lhs,
266  const char* opName, bool (*op)(unsigned long lhs, unsigned long rhs),
267  unsigned long rhs) {
268  if (isDone()) return false;
269  bool ok = op(lhs, rhs);
270  if (isOutputEnabled(ok)) {
271  printAssertionMessage(Printer::getPrinter(), ok, file, line,
272  lhs, opName, rhs);
273  }
274  setPassOrFail(ok);
275  return ok;
276 }
277 
278 bool Assertion::assertion(const char* file, uint16_t line, long long lhs,
279  const char* opName, bool (*op)(long long lhs, long long rhs),
280  long long rhs) {
281  if (isDone()) return false;
282  bool ok = op(lhs, rhs);
283  if (isOutputEnabled(ok)) {
284  printAssertionMessage(Printer::getPrinter(), ok, file, line,
285  lhs, opName, rhs);
286  }
287  setPassOrFail(ok);
288  return ok;
289 }
290 
291 bool Assertion::assertion(const char* file, uint16_t line,
292  unsigned long long lhs, const char* opName,
293  bool (*op)(unsigned long long lhs, unsigned long long rhs),
294  unsigned long long rhs) {
295  if (isDone()) return false;
296  bool ok = op(lhs, rhs);
297  if (isOutputEnabled(ok)) {
298  printAssertionMessage(Printer::getPrinter(), ok, file, line,
299  lhs, opName, rhs);
300  }
301  setPassOrFail(ok);
302  return ok;
303 }
304 
305 bool Assertion::assertion(const char* file, uint16_t line, double lhs,
306  const char* opName, bool (*op)(double lhs, double rhs),
307  double rhs) {
308  if (isDone()) return false;
309  bool ok = op(lhs, rhs);
310  if (isOutputEnabled(ok)) {
311  printAssertionMessage(Printer::getPrinter(), ok, file, line,
312  lhs, opName, rhs);
313  }
314  setPassOrFail(ok);
315  return ok;
316 }
317 
318 bool Assertion::assertion(const char* file, uint16_t line, const char* lhs,
319  const char* opName, bool (*op)(const char* lhs, const char* rhs),
320  const char* rhs) {
321  if (isDone()) return false;
322  bool ok = op(lhs, rhs);
323  if (isOutputEnabled(ok)) {
324  printAssertionMessage(Printer::getPrinter(), ok, file, line,
325  lhs, opName, rhs);
326  }
327  setPassOrFail(ok);
328  return ok;
329 }
330 
331 bool Assertion::assertion(const char* file, uint16_t line, const char* lhs,
332  const char* opName, bool (*op)(const char* lhs, const String& rhs),
333  const String& rhs) {
334  if (isDone()) return false;
335  bool ok = op(lhs, rhs);
336  if (isOutputEnabled(ok)) {
337  printAssertionMessage(Printer::getPrinter(), ok, file, line,
338  lhs, opName, rhs);
339  }
340  setPassOrFail(ok);
341  return ok;
342 }
343 
344 bool Assertion::assertion(const char* file, uint16_t line, const char* lhs,
345  const char* opName,
346  bool (*op)(const char* lhs, const __FlashStringHelper* rhs),
347  const __FlashStringHelper* rhs) {
348  if (isDone()) return false;
349  bool ok = op(lhs, rhs);
350  if (isOutputEnabled(ok)) {
351  printAssertionMessage(Printer::getPrinter(), ok, file, line,
352  lhs, opName, rhs);
353  }
354  setPassOrFail(ok);
355  return ok;
356 }
357 
358 bool Assertion::assertion(const char* file, uint16_t line, const String& lhs,
359  const char* opName, bool (*op)(const String& lhs, const char* rhs),
360  const char* rhs) {
361  if (isDone()) return false;
362  bool ok = op(lhs, rhs);
363  if (isOutputEnabled(ok)) {
364  printAssertionMessage(Printer::getPrinter(), ok, file, line,
365  lhs, opName, rhs);
366  }
367  setPassOrFail(ok);
368  return ok;
369 }
370 
371 bool Assertion::assertion(const char* file, uint16_t line, const String& lhs,
372  const char* opName, bool (*op)(const String& lhs, const String& rhs),
373  const String& rhs) {
374  if (isDone()) return false;
375  bool ok = op(lhs, rhs);
376  if (isOutputEnabled(ok)) {
377  printAssertionMessage(Printer::getPrinter(), ok, file, line,
378  lhs, opName, rhs);
379  }
380  setPassOrFail(ok);
381  return ok;
382 }
383 
384 bool Assertion::assertion(const char* file, uint16_t line, const String& lhs,
385  const char* opName,
386  bool (*op)(const String& lhs, const __FlashStringHelper* rhs),
387  const __FlashStringHelper* rhs) {
388  if (isDone()) return false;
389  bool ok = op(lhs, rhs);
390  if (isOutputEnabled(ok)) {
391  printAssertionMessage(Printer::getPrinter(), ok, file, line,
392  lhs, opName, rhs);
393  }
394  setPassOrFail(ok);
395  return ok;
396 }
397 
398 bool Assertion::assertion(const char* file, uint16_t line,
399  const __FlashStringHelper* lhs, const char* opName,
400  bool (*op)(const __FlashStringHelper* lhs, const char* rhs),
401  const char* rhs) {
402  if (isDone()) return false;
403  bool ok = op(lhs, rhs);
404  if (isOutputEnabled(ok)) {
405  printAssertionMessage(Printer::getPrinter(), ok, file, line,
406  lhs, opName, rhs);
407  }
408  setPassOrFail(ok);
409  return ok;
410 }
411 
412 bool Assertion::assertion(const char* file, uint16_t line,
413  const __FlashStringHelper* lhs, const char* opName,
414  bool (*op)(const __FlashStringHelper* lhs, const String& rhs),
415  const String& rhs) {
416  if (isDone()) return false;
417  bool ok = op(lhs, rhs);
418  if (isOutputEnabled(ok)) {
419  printAssertionMessage(Printer::getPrinter(), ok, file, line,
420  lhs, opName, rhs);
421  }
422  setPassOrFail(ok);
423  return ok;
424 }
425 
426 bool Assertion::assertion(const char* file, uint16_t line,
427  const __FlashStringHelper* lhs, const char* opName,
428  bool (*op)(const __FlashStringHelper* lhs, const __FlashStringHelper* rhs),
429  const __FlashStringHelper* rhs) {
430  if (isDone()) return false;
431  bool ok = op(lhs, rhs);
432  if (isOutputEnabled(ok)) {
433  printAssertionMessage(Printer::getPrinter(), ok, file, line,
434  lhs, opName, rhs);
435  }
436  setPassOrFail(ok);
437  return ok;
438 }
439 
440 bool Assertion::assertionNear(const char* file, uint16_t line,
441  int lhs, int rhs, int error, const char* opName,
442  bool (*opNear)(int lhs, int rhs, int error)) {
443  if (isDone()) return false;
444  bool ok = opNear(lhs, rhs, error);
445  if (isOutputEnabled(ok)) {
446  printAssertionNearMessage(Printer::getPrinter(), ok, file, line,
447  lhs, rhs, opName, error);
448  }
449  setPassOrFail(ok);
450  return ok;
451 }
452 
453 bool Assertion::assertionNear(const char* file, uint16_t line,
454  unsigned int lhs, unsigned int rhs, unsigned int error, const char* opName,
455  bool (*opNear)(unsigned int lhs, unsigned int rhs, unsigned int error)) {
456  if (isDone()) return false;
457  bool ok = opNear(lhs, rhs, error);
458  if (isOutputEnabled(ok)) {
459  printAssertionNearMessage(Printer::getPrinter(), ok, file, line,
460  lhs, rhs, opName, error);
461  }
462  setPassOrFail(ok);
463  return ok;
464 }
465 
466 bool Assertion::assertionNear(const char* file, uint16_t line,
467  long lhs, long rhs, long error, const char* opName,
468  bool (*opNear)(long lhs, long rhs, long error)) {
469  if (isDone()) return false;
470  bool ok = opNear(lhs, rhs, error);
471  if (isOutputEnabled(ok)) {
472  printAssertionNearMessage(Printer::getPrinter(), ok, file, line,
473  lhs, rhs, opName, error);
474  }
475  setPassOrFail(ok);
476  return ok;
477 }
478 
479 bool Assertion::assertionNear(const char* file, uint16_t line,
480  unsigned long lhs, unsigned long rhs, unsigned long error,
481  const char* opName,
482  bool (*opNear)(unsigned long lhs, unsigned long rhs, unsigned long error)) {
483  if (isDone()) return false;
484  bool ok = opNear(lhs, rhs, error);
485  if (isOutputEnabled(ok)) {
486  printAssertionNearMessage(Printer::getPrinter(), ok, file, line,
487  lhs, rhs, opName, error);
488  }
489  setPassOrFail(ok);
490  return ok;
491 }
492 
493 bool Assertion::assertionNear(const char* file, uint16_t line,
494  double lhs, double rhs, double error, const char* opName,
495  bool (*opNear)(double lhs, double rhs, double error)) {
496  if (isDone()) return false;
497  bool ok = opNear(lhs, rhs, error);
498  if (isOutputEnabled(ok)) {
499  printAssertionNearMessage(Printer::getPrinter(), ok, file, line,
500  lhs, rhs, opName, error);
501  }
502  setPassOrFail(ok);
503  return ok;
504 }
505 
506 //---------------------------------------------------------------------------
507 
508 namespace internal {
509 
510 // Verbose versions of above which accept the string arguments of the
511 // assertXxx() macros, so that the error messages are more verbose.
512 //
513 // Prints something like the following:
514 // Assertion failed: (x=5) == (y=6), file Test.ino, line 820.
515 // Assertion passed: (x=6) == (y=6), file Test.ino, line 820.
516 template <typename A, typename B>
517 void printAssertionMessageVerbose(Print* printer, bool ok, const char* file,
518  uint16_t line, const A& lhs, const __FlashStringHelper* lhsString,
519  const char* opName, const B& rhs, const __FlashStringHelper* rhsString) {
520 
521  // Don't use F() strings here because flash memory strings are not deduped by
522  // the compiler, so each template instantiation of this method causes a
523  // duplication of all the strings below. See
524  // https://github.com/mmurdoch/arduinounit/issues/70
525  // for more info.
526  printer->print("Assertion ");
527  printer->print(ok ? "passed" : "failed");
528  printer->print(": (");
529  printer->print(lhsString);
530  printer->print('=');
531  printer->print(lhs);
532  printer->print(") ");
533  printer->print(opName);
534  printer->print(" (");
535  printer->print(rhsString);
536  printer->print('=');
537  printer->print(rhs);
538  printer->print(')');
539  // reuse string in MataAssertion::printAssertionTestStatusMessage()
540  printer->print(", file ");
541  printer->print(file);
542  printer->print(", line ");
543  printer->print(line);
544  printer->println('.');
545 }
546 
547 // Special version of (bool, bool) because Arduino Print.h converts
548 // bool into int, which prints out "(1) == (0)", which isn't as useful.
549 // This prints "(x=true) == (y=false)".
550 void printAssertionMessageVerbose(Print* printer, bool ok, const char* file,
551  uint16_t line, bool lhs, const __FlashStringHelper* lhsString,
552  const char* opName, bool rhs, const __FlashStringHelper* rhsString) {
553 
554  // Don't use F() strings here. Same reason as above.
555  printer->print("Assertion ");
556  printer->print(ok ? "passed" : "failed");
557  printer->print(": (");
558  printer->print(lhsString);
559  printer->print('=');
560  printer->print(lhs ? "true" : "false");
561  printer->print(") ");
562  printer->print(opName);
563  printer->print(" (");
564  printer->print(rhsString);
565  printer->print('=');
566  printer->print(rhs ? "true" : "false");
567  printer->print(')');
568  printer->print(", file ");
569  printer->print(file);
570  printer->print(", line ");
571  printer->print(line);
572  printer->println('.');
573 }
574 
575 // Version for (long long, long long) because Print.h does not support int64.
576 void printAssertionMessageVerbose(Print* printer, bool ok, const char* file,
577  uint16_t line, long long& lhs, const __FlashStringHelper* lhsString,
578  const char* opName, long long& rhs, const __FlashStringHelper* rhsString) {
579 
580  // Don't use F() strings here. Same reason as above.
581  printer->print("Assertion ");
582  printer->print(ok ? "passed" : "failed");
583  printer->print(": (");
584  printer->print(lhsString);
585  printer->print('=');
586  print64(*printer, lhs);
587  printer->print(") ");
588  printer->print(opName);
589  printer->print(" (");
590  printer->print(rhsString);
591  printer->print('=');
592  print64(*printer, rhs);
593  printer->print(')');
594  printer->print(", file ");
595  printer->print(file);
596  printer->print(", line ");
597  printer->print(line);
598  printer->println('.');
599 }
600 
601 // Version for (unsigned long long, unsigned long long) because Print.h does
602 // not support int64.
603 void printAssertionMessageVerbose(Print* printer, bool ok, const char* file,
604  uint16_t line, unsigned long long& lhs,
605  const __FlashStringHelper* lhsString, const char* opName,
606  unsigned long long& rhs, const __FlashStringHelper* rhsString) {
607 
608  // Don't use F() strings here. Same reason as above.
609  printer->print("Assertion ");
610  printer->print(ok ? "passed" : "failed");
611  printer->print(": (");
612  printer->print(lhsString);
613  printer->print('=');
614  print64(*printer, lhs);
615  printer->print(") ");
616  printer->print(opName);
617  printer->print(" (");
618  printer->print(rhsString);
619  printer->print('=');
620  print64(*printer, rhs);
621  printer->print(')');
622  printer->print(", file ");
623  printer->print(file);
624  printer->print(", line ");
625  printer->print(line);
626  printer->println('.');
627 }
628 
629 // Special version for assertTrue(arg) and assertFalse(arg).
630 // Prints:
631 // "Assertion passed/failed: (x=arg) is true"
632 // "Assertion passed/failed: (x=arg) is false"
633 void printAssertionBoolMessageVerbose(Print* printer, bool ok, const char* file,
634  uint16_t line, bool arg, const __FlashStringHelper* argString, bool value) {
635 
636  // Don't use F() strings here. Same reason as above.
637  printer->print("Assertion ");
638  printer->print(ok ? "passed" : "failed");
639  printer->print(": (");
640  printer->print(argString);
641  printer->print('=');
642  printer->print(arg ? "true" : "false");
643  printer->print(") is ");
644  printer->print(value ? "true" : "false");
645  printer->print(", file ");
646  printer->print(file);
647  printer->print(", line ");
648  printer->print(line);
649  printer->println('.');
650 }
651 
652 template <typename A>
653 void printAssertionNearMessageVerbose(Print* printer, bool ok, const char* file,
654  uint16_t line, const A& lhs, const __FlashStringHelper* lhsString,
655  const A& rhs, const __FlashStringHelper* rhsString,
656  const char* opName,
657  const A& error, const __FlashStringHelper* errorString) {
658  printer->print("Assertion ");
659  printer->print(ok ? "passed" : "failed");
660  printer->print(": |(");
661  printer->print(lhsString);
662  printer->print('=');
663  printer->print(lhs);
664  printer->print(") - (");
665  printer->print(rhsString);
666  printer->print('=');
667  printer->print(rhs);
668  printer->print(")| ");
669  printer->print(opName);
670  printer->print(" (");
671  printer->print(errorString);
672  printer->print('=');
673  printer->print(error);
674  printer->print(')');
675  printer->print(", file ");
676  printer->print(file);
677  printer->print(", line ");
678  printer->print(line);
679  printer->println('.');
680 }
681 
682 } // namespace
683 
684 bool Assertion::assertionBoolVerbose(const char* file, uint16_t line, bool arg,
685  const __FlashStringHelper* argString, bool value) {
686  if (isDone()) return false;
687  bool ok = (arg == value);
688  if (isOutputEnabled(ok)) {
689  printAssertionBoolMessageVerbose(Printer::getPrinter(), ok, file, line,
690  arg, argString, value);
691  }
692  setPassOrFail(ok);
693  return ok;
694 }
695 
696 bool Assertion::assertionVerbose(const char* file, uint16_t line, bool lhs,
697  const __FlashStringHelper* lhsString, const char* opName,
698  bool (*op)(bool lhs, bool rhs), bool rhs,
699  const __FlashStringHelper* rhsString) {
700  if (isDone()) return false;
701  bool ok = op(lhs, rhs);
702  if (isOutputEnabled(ok)) {
703  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
704  lhs, lhsString, opName, rhs, rhsString);
705  }
706  setPassOrFail(ok);
707  return ok;
708 }
709 
710 bool Assertion::assertionVerbose(const char* file, uint16_t line, char lhs,
711  const __FlashStringHelper* lhsString, const char* opName,
712  bool (*op)(char lhs, char rhs), char rhs,
713  const __FlashStringHelper* rhsString) {
714  if (isDone()) return false;
715  bool ok = op(lhs, rhs);
716  if (isOutputEnabled(ok)) {
717  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
718  lhs, lhsString, opName, rhs, rhsString);
719  }
720  setPassOrFail(ok);
721  return ok;
722 }
723 
724 bool Assertion::assertionVerbose(const char* file, uint16_t line, int lhs,
725  const __FlashStringHelper* lhsString, const char* opName,
726  bool (*op)(int lhs, int rhs), int rhs,
727  const __FlashStringHelper* rhsString) {
728  if (isDone()) return false;
729  bool ok = op(lhs, rhs);
730  if (isOutputEnabled(ok)) {
731  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
732  lhs, lhsString, opName, rhs, rhsString);
733  }
734  setPassOrFail(ok);
735  return ok;
736 }
737 
738 bool Assertion::assertionVerbose(const char* file, uint16_t line,
739  unsigned int lhs, const __FlashStringHelper* lhsString, const char* opName,
740  bool (*op)(unsigned int lhs, unsigned int rhs),
741  unsigned int rhs, const __FlashStringHelper* rhsString) {
742  if (isDone()) return false;
743  bool ok = op(lhs, rhs);
744  if (isOutputEnabled(ok)) {
745  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
746  lhs, lhsString, opName, rhs, rhsString);
747  }
748  setPassOrFail(ok);
749  return ok;
750 }
751 
752 bool Assertion::assertionVerbose(const char* file, uint16_t line, long lhs,
753  const __FlashStringHelper* lhsString, const char* opName,
754  bool (*op)(long lhs, long rhs), long rhs,
755  const __FlashStringHelper* rhsString) {
756  if (isDone()) return false;
757  bool ok = op(lhs, rhs);
758  if (isOutputEnabled(ok)) {
759  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
760  lhs, lhsString, opName, rhs, rhsString);
761  }
762  setPassOrFail(ok);
763  return ok;
764 }
765 
766 bool Assertion::assertionVerbose(const char* file, uint16_t line,
767  unsigned long lhs, const __FlashStringHelper* lhsString, const char* opName,
768  bool (*op)(unsigned long lhs, unsigned long rhs),
769  unsigned long rhs, const __FlashStringHelper* rhsString) {
770  if (isDone()) return false;
771  bool ok = op(lhs, rhs);
772  if (isOutputEnabled(ok)) {
773  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
774  lhs, lhsString, opName, rhs, rhsString);
775  }
776  setPassOrFail(ok);
777  return ok;
778 }
779 
780 bool Assertion::assertionVerbose(const char* file, uint16_t line, long long lhs,
781  const __FlashStringHelper* lhsString, const char* opName,
782  bool (*op)(long long lhs, long long rhs), long long rhs,
783  const __FlashStringHelper* rhsString) {
784  if (isDone()) return false;
785  bool ok = op(lhs, rhs);
786  if (isOutputEnabled(ok)) {
787  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
788  lhs, lhsString, opName, rhs, rhsString);
789  }
790  setPassOrFail(ok);
791  return ok;
792 }
793 
794 bool Assertion::assertionVerbose(const char* file, uint16_t line,
795  unsigned long long lhs, const __FlashStringHelper* lhsString,
796  const char* opName,
797  bool (*op)(unsigned long long lhs, unsigned long long rhs),
798  unsigned long long rhs, const __FlashStringHelper* rhsString) {
799  if (isDone()) return false;
800  bool ok = op(lhs, rhs);
801  if (isOutputEnabled(ok)) {
802  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
803  lhs, lhsString, opName, rhs, rhsString);
804  }
805  setPassOrFail(ok);
806  return ok;
807 }
808 
809 bool Assertion::assertionVerbose(const char* file, uint16_t line, double lhs,
810  const __FlashStringHelper* lhsString, const char* opName,
811  bool (*op)(double lhs, double rhs), double rhs,
812  const __FlashStringHelper* rhsString) {
813  if (isDone()) return false;
814  bool ok = op(lhs, rhs);
815  if (isOutputEnabled(ok)) {
816  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
817  lhs, lhsString, opName, rhs, rhsString);
818  }
819  setPassOrFail(ok);
820  return ok;
821 }
822 
823 bool Assertion::assertionVerbose(const char* file, uint16_t line,
824  const char* lhs, const __FlashStringHelper* lhsString, const char* opName,
825  bool (*op)(const char* lhs, const char* rhs),
826  const char* rhs, const __FlashStringHelper* rhsString) {
827  if (isDone()) return false;
828  bool ok = op(lhs, rhs);
829  if (isOutputEnabled(ok)) {
830  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
831  lhs, lhsString, opName, rhs, rhsString);
832  }
833  setPassOrFail(ok);
834  return ok;
835 }
836 
837 bool Assertion::assertionVerbose(const char* file, uint16_t line,
838  const char* lhs, const __FlashStringHelper* lhsString,
839  const char* opName, bool (*op)(const char* lhs, const String& rhs),
840  const String& rhs, const __FlashStringHelper* rhsString) {
841  if (isDone()) return false;
842  bool ok = op(lhs, rhs);
843  if (isOutputEnabled(ok)) {
844  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
845  lhs, lhsString, opName, rhs, rhsString);
846  }
847  setPassOrFail(ok);
848  return ok;
849 }
850 
851 bool Assertion::assertionVerbose(const char* file, uint16_t line,
852  const char* lhs, const __FlashStringHelper* lhsString, const char* opName,
853  bool (*op)(const char* lhs, const __FlashStringHelper* rhs),
854  const __FlashStringHelper* rhs, const __FlashStringHelper* rhsString) {
855  if (isDone()) return false;
856  bool ok = op(lhs, rhs);
857  if (isOutputEnabled(ok)) {
858  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
859  lhs, lhsString, opName, rhs, rhsString);
860  }
861  setPassOrFail(ok);
862  return ok;
863 }
864 
865 bool Assertion::assertionVerbose(const char* file, uint16_t line,
866  const String& lhs, const __FlashStringHelper* lhsString, const char* opName,
867  bool (*op)(const String& lhs, const char* rhs),
868  const char* rhs, const __FlashStringHelper* rhsString) {
869  if (isDone()) return false;
870  bool ok = op(lhs, rhs);
871  if (isOutputEnabled(ok)) {
872  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
873  lhs, lhsString, opName, rhs, rhsString);
874  }
875  setPassOrFail(ok);
876  return ok;
877 }
878 
879 bool Assertion::assertionVerbose(const char* file, uint16_t line,
880  const String& lhs, const __FlashStringHelper* lhsString, const char* opName,
881  bool (*op)(const String& lhs, const String& rhs),
882  const String& rhs, const __FlashStringHelper* rhsString) {
883  if (isDone()) return false;
884  bool ok = op(lhs, rhs);
885  if (isOutputEnabled(ok)) {
886  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
887  lhs, lhsString, opName, rhs, rhsString);
888  }
889  setPassOrFail(ok);
890  return ok;
891 }
892 
893 bool Assertion::assertionVerbose(const char* file, uint16_t line,
894  const String& lhs, const __FlashStringHelper* lhsString, const char* opName,
895  bool (*op)(const String& lhs, const __FlashStringHelper* rhs),
896  const __FlashStringHelper* rhs, const __FlashStringHelper* rhsString) {
897  if (isDone()) return false;
898  bool ok = op(lhs, rhs);
899  if (isOutputEnabled(ok)) {
900  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
901  lhs, lhsString, opName, rhs, rhsString);
902  }
903  setPassOrFail(ok);
904  return ok;
905 }
906 
907 bool Assertion::assertionVerbose(const char* file, uint16_t line,
908  const __FlashStringHelper* lhs, const __FlashStringHelper* lhsString,
909  const char* opName,
910  bool (*op)(const __FlashStringHelper* lhs, const char* rhs),
911  const char* rhs, const __FlashStringHelper* rhsString) {
912  if (isDone()) return false;
913  bool ok = op(lhs, rhs);
914  if (isOutputEnabled(ok)) {
915  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
916  lhs, lhsString, opName, rhs, rhsString);
917  }
918  setPassOrFail(ok);
919  return ok;
920 }
921 
922 bool Assertion::assertionVerbose(const char* file, uint16_t line,
923  const __FlashStringHelper* lhs, const __FlashStringHelper* lhsString,
924  const char* opName,
925  bool (*op)(const __FlashStringHelper* lhs, const String& rhs),
926  const String& rhs, const __FlashStringHelper* rhsString) {
927  if (isDone()) return false;
928  bool ok = op(lhs, rhs);
929  if (isOutputEnabled(ok)) {
930  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
931  lhs, lhsString, opName, rhs, rhsString);
932  }
933  setPassOrFail(ok);
934  return ok;
935 }
936 
937 bool Assertion::assertionVerbose(const char* file, uint16_t line,
938  const __FlashStringHelper* lhs, const __FlashStringHelper* lhsString,
939  const char* opName,
940  bool (*op)(const __FlashStringHelper* lhs, const __FlashStringHelper* rhs),
941  const __FlashStringHelper* rhs, const __FlashStringHelper* rhsString) {
942  if (isDone()) return false;
943  bool ok = op(lhs, rhs);
944  if (isOutputEnabled(ok)) {
945  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
946  lhs, lhsString, opName, rhs, rhsString);
947  }
948  setPassOrFail(ok);
949  return ok;
950 }
951 
952 bool Assertion::assertionNearVerbose(const char* file, uint16_t line,
953  int lhs, const __FlashStringHelper* lhsString,
954  int rhs, const __FlashStringHelper* rhsString,
955  int error, const __FlashStringHelper* errorString,
956  const char* opName,
957  bool (*opNear)(int lhs, int rhs, int error)) {
958  if (isDone()) return false;
959  bool ok = opNear(lhs, rhs, error);
960  if (isOutputEnabled(ok)) {
961  printAssertionNearMessageVerbose(Printer::getPrinter(), ok, file, line,
962  lhs, lhsString, rhs, rhsString, opName, error, errorString);
963  }
964  setPassOrFail(ok);
965  return ok;
966 }
967 
968 bool Assertion::assertionNearVerbose(const char* file, uint16_t line,
969  unsigned int lhs, const __FlashStringHelper* lhsString,
970  unsigned int rhs, const __FlashStringHelper* rhsString,
971  unsigned int error, const __FlashStringHelper* errorString,
972  const char* opName,
973  bool (*opNear)(unsigned int lhs, unsigned int rhs, unsigned int error)) {
974  if (isDone()) return false;
975  bool ok = opNear(lhs, rhs, error);
976  if (isOutputEnabled(ok)) {
977  printAssertionNearMessageVerbose(Printer::getPrinter(), ok, file, line,
978  lhs, lhsString, rhs, rhsString, opName, error, errorString);
979  }
980  setPassOrFail(ok);
981  return ok;
982 }
983 
984 bool Assertion::assertionNearVerbose(const char* file, uint16_t line,
985  long lhs, const __FlashStringHelper* lhsString,
986  long rhs, const __FlashStringHelper* rhsString,
987  long error, const __FlashStringHelper* errorString,
988  const char* opName,
989  bool (*opNear)(long lhs, long rhs, long error)) {
990  if (isDone()) return false;
991  bool ok = opNear(lhs, rhs, error);
992  if (isOutputEnabled(ok)) {
993  printAssertionNearMessageVerbose(Printer::getPrinter(), ok, file, line,
994  lhs, lhsString, rhs, rhsString, opName, error, errorString);
995  }
996  setPassOrFail(ok);
997  return ok;
998 }
999 
1000 bool Assertion::assertionNearVerbose(const char* file, uint16_t line,
1001  unsigned long lhs, const __FlashStringHelper* lhsString,
1002  unsigned long rhs, const __FlashStringHelper* rhsString,
1003  unsigned long error, const __FlashStringHelper* errorString,
1004  const char* opName,
1005  bool (*opNear)(unsigned long lhs, unsigned long rhs, unsigned long error)) {
1006  if (isDone()) return false;
1007  bool ok = opNear(lhs, rhs, error);
1008  if (isOutputEnabled(ok)) {
1009  printAssertionNearMessageVerbose(Printer::getPrinter(), ok, file, line,
1010  lhs, lhsString, rhs, rhsString, opName, error, errorString);
1011  }
1012  setPassOrFail(ok);
1013  return ok;
1014 }
1015 
1016 bool Assertion::assertionNearVerbose(const char* file, uint16_t line,
1017  double lhs, const __FlashStringHelper* lhsString,
1018  double rhs, const __FlashStringHelper* rhsString,
1019  double error, const __FlashStringHelper* errorString,
1020  const char* opName,
1021  bool (*opNear)(double lhs, double rhs, double error)) {
1022  if (isDone()) return false;
1023  bool ok = opNear(lhs, rhs, error);
1024  if (isOutputEnabled(ok)) {
1025  printAssertionNearMessageVerbose(Printer::getPrinter(), ok, file, line,
1026  lhs, lhsString, rhs, rhsString, opName, error, errorString);
1027  }
1028  setPassOrFail(ok);
1029  return ok;
1030 }
1031 
1032 }
Helper routines to print &#39;long long&#39; and &#39;unsigned long long&#39; because the Print::print() methods in P...
bool isOutputEnabled(bool ok)
Returns true if an assertion message should be printed.
Definition: Assertion.cpp:183
static const uint8_t kAssertionPassed
Print assertXxx() passed message.
Definition: Verbosity.h:40
static Print * getPrinter()
Get the output printer used by the various assertion() methods and the TestRunner.
Definition: Printer.h:48
static const uint8_t kAssertionFailed
Print assertXxx() failed message.
Definition: Verbosity.h:43
Various macros to smooth over the differences among the various platforms with regards to their suppo...