ctkCoreTestingMacros.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkCoreTestingMacros_h
  15. #define __ctkCoreTestingMacros_h
  16. #include "ctkCoreTestingUtilities.h"
  17. /// Convenience macros for unit tests.
  18. ///
  19. /// The macro returns from the current method with EXIT_FAILURE if the check fails.
  20. /// Expressions can be passed as arguments, they are guaranteed to be executed only once.
  21. ///
  22. /// Example:
  23. ///
  24. /// \code{.cpp}
  25. /// int testedFunction(int a, int b) { return a+b; }
  26. ///
  27. /// int MyTest1(int , char * [])
  28. /// {
  29. ///
  30. /// int current = 40 + 2;
  31. /// int expected = 42;
  32. /// CHECK_INT(current, expected);
  33. ///
  34. /// CHECK_INT(testedFunction(40,2), 42);
  35. /// CHECK_INT(testedFunction(35,5), 40);
  36. ///
  37. /// return EXIT_SUCCESS;
  38. /// }
  39. ///
  40. /// \endcode
  41. /// Verifies that pointer is NULL
  42. #define CHECK_NULL(pointer) \
  43. { \
  44. const void* pointerValue = (pointer); \
  45. if (!ctkCoreTestingUtilities::CheckNull(__LINE__,#pointer " is not NULL", pointerValue)) \
  46. { \
  47. return EXIT_FAILURE; \
  48. } \
  49. }
  50. /// Verifies that pointer is not NULL
  51. #define CHECK_NOT_NULL(pointer) \
  52. { \
  53. if (!ctkCoreTestingUtilities::CheckNotNull(__LINE__,#pointer " is NULL", (pointer))) \
  54. { \
  55. return EXIT_FAILURE; \
  56. } \
  57. }
  58. #define CHECK_EXIT_SUCCESS(actual) \
  59. { \
  60. if (!ctkCoreTestingUtilities::CheckInt(__LINE__,#actual " != EXIT_SUCCESS", (actual), EXIT_SUCCESS)) \
  61. { \
  62. return EXIT_FAILURE; \
  63. } \
  64. }
  65. /// Verifies if actual int value is the same as expected
  66. #define CHECK_INT(actual, expected) \
  67. { \
  68. if (!ctkCoreTestingUtilities::CheckInt(__LINE__,#actual " != " #expected, (actual), (expected))) \
  69. { \
  70. return EXIT_FAILURE; \
  71. } \
  72. }
  73. /// Verifies if actual pointer value is the same as expected
  74. #define CHECK_POINTER(actual, expected) \
  75. { \
  76. if (!ctkCoreTestingUtilities::CheckPointer(__LINE__,#actual " != " #expected, (actual), (expected))) \
  77. { \
  78. return EXIT_FAILURE; \
  79. } \
  80. }
  81. /// Verifies if actual pointer value is the same as expected
  82. #define CHECK_POINTER_DIFFERENT(actual, expected) \
  83. { \
  84. if (!ctkCoreTestingUtilities::CheckPointer(__LINE__,#actual " == " #expected, (actual), (expected), false)) \
  85. { \
  86. return EXIT_FAILURE; \
  87. } \
  88. }
  89. /// Verifies if actual bool value is the same as expected
  90. #define CHECK_BOOL(actual, expected) \
  91. { \
  92. if (!ctkCoreTestingUtilities::CheckInt(__LINE__,#actual " != " #expected, (actual)?1:0, (expected)?1:0)) \
  93. { \
  94. return EXIT_FAILURE; \
  95. } \
  96. }
  97. /// Verifies if actual const char* value is the same as expected.
  98. /// It can handle NULL pointer inputs.
  99. #define CHECK_STRING(actual, expected) \
  100. { \
  101. if (!ctkCoreTestingUtilities::CheckString(__LINE__,#actual " != " #expected, (actual), (expected))) \
  102. { \
  103. return EXIT_FAILURE; \
  104. } \
  105. }
  106. /// Verifies if actual std::string value is the same as expected.
  107. /// It is safe to use for comparing std::string values.
  108. /// It cannot handle NULL pointer inputs.
  109. #define CHECK_STD_STRING(actual, expected) \
  110. { \
  111. std::string a = (actual); \
  112. std::string e = (expected); \
  113. if (!ctkCoreTestingUtilities::CheckString(__LINE__,#actual " != " #expected, a.c_str(), e.c_str())) \
  114. { \
  115. return EXIT_FAILURE; \
  116. } \
  117. }
  118. /// Verifies if actual QString value is the same as expected.
  119. /// It is safe to use for comparing QString values.
  120. /// It cannot handle NULL pointer inputs.
  121. #define CHECK_QSTRING(actual, expected) \
  122. { \
  123. QString a = (actual); \
  124. QString e = (expected); \
  125. if (!ctkCoreTestingUtilities::CheckString(__LINE__,#actual " != " #expected, qPrintable(a), qPrintable(e))) \
  126. { \
  127. return EXIT_FAILURE; \
  128. } \
  129. }
  130. /// Verifies if actual const char* value is not the same as expected.
  131. /// It can handle NULL pointer inputs.
  132. #define CHECK_STRING_DIFFERENT(actual, expected) \
  133. { \
  134. if (!ctkCoreTestingUtilities::CheckString(__LINE__,#actual " != " #expected, (actual), (expected), false)) \
  135. { \
  136. return EXIT_FAILURE; \
  137. } \
  138. }
  139. /// Verifies if actual std::string value is not the same as expected.
  140. /// It is safe to use for comparing std::string values.
  141. /// It cannot handle NULL pointer inputs.
  142. #define CHECK_STD_STRING_DIFFERENT(actual, expected) \
  143. { \
  144. std::string a = (actual); \
  145. std::string e = (expected); \
  146. if (!ctkCoreTestingUtilities::CheckString(__LINE__,#actual " != " #expected, a.c_str(), e.c_str(), false)) \
  147. { \
  148. return EXIT_FAILURE; \
  149. } \
  150. }
  151. /// Verifies if actual QString value is not the same as expected.
  152. /// It is safe to use for comparing QString values.
  153. /// It cannot handle NULL pointer inputs.
  154. #define CHECK_QSTRING_DIFFERENT(actual, expected) \
  155. { \
  156. QString a = (actual); \
  157. QString e = (expected); \
  158. if (!ctkCoreTestingUtilities::CheckString(__LINE__,#actual " != " #expected, qPrintable(a), qPrintable(e), false)) \
  159. { \
  160. return EXIT_FAILURE; \
  161. } \
  162. }
  163. /// Verifies if actual QStringList is the same as expected.
  164. #define CHECK_QSTRINGLIST(actual, expected) \
  165. { \
  166. QStringList a = (actual); \
  167. QStringList e = (expected); \
  168. if (!ctkCoreTestingUtilities::CheckStringList(__LINE__,#actual " != " #expected, a, e)) \
  169. { \
  170. return EXIT_FAILURE; \
  171. } \
  172. }
  173. /// Verifies if actual QVariant is the same as expected.
  174. #define CHECK_QVARIANT(actual, expected) \
  175. { \
  176. QVariant a = (actual); \
  177. QVariant e = (expected); \
  178. if (!ctkCoreTestingUtilities::CheckVariant(__LINE__,#actual " != " #expected, a, e)) \
  179. { \
  180. return EXIT_FAILURE; \
  181. } \
  182. }
  183. #endif