ITK  5.2.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, ClassName, SuperclassName) \
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(), #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  if (!std::strcmp(object->Superclass::GetNameOfClass(), #SuperclassName)) \
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  ITK_MACROEND_NOOP_STATEMENT
89 
90 #define ITK_TRY_EXPECT_EXCEPTION(command) \
91  try \
92  { \
93  std::cout << "Trying " << #command << std::endl; \
94  command; \
95  std::cerr << "Failed to catch expected exception" << std::endl; \
96  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
97  return EXIT_FAILURE; \
98  } \
99  catch (const itk::ExceptionObject & excp) \
100  { \
101  std::cout << "Caught expected exception" << std::endl; \
102  std::cout << excp << std::endl; \
103  } \
104  ITK_MACROEND_NOOP_STATEMENT
105 
106 
107 #define ITK_TRY_EXPECT_NO_EXCEPTION(command) \
108  try \
109  { \
110  std::cout << "Trying " << #command << std::endl; \
111  command; \
112  } \
113  catch (const itk::ExceptionObject & excp) \
114  { \
115  std::cerr << excp << std::endl; \
116  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
117  return EXIT_FAILURE; \
118  } \
119  ITK_MACROEND_NOOP_STATEMENT
120 
121 #define ITK_TEST_EXPECT_TRUE_STATUS_VALUE(command, statusVal) \
122  { \
123  CLANG_PRAGMA_PUSH \
124  CLANG_SUPPRESS_Wfloat_equal bool _ITK_TEST_EXPECT_TRUE_command(command); \
125  CLANG_PRAGMA_POP \
126  if (!(_ITK_TEST_EXPECT_TRUE_command)) \
127  { \
128  std::cerr << "Error in " << #command << std::endl; \
129  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
130  std::cerr << "Expected true" << std::endl; \
131  std::cerr << " but got " << _ITK_TEST_EXPECT_TRUE_command << std::endl; \
132  statusVal = EXIT_FAILURE; \
133  } \
134  } \
135  ITK_MACROEND_NOOP_STATEMENT
136 
137 #define ITK_TEST_EXPECT_TRUE(command) \
138  { \
139  CLANG_PRAGMA_PUSH \
140  CLANG_SUPPRESS_Wfloat_equal bool _ITK_TEST_EXPECT_TRUE_command(command); \
141  CLANG_PRAGMA_POP \
142  if (!(_ITK_TEST_EXPECT_TRUE_command)) \
143  { \
144  std::cerr << "Error in " << #command << std::endl; \
145  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
146  std::cerr << "Expected true" << std::endl; \
147  std::cerr << " but got " << _ITK_TEST_EXPECT_TRUE_command << std::endl; \
148  return EXIT_FAILURE; \
149  } \
150  } \
151  ITK_MACROEND_NOOP_STATEMENT
152 
153 
154 #define ITK_TEST_EXPECT_EQUAL_STATUS_VALUE(lh, rh, statusVal) \
155  { \
156  CLANG_PRAGMA_PUSH \
157  CLANG_SUPPRESS_Wfloat_equal bool _ITK_TEST_EXPECT_EQUAL_result((lh) == (rh)); \
158  CLANG_PRAGMA_POP \
159  if (!(_ITK_TEST_EXPECT_EQUAL_result)) \
160  { \
161  std::cerr << "Error in " << #lh << " == " << #rh << std::endl; \
162  std::cerr << "\tIn " __FILE__ ", line " << __LINE__ << std::endl; \
163  std::cerr << "\tlh: " << (lh) << std::endl; \
164  std::cerr << "\trh: " << (rh) << std::endl; \
165  std::cerr << "Expression is not equal" << std::endl; \
166  statusVal = EXIT_FAILURE; \
167  } \
168  } \
169  ITK_MACROEND_NOOP_STATEMENT
170 
171 #define ITK_TEST_EXPECT_EQUAL(lh, rh) \
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  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  CLANG_PRAGMA_PUSH \
203  CLANG_SUPPRESS_Wfloat_equal if (variable != command) CLANG_PRAGMA_POP \
204  { \
205  std::cerr << "Error in " << #command << std::endl; \
206  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
207  std::cerr << "Expected " << variable << std::endl; \
208  std::cerr << "but got " << command << std::endl; \
209  return EXIT_FAILURE; \
210  } \
211  ITK_MACROEND_NOOP_STATEMENT
212 
213 #define ITK_TEST_SET_GET_NULL_VALUE(command) \
214  if (nullptr != command) \
215  { \
216  std::cerr << "Error in " << #command << std::endl; \
217  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
218  std::cerr << "Expected " \
219  << "nullptr" << std::endl; \
220  std::cerr << "but got " << command << std::endl; \
221  return EXIT_FAILURE; \
222  } \
223  ITK_MACROEND_NOOP_STATEMENT
224 
225 #define ITK_TEST_SET_GET_BOOLEAN(object, variable, value) \
226  object->Set##variable(false); \
227  object->Set##variable(true); \
228  if (object->Get##variable() != 1) \
229  { \
230  std::cerr << "Error in Set/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
231  << " instead of 1" << std::endl; \
232  return EXIT_FAILURE; \
233  } \
234  object->Set##variable(false); \
235  if (object->Get##variable() != 0) \
236  { \
237  std::cerr << "Error in Set/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
238  << " instead of 0" << std::endl; \
239  return EXIT_FAILURE; \
240  } \
241  object->variable##On(); \
242  if (object->Get##variable() != 1) \
243  { \
244  std::cerr << "Error in On/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
245  << " instead of 1" << std::endl; \
246  return EXIT_FAILURE; \
247  } \
248  object->variable##Off(); \
249  if (object->Get##variable() != 0) \
250  { \
251  std::cerr << "Error in Off/Get" #variable << ", Get" #variable << " is " << object->Get##variable() \
252  << " instead of 0" << std::endl; \
253  return EXIT_FAILURE; \
254  } \
255  object->Set##variable(value)
256 
260 #define itkNameOfTestExecutableMacro(argv) \
261  [argv](const std::string & functionName) { \
262  return ((argv == nullptr) || (argv[0] == nullptr) || (argv[0][0] == '\0')) ? ("<" + functionName + " executable>") \
263  : argv[0]; \
264  }(__func__)
265 
266 #endif
itkMacro.h
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24