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  CLANG_PRAGMA_PUSH \
147  CLANG_SUPPRESS_Wfloat_equal bool _ITK_TEST_EXPECT_TRUE_command(command); \
148  CLANG_PRAGMA_POP \
149  if (!(_ITK_TEST_EXPECT_TRUE_command)) \
150  { \
151  std::cerr << "Error in " << #command << std::endl; \
152  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
153  std::cerr << "Expected true" << std::endl; \
154  std::cerr << " but got " << _ITK_TEST_EXPECT_TRUE_command << std::endl; \
155  statusVal = EXIT_FAILURE; \
156  } \
157  } \
158  ITK_MACROEND_NOOP_STATEMENT
159 
160 #define ITK_TEST_EXPECT_TRUE(command) \
161  { \
162  CLANG_PRAGMA_PUSH \
163  CLANG_SUPPRESS_Wfloat_equal bool _ITK_TEST_EXPECT_TRUE_command(command); \
164  CLANG_PRAGMA_POP \
165  if (!(_ITK_TEST_EXPECT_TRUE_command)) \
166  { \
167  std::cerr << "Error in " << #command << std::endl; \
168  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
169  std::cerr << "Expected true" << std::endl; \
170  std::cerr << " but got " << _ITK_TEST_EXPECT_TRUE_command << std::endl; \
171  return EXIT_FAILURE; \
172  } \
173  } \
174  ITK_MACROEND_NOOP_STATEMENT
175 
176 
177 #define ITK_TEST_EXPECT_EQUAL_STATUS_VALUE(lh, rh, statusVal) \
178  { \
179  CLANG_PRAGMA_PUSH \
180  CLANG_SUPPRESS_Wfloat_equal bool _ITK_TEST_EXPECT_EQUAL_result((lh) == (rh)); \
181  CLANG_PRAGMA_POP \
182  if (!(_ITK_TEST_EXPECT_EQUAL_result)) \
183  { \
184  std::cerr << "Error in " << #lh << " == " << #rh << std::endl; \
185  std::cerr << "\tIn " __FILE__ ", line " << __LINE__ << std::endl; \
186  std::cerr << "\tlh: " << (lh) << std::endl; \
187  std::cerr << "\trh: " << (rh) << std::endl; \
188  std::cerr << "Expression is not equal" << std::endl; \
189  statusVal = EXIT_FAILURE; \
190  } \
191  } \
192  ITK_MACROEND_NOOP_STATEMENT
193 
194 #define ITK_TEST_EXPECT_EQUAL(lh, rh) \
195  { \
196  CLANG_PRAGMA_PUSH \
197  CLANG_SUPPRESS_Wfloat_equal bool _ITK_TEST_EXPECT_EQUAL_result((lh) == (rh)); \
198  CLANG_PRAGMA_POP \
199  if (!(_ITK_TEST_EXPECT_EQUAL_result)) \
200  { \
201  std::cerr << "Error in " << #lh << " == " << #rh << std::endl; \
202  std::cerr << "\tIn " __FILE__ ", line " << __LINE__ << std::endl; \
203  std::cerr << "\tlh: " << (lh) << std::endl; \
204  std::cerr << "\trh: " << (rh) << std::endl; \
205  std::cerr << "Expression is not equal" << std::endl; \
206  return EXIT_FAILURE; \
207  } \
208  } \
209  ITK_MACROEND_NOOP_STATEMENT
210 
211 
212 #define ITK_TEST_SET_GET(variable, command) \
213  if (variable != command) \
214  { \
215  std::cerr << "Error in " << #command << std::endl; \
216  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
217  std::cerr << "Expected " << variable.GetPointer() << std::endl; \
218  std::cerr << "but got " << command << std::endl; \
219  return EXIT_FAILURE; \
220  } \
221  ITK_MACROEND_NOOP_STATEMENT
222 
223 
224 #define ITK_TEST_SET_GET_VALUE(variable, command) \
225  CLANG_PRAGMA_PUSH \
226  CLANG_SUPPRESS_Wfloat_equal if (variable != command) CLANG_PRAGMA_POP \
227  { \
228  std::cerr << "Error in " << #command << std::endl; \
229  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
230  std::cerr << "Expected " << variable << std::endl; \
231  std::cerr << "but got " << command << std::endl; \
232  return EXIT_FAILURE; \
233  } \
234  ITK_MACROEND_NOOP_STATEMENT
235 
236 #define ITK_TEST_SET_GET_NULL_VALUE(command) \
237  if (nullptr != command) \
238  { \
239  std::cerr << "Error in " << #command << std::endl; \
240  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
241  std::cerr << "Expected " \
242  << "nullptr" << std::endl; \
243  std::cerr << "but got " << command << std::endl; \
244  return EXIT_FAILURE; \
245  } \
246  ITK_MACROEND_NOOP_STATEMENT
247 
248 #define ITK_TEST_SET_GET_BOOLEAN(object, variable, value) \
249  object->Set##variable(false); \
250  object->Set##variable(true); \
251  if (object->Get##variable() != 1) \
252  { \
253  std::cerr << "Error in Set/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
254  << " instead of 1" << std::endl; \
255  return EXIT_FAILURE; \
256  } \
257  object->Set##variable(false); \
258  if (object->Get##variable() != 0) \
259  { \
260  std::cerr << "Error in Set/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
261  << " instead of 0" << std::endl; \
262  return EXIT_FAILURE; \
263  } \
264  object->variable##On(); \
265  if (object->Get##variable() != 1) \
266  { \
267  std::cerr << "Error in On/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
268  << " instead of 1" << std::endl; \
269  return EXIT_FAILURE; \
270  } \
271  object->variable##Off(); \
272  if (object->Get##variable() != 0) \
273  { \
274  std::cerr << "Error in Off/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
275  << " instead of 0" << std::endl; \
276  return EXIT_FAILURE; \
277  } \
278  object->Set##variable(value)
279 
283 #define itkNameOfTestExecutableMacro(argv) \
284  [argv](const std::string & functionName) { \
285  return ((argv == nullptr) || (argv[0] == nullptr) || (argv[0][0] == '\0')) ? ("<" + functionName + " executable>") \
286  : argv[0]; \
287  }(__func__)
288 
289 #endif
itkMacro.h
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24