ITK  5.4.0
Insight Toolkit
itkTestingMacros.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 
19 #ifndef itkTestingMacros_h
20 #define itkTestingMacros_h
21 
22 #include "itkMacro.h"
23 
24 #include <cstring>
25 
30 namespace itk
31 {
32 // end namespace itk - this is here for documentation purposes
33 }
34 
35 // DEPRECATED: These macros are left here for compatibility.
36 // In the future, they will be removed in favor of the "ITK_" prefixed
37 // versions.
38 #if defined(ITK_FUTURE_LEGACY_REMOVE)
39 
40 # if defined(__clang__) || defined(__GNUC__)
41 # pragma GCC poison EXERCISE_BASIC_OBJECT_METHODS
42 # pragma GCC poison TRY_EXPECT_EXCEPTION
43 # pragma GCC poison TRY_EXPECT_NO_EXCEPTION
44 # pragma GCC poison TEST_EXPECT_TRUE_STATUS_VALUE
45 # pragma GCC poison TEST_EXPECT_TRUE
46 # pragma GCC poison TEST_EXPECT_EQUAL_STATUS_VALUE
47 # pragma GCC poison TEST_EXPECT_EQUAL
48 # pragma GCC poison TEST_SET_GET
49 # pragma GCC poison TEST_SET_GET_VALUE
50 # pragma GCC poison TEST_SET_GET_NULL_VALUE
51 # pragma GCC poison TEST_SET_GET_BOOLEAN
52 # else
53 # define EXERCISE_BASIC_OBJECT_METHODS "Replace EXERCISE_BASIC_OBJECT_METHODS with ITK_EXERCISE_BASIC_OBJECT_METHODS"
54 # define TRY_EXPECT_EXCEPTION "Replace TRY_EXPECT_EXCEPTION with ITK_TRY_EXPECT_EXCEPTION"
55 # define TRY_EXPECT_NO_EXCEPTION "Replace TRY_EXPECT_NO_EXCEPTION with ITK_TRY_EXPECT_NO_EXCEPTION"
56 # define TEST_EXPECT_TRUE_STATUS_VALUE "Replace TEST_EXPECT_TRUE_STATUS_VALUE with ITK_TEST_EXPECT_TRUE_STATUS_VALUE"
57 # define TEST_EXPECT_TRUE "Replace TEST_EXPECT_TRUE with ITK_TEST_EXPECT_TRUE"
58 # define TEST_EXPECT_EQUAL_STATUS_VALUE \
59  "Replace TEST_EXPECT_EQUAL_STATUS_VALUE with ITK_TEST_EXPECT_EQUAL_STATUS_VALUE"
60 # define TEST_EXPECT_EQUAL "Replace TEST_EXPECT_EQUAL with ITK_TEST_EXPECT_EQUAL"
61 # define TEST_SET_GET "Replace TEST_SET_GET with ITK_TEST_SET_GET"
62 # define TEST_SET_GET_VALUE "Replace TEST_SET_GET_VALUE with ITK_TEST_SET_GET_VALUE"
63 # define TEST_SET_GET_NULL_VALUE "Replace TEST_SET_GET_NULL_VALUE with ITK_TEST_SET_GET_NULL_VALUE"
64 # define TEST_SET_GET_BOOLEAN "Replace TEST_SET_GET_BOOLEAN with ITK_TEST_SET_GET_BOOLEAN"
65 # endif
66 #else
67 # define EXERCISE_BASIC_OBJECT_METHODS ITK_EXERCISE_BASIC_OBJECT_METHODS
68 # define TRY_EXPECT_EXCEPTION ITK_TRY_EXPECT_EXCEPTION
69 # define TRY_EXPECT_NO_EXCEPTION ITK_TRY_EXPECT_NO_EXCEPTION
70 # define TEST_EXPECT_TRUE_STATUS_VALUE ITK_TEST_EXPECT_TRUE_STATUS_VALUE
71 # define TEST_EXPECT_TRUE ITK_TEST_EXPECT_TRUE
72 # define TEST_EXPECT_EQUAL_STATUS_VALUE ITK_TEST_EXPECT_EQUAL_STATUS_VALUE
73 # define TEST_EXPECT_EQUAL ITK_TEST_EXPECT_EQUAL
74 # define TEST_SET_GET ITK_TEST_SET_GET
75 # define TEST_SET_GET_VALUE ITK_TEST_SET_GET_VALUE
76 # define TEST_SET_GET_NULL_VALUE ITK_TEST_SET_GET_NULL_VALUE
77 # define TEST_SET_GET_BOOLEAN ITK_TEST_SET_GET_BOOLEAN
78 #endif
79 
80 /* clang-format off */
81 #if defined(__GNUC__)
82 #define ITK_EXERCISE_BASIC_OBJECT_METHODS(object, ClassName, SuperclassName) \
83  object->Print(std::cout); \
84  std::cout << "Name of Class = " << object->GetNameOfClass() << std::endl; \
85  ITK_MACROEND_NOOP_STATEMENT
86 #else // not GCC
87 #define ITK_EXERCISE_BASIC_OBJECT_METHODS(object, ClassName, SuperclassName) \
88  object->Print(std::cout); \
89  std::cout << "Name of Class = " << object->Self::GetNameOfClass() << std::endl; \
90  std::cout << "Name of Superclass = " << object->Superclass::GetNameOfClass() << std::endl; \
91  if (!std::strcmp(object->Self::GetNameOfClass(), #ClassName)) \
92  { \
93  std::cout << "Class name is correct" << std::endl; \
94  } \
95  else \
96  { \
97  std::cerr << "Class name provided does not match object's NameOfClass" << std::endl; \
98  return EXIT_FAILURE; \
99  } \
100  if (!std::strcmp(object->Superclass::GetNameOfClass(), #SuperclassName)) \
101  { \
102  std::cout << "Superclass name is correct" << std::endl; \
103  } \
104  else \
105  { \
106  std::cerr << "Superclass name provided does not match object's Superclass::NameOfClass" << std::endl; \
107  return EXIT_FAILURE; \
108  } \
109  ITK_MACROEND_NOOP_STATEMENT
110 #endif // GCC
111 /* clang-format on */
112 
113 #define ITK_TRY_EXPECT_EXCEPTION(command) \
114  try \
115  { \
116  std::cout << "Trying " << #command << std::endl; \
117  command; \
118  std::cerr << "Failed to catch expected exception" << std::endl; \
119  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
120  return EXIT_FAILURE; \
121  } \
122  catch (const itk::ExceptionObject & excp) \
123  { \
124  std::cout << "Caught expected exception" << std::endl; \
125  std::cout << excp << std::endl; \
126  } \
127  ITK_MACROEND_NOOP_STATEMENT
128 
129 
130 #define ITK_TRY_EXPECT_NO_EXCEPTION(command) \
131  try \
132  { \
133  std::cout << "Trying " << #command << std::endl; \
134  command; \
135  } \
136  catch (const itk::ExceptionObject & excp) \
137  { \
138  std::cerr << excp << std::endl; \
139  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
140  return EXIT_FAILURE; \
141  } \
142  ITK_MACROEND_NOOP_STATEMENT
143 
144 #define ITK_TEST_EXPECT_TRUE_STATUS_VALUE(command, statusVal) \
145  { \
146  ITK_GCC_PRAGMA_PUSH \
147  ITK_GCC_SUPPRESS_Wfloat_equal \
148  bool _ITK_TEST_EXPECT_TRUE_command(command); \
149  ITK_GCC_PRAGMA_POP \
150  if (!(_ITK_TEST_EXPECT_TRUE_command)) \
151  { \
152  std::cerr << "Error in " << #command << std::endl; \
153  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
154  std::cerr << "Expected true" << std::endl; \
155  std::cerr << " but got " << _ITK_TEST_EXPECT_TRUE_command << std::endl; \
156  statusVal = EXIT_FAILURE; \
157  } \
158  } \
159  ITK_MACROEND_NOOP_STATEMENT
160 
161 #define ITK_TEST_EXPECT_TRUE(command) \
162  { \
163  ITK_GCC_PRAGMA_PUSH \
164  ITK_GCC_SUPPRESS_Wfloat_equal \
165  bool _ITK_TEST_EXPECT_TRUE_command(command); \
166  ITK_GCC_PRAGMA_POP \
167  if (!(_ITK_TEST_EXPECT_TRUE_command)) \
168  { \
169  std::cerr << "Error in " << #command << std::endl; \
170  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
171  std::cerr << "Expected true" << std::endl; \
172  std::cerr << " but got " << _ITK_TEST_EXPECT_TRUE_command << std::endl; \
173  return EXIT_FAILURE; \
174  } \
175  } \
176  ITK_MACROEND_NOOP_STATEMENT
177 
178 
179 #define ITK_TEST_EXPECT_EQUAL_STATUS_VALUE(lh, rh, statusVal) \
180  { \
181  ITK_GCC_PRAGMA_PUSH \
182  ITK_GCC_SUPPRESS_Wfloat_equal \
183  bool _ITK_TEST_EXPECT_EQUAL_result((lh) == (rh)); \
184  ITK_GCC_PRAGMA_POP \
185  if (!(_ITK_TEST_EXPECT_EQUAL_result)) \
186  { \
187  std::cerr << "Error in " << #lh << " == " << #rh << std::endl; \
188  std::cerr << "\tIn " __FILE__ ", line " << __LINE__ << std::endl; \
189  std::cerr << "\tlh: " << (lh) << std::endl; \
190  std::cerr << "\trh: " << (rh) << std::endl; \
191  std::cerr << "Expression is not equal" << std::endl; \
192  statusVal = EXIT_FAILURE; \
193  } \
194  } \
195  ITK_MACROEND_NOOP_STATEMENT
196 
197 #define ITK_TEST_EXPECT_EQUAL(lh, rh) \
198  { \
199  ITK_GCC_PRAGMA_PUSH \
200  ITK_GCC_SUPPRESS_Wfloat_equal \
201  bool _ITK_TEST_EXPECT_EQUAL_result((lh) == (rh)); \
202  ITK_GCC_PRAGMA_POP \
203  if (!(_ITK_TEST_EXPECT_EQUAL_result)) \
204  { \
205  std::cerr << "Error in " << #lh << " == " << #rh << std::endl; \
206  std::cerr << "\tIn " __FILE__ ", line " << __LINE__ << std::endl; \
207  std::cerr << "\tlh: " << (lh) << std::endl; \
208  std::cerr << "\trh: " << (rh) << std::endl; \
209  std::cerr << "Expression is not equal" << std::endl; \
210  return EXIT_FAILURE; \
211  } \
212  } \
213  ITK_MACROEND_NOOP_STATEMENT
214 
215 
216 #define ITK_TEST_SET_GET(variable, command) \
217  if (variable != command) \
218  { \
219  std::cerr << "Error in " << #command << std::endl; \
220  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
221  std::cerr << "Expected " << variable.GetPointer() << std::endl; \
222  std::cerr << "but got " << command << std::endl; \
223  return EXIT_FAILURE; \
224  } \
225  ITK_MACROEND_NOOP_STATEMENT
226 
227 
228 #define ITK_TEST_SET_GET_VALUE(variable, command) \
229  ITK_GCC_PRAGMA_PUSH \
230  ITK_GCC_SUPPRESS_Wfloat_equal \
231  if (variable != command) \
232  { \
233  std::cerr << "Error in " << #command << std::endl; \
234  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
235  std::cerr << "Expected " << variable << std::endl; \
236  std::cerr << "but got " << command << std::endl; \
237  return EXIT_FAILURE; \
238  } \
239  ITK_GCC_PRAGMA_POP \
240  ITK_MACROEND_NOOP_STATEMENT
241 
242 #define ITK_TEST_SET_GET_NULL_VALUE(command) \
243  if (nullptr != command) \
244  { \
245  std::cerr << "Error in " << #command << std::endl; \
246  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
247  std::cerr << "Expected " \
248  << "nullptr" << std::endl; \
249  std::cerr << "but got " << command << std::endl; \
250  return EXIT_FAILURE; \
251  } \
252  ITK_MACROEND_NOOP_STATEMENT
253 
254 #define ITK_TEST_SET_GET_BOOLEAN(object, variable, value) \
255  object->Set##variable(false); \
256  object->Set##variable(true); \
257  if (object->Get##variable() != 1) \
258  { \
259  std::cerr << "Error in Set/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
260  << " instead of 1" << std::endl; \
261  return EXIT_FAILURE; \
262  } \
263  object->Set##variable(false); \
264  if (object->Get##variable() != 0) \
265  { \
266  std::cerr << "Error in Set/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
267  << " instead of 0" << std::endl; \
268  return EXIT_FAILURE; \
269  } \
270  object->variable##On(); \
271  if (object->Get##variable() != 1) \
272  { \
273  std::cerr << "Error in On/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
274  << " instead of 1" << std::endl; \
275  return EXIT_FAILURE; \
276  } \
277  object->variable##Off(); \
278  if (object->Get##variable() != 0) \
279  { \
280  std::cerr << "Error in Off/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
281  << " instead of 0" << std::endl; \
282  return EXIT_FAILURE; \
283  } \
284  object->Set##variable(value)
285 
289 #define itkNameOfTestExecutableMacro(argv) \
290  [argv](const std::string & functionName) { \
291  return ((argv == nullptr) || (argv[0] == nullptr) || (argv[0][0] == '\0')) ? ("<" + functionName + " executable>") \
292  : argv[0]; \
293  }(__func__)
294 
295 #endif
itkMacro.h
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24