ITK  5.1.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 // object's Class must be specified to build on sun studio
66 #define ITK_EXERCISE_BASIC_OBJECT_METHODS(object, Class, SuperClass) \
67  object->Print(std::cout); \
68  std::cout << "Name of Class = " << object->GetNameOfClass() << std::endl; \
69  std::cout << "Name of Superclass = " << object->Superclass::GetNameOfClass() << std::endl; \
70  if (!std::strcmp(object->GetNameOfClass(), #Class)) \
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  if (!std::strcmp(object->Superclass::GetNameOfClass(), #SuperClass)) \
80  { \
81  std::cout << "Superclass name is correct" << std::endl; \
82  } \
83  else \
84  { \
85  std::cerr << "Superclass name provided does not match object's Superclass::NameOfClass" << std::endl; \
86  return EXIT_FAILURE; \
87  }
88 
89 #define ITK_TRY_EXPECT_EXCEPTION(command) \
90  try \
91  { \
92  std::cout << "Trying " << #command << std::endl; \
93  command; \
94  std::cerr << "Failed to catch expected exception" << std::endl; \
95  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
96  return EXIT_FAILURE; \
97  } \
98  catch (const itk::ExceptionObject & excp) \
99  { \
100  std::cout << "Caught expected exception" << std::endl; \
101  std::cout << excp << std::endl; \
102  }
103 
104 
105 #define ITK_TRY_EXPECT_NO_EXCEPTION(command) \
106  try \
107  { \
108  std::cout << "Trying " << #command << std::endl; \
109  command; \
110  } \
111  catch (const itk::ExceptionObject & excp) \
112  { \
113  std::cerr << excp << std::endl; \
114  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
115  return EXIT_FAILURE; \
116  }
117 
118 #define ITK_TEST_EXPECT_TRUE_STATUS_VALUE(command, statusVal) \
119  { \
120  CLANG_PRAGMA_PUSH \
121  CLANG_SUPPRESS_Wfloat_equal bool _ITK_TEST_EXPECT_TRUE_command(command); \
122  CLANG_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 
133 #define ITK_TEST_EXPECT_TRUE(command) \
134  { \
135  CLANG_PRAGMA_PUSH \
136  CLANG_SUPPRESS_Wfloat_equal bool _ITK_TEST_EXPECT_TRUE_command(command); \
137  CLANG_PRAGMA_POP \
138  if (!(_ITK_TEST_EXPECT_TRUE_command)) \
139  { \
140  std::cerr << "Error in " << #command << std::endl; \
141  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
142  std::cerr << "Expected true" << std::endl; \
143  std::cerr << " but got " << _ITK_TEST_EXPECT_TRUE_command << std::endl; \
144  return EXIT_FAILURE; \
145  } \
146  }
147 
148 
149 #define ITK_TEST_EXPECT_EQUAL_STATUS_VALUE(lh, rh, statusVal) \
150  { \
151  CLANG_PRAGMA_PUSH \
152  CLANG_SUPPRESS_Wfloat_equal bool _ITK_TEST_EXPECT_EQUAL_result((lh) == (rh)); \
153  CLANG_PRAGMA_POP \
154  if (!(_ITK_TEST_EXPECT_EQUAL_result)) \
155  { \
156  std::cerr << "Error in " << #lh << " == " << #rh << std::endl; \
157  std::cerr << "\tIn " __FILE__ ", line " << __LINE__ << std::endl; \
158  std::cerr << "\tlh: " << (lh) << std::endl; \
159  std::cerr << "\trh: " << (rh) << std::endl; \
160  std::cerr << "Expression is not equal" << std::endl; \
161  statusVal = EXIT_FAILURE; \
162  } \
163  }
164 
165 #define ITK_TEST_EXPECT_EQUAL(lh, rh) \
166  { \
167  CLANG_PRAGMA_PUSH \
168  CLANG_SUPPRESS_Wfloat_equal bool _ITK_TEST_EXPECT_EQUAL_result((lh) == (rh)); \
169  CLANG_PRAGMA_POP \
170  if (!(_ITK_TEST_EXPECT_EQUAL_result)) \
171  { \
172  std::cerr << "Error in " << #lh << " == " << #rh << std::endl; \
173  std::cerr << "\tIn " __FILE__ ", line " << __LINE__ << std::endl; \
174  std::cerr << "\tlh: " << (lh) << std::endl; \
175  std::cerr << "\trh: " << (rh) << std::endl; \
176  std::cerr << "Expression is not equal" << std::endl; \
177  return EXIT_FAILURE; \
178  } \
179  }
180 
181 
182 #define ITK_TEST_SET_GET(variable, command) \
183  if (variable != command) \
184  { \
185  std::cerr << "Error in " << #command << std::endl; \
186  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
187  std::cerr << "Expected " << variable.GetPointer() << std::endl; \
188  std::cerr << "but got " << command << std::endl; \
189  return EXIT_FAILURE; \
190  }
191 
192 
193 #define ITK_TEST_SET_GET_VALUE(variable, command) \
194  CLANG_PRAGMA_PUSH \
195  CLANG_SUPPRESS_Wfloat_equal if (variable != command) CLANG_PRAGMA_POP \
196  { \
197  std::cerr << "Error in " << #command << std::endl; \
198  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
199  std::cerr << "Expected " << variable << std::endl; \
200  std::cerr << "but got " << command << std::endl; \
201  return EXIT_FAILURE; \
202  }
203 
204 #define ITK_TEST_SET_GET_NULL_VALUE(command) \
205  if (nullptr != command) \
206  { \
207  std::cerr << "Error in " << #command << std::endl; \
208  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
209  std::cerr << "Expected " \
210  << "nullptr" << std::endl; \
211  std::cerr << "but got " << command << std::endl; \
212  return EXIT_FAILURE; \
213  }
214 
215 #define ITK_TEST_SET_GET_BOOLEAN(object, variable, value) \
216  object->Set##variable(false); \
217  object->Set##variable(true); \
218  if (object->Get##variable() != 1) \
219  { \
220  std::cerr << "Error in Set/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
221  << " instead of 1" << std::endl; \
222  return EXIT_FAILURE; \
223  } \
224  object->Set##variable(false); \
225  if (object->Get##variable() != 0) \
226  { \
227  std::cerr << "Error in Set/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
228  << " instead of 0" << std::endl; \
229  return EXIT_FAILURE; \
230  } \
231  object->variable##On(); \
232  if (object->Get##variable() != 1) \
233  { \
234  std::cerr << "Error in On/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
235  << " instead of 1" << std::endl; \
236  return EXIT_FAILURE; \
237  } \
238  object->variable##Off(); \
239  if (object->Get##variable() != 0) \
240  { \
241  std::cerr << "Error in Off/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
242  << " instead of 0" << std::endl; \
243  return EXIT_FAILURE; \
244  } \
245  object->Set##variable(value);
246 
250 #define itkNameOfTestExecutableMacro(argv) \
251  [argv](const std::string & functionName) { \
252  return ((argv == nullptr) || (argv[0] == nullptr) || (argv[0][0] == '\0')) ? ("<" + functionName + " executable>") \
253  : argv[0]; \
254  }(__func__)
255 
256 #endif
itkMacro.h
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26