Commit patch to not break on spaces.
[bowtie.git] / assert_helpers.h
1 #ifndef ASSERT_HELPERS_H_
2 #define ASSERT_HELPERS_H_
3
4 #include <stdexcept>
5 #include <string>
6 #include <cassert>
7
8 /**
9  * Assertion for release-enabled assertions
10  */
11 class ReleaseAssertException : public std::runtime_error {
12 public:
13         ReleaseAssertException(const std::string& msg = "") : std::runtime_error(msg) {}
14 };
15
16 /**
17  * Macros for release-enabled assertions, and helper macros to make
18  * all assertion error messages more helpful.
19  */
20 #ifndef NDEBUG
21 #define ASSERT_ONLY(x...) x
22 #else
23 #define ASSERT_ONLY(x...)
24 #endif
25
26 #define rt_assert(b)  \
27         if(!(b)) { \
28                 std::cout << "rt_assert at " << __FILE__ << ":" << __LINE__ << std::endl; \
29                 throw ReleaseAssertException(); \
30         }
31 #define rt_assert_msg(b,msg)  \
32         if(!(b)) { \
33                 std::cout << msg <<  " at " << __FILE__ << ":" << __LINE__ << std::endl; \
34                 throw ReleaseAssertException(msg); \
35         }
36
37 #define rt_assert_eq(ex,ac)  \
38         if(!((ex) == (ac))) { \
39                 std::cout << "rt_assert_eq: expected (" << (ex) << ", 0x" << std::hex << (ex) << std::dec << ") got (" << (ac) << ", 0x" << std::hex << (ac) << std::dec << ")" << std::endl; \
40                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
41                 throw ReleaseAssertException(); \
42         }
43 #define rt_assert_eq_msg(ex,ac,msg)  \
44         if(!((ex) == (ac))) { \
45                 std::cout << "rt_assert_eq: " << msg <<  ": (" << (ex) << ", 0x" << std::hex << (ex) << std::dec << ") got (" << (ac) << ", 0x" << std::hex << (ac) << std::dec << ")" << std::endl; \
46                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
47                 throw ReleaseAssertException(msg); \
48         }
49
50 #ifndef NDEBUG
51 #define assert_eq(ex,ac)  \
52         if(!((ex) == (ac))) { \
53                 std::cout << "assert_eq: expected (" << (ex) << ", 0x" << std::hex << (ex) << std::dec << ") got (" << (ac) << ", 0x" << std::hex << (ac) << std::dec << ")" << std::endl; \
54                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
55                 assert(0); \
56         }
57 #define assert_eq_msg(ex,ac,msg)  \
58         if(!((ex) == (ac))) { \
59                 std::cout << "assert_eq: " << msg <<  ": (" << (ex) << ", 0x" << std::hex << (ex) << std::dec << ") got (" << (ac) << ", 0x" << std::hex << (ac) << std::dec << ")" << std::endl; \
60                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
61                 assert(0); \
62         }
63 #else
64 #define assert_eq(ex,ac)
65 #define assert_eq_msg(ex,ac,msg)
66 #endif
67
68 #define rt_assert_neq(ex,ac)  \
69         if(!((ex) != (ac))) { \
70                 std::cout << "rt_assert_neq: expected not (" << (ex) << ", 0x" << std::hex << (ex) << std::dec << ") got (" << (ac) << ", 0x" << std::hex << (ac) << std::dec << ")" << std::endl; \
71                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
72                 throw ReleaseAssertException(); \
73         }
74 #define rt_assert_neq_msg(ex,ac,msg)  \
75         if(!((ex) != (ac))) { \
76                 std::cout << "rt_assert_neq: " << msg << ": (" << (ex) << ", 0x" << std::hex << (ex) << std::dec << ") got (" << (ac) << ", 0x" << std::hex << (ac) << std::dec << ")" << std::endl; \
77                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
78                 throw ReleaseAssertException(msg); \
79         }
80
81 #ifndef NDEBUG
82 #define assert_neq(ex,ac)  \
83         if(!((ex) != (ac))) { \
84                 std::cout << "assert_neq: expected not (" << (ex) << ", 0x" << std::hex << (ex) << std::dec << ") got (" << (ac) << ", 0x" << std::hex << (ac) << std::dec << ")" << std::endl; \
85                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
86                 assert(0); \
87         }
88 #define assert_neq_msg(ex,ac,msg)  \
89         if(!((ex) != (ac))) { \
90                 std::cout << "assert_neq: " << msg << ": (" << (ex) << ", 0x" << std::hex << (ex) << std::dec << ") got (" << (ac) << ", 0x" << std::hex << (ac) << std::dec << ")" << std::endl; \
91                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
92                 assert(0); \
93         }
94 #else
95 #define assert_neq(ex,ac)
96 #define assert_neq_msg(ex,ac,msg)
97 #endif
98
99 #define rt_assert_gt(a,b) \
100         if(!((a) > (b))) { \
101                 std::cout << "rt_assert_gt: expected (" << (a) << ") > (" << (b) << ")" << std::endl; \
102                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
103                 throw ReleaseAssertException(); \
104         }
105 #define rt_assert_gt_msg(a,b,msg) \
106         if(!((a) > (b))) { \
107                 std::cout << "rt_assert_gt: " << msg << ": (" << (a) << ") > (" << (b) << ")" << std::endl; \
108                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
109                 throw ReleaseAssertException(msg); \
110         }
111
112 #ifndef NDEBUG
113 #define assert_gt(a,b) \
114         if(!((a) > (b))) { \
115                 std::cout << "assert_gt: expected (" << (a) << ") > (" << (b) << ")" << std::endl; \
116                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
117                 assert(0); \
118         }
119 #define assert_gt_msg(a,b,msg) \
120         if(!((a) > (b))) { \
121                 std::cout << "assert_gt: " << msg << ": (" << (a) << ") > (" << (b) << ")" << std::endl; \
122                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
123                 assert(0); \
124         }
125 #else
126 #define assert_gt(a,b)
127 #define assert_gt_msg(a,b,msg)
128 #endif
129
130 #define rt_assert_geq(a,b) \
131         if(!((a) >= (b))) { \
132                 std::cout << "rt_assert_geq: expected (" << (a) << ") >= (" << (b) << ")" << std::endl; \
133                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
134                 throw ReleaseAssertException(); \
135         }
136 #define rt_assert_geq_msg(a,b,msg) \
137         if(!((a) >= (b))) { \
138                 std::cout << "rt_assert_geq: " << msg << ": (" << (a) << ") >= (" << (b) << ")" << std::endl; \
139                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
140                 throw ReleaseAssertException(msg); \
141         }
142
143 #ifndef NDEBUG
144 #define assert_geq(a,b) \
145         if(!((a) >= (b))) { \
146                 std::cout << "assert_geq: expected (" << (a) << ") >= (" << (b) << ")" << std::endl; \
147                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
148                 assert(0); \
149         }
150 #define assert_geq_msg(a,b,msg) \
151         if(!((a) >= (b))) { \
152                 std::cout << "assert_geq: " << msg << ": (" << (a) << ") >= (" << (b) << ")" << std::endl; \
153                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
154                 assert(0); \
155         }
156 #else
157 #define assert_geq(a,b)
158 #define assert_geq_msg(a,b,msg)
159 #endif
160
161 #define rt_assert_lt(a,b) \
162         if(!(a < b)) { \
163                 std::cout << "rt_assert_lt: expected (" << a << ") < (" << b << ")" << std::endl; \
164                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
165                 throw ReleaseAssertException(); \
166         }
167 #define rt_assert_lt_msg(a,b,msg) \
168         if(!(a < b)) { \
169                 std::cout << "rt_assert_lt: " << msg << ": (" << a << ") < (" << b << ")" << std::endl; \
170                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
171                 throw ReleaseAssertException(msg); \
172         }
173
174 #ifndef NDEBUG
175 #define assert_lt(a,b) \
176         if(!(a < b)) { \
177                 std::cout << "assert_lt: expected (" << a << ") < (" << b << ")" << std::endl; \
178                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
179                 assert(0); \
180         }
181 #define assert_lt_msg(a,b,msg) \
182         if(!(a < b)) { \
183                 std::cout << "assert_lt: " << msg << ": (" << a << ") < (" << b << ")" << std::endl; \
184                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
185                 assert(0); \
186         }
187 #else
188 #define assert_lt(a,b)
189 #define assert_lt_msg(a,b,msg)
190 #endif
191
192 #define rt_assert_leq(a,b) \
193         if(!((a) <= (b))) { \
194                 std::cout << "rt_assert_leq: expected (" << (a) << ") <= (" << (b) << ")" << std::endl; \
195                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
196                 throw ReleaseAssertException(); \
197         }
198 #define rt_assert_leq_msg(a,b,msg) \
199         if(!((a) <= (b))) { \
200                 std::cout << "rt_assert_leq: " << msg << ": (" << (a) << ") <= (" << (b) << ")" << std::endl; \
201                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
202                 throw ReleaseAssertException(msg); \
203         }
204
205 #ifndef NDEBUG
206 #define assert_leq(a,b) \
207         if(!((a) <= (b))) { \
208                 std::cout << "assert_leq: expected (" << (a) << ") <= (" << (b) << ")" << std::endl; \
209                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
210                 assert(0); \
211         }
212 #define assert_leq_msg(a,b,msg) \
213         if(!((a) <= (b))) { \
214                 std::cout << "assert_leq: " << msg << ": (" << (a) << ") <= (" << (b) << ")" << std::endl; \
215                 std::cout << __FILE__ << ":" << __LINE__ << std::endl; \
216                 assert(0); \
217         }
218 #else
219 #define assert_leq(a,b)
220 #define assert_leq_msg(a,b,msg)
221 #endif
222
223 #ifndef NDEBUG
224 #define assert_in(c, s) assert_in2(c, s, __FILE__, __LINE__)
225 static inline void assert_in2(char c, const char *str, const char *file, int line) {
226         const char *s = str;
227         while(*s != '\0') {
228                 if(c == *s) return;
229                 s++;
230         }
231         std::cout << "assert_in: (" << c << ") not in  (" << str << ")" << std::endl;
232         std::cout << file << ":" << line << std::endl;
233         assert(0);
234 }
235 #else
236 #define assert_in(c, s)
237 #endif
238
239 #ifndef NDEBUG
240 #define assert_range(b, e, v) assert_range_helper(b, e, v, __FILE__, __LINE__)
241 template<typename T>
242 inline static void assert_range_helper(const T& begin,
243                                        const T& end,
244                                        const T& val,
245                                        const char *file,
246                                        int line)
247 {
248         if(val < begin || val > end) {
249                 std::cout << "assert_range: (" << val << ") not in  ["
250                           << begin << ", " << end << "]" << std::endl;
251                 std::cout << file << ":" << line << std::endl;
252                 assert(0);
253         }
254 }
255 #else
256 #define assert_range(b, e, v)
257 #endif
258
259 #endif /*ASSERT_HELPERS_H_*/