1 /***********************************************************************************************************************************
2 Assert Routines
3 ***********************************************************************************************************************************/
4 #ifndef COMMON_ASSERT_H
5 #define COMMON_ASSERT_H
6 
7 #include "common/error.h"
8 
9 /***********************************************************************************************************************************
10 Asserts are used in test code to ensure that certain conditions are true.  They are omitted from production builds.
11 ***********************************************************************************************************************************/
12 #ifdef DEBUG
13     #define ASSERT(condition)                                                                                                      \
14         do                                                                                                                         \
15         {                                                                                                                          \
16             if (!(condition))                                                                                                      \
17                 THROW_FMT(AssertError, "assertion '%s' failed", #condition);                                                       \
18         }                                                                                                                          \
19         while (0)
20 
21     // Skip inline asserts when coverage testing because they will not have branch coverage. Generally speaking inline assertions
22     // should be of the "this != NULL" variety which is also caught effectively by Valgrind.
23     #ifndef DEBUG_COVERAGE
24         #define ASSERT_INLINE(condition)                                                                                           \
25             do                                                                                                                     \
26             {                                                                                                                      \
27                 if (!(condition))                                                                                                  \
28                     THROW_FMT(AssertError, "assertion '%s' failed", #condition);                                                   \
29             }                                                                                                                      \
30             while (0)
31     #else
32         #define ASSERT_INLINE(condition)
33     #endif
34 
35     // Used when execution reaches an invalid location rather than an invalid condition
36     #define ASSERT_MSG(message)                                                                                                    \
37         THROW_FMT(AssertError, message);
38 #else
39     #define ASSERT(condition)
40     #define ASSERT_INLINE(condition)
41     #define ASSERT_MSG(message)
42 #endif
43 
44 /***********************************************************************************************************************************
45 Checks are used in production builds to test very important conditions.  Be sure to limit use to the most critical cases.
46 ***********************************************************************************************************************************/
47 #define CHECK(condition)                                                                                                           \
48     do                                                                                                                             \
49     {                                                                                                                              \
50         if (!(condition))                                                                                                          \
51             THROW_FMT(AssertError, "check '%s' failed", #condition);                                                               \
52     }                                                                                                                              \
53     while (0)
54 
55 #endif
56