AUnit  1.7.1
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test.
Assertion.h
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 #ifndef AUNIT_ASSERTION_H
26 #define AUNIT_ASSERTION_H
27 
28 #include "Flash.h"
29 #include "Test.h"
30 
31 class __FlashStringHelper;
32 class String;
33 
34 namespace aunit {
35 
55 class Assertion: public Test {
56  protected:
58  Assertion() = default;
59 
61  bool isOutputEnabled(bool ok) const;
62 
63  // Terse assertions. Prints only the argument values.
64 
66  bool assertionBool(
67  const char* file,
68  uint16_t line,
69  bool arg,
70  bool value);
71 
73  bool assertion(
74  const char* file,
75  uint16_t line,
76  bool lhs,
77  const char* opName,
78  bool (*op)(bool lhs, bool rhs),
79  bool rhs);
80 
82  bool assertion(
83  const char* file,
84  uint16_t line,
85  char lhs,
86  const char* opName,
87  bool (*op)(char lhs, char rhs),
88  char rhs);
89 
91  bool assertion(
92  const char* file,
93  uint16_t line,
94  int lhs,
95  const char* opName,
96  bool (*op)(int lhs, int rhs),
97  int rhs);
98 
100  bool assertion(
101  const char* file,
102  uint16_t line,
103  unsigned int lhs,
104  const char* opName,
105  bool (*op)(unsigned int lhs, unsigned int rhs),
106  unsigned int rhs);
107 
109  bool assertion(
110  const char* file,
111  uint16_t line,
112  long lhs,
113  const char* opName,
114  bool (*op)(long lhs, long rhs),
115  long rhs);
116 
118  bool assertion(
119  const char* file,
120  uint16_t line,
121  unsigned long lhs,
122  const char* opName,
123  bool (*op)(unsigned long lhs, unsigned long rhs),
124  unsigned long rhs);
125 
127  bool assertion(
128  const char* file,
129  uint16_t line,
130  long long lhs,
131  const char* opName,
132  bool (*op)(long long lhs, long long rhs),
133  long long rhs);
134 
136  bool assertion(
137  const char* file,
138  uint16_t line,
139  unsigned long long lhs,
140  const char* opName,
141  bool (*op)(unsigned long long lhs, unsigned long long rhs),
142  unsigned long long rhs);
143 
145  bool assertion(
146  const char* file,
147  uint16_t line,
148  double lhs,
149  const char* opName,
150  bool (*op)(double lhs, double rhs),
151  double rhs);
152 
154  bool assertion(
155  const char* file,
156  uint16_t line,
157  const void* lhs,
158  const char* opName,
159  bool (*op)(const void* lhs, const void* rhs),
160  const void* rhs);
161 
163  bool assertion(
164  const char* file,
165  uint16_t line,
166  const char* lhs,
167  const char* opName,
168  bool (*op)(const char* lhs, const char* rhs),
169  const char* rhs);
170 
172  bool assertion(
173  const char* file,
174  uint16_t line,
175  const char* lhs,
176  const char* opName,
177  bool (*op)(const char* lhs, const String& rhs),
178  const String& rhs);
179 
181  bool assertion(
182  const char* file,
183  uint16_t line,
184  const char* lhs,
185  const char* opName,
186  bool (*op)(const char* lhs, const __FlashStringHelper* rhs),
187  const __FlashStringHelper* rhs);
188 
190  bool assertion(
191  const char* file,
192  uint16_t line,
193  const String& lhs,
194  const char* opName,
195  bool (*op)(const String& lhs, const char* rhs),
196  const char* rhs);
197 
199  bool assertion(
200  const char* file,
201  uint16_t line,
202  const String& lhs,
203  const char* opName,
204  bool (*op)(const String& lhs, const String& rhs),
205  const String& rhs);
206 
208  bool assertion(
209  const char* file,
210  uint16_t line,
211  const String& lhs,
212  const char* opName,
213  bool (*op)(const String& lhs, const __FlashStringHelper* rhs),
214  const __FlashStringHelper* rhs);
215 
217  bool assertion(
218  const char* file,
219  uint16_t line,
220  const __FlashStringHelper* lhs,
221  const char* opName,
222  bool (*op)(const __FlashStringHelper* lhs, const char* rhs),
223  const char* rhs);
224 
226  bool assertion(
227  const char* file,
228  uint16_t line,
229  const __FlashStringHelper* lhs,
230  const char* opName,
231  bool (*op)(const __FlashStringHelper* lhs, const String& rhs),
232  const String& rhs);
233 
239  bool assertion(
240  const char* file,
241  uint16_t line,
242  const __FlashStringHelper* lhs,
243  const char* opName,
244  bool (*op)(
245  const __FlashStringHelper* lhs,
246  const __FlashStringHelper* rhs),
247  const __FlashStringHelper* rhs);
248 
250  bool assertionNear(
251  const char* file,
252  uint16_t line,
253  int lhs,
254  int rhs,
255  int error,
256  const char* opName,
257  bool (*compareNear)(int lhs, int rhs, int error));
258 
260  bool assertionNear(
261  const char* file,
262  uint16_t line,
263  unsigned int lhs,
264  unsigned int rhs,
265  unsigned int error,
266  const char* opName,
267  bool (*compareNear)(
268  unsigned int lhs, unsigned int rhs, unsigned int error));
269 
271  bool assertionNear(
272  const char* file,
273  uint16_t line,
274  long lhs,
275  long rhs,
276  long error,
277  const char* opName,
278  bool (*compareNear)(long lhs, long rhs, long error));
279 
281  bool assertionNear(
282  const char* file,
283  uint16_t line,
284  unsigned long lhs,
285  unsigned long rhs,
286  unsigned long error,
287  const char* opName,
288  bool (*compareNear)(
289  unsigned long lhs, unsigned long rhs, unsigned long error));
290 
292  bool assertionNear(
293  const char* file,
294  uint16_t line,
295  double lhs,
296  double rhs,
297  double error,
298  const char* opName,
299  bool (*compareNear)(double lhs, double rhs, double error));
300 
301  // Verbose versions of above.
302 
305  const char* file,
306  uint16_t line,
307  bool arg,
308  const __FlashStringHelper* argString,
309  bool value);
310 
312  bool assertionVerbose(
313  const char* file,
314  uint16_t line,
315  bool lhs,
316  const __FlashStringHelper* lhsString,
317  const char* opName,
318  bool (*op)(bool lhs, bool rhs),
319  bool rhs,
320  const __FlashStringHelper* rhsString);
321 
323  bool assertionVerbose(
324  const char* file,
325  uint16_t line,
326  char lhs,
327  const __FlashStringHelper* lhsString,
328  const char* opName,
329  bool (*op)(char lhs, char rhs),
330  char rhs,
331  const __FlashStringHelper* rhsString);
332 
334  bool assertionVerbose(
335  const char* file,
336  uint16_t line,
337  int lhs,
338  const __FlashStringHelper* lhsString,
339  const char* opName,
340  bool (*op)(int lhs, int rhs),
341  int rhs,
342  const __FlashStringHelper* rhsString);
343 
345  bool assertionVerbose(
346  const char* file,
347  uint16_t line,
348  unsigned int lhs,
349  const __FlashStringHelper* lhsString,
350  const char* opName,
351  bool (*op)(unsigned int lhs, unsigned int rhs),
352  unsigned int rhs,
353  const __FlashStringHelper* rhsString);
354 
356  bool assertionVerbose(
357  const char* file,
358  uint16_t line,
359  long lhs,
360  const __FlashStringHelper* lhsString,
361  const char* opName,
362  bool (*op)(long lhs, long rhs),
363  long rhs,
364  const __FlashStringHelper* rhsString);
365 
367  bool assertionVerbose(
368  const char* file,
369  uint16_t line,
370  unsigned long lhs,
371  const __FlashStringHelper* lhsString,
372  const char* opName,
373  bool (*op)(unsigned long lhs, unsigned long rhs),
374  unsigned long rhs,
375  const __FlashStringHelper* rhsString);
376 
378  bool assertionVerbose(
379  const char* file,
380  uint16_t line,
381  long long lhs,
382  const __FlashStringHelper* lhsString,
383  const char* opName,
384  bool (*op)(long long lhs, long long rhs),
385  long long rhs,
386  const __FlashStringHelper* rhsString);
387 
389  bool assertionVerbose(
390  const char* file,
391  uint16_t line,
392  unsigned long long lhs,
393  const __FlashStringHelper* lhsString,
394  const char* opName,
395  bool (*op)(unsigned long long lhs, unsigned long long rhs),
396  unsigned long long rhs,
397  const __FlashStringHelper* rhsString);
398 
400  bool assertionVerbose(
401  const char* file,
402  uint16_t line,
403  double lhs,
404  const __FlashStringHelper* lhsString,
405  const char* opName,
406  bool (*op)(double lhs, double rhs),
407  double rhs,
408  const __FlashStringHelper* rhsString);
409 
411  bool assertionVerbose(
412  const char* file,
413  uint16_t line,
414  const void* lhs,
415  const __FlashStringHelper* lhsString,
416  const char* opName,
417  bool (*op)(const void* lhs, const void* rhs),
418  const void* rhs,
419  const __FlashStringHelper* rhsString);
420 
422  bool assertionVerbose(
423  const char* file,
424  uint16_t line,
425  const char* lhs,
426  const __FlashStringHelper* lhsString,
427  const char* opName,
428  bool (*op)(const char* lhs, const char* rhs),
429  const char* rhs,
430  const __FlashStringHelper* rhsString);
431 
433  bool assertionVerbose(
434  const char* file,
435  uint16_t line,
436  const char* lhs,
437  const __FlashStringHelper* lhsString,
438  const char* opName,
439  bool (*op)(const char* lhs, const String& rhs),
440  const String& rhs,
441  const __FlashStringHelper* rhsString);
442 
444  bool assertionVerbose(
445  const char* file,
446  uint16_t line,
447  const char* lhs,
448  const __FlashStringHelper* lhsString,
449  const char* opName,
450  bool (*op)(const char* lhs, const __FlashStringHelper* rhs),
451  const __FlashStringHelper* rhs,
452  const __FlashStringHelper* rhsString);
453 
455  bool assertionVerbose(
456  const char* file,
457  uint16_t line,
458  const String& lhs,
459  const __FlashStringHelper* lhsString,
460  const char* opName,
461  bool (*op)(const String& lhs, const char* rhs),
462  const char* rhs,
463  const __FlashStringHelper* rhsString);
464 
466  bool assertionVerbose(
467  const char* file,
468  uint16_t line,
469  const String& lhs,
470  const __FlashStringHelper* lhsString,
471  const char* opName,
472  bool (*op)(const String& lhs, const String& rhs),
473  const String& rhs,
474  const __FlashStringHelper* rhsString);
475 
477  bool assertionVerbose(
478  const char* file,
479  uint16_t line,
480  const String& lhs,
481  const __FlashStringHelper* lhsString,
482  const char* opName,
483  bool (*op)(const String& lhs, const __FlashStringHelper* rhs),
484  const __FlashStringHelper* rhs,
485  const __FlashStringHelper* rhsString);
486 
488  bool assertionVerbose(
489  const char* file,
490  uint16_t line,
491  const __FlashStringHelper* lhs,
492  const __FlashStringHelper* lhsString,
493  const char* opName,
494  bool (*op)(const __FlashStringHelper* lhs, const char* rhs),
495  const char* rhs,
496  const __FlashStringHelper* rhsString);
497 
499  bool assertionVerbose(
500  const char* file,
501  uint16_t line,
502  const __FlashStringHelper* lhs,
503  const __FlashStringHelper* lhsString,
504  const char* opName,
505  bool (*op)(const __FlashStringHelper* lhs, const String& rhs),
506  const String& rhs,
507  const __FlashStringHelper* rhsString);
508 
514  bool assertionVerbose(
515  const char* file,
516  uint16_t line,
517  const __FlashStringHelper* lhs,
518  const __FlashStringHelper* lhsString,
519  const char* opName,
520  bool (*op)(
521  const __FlashStringHelper* lhs,
522  const __FlashStringHelper* rhs),
523  const __FlashStringHelper* rhs,
524  const __FlashStringHelper* rhsString);
525 
528  const char* file,
529  uint16_t line,
530  int lhs,
531  const __FlashStringHelper* lhsString,
532  int rhs,
533  const __FlashStringHelper* rhsString,
534  int error,
535  const __FlashStringHelper* errorString,
536  const char* opName,
537  bool (*compareNear)(int lhs, int rhs, int error));
538 
541  const char* file,
542  uint16_t line,
543  unsigned int lhs,
544  const __FlashStringHelper* lhsString,
545  unsigned int rhs,
546  const __FlashStringHelper* rhsString,
547  unsigned int error,
548  const __FlashStringHelper* errorString,
549  const char* opName,
550  bool (*compareNear)(
551  unsigned int lhs, unsigned int rhs, unsigned int error));
552 
555  const char* file,
556  uint16_t line,
557  long lhs,
558  const __FlashStringHelper* lhsString,
559  long rhs,
560  const __FlashStringHelper* rhsString,
561  long error,
562  const __FlashStringHelper* errorString,
563  const char* opName,
564  bool (*compareNear)(long lhs, long rhs, long error));
565 
568  const char* file,
569  uint16_t line,
570  unsigned long lhs,
571  const __FlashStringHelper* lhsString,
572  unsigned long rhs,
573  const __FlashStringHelper* rhsString,
574  unsigned long error,
575  const __FlashStringHelper* errorString,
576  const char* opName,
577  bool (*compareNear)(
578  unsigned long lhs, unsigned long rhs, unsigned long error));
579 
582  const char* file,
583  uint16_t line,
584  double lhs,
585  const __FlashStringHelper* lhsString,
586  double rhs,
587  const __FlashStringHelper* rhsString,
588  double error,
589  const __FlashStringHelper* errorString,
590  const char* opName,
591  bool (*compareNear)(double lhs, double rhs, double error));
592 
593  private:
594  // Disable copy-constructor and assignment operator
595  Assertion(const Assertion&) = delete;
596  Assertion& operator=(const Assertion&) = delete;
597 };
598 
599 }
600 
601 #endif
Various macros to smooth over the differences among the various platforms with regards to their suppo...
An Assertion class is a subclass of Test and provides various overloaded assertion() functions.
Definition: Assertion.h:55
bool assertionBool(const char *file, uint16_t line, bool arg, bool value)
Used by assertTrue() and assertFalse().
Definition: Assertion.cpp:259
bool assertionBoolVerbose(const char *file, uint16_t line, bool arg, const __FlashStringHelper *argString, bool value)
Used by assertTrue() and assertFalse().
Definition: Assertion.cpp:969
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:275
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:1367
Assertion()=default
Empty constructor.
bool isOutputEnabled(bool ok) const
Returns true if an assertion message should be printed.
Definition: Assertion.cpp:254
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:986
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:617
Base class of all test cases.
Definition: Test.h:43