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