SolidUtils
Debug.hpp
Go to the documentation of this file.
1 
29 #ifndef SOLIDUTILS_INCLUDE_DEBUG_HPP
30 #define SOLIDUTILS_INCLUDE_DEBUG_HPP
31 
32 /******************************************************************************
33 * MACROS **********************************************************************
34 ******************************************************************************/
35 
36 #ifndef NDEBUG
37 #include <cassert> // assert
38 #include <cstdio> // vprintf
39 #include <cstdarg> // va_start, va_end
40 #include <iostream> // std::cerr, std::cout
41 
42 namespace sl
43 {
44 
45 namespace
46 {
47 
48 // NOTE: there is no stream based debugging, as expression in the '<<' would
49 // still be evaluated even if not printed.
50 
57 void debugMessage(
58  char const * const fmt,
59  ...)
60 {
61  va_list argptr;
62  va_start(argptr, fmt);
63  fprintf(stdout, "DEBUG: ");
64  vfprintf(stdout, fmt, argptr);
65  fprintf(stdout, "\n");
66  va_end(argptr);
67  fflush(stdout);
68 }
69 
75 void debugMessage(
76  std::string const & msg)
77 {
78  fprintf(stdout, "DEBUG: %s\n", msg.c_str());
79 }
80 
81 }
82 
83 }
84 
85 #define ASSERT_TRUE(a) \
86  do { \
87  if (!(a)) { \
88  std::cerr << "("#a" = " << (a) << ")" << std::endl; \
89  assert(false); \
90  } \
91  } while (false)
92 #define ASSERT_FALSE(a) \
93  do { \
94  if (a) { \
95  std::cerr << "("#a" = " << (a) << ")" << std::endl; \
96  assert(false); \
97  } \
98  } while (false)
99 #define ASSERT_EQUAL(a,b) \
100  do { \
101  if (a != b) { \
102  std::cerr << "("#a" = " << (a) << ") != ("#b" = " << (b) << ")" << \
103  std::endl; \
104  assert(false); \
105  } \
106  } while (false)
107 #define ASSERT_NOTEQUAL(a,b) \
108  do { \
109  if (a == b) { \
110  std::cerr << "("#a" = " << (a) << ") == ("#b" = " << (b) << ")" << \
111  std::endl; \
112  assert(false); \
113  } \
114  } while (false)
115 #define ASSERT_NULL(a) \
116  do { \
117  if (a != nullptr) { \
118  std::cerr << "("#a" = " << (a) << ") != nullptr" << \
119  std::endl; \
120  assert(false); \
121  } \
122  } while (false)
123 #define ASSERT_NOTNULL(a) \
124  do { \
125  if (a == nullptr) { \
126  std::cerr << #a" is null" << \
127  std::endl; \
128  assert(false); \
129  } \
130  } while (false)
131 #define ASSERT_LESS(a,b) \
132  do { \
133  if (a >= b) { \
134  std::cerr << "("#a" = " << (a) << ") !< ("#b" = " << (b) << ")" << \
135  std::endl; \
136  assert(false); \
137  } \
138  } while (false)
139 #define ASSERT_LESSEQUAL(a,b) \
140  do { \
141  if (a > b) { \
142  std::cerr << "("#a" = " << (a) << ") !<= ("#b" = " << (b) << ")" << \
143  std::endl; \
144  assert(false); \
145  } \
146  } while (false)
147 #define ASSERT_GREATER(a,b) \
148  do { \
149  if (a <= b) { \
150  std::cerr << "("#a" = " << (a) << ") !> ("#b" = " << (b) << ")" << \
151  std::endl; \
152  assert(false); \
153  } \
154  } while (false)
155 #define ASSERT_GREATEREQUAL(a,b) \
156  do { \
157  if (a < b) { \
158  std::cerr << "("#a" = " << (a) << ") !>= ("#b" = " << (b) << ")" << \
159  std::endl; \
160  assert(false); \
161  } \
162  } while (false)
163 #define DEBUG_MESSAGE( ...) \
164  sl::debugMessage( __VA_ARGS__ )
165 
166 #else
167 #define ASSERT_TRUE(a)
168 #define ASSERT_FALSE(a)
169 #define ASSERT_EQUAL(a,b)
170 #define ASSERT_NOTEQUAL(a,b)
171 #define ASSERT_NULL(a)
172 #define ASSERT_NOTNULL(a)
173 #define ASSERT_LESS(a,b)
174 #define ASSERT_LESSEQUAL(a,b)
175 #define ASSERT_GREATER(a,b)
176 #define ASSERT_GREATEREQUAL(a,b)
177 #define DEBUG_MESSAGE( ...)
178 
179 #endif
180 
181 #endif
182 
Definition: Alloc.hpp:40