CppTest  Version 2.0.0
cpptest-assert.h
Go to the documentation of this file.
1 // ---
2 //
3 // $Id: cpptest-assert.h,v 1.3 2005/06/08 08:08:06 nilu Exp $
4 //
5 // CppTest - A C++ Unit Testing Framework
6 // Copyright (c) 2003 Niklas Lundell
7 // Copyright (c) 2010 Nate Gallaher
8 //
9 // ---
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the
23 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 // Boston, MA 02111-1307, USA.
25 //
26 // ---
27 
30 #ifndef CPPTEST_ASSERT_H
31 #define CPPTEST_ASSERT_H
32 
33 #include <sstream>
34 
59 #define TEST_FAIL(msg) \
60  { \
61  assertment(::Test::Source(__FILE__, __LINE__, (msg) != 0 ? #msg : "")); \
62  if (!continue_after_failure()) return; \
63  }
64 
87 #define TEST_ASSERT(expr) \
88  { \
89  if (!(expr)) \
90  { \
91  assertment(::Test::Source(__FILE__, __LINE__, #expr)); \
92  if (!continue_after_failure()) return; \
93  } \
94  }
95 
107 #define TEST_ASSERT_MSG(expr, msg) \
108  { \
109  if (!(expr)) \
110  { \
111  assertment(::Test::Source(__FILE__, __LINE__, msg)); \
112  if (!continue_after_failure()) return; \
113  } \
114  }
115 
133 #define TEST_ASSERT_EQUALS(expected, got) \
134  { \
135  if (!((got) == (expected))) \
136  { \
137  std::stringstream tmpstream; \
138  tmpstream << "Got " << (got) << ", expected " << (expected);\
139  assertment(::Test::Source(__FILE__, __LINE__, \
140  tmpstream.str().c_str())); \
141  if (!continue_after_failure()) return; \
142  } \
143  }
144 
162 #define TEST_ASSERT_EQUALS_OBJ(expected, got) \
163  { \
164  if (!((got) == (expected))) \
165  { \
166  std::stringstream tmpstream; \
167  tmpstream << #expected << " object not equal to "; \
168  tmpstream << #got << " object."; \
169  assertment(::Test::Source(__FILE__, __LINE__, \
170  tmpstream.str().c_str())); \
171  if (!continue_after_failure()) return; \
172  } \
173  }
174 
190 #define TEST_ASSERT_EQUALS_MSG(expected, got, msg) \
191  { \
192  if (!((got) == (expected))) \
193  { \
194  std::stringstream tmpstream; \
195  tmpstream << (msg) << ": "; \
196  tmpstream << "Got " << (got) << ", expected " << (expected);\
197  assertment(::Test::Source(__FILE__, __LINE__, \
198  tmpstream.str().c_str())); \
199  if (!continue_after_failure()) return; \
200  } \
201  }
202 
216 #define TEST_ASSERT_DELTA(a, b, delta) \
217  { \
218  if (((b) < (a) - (delta)) || ((b) > (a) + (delta))) \
219  { \
220  assertment(::Test::Source(__FILE__, __LINE__, \
221  "delta(" #a ", " #b ", " #delta ")" )); \
222  if (!continue_after_failure()) return; \
223  } \
224  }
225 
240 #define TEST_ASSERT_DELTA_MSG(a, b, delta, msg) \
241  { \
242  if (((b) < (a) - (delta)) || ((b) > (a) + (delta))) \
243  { \
244  assertment(::Test::Source(__FILE__, __LINE__, msg)); \
245  if (!continue_after_failure()) return; \
246  } \
247  }
248 
261 #define TEST_THROWS(expr, x) \
262  { \
263  bool __expected = false; \
264  try { expr; } \
265  catch (x) { __expected = true; } \
266  catch (...) {} \
267  if (!__expected) \
268  { \
269  assertment(::Test::Source(__FILE__, __LINE__, #expr)); \
270  if (!continue_after_failure()) return; \
271  } \
272  }
273 
287 #define TEST_THROWS_MSG(expr, x, msg) \
288  { \
289  bool __expected = false; \
290  try { expr; } \
291  catch (x) { __expected = true; } \
292  catch (...) {} \
293  if (!__expected) \
294  { \
295  assertment(::Test::Source(__FILE__, __LINE__, msg)); \
296  if (!continue_after_failure()) return; \
297  } \
298  }
299 
311 #define TEST_THROWS_ANYTHING(expr) \
312  { \
313  bool __expected = false; \
314  try { expr; } \
315  catch (...) { __expected = true; } \
316  if (!__expected) \
317  { \
318  assertment(::Test::Source(__FILE__, __LINE__, #expr)); \
319  if (!continue_after_failure()) return; \
320  } \
321  }
322 
335 #define TEST_THROWS_ANYTHING_MSG(expr, msg) \
336  { \
337  bool __expected = false; \
338  try { expr; } \
339  catch (...) { __expected = true; } \
340  if (!__expected) \
341  { \
342  assertment(::Test::Source(__FILE__, __LINE__, msg)); \
343  if (!continue_after_failure()) return; \
344  } \
345  }
346 
358 #define TEST_THROWS_NOTHING(expr) \
359  { \
360  bool __expected = true; \
361  try { expr; } \
362  catch (...) { __expected = false; } \
363  if (!__expected) \
364  { \
365  assertment(::Test::Source(__FILE__, __LINE__, #expr)); \
366  if (!continue_after_failure()) return; \
367  } \
368  }
369 
382 #define TEST_THROWS_NOTHING_MSG(expr, msg) \
383  { \
384  bool __expected = true; \
385  try { expr; } \
386  catch (...) { __expected = false; } \
387  if (!__expected) \
388  { \
389  assertment(::Test::Source(__FILE__, __LINE__, msg)); \
390  if (!continue_after_failure()) return; \
391  } \
392  }
393 
422 
423 #endif // #ifndef CPPTEST_ASSERT_H
424