ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkTestingMacros.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 
36 // object's Class must be specified to build on sun studio
37 #define EXERCISE_BASIC_OBJECT_METHODS( object, Class, SuperClass ) \
38  object->Print( std::cout ); \
39  std::cout << "Name of Class = " << object->GetNameOfClass() << std::endl; \
40  std::cout << "Name of Superclass = " << object->Superclass::GetNameOfClass() << std::endl; \
41  if( !std::strcmp(object->GetNameOfClass(), #Class) ) \
42  { \
43  std::cout << "Class name is correct" << std::endl; \
44  } \
45  else \
46  { \
47  std::cerr << "Class name provided does not match object's NameOfClass" << std::endl; \
48  return EXIT_FAILURE; \
49  } \
50  if( !std::strcmp(object->Superclass::GetNameOfClass(), #SuperClass) ) \
51  { \
52  std::cout << "Superclass name is correct" << std::endl; \
53  } \
54  else \
55  { \
56  std::cerr << "Superclass name provided does not match object's Superclass::NameOfClass" << std::endl; \
57  return EXIT_FAILURE; \
58  }
59 
60 #define TRY_EXPECT_EXCEPTION( command ) \
61  try \
62  { \
63  std::cout << "Trying " << #command << std::endl; \
64  command; \
65  std::cerr << "Failed to catch expected exception" << std::endl; \
66  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl;\
67  return EXIT_FAILURE; \
68  } \
69  catch( itk::ExceptionObject & excp ) \
70  { \
71  std::cout << "Caught expected exception" << std::endl; \
72  std::cout << excp << std::endl; \
73  }
74 
75 
76 #define TRY_EXPECT_NO_EXCEPTION( command ) \
77  try \
78  { \
79  std::cout << "Trying " << #command << std::endl; \
80  command; \
81  } \
82  catch( itk::ExceptionObject & excp ) \
83  { \
84  std::cerr << excp << std::endl; \
85  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
86  return EXIT_FAILURE; \
87  }
88 
89 #define TEST_EXPECT_TRUE_STATUS_VALUE( command, statusVal ) \
90  { \
91 CLANG_PRAGMA_PUSH \
92 CLANG_SUPPRESS_Wfloat_equal \
93  bool _TEST_EXPECT_TRUE_command(command); \
94 CLANG_PRAGMA_POP \
95  if( !(_TEST_EXPECT_TRUE_command) ) \
96  { \
97  std::cerr << "Error in " << #command << std::endl; \
98  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
99  std::cerr << "Expected true" << std::endl; \
100  std::cerr << " but got " << _TEST_EXPECT_TRUE_command << std::endl; \
101  statusVal = EXIT_FAILURE; \
102  } \
103  }
104 
105 #define TEST_EXPECT_TRUE( command ) \
106  { \
107 CLANG_PRAGMA_PUSH \
108 CLANG_SUPPRESS_Wfloat_equal \
109  bool _TEST_EXPECT_TRUE_command(command); \
110 CLANG_PRAGMA_POP \
111  if( !(_TEST_EXPECT_TRUE_command) ) \
112  { \
113  std::cerr << "Error in " << #command << std::endl; \
114  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
115  std::cerr << "Expected true" << std::endl; \
116  std::cerr << " but got " << _TEST_EXPECT_TRUE_command << std::endl; \
117  return EXIT_FAILURE; \
118  } \
119  }
120 
121 
122 #define TEST_EXPECT_EQUAL_STATUS_VALUE( lh, rh, statusVal ) \
123  { \
124 CLANG_PRAGMA_PUSH \
125 CLANG_SUPPRESS_Wfloat_equal \
126  bool _TEST_EXPECT_EQUAL_result((lh) == (rh)); \
127 CLANG_PRAGMA_POP \
128  if( !(_TEST_EXPECT_EQUAL_result) ) \
129  { \
130  std::cerr << "Error in " << #lh << " == " << #rh << std::endl; \
131  std::cerr << "\tIn " __FILE__ ", line " << __LINE__ << std::endl; \
132  std::cerr << "\tlh: " << (lh) << std::endl; \
133  std::cerr << "\trh: " << (rh) << std::endl; \
134  std::cerr << "Expression is not equal" << std::endl; \
135  statusVal = EXIT_FAILURE; \
136  } \
137  }
138 
139 #define TEST_EXPECT_EQUAL( lh, rh ) \
140  { \
141 CLANG_PRAGMA_PUSH \
142 CLANG_SUPPRESS_Wfloat_equal \
143  bool _TEST_EXPECT_EQUAL_result((lh) == (rh)); \
144 CLANG_PRAGMA_POP \
145  if( !(_TEST_EXPECT_EQUAL_result) ) \
146  { \
147  std::cerr << "Error in " << #lh << " == " << #rh << std::endl; \
148  std::cerr << "\tIn " __FILE__ ", line " << __LINE__ << std::endl; \
149  std::cerr << "\tlh: " << (lh) << std::endl; \
150  std::cerr << "\trh: " << (rh) << std::endl; \
151  std::cerr << "Expression is not equal" << std::endl; \
152  return EXIT_FAILURE; \
153  } \
154  }
155 
156 
157 #define TEST_SET_GET( variable, command ) \
158  if( variable != command ) \
159  { \
160  std::cerr << "Error in " << #command << std::endl; \
161  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
162  std::cerr << "Expected " << variable.GetPointer() << std::endl; \
163  std::cerr << "but got " << command << std::endl; \
164  return EXIT_FAILURE; \
165  }
166 
167 
168 #define TEST_SET_GET_VALUE( variable, command ) \
169 CLANG_PRAGMA_PUSH \
170 CLANG_SUPPRESS_Wfloat_equal \
171  if( variable != command ) \
172 CLANG_PRAGMA_POP \
173  { \
174  std::cerr << "Error in " << #command << std::endl; \
175  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
176  std::cerr << "Expected " << variable << std::endl; \
177  std::cerr << "but got " << command << std::endl; \
178  return EXIT_FAILURE; \
179  }
180 
181 #define TEST_SET_GET_NULL_VALUE( command ) \
182  if( nullptr != command ) \
183  { \
184  std::cerr << "Error in " << #command << std::endl; \
185  std::cerr << " In " __FILE__ ", line " << __LINE__ << std::endl; \
186  std::cerr << "Expected " << "nullptr" << std::endl; \
187  std::cerr << "but got " << command << std::endl; \
188  return EXIT_FAILURE; \
189  }
190 
191 #define TEST_SET_GET_BOOLEAN( object, variable, value ) \
192  object->Set##variable( false ); \
193  object->Set##variable( true ); \
194  if( object->Get##variable() != 1 ) \
195  { \
196  std::cerr << "Error in Set/Get"#variable << ", Get"#variable << " is " << object->Get##variable() << " instead of 1" << std::endl; \
197  return EXIT_FAILURE; \
198  } \
199  object->Set##variable( false ); \
200  if( object->Get##variable() != 0 ) \
201  { \
202  std::cerr << "Error in Set/Get"#variable << ", Get"#variable << " is " << object->Get##variable() << " instead of 0" << std::endl; \
203  return EXIT_FAILURE; \
204  } \
205  object->variable##On(); \
206  if( object->Get##variable() != 1 ) \
207  { \
208  std::cerr << "Error in On/Get"#variable << ", Get"#variable << " is " << object->Get##variable() << " instead of 1" << std::endl; \
209  return EXIT_FAILURE; \
210  } \
211  object->variable##Off(); \
212  if( object->Get##variable() != 0 ) \
213  { \
214  std::cerr << "Error in Off/Get"#variable << ", Get"#variable << " is " << object->Get##variable() << " instead of 0" << std::endl; \
215  return EXIT_FAILURE; \
216  } \
217  object->Set##variable( value );
218 
222 #define itkNameOfTestExecutableMacro(argv) [argv](const std::string& functionName) \
223  { \
224  return ((argv == nullptr) || (argv[0] == nullptr) || (argv[0][0] == '\0')) ? \
225  ("<" + functionName + " executable>") : argv[0]; \
226  }(__func__)
227 
228 #endif