AUnit  1.5.4
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 <stdint.h>
26 #include <Arduino.h> // definition of Print
27 #include "Flash.h"
28 #include "Printer.h"
29 #include "Assertion.h"
30 
31 #if ! defined(ARDUINO_ARCH_STM32)
32 #include "print64.h"
33 #endif
34 
35 namespace aunit {
36 
37 using namespace internal;
38 
39 namespace internal {
40 
41 // This can be a template function because it is accessed only through the
42 // various assertXxx() methods. Those assertXxx() methods are explicitly
43 // overloaded for the various types that we want to support.
44 //
45 // Prints something like the following:
46 // Assertion failed: (5) == (6), file Test.ino, line 820.
47 // Assertion passed: (6) == (6), file Test.ino, line 820.
48 template <typename A, typename B>
49 void printAssertionMessage(
50  Print* printer,
51  bool ok,
52  const char* file,
53  uint16_t line,
54  const A& lhs,
55  const char* opName,
56  const B& rhs
57 ) {
58 
59  // Don't use F() strings here because flash memory strings are not deduped by
60  // the compiler, so each template instantiation of this method causes a
61  // duplication of all the strings below. See
62  // https://github.com/mmurdoch/arduinounit/issues/70
63  // for more info. Normal (const char*) strings will be deduped by the
64  // compiler/linker.
65  printer->print("Assertion ");
66  printer->print(ok ? "passed" : "failed");
67  printer->print(": (");
68  printer->print(lhs);
69  printer->print(") ");
70  printer->print(opName);
71  printer->print(" (");
72  printer->print(rhs);
73  printer->print(')');
74  // reuse string in MataAssertion::printAssertionTestStatusMessage()
75  printer->print(", file ");
76  printer->print(file);
77  printer->print(", line ");
78  printer->print(line);
79  printer->println('.');
80 }
81 
82 // Special version of (bool, bool) because Arduino Print.h converts
83 // bool into int, which prints out "(1) == (0)", which isn't as useful.
84 // This prints "(true) == (false)".
85 void printAssertionMessage(
86  Print* printer,
87  bool ok,
88  const char* file,
89  uint16_t line,
90  bool lhs,
91  const char* opName,
92  bool rhs
93 ) {
94 
95  // Don't use F() strings here. Same reason as above.
96  printer->print("Assertion ");
97  printer->print(ok ? "passed" : "failed");
98  printer->print(": (");
99  printer->print(lhs ? "true" : "false");
100  printer->print(") ");
101  printer->print(opName);
102  printer->print(" (");
103  printer->print(rhs ? "true" : "false");
104  printer->print(')');
105  printer->print(", file ");
106  printer->print(file);
107  printer->print(", line ");
108  printer->print(line);
109  printer->println('.');
110 }
111 
112 #if ! defined(ARDUINO_ARCH_STM32)
113 
114 // Version for (long long, long long) because Print.h does not support int64.
115 void printAssertionMessage(
116  Print* printer,
117  bool ok,
118  const char* file,
119  uint16_t line,
120  long long& lhs,
121  const char* opName,
122  long long& rhs
123 ) {
124 
125  // Don't use F() strings here. Same reason as above.
126  printer->print("Assertion ");
127  printer->print(ok ? "passed" : "failed");
128  printer->print(": (");
129  print64(*printer, lhs);
130  printer->print(") ");
131  printer->print(opName);
132  printer->print(" (");
133  print64(*printer, rhs);
134  printer->print(')');
135  printer->print(", file ");
136  printer->print(file);
137  printer->print(", line ");
138  printer->print(line);
139  printer->println('.');
140 }
141 
142 // Version for (unsigned long long, unsigned long long) because Print.h does
143 // not support int64.
144 void printAssertionMessage(
145  Print* printer,
146  bool ok,
147  const char* file,
148  uint16_t line,
149  unsigned long long& lhs,
150  const char* opName,
151  unsigned long long& rhs
152 ) {
153 
154  // Don't use F() strings here. Same reason as above.
155  printer->print("Assertion ");
156  printer->print(ok ? "passed" : "failed");
157  printer->print(": (");
158  print64(*printer, lhs);
159  printer->print(") ");
160  printer->print(opName);
161  printer->print(" (");
162  print64(*printer, rhs);
163  printer->print(')');
164  printer->print(", file ");
165  printer->print(file);
166  printer->print(", line ");
167  printer->print(line);
168  printer->println('.');
169 }
170 
171 #endif // ARDUINO_ARCH_STM32
172 
173 // Special version for (const void*, const void*).
174 void printAssertionMessage(
175  Print* printer,
176  bool ok,
177  const char* file,
178  uint16_t line,
179  const void* lhs,
180  const char* opName,
181  const void* rhs
182 ) {
183 
184  // Don't use F() strings here. Same reason as above.
185  // Technically, we should cast to (uintptr_t). But all Arduino
186  // microcontrollers are 32-bit, so we can cast to (unsigned long) to avoid
187  // calling print64().
188  printer->print("Assertion ");
189  printer->print(ok ? "passed" : "failed");
190  printer->print(": (0x");
191  printer->print((unsigned long) lhs, HEX);
192  printer->print(") ");
193  printer->print(opName);
194  printer->print(" (0x");
195  printer->print((unsigned long) rhs, HEX);
196  printer->print(')');
197  printer->print(", file ");
198  printer->print(file);
199  printer->print(", line ");
200  printer->print(line);
201  printer->println('.');
202 }
203 
204 // Special version for assertTrue(arg) and assertFalse(arg).
205 // Prints:
206 // "Assertion passed/failed: (arg) is true"
207 // "Assertion passed/failed: (arg) is false"
208 void printAssertionBoolMessage(
209  Print* printer,
210  bool ok,
211  const char* file,
212  uint16_t line,
213  bool arg,
214  bool value
215 ) {
216 
217  // Don't use F() strings here. Same reason as above.
218  printer->print("Assertion ");
219  printer->print(ok ? "passed" : "failed");
220  printer->print(": (");
221  printer->print(arg ? "true" : "false");
222  printer->print(") is ");
223  printer->print(value ? "true" : "false");
224  printer->print(", file ");
225  printer->print(file);
226  printer->print(", line ");
227  printer->print(line);
228  printer->println('.');
229 }
230 
231 template <typename A>
232 void printAssertionNearMessage(
233  Print* printer,
234  bool ok,
235  const char* file,
236  uint16_t line,
237  const A& lhs,
238  const A& rhs,
239  const char* opName,
240  const A& error
241 ) {
242  printer->print("Assertion ");
243  printer->print(ok ? "passed" : "failed");
244  printer->print(": |(");
245  printer->print(lhs);
246  printer->print(") - (");
247  printer->print(rhs);
248  printer->print(")| ");
249  printer->print(opName);
250  printer->print(" (");
251  printer->print(error);
252  printer->print(')');
253  printer->print(", file ");
254  printer->print(file);
255  printer->print(", line ");
256  printer->print(line);
257  printer->println('.');
258 }
259 
260 } // namespace
261 
262 bool Assertion::isOutputEnabled(bool ok) const {
263  return (ok && isVerbosity(Verbosity::kAssertionPassed)) ||
264  (!ok && isVerbosity(Verbosity::kAssertionFailed));
265 }
266 
268  const char* file,
269  uint16_t line,
270  bool arg,
271  bool value
272 ) {
273  if (isDone()) return false;
274  bool ok = (arg == value);
275  if (isOutputEnabled(ok)) {
276  printAssertionBoolMessage(Printer::getPrinter(), ok, file, line,
277  arg, value);
278  }
279  setPassOrFail(ok);
280  return ok;
281 }
282 
284  const char* file,
285  uint16_t line,
286  bool lhs,
287  const char* opName,
288  bool (*op)(bool lhs, bool rhs),
289  bool rhs
290 ) {
291  if (isDone()) return false;
292  bool ok = op(lhs, rhs);
293  if (isOutputEnabled(ok)) {
294  printAssertionMessage(Printer::getPrinter(), ok, file, line,
295  lhs, opName, rhs);
296  }
297  setPassOrFail(ok);
298  return ok;
299 }
300 
302  const char* file,
303  uint16_t line,
304  char lhs,
305  const char* opName,
306  bool (*op)(char lhs, char rhs),
307  char rhs
308 ) {
309  if (isDone()) return false;
310  bool ok = op(lhs, rhs);
311  if (isOutputEnabled(ok)) {
312  printAssertionMessage(Printer::getPrinter(), ok, file, line,
313  lhs, opName, rhs);
314  }
315  setPassOrFail(ok);
316  return ok;
317 }
318 
320  const char* file,
321  uint16_t line,
322  int lhs,
323  const char* opName,
324  bool (*op)(int lhs, int rhs),
325  int rhs
326 ) {
327  if (isDone()) return false;
328  bool ok = op(lhs, rhs);
329  if (isOutputEnabled(ok)) {
330  printAssertionMessage(Printer::getPrinter(), ok, file, line,
331  lhs, opName, rhs);
332  }
333  setPassOrFail(ok);
334  return ok;
335 }
336 
338  const char* file,
339  uint16_t line,
340  unsigned int lhs,
341  const char* opName,
342  bool (*op)(unsigned int lhs, unsigned int rhs),
343  unsigned int rhs
344 ) {
345  if (isDone()) return false;
346  bool ok = op(lhs, rhs);
347  if (isOutputEnabled(ok)) {
348  printAssertionMessage(Printer::getPrinter(), ok, file, line,
349  lhs, opName, rhs);
350  }
351  setPassOrFail(ok);
352  return ok;
353 }
354 
356  const char* file,
357  uint16_t line,
358  long lhs,
359  const char* opName,
360  bool (*op)(long lhs, long rhs),
361  long rhs
362 ) {
363  if (isDone()) return false;
364  bool ok = op(lhs, rhs);
365  if (isOutputEnabled(ok)) {
366  printAssertionMessage(Printer::getPrinter(), ok, file, line,
367  lhs, opName, rhs);
368  }
369  setPassOrFail(ok);
370  return ok;
371 }
372 
374  const char* file,
375  uint16_t line,
376  unsigned long lhs,
377  const char* opName,
378  bool (*op)(unsigned long lhs, unsigned long rhs),
379  unsigned long rhs
380 ) {
381  if (isDone()) return false;
382  bool ok = op(lhs, rhs);
383  if (isOutputEnabled(ok)) {
384  printAssertionMessage(Printer::getPrinter(), ok, file, line,
385  lhs, opName, rhs);
386  }
387  setPassOrFail(ok);
388  return ok;
389 }
390 
392  const char* file,
393  uint16_t line,
394  long long lhs,
395  const char* opName,
396  bool (*op)(long long lhs, long long rhs),
397  long long rhs
398 ) {
399  if (isDone()) return false;
400  bool ok = op(lhs, rhs);
401  if (isOutputEnabled(ok)) {
402  printAssertionMessage(Printer::getPrinter(), ok, file, line,
403  lhs, opName, rhs);
404  }
405  setPassOrFail(ok);
406  return ok;
407 }
408 
410  const char* file,
411  uint16_t line,
412  unsigned long long lhs,
413  const char* opName,
414  bool (*op)(unsigned long long lhs, unsigned long long rhs),
415  unsigned long long rhs
416 ) {
417  if (isDone()) return false;
418  bool ok = op(lhs, rhs);
419  if (isOutputEnabled(ok)) {
420  printAssertionMessage(Printer::getPrinter(), ok, file, line,
421  lhs, opName, rhs);
422  }
423  setPassOrFail(ok);
424  return ok;
425 }
426 
428  const char* file,
429  uint16_t line,
430  double lhs,
431  const char* opName,
432  bool (*op)(double lhs, double rhs),
433  double rhs
434 ) {
435  if (isDone()) return false;
436  bool ok = op(lhs, rhs);
437  if (isOutputEnabled(ok)) {
438  printAssertionMessage(Printer::getPrinter(), ok, file, line,
439  lhs, opName, rhs);
440  }
441  setPassOrFail(ok);
442  return ok;
443 }
444 
446  const char* file,
447  uint16_t line,
448  const void* lhs,
449  const char* opName,
450  bool (*op)(const void* lhs, const void* rhs),
451  const void* rhs
452 ) {
453  if (isDone()) return false;
454  bool ok = op(lhs, rhs);
455  if (isOutputEnabled(ok)) {
456  printAssertionMessage(Printer::getPrinter(), ok, file, line,
457  lhs, opName, rhs);
458  }
459  setPassOrFail(ok);
460  return ok;
461 }
462 
464  const char* file,
465  uint16_t line,
466  const char* lhs,
467  const char* opName,
468  bool (*op)(const char* lhs, const char* rhs),
469  const char* rhs
470 ) {
471  if (isDone()) return false;
472  bool ok = op(lhs, rhs);
473  if (isOutputEnabled(ok)) {
474  printAssertionMessage(Printer::getPrinter(), ok, file, line,
475  lhs, opName, rhs);
476  }
477  setPassOrFail(ok);
478  return ok;
479 }
480 
482  const char* file,
483  uint16_t line,
484  const char* lhs,
485  const char* opName,
486  bool (*op)(const char* lhs, const String& rhs),
487  const String& rhs
488 ) {
489  if (isDone()) return false;
490  bool ok = op(lhs, rhs);
491  if (isOutputEnabled(ok)) {
492  printAssertionMessage(Printer::getPrinter(), ok, file, line,
493  lhs, opName, rhs);
494  }
495  setPassOrFail(ok);
496  return ok;
497 }
498 
500  const char* file,
501  uint16_t line,
502  const char* lhs,
503  const char* opName,
504  bool (*op)(const char* lhs, const __FlashStringHelper* rhs),
505  const __FlashStringHelper* rhs
506 ) {
507  if (isDone()) return false;
508  bool ok = op(lhs, rhs);
509  if (isOutputEnabled(ok)) {
510  printAssertionMessage(Printer::getPrinter(), ok, file, line,
511  lhs, opName, rhs);
512  }
513  setPassOrFail(ok);
514  return ok;
515 }
516 
518  const char* file,
519  uint16_t line,
520  const String& lhs,
521  const char* opName,
522  bool (*op)(const String& lhs, const char* rhs),
523  const char* rhs
524 ) {
525  if (isDone()) return false;
526  bool ok = op(lhs, rhs);
527  if (isOutputEnabled(ok)) {
528  printAssertionMessage(Printer::getPrinter(), ok, file, line,
529  lhs, opName, rhs);
530  }
531  setPassOrFail(ok);
532  return ok;
533 }
534 
536  const char* file,
537  uint16_t line,
538  const String& lhs,
539  const char* opName,
540  bool (*op)(const String& lhs, const String& rhs),
541  const String& rhs
542 ) {
543  if (isDone()) return false;
544  bool ok = op(lhs, rhs);
545  if (isOutputEnabled(ok)) {
546  printAssertionMessage(Printer::getPrinter(), ok, file, line,
547  lhs, opName, rhs);
548  }
549  setPassOrFail(ok);
550  return ok;
551 }
552 
554  const char* file,
555  uint16_t line,
556  const String& lhs,
557  const char* opName,
558  bool (*op)(const String& lhs, const __FlashStringHelper* rhs),
559  const __FlashStringHelper* rhs
560 ) {
561  if (isDone()) return false;
562  bool ok = op(lhs, rhs);
563  if (isOutputEnabled(ok)) {
564  printAssertionMessage(Printer::getPrinter(), ok, file, line,
565  lhs, opName, rhs);
566  }
567  setPassOrFail(ok);
568  return ok;
569 }
570 
572  const char* file,
573  uint16_t line,
574  const __FlashStringHelper* lhs,
575  const char* opName,
576  bool (*op)(const __FlashStringHelper* lhs, const char* rhs),
577  const char* rhs
578 ) {
579  if (isDone()) return false;
580  bool ok = op(lhs, rhs);
581  if (isOutputEnabled(ok)) {
582  printAssertionMessage(Printer::getPrinter(), ok, file, line,
583  lhs, opName, rhs);
584  }
585  setPassOrFail(ok);
586  return ok;
587 }
588 
590  const char* file,
591  uint16_t line,
592  const __FlashStringHelper* lhs,
593  const char* opName,
594  bool (*op)(const __FlashStringHelper* lhs, const String& rhs),
595  const String& rhs
596 ) {
597  if (isDone()) return false;
598  bool ok = op(lhs, rhs);
599  if (isOutputEnabled(ok)) {
600  printAssertionMessage(Printer::getPrinter(), ok, file, line,
601  lhs, opName, rhs);
602  }
603  setPassOrFail(ok);
604  return ok;
605 }
606 
608  const char* file,
609  uint16_t line,
610  const __FlashStringHelper* lhs,
611  const char* opName,
612  bool (*op)(const __FlashStringHelper* lhs, const __FlashStringHelper* rhs),
613  const __FlashStringHelper* rhs
614 ) {
615  if (isDone()) return false;
616  bool ok = op(lhs, rhs);
617  if (isOutputEnabled(ok)) {
618  printAssertionMessage(Printer::getPrinter(), ok, file, line,
619  lhs, opName, rhs);
620  }
621  setPassOrFail(ok);
622  return ok;
623 }
624 
626  const char* file,
627  uint16_t line,
628  int lhs,
629  int rhs,
630  int error,
631  const char* opName,
632  bool (*opNear)(int lhs, int rhs, int error)
633 ) {
634  if (isDone()) return false;
635  bool ok = opNear(lhs, rhs, error);
636  if (isOutputEnabled(ok)) {
637  printAssertionNearMessage(Printer::getPrinter(), ok, file, line,
638  lhs, rhs, opName, error);
639  }
640  setPassOrFail(ok);
641  return ok;
642 }
643 
645  const char* file,
646  uint16_t line,
647  unsigned int lhs,
648  unsigned int rhs,
649  unsigned int error,
650  const char* opName,
651  bool (*opNear)(unsigned int lhs, unsigned int rhs, unsigned int error)
652 ) {
653  if (isDone()) return false;
654  bool ok = opNear(lhs, rhs, error);
655  if (isOutputEnabled(ok)) {
656  printAssertionNearMessage(Printer::getPrinter(), ok, file, line,
657  lhs, rhs, opName, error);
658  }
659  setPassOrFail(ok);
660  return ok;
661 }
662 
664  const char* file,
665  uint16_t line,
666  long lhs,
667  long rhs,
668  long error,
669  const char* opName,
670  bool (*opNear)(long lhs, long rhs, long error)
671 ) {
672  if (isDone()) return false;
673  bool ok = opNear(lhs, rhs, error);
674  if (isOutputEnabled(ok)) {
675  printAssertionNearMessage(Printer::getPrinter(), ok, file, line,
676  lhs, rhs, opName, error);
677  }
678  setPassOrFail(ok);
679  return ok;
680 }
681 
683  const char* file,
684  uint16_t line,
685  unsigned long lhs,
686  unsigned long rhs,
687  unsigned long error,
688  const char* opName,
689  bool (*opNear)(unsigned long lhs, unsigned long rhs, unsigned long error)
690 ) {
691  if (isDone()) return false;
692  bool ok = opNear(lhs, rhs, error);
693  if (isOutputEnabled(ok)) {
694  printAssertionNearMessage(Printer::getPrinter(), ok, file, line,
695  lhs, rhs, opName, error);
696  }
697  setPassOrFail(ok);
698  return ok;
699 }
700 
702  const char* file,
703  uint16_t line,
704  double lhs,
705  double rhs,
706  double error,
707  const char* opName,
708  bool (*opNear)(double lhs, double rhs, double error)
709 ) {
710  if (isDone()) return false;
711  bool ok = opNear(lhs, rhs, error);
712  if (isOutputEnabled(ok)) {
713  printAssertionNearMessage(Printer::getPrinter(), ok, file, line,
714  lhs, rhs, opName, error);
715  }
716  setPassOrFail(ok);
717  return ok;
718 }
719 
720 //---------------------------------------------------------------------------
721 
722 namespace internal {
723 
724 // Verbose versions of above which accept the string arguments of the
725 // assertXxx() macros, so that the error messages are more verbose.
726 //
727 // Prints something like the following:
728 // Assertion failed: (x=5) == (y=6), file Test.ino, line 820.
729 // Assertion passed: (x=6) == (y=6), file Test.ino, line 820.
730 template <typename A, typename B>
731 void printAssertionMessageVerbose(
732  Print* printer,
733  bool ok,
734  const char* file,
735  uint16_t line,
736  const A& lhs,
737  const __FlashStringHelper* lhsString,
738  const char* opName,
739  const B& rhs,
740  const __FlashStringHelper* rhsString
741 ) {
742 
743  // Don't use F() strings here because flash memory strings are not deduped by
744  // the compiler, so each template instantiation of this method causes a
745  // duplication of all the strings below. See
746  // https://github.com/mmurdoch/arduinounit/issues/70
747  // for more info.
748  printer->print("Assertion ");
749  printer->print(ok ? "passed" : "failed");
750  printer->print(": (");
751  printer->print(lhsString);
752  printer->print('=');
753  printer->print(lhs);
754  printer->print(") ");
755  printer->print(opName);
756  printer->print(" (");
757  printer->print(rhsString);
758  printer->print('=');
759  printer->print(rhs);
760  printer->print(')');
761  // reuse string in MataAssertion::printAssertionTestStatusMessage()
762  printer->print(", file ");
763  printer->print(file);
764  printer->print(", line ");
765  printer->print(line);
766  printer->println('.');
767 }
768 
769 // Special version of (bool, bool) because Arduino Print.h converts
770 // bool into int, which prints out "(1) == (0)", which isn't as useful.
771 // This prints "(x=true) == (y=false)".
772 void printAssertionMessageVerbose(
773  Print* printer,
774  bool ok,
775  const char* file,
776  uint16_t line,
777  bool lhs,
778  const __FlashStringHelper* lhsString,
779  const char* opName,
780  bool rhs,
781  const __FlashStringHelper* rhsString
782 ) {
783 
784  // Don't use F() strings here. Same reason as above.
785  printer->print("Assertion ");
786  printer->print(ok ? "passed" : "failed");
787  printer->print(": (");
788  printer->print(lhsString);
789  printer->print('=');
790  printer->print(lhs ? "true" : "false");
791  printer->print(") ");
792  printer->print(opName);
793  printer->print(" (");
794  printer->print(rhsString);
795  printer->print('=');
796  printer->print(rhs ? "true" : "false");
797  printer->print(')');
798  printer->print(", file ");
799  printer->print(file);
800  printer->print(", line ");
801  printer->print(line);
802  printer->println('.');
803 }
804 
805 #if ! defined(ARDUINO_ARCH_STM32)
806 
807 // Version for (long long, long long) because Print.h does not support int64.
808 void printAssertionMessageVerbose(
809  Print* printer,
810  bool ok,
811  const char* file,
812  uint16_t line,
813  long long& lhs,
814  const __FlashStringHelper* lhsString,
815  const char* opName,
816  long long& rhs,
817  const __FlashStringHelper* rhsString
818 ) {
819 
820  // Don't use F() strings here. Same reason as above.
821  printer->print("Assertion ");
822  printer->print(ok ? "passed" : "failed");
823  printer->print(": (");
824  printer->print(lhsString);
825  printer->print('=');
826  print64(*printer, lhs);
827  printer->print(") ");
828  printer->print(opName);
829  printer->print(" (");
830  printer->print(rhsString);
831  printer->print('=');
832  print64(*printer, rhs);
833  printer->print(')');
834  printer->print(", file ");
835  printer->print(file);
836  printer->print(", line ");
837  printer->print(line);
838  printer->println('.');
839 }
840 
841 // Version for (unsigned long long, unsigned long long) because Print.h does
842 // not support int64.
843 void printAssertionMessageVerbose(
844  Print* printer,
845  bool ok,
846  const char* file,
847  uint16_t line,
848  unsigned long long& lhs,
849  const __FlashStringHelper* lhsString,
850  const char* opName,
851  unsigned long long& rhs,
852  const __FlashStringHelper* rhsString
853 ) {
854 
855  // Don't use F() strings here. Same reason as above.
856  printer->print("Assertion ");
857  printer->print(ok ? "passed" : "failed");
858  printer->print(": (");
859  printer->print(lhsString);
860  printer->print('=');
861  print64(*printer, lhs);
862  printer->print(") ");
863  printer->print(opName);
864  printer->print(" (");
865  printer->print(rhsString);
866  printer->print('=');
867  print64(*printer, rhs);
868  printer->print(')');
869  printer->print(", file ");
870  printer->print(file);
871  printer->print(", line ");
872  printer->print(line);
873  printer->println('.');
874 }
875 
876 #endif // ARDUINO_ARCH_STM32
877 
878 // Special version for (const void*, const void *).
879 void printAssertionMessageVerbose(
880  Print* printer,
881  bool ok,
882  const char* file,
883  uint16_t line,
884  const void* lhs,
885  const __FlashStringHelper* lhsString,
886  const char* opName,
887  const void* rhs,
888  const __FlashStringHelper* rhsString
889 ) {
890 
891  // Don't use F() strings here. Same reason as above.
892  // Technically, we should cast to (uintptr_t). But all Arduino
893  // microcontrollers are 32-bit, so we can cast to (unsigned long) to avoid
894  // calling print64().
895  printer->print("Assertion ");
896  printer->print(ok ? "passed" : "failed");
897  printer->print(": (");
898  printer->print(lhsString);
899  printer->print("=0x");
900  printer->print((unsigned long) lhs, HEX);
901  printer->print(") ");
902  printer->print(opName);
903  printer->print(" (");
904  printer->print(rhsString);
905  printer->print("=0x");
906  printer->print((unsigned long) rhs, HEX);
907  printer->print(')');
908  printer->print(", file ");
909  printer->print(file);
910  printer->print(", line ");
911  printer->print(line);
912  printer->println('.');
913 }
914 
915 // Special version for assertTrue(arg) and assertFalse(arg).
916 // Prints:
917 // "Assertion passed/failed: (x=arg) is true"
918 // "Assertion passed/failed: (x=arg) is false"
919 void printAssertionBoolMessageVerbose(
920  Print* printer,
921  bool ok,
922  const char* file,
923  uint16_t line,
924  bool arg,
925  const __FlashStringHelper* argString,
926  bool value
927 ) {
928 
929  // Don't use F() strings here. Same reason as above.
930  printer->print("Assertion ");
931  printer->print(ok ? "passed" : "failed");
932  printer->print(": (");
933  printer->print(argString);
934  printer->print('=');
935  printer->print(arg ? "true" : "false");
936  printer->print(") is ");
937  printer->print(value ? "true" : "false");
938  printer->print(", file ");
939  printer->print(file);
940  printer->print(", line ");
941  printer->print(line);
942  printer->println('.');
943 }
944 
945 template <typename A>
946 void printAssertionNearMessageVerbose(
947  Print* printer,
948  bool ok,
949  const char* file,
950  uint16_t line,
951  const A& lhs,
952  const __FlashStringHelper* lhsString,
953  const A& rhs,
954  const __FlashStringHelper* rhsString,
955  const char* opName,
956  const A& error,
957  const __FlashStringHelper* errorString
958 ) {
959  printer->print("Assertion ");
960  printer->print(ok ? "passed" : "failed");
961  printer->print(": |(");
962  printer->print(lhsString);
963  printer->print('=');
964  printer->print(lhs);
965  printer->print(") - (");
966  printer->print(rhsString);
967  printer->print('=');
968  printer->print(rhs);
969  printer->print(")| ");
970  printer->print(opName);
971  printer->print(" (");
972  printer->print(errorString);
973  printer->print('=');
974  printer->print(error);
975  printer->print(')');
976  printer->print(", file ");
977  printer->print(file);
978  printer->print(", line ");
979  printer->print(line);
980  printer->println('.');
981 }
982 
983 } // namespace
984 
986  const char* file,
987  uint16_t line,
988  bool arg,
989  const __FlashStringHelper* argString,
990  bool value
991 ) {
992  if (isDone()) return false;
993  bool ok = (arg == value);
994  if (isOutputEnabled(ok)) {
995  printAssertionBoolMessageVerbose(Printer::getPrinter(), ok, file, line,
996  arg, argString, value);
997  }
998  setPassOrFail(ok);
999  return ok;
1000 }
1001 
1003  const char* file,
1004  uint16_t line,
1005  bool lhs,
1006  const __FlashStringHelper* lhsString,
1007  const char* opName,
1008  bool (*op)(bool lhs, bool rhs),
1009  bool rhs,
1010  const __FlashStringHelper* rhsString
1011 ) {
1012  if (isDone()) return false;
1013  bool ok = op(lhs, rhs);
1014  if (isOutputEnabled(ok)) {
1015  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1016  lhs, lhsString, opName, rhs, rhsString);
1017  }
1018  setPassOrFail(ok);
1019  return ok;
1020 }
1021 
1023  const char* file,
1024  uint16_t line,
1025  char lhs,
1026  const __FlashStringHelper* lhsString,
1027  const char* opName,
1028  bool (*op)(char lhs, char rhs),
1029  char rhs,
1030  const __FlashStringHelper* rhsString
1031 ) {
1032  if (isDone()) return false;
1033  bool ok = op(lhs, rhs);
1034  if (isOutputEnabled(ok)) {
1035  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1036  lhs, lhsString, opName, rhs, rhsString);
1037  }
1038  setPassOrFail(ok);
1039  return ok;
1040 }
1041 
1043  const char* file,
1044  uint16_t line,
1045  int lhs,
1046  const __FlashStringHelper* lhsString,
1047  const char* opName,
1048  bool (*op)(int lhs, int rhs),
1049  int rhs,
1050  const __FlashStringHelper* rhsString
1051 ) {
1052  if (isDone()) return false;
1053  bool ok = op(lhs, rhs);
1054  if (isOutputEnabled(ok)) {
1055  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1056  lhs, lhsString, opName, rhs, rhsString);
1057  }
1058  setPassOrFail(ok);
1059  return ok;
1060 }
1061 
1063  const char* file,
1064  uint16_t line,
1065  unsigned int lhs,
1066  const __FlashStringHelper* lhsString,
1067  const char* opName,
1068  bool (*op)(unsigned int lhs, unsigned int rhs),
1069  unsigned int rhs,
1070  const __FlashStringHelper* rhsString
1071 ) {
1072  if (isDone()) return false;
1073  bool ok = op(lhs, rhs);
1074  if (isOutputEnabled(ok)) {
1075  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1076  lhs, lhsString, opName, rhs, rhsString);
1077  }
1078  setPassOrFail(ok);
1079  return ok;
1080 }
1081 
1083  const char* file,
1084  uint16_t line,
1085  long lhs,
1086  const __FlashStringHelper* lhsString,
1087  const char* opName,
1088  bool (*op)(long lhs, long rhs),
1089  long rhs,
1090  const __FlashStringHelper* rhsString
1091 ) {
1092  if (isDone()) return false;
1093  bool ok = op(lhs, rhs);
1094  if (isOutputEnabled(ok)) {
1095  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1096  lhs, lhsString, opName, rhs, rhsString);
1097  }
1098  setPassOrFail(ok);
1099  return ok;
1100 }
1101 
1103  const char* file,
1104  uint16_t line,
1105  unsigned long lhs,
1106  const __FlashStringHelper* lhsString,
1107  const char* opName,
1108  bool (*op)(unsigned long lhs, unsigned long rhs),
1109  unsigned long rhs,
1110  const __FlashStringHelper* rhsString
1111 ) {
1112  if (isDone()) return false;
1113  bool ok = op(lhs, rhs);
1114  if (isOutputEnabled(ok)) {
1115  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1116  lhs, lhsString, opName, rhs, rhsString);
1117  }
1118  setPassOrFail(ok);
1119  return ok;
1120 }
1121 
1123  const char* file,
1124  uint16_t line,
1125  long long lhs,
1126  const __FlashStringHelper* lhsString,
1127  const char* opName,
1128  bool (*op)(long long lhs, long long rhs),
1129  long long rhs,
1130  const __FlashStringHelper* rhsString
1131 ) {
1132  if (isDone()) return false;
1133  bool ok = op(lhs, rhs);
1134  if (isOutputEnabled(ok)) {
1135  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1136  lhs, lhsString, opName, rhs, rhsString);
1137  }
1138  setPassOrFail(ok);
1139  return ok;
1140 }
1141 
1143  const char* file,
1144  uint16_t line,
1145  unsigned long long lhs,
1146  const __FlashStringHelper* lhsString,
1147  const char* opName,
1148  bool (*op)(unsigned long long lhs, unsigned long long rhs),
1149  unsigned long long rhs,
1150  const __FlashStringHelper* rhsString
1151 ) {
1152  if (isDone()) return false;
1153  bool ok = op(lhs, rhs);
1154  if (isOutputEnabled(ok)) {
1155  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1156  lhs, lhsString, opName, rhs, rhsString);
1157  }
1158  setPassOrFail(ok);
1159  return ok;
1160 }
1161 
1163  const char* file,
1164  uint16_t line,
1165  double lhs,
1166  const __FlashStringHelper* lhsString,
1167  const char* opName,
1168  bool (*op)(double lhs, double rhs),
1169  double rhs,
1170  const __FlashStringHelper* rhsString
1171 ) {
1172  if (isDone()) return false;
1173  bool ok = op(lhs, rhs);
1174  if (isOutputEnabled(ok)) {
1175  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1176  lhs, lhsString, opName, rhs, rhsString);
1177  }
1178  setPassOrFail(ok);
1179  return ok;
1180 }
1181 
1183  const char* file,
1184  uint16_t line,
1185  const void* lhs,
1186  const __FlashStringHelper* lhsString,
1187  const char* opName,
1188  bool (*op)(const void* lhs, const void* rhs),
1189  const void* rhs,
1190  const __FlashStringHelper* rhsString
1191 ) {
1192  if (isDone()) return false;
1193  bool ok = op(lhs, rhs);
1194  if (isOutputEnabled(ok)) {
1195  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1196  lhs, lhsString, opName, rhs, rhsString);
1197  }
1198  setPassOrFail(ok);
1199  return ok;
1200 }
1201 
1203  const char* file,
1204  uint16_t line,
1205  const char* lhs,
1206  const __FlashStringHelper* lhsString,
1207  const char* opName,
1208  bool (*op)(const char* lhs, const char* rhs),
1209  const char* rhs,
1210  const __FlashStringHelper* rhsString
1211 ) {
1212  if (isDone()) return false;
1213  bool ok = op(lhs, rhs);
1214  if (isOutputEnabled(ok)) {
1215  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1216  lhs, lhsString, opName, rhs, rhsString);
1217  }
1218  setPassOrFail(ok);
1219  return ok;
1220 }
1221 
1223  const char* file,
1224  uint16_t line,
1225  const char* lhs,
1226  const __FlashStringHelper* lhsString,
1227  const char* opName,
1228  bool (*op)(const char* lhs, const String& rhs),
1229  const String& rhs,
1230  const __FlashStringHelper* rhsString
1231 ) {
1232  if (isDone()) return false;
1233  bool ok = op(lhs, rhs);
1234  if (isOutputEnabled(ok)) {
1235  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1236  lhs, lhsString, opName, rhs, rhsString);
1237  }
1238  setPassOrFail(ok);
1239  return ok;
1240 }
1241 
1243  const char* file,
1244  uint16_t line,
1245  const char* lhs,
1246  const __FlashStringHelper* lhsString,
1247  const char* opName,
1248  bool (*op)(const char* lhs, const __FlashStringHelper* rhs),
1249  const __FlashStringHelper* rhs,
1250  const __FlashStringHelper* rhsString
1251 ) {
1252  if (isDone()) return false;
1253  bool ok = op(lhs, rhs);
1254  if (isOutputEnabled(ok)) {
1255  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1256  lhs, lhsString, opName, rhs, rhsString);
1257  }
1258  setPassOrFail(ok);
1259  return ok;
1260 }
1261 
1263  const char* file,
1264  uint16_t line,
1265  const String& lhs,
1266  const __FlashStringHelper* lhsString,
1267  const char* opName,
1268  bool (*op)(const String& lhs, const char* rhs),
1269  const char* rhs,
1270  const __FlashStringHelper* rhsString
1271 ) {
1272  if (isDone()) return false;
1273  bool ok = op(lhs, rhs);
1274  if (isOutputEnabled(ok)) {
1275  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1276  lhs, lhsString, opName, rhs, rhsString);
1277  }
1278  setPassOrFail(ok);
1279  return ok;
1280 }
1281 
1283  const char* file,
1284  uint16_t line,
1285  const String& lhs,
1286  const __FlashStringHelper* lhsString,
1287  const char* opName,
1288  bool (*op)(const String& lhs, const String& rhs),
1289  const String& rhs,
1290  const __FlashStringHelper* rhsString
1291 ) {
1292  if (isDone()) return false;
1293  bool ok = op(lhs, rhs);
1294  if (isOutputEnabled(ok)) {
1295  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1296  lhs, lhsString, opName, rhs, rhsString);
1297  }
1298  setPassOrFail(ok);
1299  return ok;
1300 }
1301 
1303  const char* file,
1304  uint16_t line,
1305  const String& lhs,
1306  const __FlashStringHelper* lhsString,
1307  const char* opName,
1308  bool (*op)(const String& lhs, const __FlashStringHelper* rhs),
1309  const __FlashStringHelper* rhs,
1310  const __FlashStringHelper* rhsString
1311 ) {
1312  if (isDone()) return false;
1313  bool ok = op(lhs, rhs);
1314  if (isOutputEnabled(ok)) {
1315  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1316  lhs, lhsString, opName, rhs, rhsString);
1317  }
1318  setPassOrFail(ok);
1319  return ok;
1320 }
1321 
1323  const char* file,
1324  uint16_t line,
1325  const __FlashStringHelper* lhs,
1326  const __FlashStringHelper* lhsString,
1327  const char* opName,
1328  bool (*op)(const __FlashStringHelper* lhs,
1329  const char* rhs),
1330  const char* rhs,
1331  const __FlashStringHelper* rhsString
1332 ) {
1333  if (isDone()) return false;
1334  bool ok = op(lhs, rhs);
1335  if (isOutputEnabled(ok)) {
1336  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1337  lhs, lhsString, opName, rhs, rhsString);
1338  }
1339  setPassOrFail(ok);
1340  return ok;
1341 }
1342 
1344  const char* file,
1345  uint16_t line,
1346  const __FlashStringHelper* lhs,
1347  const __FlashStringHelper* lhsString,
1348  const char* opName,
1349  bool (*op)(const __FlashStringHelper* lhs, const String& rhs),
1350  const String& rhs,
1351  const __FlashStringHelper* rhsString
1352 ) {
1353  if (isDone()) return false;
1354  bool ok = op(lhs, rhs);
1355  if (isOutputEnabled(ok)) {
1356  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1357  lhs, lhsString, opName, rhs, rhsString);
1358  }
1359  setPassOrFail(ok);
1360  return ok;
1361 }
1362 
1364  const char* file,
1365  uint16_t line,
1366  const __FlashStringHelper* lhs,
1367  const __FlashStringHelper* lhsString,
1368  const char* opName,
1369  bool (*op)(const __FlashStringHelper* lhs, const __FlashStringHelper* rhs),
1370  const __FlashStringHelper* rhs,
1371  const __FlashStringHelper* rhsString
1372 ) {
1373  if (isDone()) return false;
1374  bool ok = op(lhs, rhs);
1375  if (isOutputEnabled(ok)) {
1376  printAssertionMessageVerbose(Printer::getPrinter(), ok, file, line,
1377  lhs, lhsString, opName, rhs, rhsString);
1378  }
1379  setPassOrFail(ok);
1380  return ok;
1381 }
1382 
1384  const char* file,
1385  uint16_t line,
1386  int lhs,
1387  const __FlashStringHelper* lhsString,
1388  int rhs,
1389  const __FlashStringHelper* rhsString,
1390  int error,
1391  const __FlashStringHelper* errorString,
1392  const char* opName,
1393  bool (*opNear)(int lhs, int rhs, int error)
1394 ) {
1395  if (isDone()) return false;
1396  bool ok = opNear(lhs, rhs, error);
1397  if (isOutputEnabled(ok)) {
1398  printAssertionNearMessageVerbose(Printer::getPrinter(), ok, file, line,
1399  lhs, lhsString, rhs, rhsString, opName, error, errorString);
1400  }
1401  setPassOrFail(ok);
1402  return ok;
1403 }
1404 
1406  const char* file,
1407  uint16_t line,
1408  unsigned int lhs,
1409  const __FlashStringHelper* lhsString,
1410  unsigned int rhs,
1411  const __FlashStringHelper* rhsString,
1412  unsigned int error,
1413  const __FlashStringHelper* errorString,
1414  const char* opName,
1415  bool (*opNear)(unsigned int lhs, unsigned int rhs, unsigned int error)
1416 ) {
1417  if (isDone()) return false;
1418  bool ok = opNear(lhs, rhs, error);
1419  if (isOutputEnabled(ok)) {
1420  printAssertionNearMessageVerbose(Printer::getPrinter(), ok, file, line,
1421  lhs, lhsString, rhs, rhsString, opName, error, errorString);
1422  }
1423  setPassOrFail(ok);
1424  return ok;
1425 }
1426 
1428  const char* file,
1429  uint16_t line,
1430  long lhs,
1431  const __FlashStringHelper* lhsString,
1432  long rhs,
1433  const __FlashStringHelper* rhsString,
1434  long error,
1435  const __FlashStringHelper* errorString,
1436  const char* opName,
1437  bool (*opNear)(long lhs, long rhs, long error)
1438 ) {
1439  if (isDone()) return false;
1440  bool ok = opNear(lhs, rhs, error);
1441  if (isOutputEnabled(ok)) {
1442  printAssertionNearMessageVerbose(Printer::getPrinter(), ok, file, line,
1443  lhs, lhsString, rhs, rhsString, opName, error, errorString);
1444  }
1445  setPassOrFail(ok);
1446  return ok;
1447 }
1448 
1450  const char* file,
1451  uint16_t line,
1452  unsigned long lhs,
1453  const __FlashStringHelper* lhsString,
1454  unsigned long rhs,
1455  const __FlashStringHelper* rhsString,
1456  unsigned long error,
1457  const __FlashStringHelper* errorString,
1458  const char* opName,
1459  bool (*opNear)(unsigned long lhs, unsigned long rhs, unsigned long error)
1460 ) {
1461  if (isDone()) return false;
1462  bool ok = opNear(lhs, rhs, error);
1463  if (isOutputEnabled(ok)) {
1464  printAssertionNearMessageVerbose(Printer::getPrinter(), ok, file, line,
1465  lhs, lhsString, rhs, rhsString, opName, error, errorString);
1466  }
1467  setPassOrFail(ok);
1468  return ok;
1469 }
1470 
1472  const char* file,
1473  uint16_t line,
1474  double lhs,
1475  const __FlashStringHelper* lhsString,
1476  double rhs,
1477  const __FlashStringHelper* rhsString,
1478  double error,
1479  const __FlashStringHelper* errorString,
1480  const char* opName,
1481  bool (*opNear)(double lhs, double rhs, double error)
1482 ) {
1483  if (isDone()) return false;
1484  bool ok = opNear(lhs, rhs, error);
1485  if (isOutputEnabled(ok)) {
1486  printAssertionNearMessageVerbose(Printer::getPrinter(), ok, file, line,
1487  lhs, lhsString, rhs, rhsString, opName, error, errorString);
1488  }
1489  setPassOrFail(ok);
1490  return ok;
1491 }
1492 
1493 }
aunit::Printer::getPrinter
static Print * getPrinter()
Get the output printer used by the various assertion() methods and the TestRunner.
Definition: Printer.h:48
aunit::Assertion::isOutputEnabled
bool isOutputEnabled(bool ok) const
Returns true if an assertion message should be printed.
Definition: Assertion.cpp:262
aunit::Assertion::assertionNear
bool assertionNear(const char *file, uint16_t line, int lhs, int rhs, int error, const char *opName, bool(*compareNear)(int lhs, int rhs, int error))
Used by assertNear(int, int).
Definition: Assertion.cpp:625
aunit::Verbosity::kAssertionPassed
static const uint8_t kAssertionPassed
Print assertXxx() passed message.
Definition: Verbosity.h:40
aunit::Verbosity::kAssertionFailed
static const uint8_t kAssertionFailed
Print assertXxx() failed message.
Definition: Verbosity.h:43
aunit::Assertion::assertionBoolVerbose
bool assertionBoolVerbose(const char *file, uint16_t line, bool arg, const __FlashStringHelper *argString, bool value)
Used by assertTrue() and assertFalse().
Definition: Assertion.cpp:985
print64.h
aunit::Assertion::assertionNearVerbose
bool assertionNearVerbose(const char *file, uint16_t line, int lhs, const __FlashStringHelper *lhsString, int rhs, const __FlashStringHelper *rhsString, int error, const __FlashStringHelper *errorString, const char *opName, bool(*compareNear)(int lhs, int rhs, int error))
Used by assertNear(int, int).
Definition: Assertion.cpp:1383
aunit::Assertion::assertionVerbose
bool assertionVerbose(const char *file, uint16_t line, bool lhs, const __FlashStringHelper *lhsString, const char *opName, bool(*op)(bool lhs, bool rhs), bool rhs, const __FlashStringHelper *rhsString)
Used by assertEqual(bool, bool).
Definition: Assertion.cpp:1002
aunit::Assertion::assertion
bool assertion(const char *file, uint16_t line, bool lhs, const char *opName, bool(*op)(bool lhs, bool rhs), bool rhs)
Used by assertXxx(bool, bool).
Definition: Assertion.cpp:283
aunit::Assertion::assertionBool
bool assertionBool(const char *file, uint16_t line, bool arg, bool value)
Used by assertTrue() and assertFalse().
Definition: Assertion.cpp:267
Flash.h