ITK  5.3.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  * http://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 # define EXERCISE_BASIC_OBJECT_METHODS "Replace EXERCISE_BASIC_OBJECT_METHODS with ITK_EXERCISE_BASIC_OBJECT_METHODS"
40 # define TRY_EXPECT_EXCEPTION "Replace TRY_EXPECT_EXCEPTION with ITK_TRY_EXPECT_EXCEPTION"
41 # define TRY_EXPECT_NO_EXCEPTION "Replace TRY_EXPECT_NO_EXCEPTION with ITK_TRY_EXPECT_NO_EXCEPTION"
42 # define TEST_EXPECT_TRUE_STATUS_VALUE "Replace TEST_EXPECT_TRUE_STATUS_VALUE with ITK_TEST_EXPECT_TRUE_STATUS_VALUE"
43 # define TEST_EXPECT_TRUE "Replace TEST_EXPECT_TRUE with ITK_TEST_EXPECT_TRUE"
44 # define TEST_EXPECT_EQUAL_STATUS_VALUE \
45  "Replace TEST_EXPECT_EQUAL_STATUS_VALUE with ITK_TEST_EXPECT_EQUAL_STATUS_VALUE"
46 # define TEST_EXPECT_EQUAL "Replace TEST_EXPECT_EQUAL with ITK_TEST_EXPECT_EQUAL"
47 # define TEST_SET_GET "Replace TEST_SET_GET with ITK_TEST_SET_GET"
48 # define TEST_SET_GET_VALUE "Replace TEST_SET_GET_VALUE with ITK_TEST_SET_GET_VALUE"
49 # define TEST_SET_GET_NULL_VALUE "Replace TEST_SET_GET_NULL_VALUE with ITK_TEST_SET_GET_NULL_VALUE"
50 # define TEST_SET_GET_BOOLEAN "Replace TEST_SET_GET_BOOLEAN with ITK_TEST_SET_GET_BOOLEAN"
51 #else
52 # define EXERCISE_BASIC_OBJECT_METHODS ITK_EXERCISE_BASIC_OBJECT_METHODS
53 # define TRY_EXPECT_EXCEPTION ITK_TRY_EXPECT_EXCEPTION
54 # define TRY_EXPECT_NO_EXCEPTION ITK_TRY_EXPECT_NO_EXCEPTION
55 # define TEST_EXPECT_TRUE_STATUS_VALUE ITK_TEST_EXPECT_TRUE_STATUS_VALUE
56 # define TEST_EXPECT_TRUE ITK_TEST_EXPECT_TRUE
57 # define TEST_EXPECT_EQUAL_STATUS_VALUE ITK_TEST_EXPECT_EQUAL_STATUS_VALUE
58 # define TEST_EXPECT_EQUAL ITK_TEST_EXPECT_EQUAL
59 # define TEST_SET_GET ITK_TEST_SET_GET
60 # define TEST_SET_GET_VALUE ITK_TEST_SET_GET_VALUE
61 # define TEST_SET_GET_NULL_VALUE ITK_TEST_SET_GET_NULL_VALUE
62 # define TEST_SET_GET_BOOLEAN ITK_TEST_SET_GET_BOOLEAN
63 #endif
64 
65 /* clang-format off */
66 #if defined(__GNUC__)
67 #define ITK_EXERCISE_BASIC_OBJECT_METHODS(object, ClassName, SuperclassName) \
68  object->Print(std::cout); \
69  std::cout << "Name of Class = " << object->GetNameOfClass() << std::endl; \
70  if (!std::strcmp(object->GetNameOfClass(), #ClassName)) \
71  { \
72  std::cout << "Class name is correct" << std::endl; \
73  } \
74  else \
75  { \
76  std::cerr << "Class name provided does not match object's NameOfClass" << std::endl; \
77  return EXIT_FAILURE; \
78  } \
79  ITK_MACROEND_NOOP_STATEMENT
80 #else // not GCC
81 #define ITK_EXERCISE_BASIC_OBJECT_METHODS(object, ClassName, SuperclassName) \
82  object->Print(std::cout); \
83  std::cout << "Name of Class = " << object->GetNameOfClass() << std::endl; \
84  std::cout << "Name of Superclass = " << object->Superclass::GetNameOfClass() << std::endl; \
85  if (!std::strcmp(object->GetNameOfClass(), #ClassName)) \
86  { \
87  std::cout << "Class name is correct" << std::endl; \
88  } \
89  else \
90  { \
91  std::cerr << "Class name provided does not match object's NameOfClass" << std::endl; \
92  return EXIT_FAILURE; \
93  } \
94  if (!std::strcmp(object->Superclass::GetNameOfClass(), #SuperclassName)) \
95  { \
96  std::cout << "Superclass name is correct" << std::endl; \
97  } \
98  else \
99  { \
100  std::cerr << "Superclass name provided does not match object's Superclass::NameOfClass" << std::endl; \
101  return EXIT_FAILURE; \
102  } \
103  ITK_MACROEND_NOOP_STATEMENT
104 #endif // GCC
105 /* clang-format on */
106 
107 #define ITK_TRY_EXPECT_EXCEPTION(command) \
108  try \
109  { \
110  std::cout << "Trying " << #command << std::endl; \
111  command; \
112  std::cerr << "Failed to catch expected exception" << std::endl; \
113  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
114  return EXIT_FAILURE; \
115  } \
116  catch (const itk::ExceptionObject & excp) \
117  { \
118  std::cout << "Caught expected exception" << std::endl; \
119  std::cout << excp << std::endl; \
120  } \
121  ITK_MACROEND_NOOP_STATEMENT
122 
123 
124 #define ITK_TRY_EXPECT_NO_EXCEPTION(command) \
125  try \
126  { \
127  std::cout << "Trying " << #command << std::endl; \
128  command; \
129  } \
130  catch (const itk::ExceptionObject & excp) \
131  { \
132  std::cerr << excp << std::endl; \
133  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
134  return EXIT_FAILURE; \
135  } \
136  ITK_MACROEND_NOOP_STATEMENT
137 
138 #define ITK_TEST_EXPECT_TRUE_STATUS_VALUE(command, statusVal) \
139  { \
140  CLANG_PRAGMA_PUSH \
141  CLANG_SUPPRESS_Wfloat_equal bool _ITK_TEST_EXPECT_TRUE_command(command); \
142  CLANG_PRAGMA_POP \
143  if (!(_ITK_TEST_EXPECT_TRUE_command)) \
144  { \
145  std::cerr << "Error in " << #command << std::endl; \
146  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
147  std::cerr << "Expected true" << std::endl; \
148  std::cerr << " but got " << _ITK_TEST_EXPECT_TRUE_command << std::endl; \
149  statusVal = EXIT_FAILURE; \
150  } \
151  } \
152  ITK_MACROEND_NOOP_STATEMENT
153 
154 #define ITK_TEST_EXPECT_TRUE(command) \
155  { \
156  CLANG_PRAGMA_PUSH \
157  CLANG_SUPPRESS_Wfloat_equal bool _ITK_TEST_EXPECT_TRUE_command(command); \
158  CLANG_PRAGMA_POP \
159  if (!(_ITK_TEST_EXPECT_TRUE_command)) \
160  { \
161  std::cerr << "Error in " << #command << std::endl; \
162  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
163  std::cerr << "Expected true" << std::endl; \
164  std::cerr << " but got " << _ITK_TEST_EXPECT_TRUE_command << std::endl; \
165  return EXIT_FAILURE; \
166  } \
167  } \
168  ITK_MACROEND_NOOP_STATEMENT
169 
170 
171 #define ITK_TEST_EXPECT_EQUAL_STATUS_VALUE(lh, rh, statusVal) \
172  { \
173  CLANG_PRAGMA_PUSH \
174  CLANG_SUPPRESS_Wfloat_equal bool _ITK_TEST_EXPECT_EQUAL_result((lh) == (rh)); \
175  CLANG_PRAGMA_POP \
176  if (!(_ITK_TEST_EXPECT_EQUAL_result)) \
177  { \
178  std::cerr << "Error in " << #lh << " == " << #rh << std::endl; \
179  std::cerr << "\tIn " __FILE__ ", line " << __LINE__ << std::endl; \
180  std::cerr << "\tlh: " << (lh) << std::endl; \
181  std::cerr << "\trh: " << (rh) << std::endl; \
182  std::cerr << "Expression is not equal" << std::endl; \
183  statusVal = EXIT_FAILURE; \
184  } \
185  } \
186  ITK_MACROEND_NOOP_STATEMENT
187 
188 #define ITK_TEST_EXPECT_EQUAL(lh, rh) \
189  { \
190  CLANG_PRAGMA_PUSH \
191  CLANG_SUPPRESS_Wfloat_equal bool _ITK_TEST_EXPECT_EQUAL_result((lh) == (rh)); \
192  CLANG_PRAGMA_POP \
193  if (!(_ITK_TEST_EXPECT_EQUAL_result)) \
194  { \
195  std::cerr << "Error in " << #lh << " == " << #rh << std::endl; \
196  std::cerr << "\tIn " __FILE__ ", line " << __LINE__ << std::endl; \
197  std::cerr << "\tlh: " << (lh) << std::endl; \
198  std::cerr << "\trh: " << (rh) << std::endl; \
199  std::cerr << "Expression is not equal" << std::endl; \
200  return EXIT_FAILURE; \
201  } \
202  } \
203  ITK_MACROEND_NOOP_STATEMENT
204 
205 
206 #define ITK_TEST_SET_GET(variable, command) \
207  if (variable != command) \
208  { \
209  std::cerr << "Error in " << #command << std::endl; \
210  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
211  std::cerr << "Expected " << variable.GetPointer() << std::endl; \
212  std::cerr << "but got " << command << std::endl; \
213  return EXIT_FAILURE; \
214  } \
215  ITK_MACROEND_NOOP_STATEMENT
216 
217 
218 #define ITK_TEST_SET_GET_VALUE(variable, command) \
219  CLANG_PRAGMA_PUSH \
220  CLANG_SUPPRESS_Wfloat_equal if (variable != command) CLANG_PRAGMA_POP \
221  { \
222  std::cerr << "Error in " << #command << std::endl; \
223  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
224  std::cerr << "Expected " << variable << std::endl; \
225  std::cerr << "but got " << command << std::endl; \
226  return EXIT_FAILURE; \
227  } \
228  ITK_MACROEND_NOOP_STATEMENT
229 
230 #define ITK_TEST_SET_GET_NULL_VALUE(command) \
231  if (nullptr != command) \
232  { \
233  std::cerr << "Error in " << #command << std::endl; \
234  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
235  std::cerr << "Expected " \
236  << "nullptr" << std::endl; \
237  std::cerr << "but got " << command << std::endl; \
238  return EXIT_FAILURE; \
239  } \
240  ITK_MACROEND_NOOP_STATEMENT
241 
242 #define ITK_TEST_SET_GET_BOOLEAN(object, variable, value) \
243  object->Set##variable(false); \
244  object->Set##variable(true); \
245  if (object->Get##variable() != 1) \
246  { \
247  std::cerr << "Error in Set/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
248  << " instead of 1" << std::endl; \
249  return EXIT_FAILURE; \
250  } \
251  object->Set##variable(false); \
252  if (object->Get##variable() != 0) \
253  { \
254  std::cerr << "Error in Set/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
255  << " instead of 0" << std::endl; \
256  return EXIT_FAILURE; \
257  } \
258  object->variable##On(); \
259  if (object->Get##variable() != 1) \
260  { \
261  std::cerr << "Error in On/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
262  << " instead of 1" << std::endl; \
263  return EXIT_FAILURE; \
264  } \
265  object->variable##Off(); \
266  if (object->Get##variable() != 0) \
267  { \
268  std::cerr << "Error in Off/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
269  << " instead of 0" << std::endl; \
270  return EXIT_FAILURE; \
271  } \
272  object->Set##variable(value)
273 
277 #define itkNameOfTestExecutableMacro(argv) \
278  [argv](const std::string & functionName) { \
279  return ((argv == nullptr) || (argv[0] == nullptr) || (argv[0][0] == '\0')) ? ("<" + functionName + " executable>") \
280  : argv[0]; \
281  }(__func__)
282 
283 #endif
itkMacro.h
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24