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