You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

332 lines
9.1KB

  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. //
  8. // ---
  9. //
  10. // This library is free software; you can redistribute it and/or
  11. // modify it under the terms of the GNU Lesser General Public
  12. // License as published by the Free Software Foundation; either
  13. // version 2 of the License, or (at your option) any later version.
  14. //
  15. // This library is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. // Lesser General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Lesser General Public
  21. // License along with this library; if not, write to the
  22. // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. // Boston, MA 02111-1307, USA.
  24. //
  25. // ---
  26. /** \file */
  27. #ifndef CPPTEST_ASSERT_H
  28. #define CPPTEST_ASSERT_H
  29. /// Unconditional failure.
  30. ///
  31. /// Used in conjunction with Test::Suite.
  32. ///
  33. /// \param msg Provided message.
  34. ///
  35. /// \par Example:
  36. /// \code
  37. /// void MySuite::test()
  38. /// {
  39. /// // ...
  40. ///
  41. /// switch (flag)
  42. /// {
  43. /// // handling valid cases ...
  44. ///
  45. /// default:
  46. /// TEST_FAIL("This should not happen")
  47. /// }
  48. /// }
  49. /// \endcode
  50. ///
  51. /// For a description of all asserts, see \ref asserts.
  52. ///
  53. #define TEST_FAIL(msg) \
  54. { \
  55. assertment(::Test::Source(__FILE__, __LINE__, (msg) != 0 ? #msg : "")); \
  56. if (!continue_after_failure()) return; \
  57. }
  58. /// Verify an expression and issues an assertment if it fails.
  59. ///
  60. /// Used in conjunction with Test::Suite.
  61. ///
  62. /// \param expr Expression to test.
  63. ///
  64. /// \see TEST_ASSERT_MSG(expr, msg)
  65. ///
  66. /// \par Example:
  67. /// \code
  68. /// void MySuite::test()
  69. /// {
  70. /// int i;
  71. ///
  72. /// // ...
  73. ///
  74. /// TEST_ASSERT(i == 13)
  75. /// }
  76. /// \endcode
  77. ///
  78. /// For a description of all asserts, see \ref asserts.
  79. ///
  80. #define TEST_ASSERT(expr) \
  81. { \
  82. if (!(expr)) \
  83. { \
  84. assertment(::Test::Source(__FILE__, __LINE__, #expr)); \
  85. if (!continue_after_failure()) return; \
  86. } \
  87. }
  88. /// Verify an expression and issues an assertment if it fails.
  89. ///
  90. /// Used in conjunction with Test::Suite.
  91. ///
  92. /// \param expr Expression to test.
  93. /// \param msg User message.
  94. ///
  95. /// \see TEST_ASSERT(expr)
  96. ///
  97. /// For a description of all asserts, see \ref asserts.
  98. ///
  99. #define TEST_ASSERT_MSG(expr, msg) \
  100. { \
  101. if (!(expr)) \
  102. { \
  103. assertment(::Test::Source(__FILE__, __LINE__, msg)); \
  104. if (!continue_after_failure()) return; \
  105. } \
  106. }
  107. /// Verify that two expressions are equal up to a constant, issues an
  108. /// assertment if it fails.
  109. ///
  110. /// Used in conjunction with Test::Suite.
  111. ///
  112. /// \param a First expression to test.
  113. /// \param b Second expression to test.
  114. /// \param delta Constant.
  115. ///
  116. /// \see TEST_ASSERT_DELTA_MSG(a, b, delta, msg)
  117. ///
  118. /// For a description of all asserts, see \ref asserts.
  119. ///
  120. #define TEST_ASSERT_DELTA(a, b, delta) \
  121. { \
  122. if (((b) < (a) - (delta)) || ((b) > (a) + (delta))) \
  123. { \
  124. assertment(::Test::Source(__FILE__, __LINE__, \
  125. "delta(" #a ", " #b ", " #delta ")" )); \
  126. if (!continue_after_failure()) return; \
  127. } \
  128. }
  129. /// Verify that two expressions are equal up to a constant, issues an
  130. /// assertment if it fails.
  131. ///
  132. /// Used in conjunction with Test::Suite.
  133. ///
  134. /// \param a First expression to test.
  135. /// \param b Second expression to test.
  136. /// \param delta Constant.
  137. /// \param msg User message.
  138. ///
  139. /// \see TEST_ASSERT_DELTA(a, b, delta)
  140. ///
  141. /// For a description of all asserts, see \ref asserts.
  142. ///
  143. #define TEST_ASSERT_DELTA_MSG(a, b, delta, msg) \
  144. { \
  145. if (((b) < (a) - (delta)) || ((b) > (a) + (delta))) \
  146. { \
  147. assertment(::Test::Source(__FILE__, __LINE__, msg)); \
  148. if (!continue_after_failure()) return; \
  149. } \
  150. }
  151. /// Verify an expression and expects an exception in return.
  152. /// An assertment is issued if the exception is not thrown.
  153. ///
  154. /// Used in conjunction with Test::Suite.
  155. ///
  156. /// \param expr Expression to test.
  157. /// \param x Expected exception.
  158. ///
  159. /// \see TEST_THROWS_MSG(expr, x, msg)
  160. ///
  161. /// For a description of all asserts, see \ref asserts.
  162. ///
  163. #define TEST_THROWS(expr, x) \
  164. { \
  165. bool __expected = false; \
  166. try { expr; } \
  167. catch (x) { __expected = true; } \
  168. catch (...) {} \
  169. if (!__expected) \
  170. { \
  171. assertment(::Test::Source(__FILE__, __LINE__, #expr)); \
  172. if (!continue_after_failure()) return; \
  173. } \
  174. }
  175. /// Verify an expression and expects an exception in return.
  176. /// An assertment is issued if the exception is not thrown.
  177. ///
  178. /// Used in conjunction with Test::Suite.
  179. ///
  180. /// \param expr Expression to test.
  181. /// \param x Expected exception.
  182. /// \param msg User message.
  183. ///
  184. /// \see TEST_THROWS(expr, x)
  185. ///
  186. /// For a description of all asserts, see \ref asserts.
  187. ///
  188. #define TEST_THROWS_MSG(expr, x, msg) \
  189. { \
  190. bool __expected = false; \
  191. try { expr; } \
  192. catch (x) { __expected = true; } \
  193. catch (...) {} \
  194. if (!__expected) \
  195. { \
  196. assertment(::Test::Source(__FILE__, __LINE__, msg)); \
  197. if (!continue_after_failure()) return; \
  198. } \
  199. }
  200. /// Verify an expression and expects any exception in return.
  201. /// An assertment is issued if no exception is thrown.
  202. ///
  203. /// Used in conjunction with Test::Suite.
  204. ///
  205. /// \param expr Expression to test.
  206. ///
  207. /// \see TEST_THROWS_ANYTHING_MSG(expr, msg)
  208. ///
  209. /// For a description of all asserts, see \ref asserts.
  210. ///
  211. #define TEST_THROWS_ANYTHING(expr) \
  212. { \
  213. bool __expected = false; \
  214. try { expr; } \
  215. catch (...) { __expected = true; } \
  216. if (!__expected) \
  217. { \
  218. assertment(::Test::Source(__FILE__, __LINE__, #expr)); \
  219. if (!continue_after_failure()) return; \
  220. } \
  221. }
  222. /// Verify an expression and expects any exception in return.
  223. /// An assertment is issued if no exception is thrown.
  224. ///
  225. /// Used in conjunction with Test::Suite.
  226. ///
  227. /// \param expr Expression to test.
  228. /// \param msg User message.
  229. ///
  230. /// \see TEST_THROWS_ANYTHING(expr)
  231. ///
  232. /// For a description of all asserts, see \ref asserts.
  233. ///
  234. #define TEST_THROWS_ANYTHING_MSG(expr, msg) \
  235. { \
  236. bool __expected = false; \
  237. try { expr; } \
  238. catch (...) { __expected = true; } \
  239. if (!__expected) \
  240. { \
  241. assertment(::Test::Source(__FILE__, __LINE__, msg)); \
  242. if (!continue_after_failure()) return; \
  243. } \
  244. }
  245. /// Verify an expression and expects no exception in return.
  246. /// An assertment is issued if any exception is thrown.
  247. ///
  248. /// Used in conjunction with Test::Suite.
  249. ///
  250. /// \param expr Expression to test.
  251. ///
  252. /// \see TEST_THROWS_NOTHING_MSG(expr, msg)
  253. ///
  254. /// For a description of all asserts, see \ref asserts.
  255. ///
  256. #define TEST_THROWS_NOTHING(expr) \
  257. { \
  258. bool __expected = true; \
  259. try { expr; } \
  260. catch (...) { __expected = false; } \
  261. if (!__expected) \
  262. { \
  263. assertment(::Test::Source(__FILE__, __LINE__, #expr)); \
  264. if (!continue_after_failure()) return; \
  265. } \
  266. }
  267. /// Verify an expression and expects no exception in return.
  268. /// An assertment is issued if any exception is thrown.
  269. ///
  270. /// Used in conjunction with Test::Suite.
  271. ///
  272. /// \param expr Expression to test.
  273. /// \param msg User message.
  274. ///
  275. /// \see TEST_THROWS_NOTHING(expr)
  276. ///
  277. /// For a description of all asserts, see \ref asserts.
  278. ///
  279. #define TEST_THROWS_NOTHING_MSG(expr, msg) \
  280. { \
  281. bool __expected = true; \
  282. try { expr; } \
  283. catch (...) { __expected = false; } \
  284. if (!__expected) \
  285. { \
  286. assertment(::Test::Source(__FILE__, __LINE__, msg)); \
  287. if (!continue_after_failure()) return; \
  288. } \
  289. }
  290. /// \page asserts Available asserts
  291. ///
  292. /// Normally, assert macros issues an assert if the given expression, if any,
  293. /// fails (as defined by the macro). Assertments include information about the
  294. /// current test suite, test function, source file, source file line, and a
  295. /// message. The message is normally the offending expression, however, for
  296. /// macros ending in _MSG it is possibly to supply a user defined message.
  297. ///
  298. /// <b>General asserts</b>
  299. /// - TEST_FAIL(msg)
  300. ///
  301. /// <b>Comparision asserts</b>
  302. /// - TEST_ASSERT(expr)
  303. /// - TEST_ASSERT_MSG(expr, msg)
  304. /// - TEST_ASSERT_DELTA(a, b, delta)
  305. /// - TEST_ASSERT_DELTA_MSG(a, b, delta, msg)
  306. ///
  307. /// <b>Exception asserts</b>
  308. /// - TEST_THROWS(expr, x)
  309. /// - TEST_THROWS_MSG(expr, x, msg)
  310. /// - TEST_THROWS_ANYTHING(expr)
  311. /// - TEST_THROWS_ANYTHING_MSG(expr, msg)
  312. /// - TEST_THROWS_NOTHING(expr)
  313. /// - TEST_THROWS_NOTHING_MSG(expr, msg)
  314. ///
  315. #endif // #ifndef CPPTEST_ASSERT_H