Slicer  4.13
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
vtkMRMLCoreTestingMacros.h
Go to the documentation of this file.
1 /*=auto=========================================================================
2 
3  Portions (c) Copyright 2005 Brigham and Women's Hospital (BWH)
4  All Rights Reserved.
5 
6  See COPYRIGHT.txt
7  or http://www.slicer.org/copyright/copyright.txt for details.
8 
9  Program: 3D Slicer
10 
11 =========================================================================auto=*/
12 
13 #ifndef __vtkMRMLCoreTestingMacros_h
14 #define __vtkMRMLCoreTestingMacros_h
15 
16 // VTK includes
17 #include <vtkMath.h>
18 
24 
26 #define CHECK_NODE_IN_SCENE_BY_ID(scene, nodeID, expected) \
27  { \
28  if (!vtkMRMLCoreTestingUtilities::CheckNodeInSceneByID(__LINE__, (scene), (nodeID), (expected))) \
29  { \
30  return EXIT_FAILURE; \
31  } \
32  }
33 
35 #define CHECK_NODE_ID_AND_NAME(node, expectedID, expectedName) \
36  { \
37  if (!vtkMRMLCoreTestingUtilities::CheckNodeIdAndName(__LINE__, (node), (expectedID), (expectedName))) \
38  { \
39  return EXIT_FAILURE; \
40  } \
41  }
42 
43 // ----------------------------------------------------------------------------
45 #define TRY_EXPECT_ITK_EXCEPTION( command ) \
46  try \
47  { \
48  std::cout << "Trying " << #command << std::endl; \
49  command; \
50  std::cerr << "Failed to catch expected exception" << std::endl; \
51  return EXIT_FAILURE; \
52  } \
53  catch( itk::ExceptionObject & excp ) \
54  { \
55  std::cout << "Caught expected exception" << std::endl; \
56  std::cout << excp << std::endl; \
57  }
58 
59 // ----------------------------------------------------------------------------
61 #define TRY_EXPECT_NO_ITK_EXCEPTION( command ) \
62  try \
63  { \
64  std::cout << "Trying " << #command << std::endl; \
65  command; \
66  } \
67  catch( itk::ExceptionObject & excp ) \
68  { \
69  std::cerr << excp << std::endl; \
70  return EXIT_FAILURE; \
71  }
72 
73 // ----------------------------------------------------------------------------
75 #define TEST_ITK_SET_GET( variable, command ) \
76  if( variable.GetPointer() != command ) \
77  { \
78  std::cerr << "Error in " << #command << std::endl; \
79  std::cerr << "Expected " << variable.GetPointer() << std::endl; \
80  std::cerr << "but got " << command << std::endl; \
81  return EXIT_FAILURE; \
82  }
83 
84 // ----------------------------------------------------------------------------
86 #define TEST_ITK_SET_GET_VALUE( variable, command ) \
87  if( variable != command ) \
88  { \
89  std::cerr << "Error in " << #command << std::endl; \
90  std::cerr << "Expected " << variable << std::endl; \
91  std::cerr << "but got " << command << std::endl; \
92  return EXIT_FAILURE; \
93  }
94 
95 // ----------------------------------------------------------------------------
97 #define TEST_SET_GET_BOOLEAN( object, variable ) \
98  object->Set##variable( false ); \
99  object->Set##variable( true ); \
100  if( object->Get##variable() == 0 ) \
101  { \
102  std::cerr << "Error in Set/Get"#variable << ", Get"#variable << " returned zero while it is expected to return non-zero" << std::endl; \
103  return EXIT_FAILURE; \
104  } \
105  object->Set##variable( false ); \
106  if( object->Get##variable() != 0 ) \
107  { \
108  std::cerr << "Error in Set/Get"#variable << ", Get"#variable << " returned " << object->Get##variable() << " instead of 0" << std::endl; \
109  return EXIT_FAILURE; \
110  } \
111  object->variable##On(); \
112  if( object->Get##variable() == 0 ) \
113  { \
114  std::cerr << "Error in On/Get"#variable << ", Get"#variable << " returned zero while it is expected to return non-zero" << std::endl; \
115  return EXIT_FAILURE; \
116  } \
117  object->variable##Off(); \
118  if( object->Get##variable() != 0 ) \
119  { \
120  std::cerr << "Error in Off/Get"#variable << ", Get"#variable << " returned " << object->Get##variable() << " while 0 is expected" << std::endl; \
121  return EXIT_FAILURE; \
122  }
123 
124 // ----------------------------------------------------------------------------
127 #define TEST_SET_GET_INT( object, variable, value ) \
128  { \
129  object->Set##variable( value ); \
130  if( object->Get##variable() != value ) \
131  { \
132  std::cerr << "Error in Set/Get"#variable << " using value " << value << std::endl; \
133  return EXIT_FAILURE; \
134  } \
135  }
136 
137 // ----------------------------------------------------------------------------
142 #define TEST_SET_GET_INT_RANGE( object, variable, min, max ) \
143  { \
144  int epsilon = 1; \
145  int val = min - epsilon; \
146  TEST_SET_GET_INT( object, variable, val); \
147  val = min; \
148  TEST_SET_GET_INT( object, variable, val); \
149  val = min + epsilon; \
150  TEST_SET_GET_INT( object, variable, val); \
151  val = (min + max) / 2; \
152  TEST_SET_GET_INT( object, variable, val); \
153  val = max - epsilon; \
154  TEST_SET_GET_INT( object, variable, val); \
155  val = max; \
156  TEST_SET_GET_INT( object, variable, val); \
157  val = max + epsilon; \
158  TEST_SET_GET_INT( object, variable, val); \
159  }
160 
161 // ----------------------------------------------------------------------------
164 #define TEST_SET_GET_INT_RANDOM( object, variable, max ) \
165  { \
166  int val = (int)(vtkMath::Random() * max); \
167  object->Set##variable( val ); \
168  if( object->Get##variable() != val ) \
169  { \
170  std::cerr << "Error in Set/Get"#variable << " using random value " << val << std::endl; \
171  return EXIT_FAILURE; \
172  } \
173  }
174 
175 // ----------------------------------------------------------------------------
180 #define TEST_SET_GET_UNSIGNED_INT_RANGE( object, variable, min, max ) \
181  { \
182  unsigned int epsilon = 1; \
183  unsigned int val = min - epsilon; \
184  TEST_SET_GET_INT( object, variable, val); \
185  val = min; \
186  TEST_SET_GET_INT( object, variable, val); \
187  val = min + epsilon; \
188  TEST_SET_GET_INT( object, variable, val); \
189  val = (min + max) / 2; \
190  TEST_SET_GET_INT( object, variable, val); \
191  val = max - epsilon; \
192  TEST_SET_GET_INT( object, variable, val); \
193  val = max; \
194  TEST_SET_GET_INT( object, variable, val); \
195  val = max + epsilon; \
196  TEST_SET_GET_INT( object, variable, val); \
197  }
198 
199 // ----------------------------------------------------------------------------
202 #define TEST_SET_GET_DOUBLE( object, variable, value ) \
203  { \
204  object->Set##variable( value ); \
205  if( object->Get##variable() != value ) \
206  { \
207  std::cerr << "Error in Set/Get"#variable << " using value " << value << std::endl; \
208  return EXIT_FAILURE; \
209  } \
210  }
211 
212 // ----------------------------------------------------------------------------
217 #define TEST_SET_GET_DOUBLE_RANGE( object, variable, min, max ) \
218  { \
219  double epsilon = 1.0; \
220  double val = min - epsilon; \
221  TEST_SET_GET_DOUBLE( object, variable, val); \
222  val = min; \
223  TEST_SET_GET_DOUBLE( object, variable, val); \
224  val = min + epsilon; \
225  TEST_SET_GET_DOUBLE( object, variable, val); \
226  val = (min + max) / 2.0; \
227  TEST_SET_GET_DOUBLE( object, variable, val); \
228  val = max - epsilon; \
229  TEST_SET_GET_DOUBLE( object, variable, val); \
230  val = max; \
231  TEST_SET_GET_DOUBLE( object, variable, val); \
232  val = max + epsilon; \
233  TEST_SET_GET_DOUBLE( object, variable, val); \
234  }
235 
236 // ----------------------------------------------------------------------------
239 #define TEST_SET_GET_DOUBLE_RANDOM( object, variable, max ) \
240  { \
241  double val = vtkMath::Random() * max; \
242  object->Set##variable( val ); \
243  if( object->Get##variable() != val ) \
244  { \
245  std::cerr << "Error in Set/Get"#variable << ", using random value " << val << std::endl; \
246  return EXIT_FAILURE; \
247  } \
248  }
249 
250 // ----------------------------------------------------------------------------
252 #define TEST_GET_OBJECT(object,variable) \
253  if ( !object->Get##variable() ) \
254  { \
255  std::cerr << "Error in Get"#variable << ", non null value is expected " << std::endl; \
256  return EXIT_FAILURE; \
257  } \
258  object->Get##variable()->Print(std::cout);
259 
260 // ----------------------------------------------------------------------------
263 #define TEST_SET_GET_VALUE(object,variable,value) \
264  object->Set##variable(value); \
265  if (object->Get##variable() != value) \
266  { \
267  std::cerr << "Error getting " << #variable << std::endl; \
268  std::cerr << "Expected " << value << std::endl; \
269  std::cerr << "but got " << object->Get##variable()<< std::endl; \
270  return EXIT_FAILURE; \
271  }
272 
273 // ----------------------------------------------------------------------------
276 #define TEST_SET_GET_VECTOR3_DOUBLE( object, variable, x, y, z ) \
277  { \
278  object->Set##variable( x, y, z ); \
279  double *val = object->Get##variable(); \
280  if( val == nullptr || val[0] != x || val[1] != y || val[2] != z ) \
281  { \
282  std::cerr << "Error in Set/Get"#variable << std::endl; \
283  return EXIT_FAILURE; \
284  } \
285  }
286 
287 // ----------------------------------------------------------------------------
293 #define TEST_SET_GET_VECTOR3_DOUBLE_RANGE( object, variable, min, max ) \
294  { \
295  double epsilon = 1.0; \
296  TEST_SET_GET_VECTOR3_DOUBLE(object, variable, min - epsilon, min - epsilon, min - epsilon); \
297  TEST_SET_GET_VECTOR3_DOUBLE(object, variable, min, min, min); \
298  TEST_SET_GET_VECTOR3_DOUBLE(object, variable, min + epsilon, min + epsilon, min + epsilon); \
299  double half = (min+max/2.0); \
300  TEST_SET_GET_VECTOR3_DOUBLE(object, variable, half, half, half); \
301  TEST_SET_GET_VECTOR3_DOUBLE(object, variable, max - epsilon, max - epsilon, max - epsilon); \
302  TEST_SET_GET_VECTOR3_DOUBLE(object, variable, max, max, max); \
303  TEST_SET_GET_VECTOR3_DOUBLE(object, variable, max + epsilon, max + epsilon, max + epsilon); \
304  }
305 
306 // ----------------------------------------------------------------------------
309 #define TEST_SET_GET_VECTOR3_DOUBLE_RANDOM( object, variable, max ) \
310  { \
311  double x = vtkMath::Random() * max; \
312  double y = vtkMath::Random() * max; \
313  double z = vtkMath::Random() * max; \
314  object->Set##variable( x, y, z ); \
315  double val[3] = {0.0, 0.0, 0.0}; \
316  object->Get##variable(val); \
317  if( val[0] != x || val[1] != y || val[2] != z ) \
318  { \
319  std::cerr << "Error in Set/Get"#variable << " with " << x << ", " << y << ", " << z << std::endl; \
320  return EXIT_FAILURE; \
321  } \
322  }
323 
324 // ----------------------------------------------------------------------------
326 #define TEST_SET_GET_STRING( object, variable ) \
327  { \
328  const char * originalStringPointer = object->Get##variable(); \
329  std::string originalString; \
330  if( originalStringPointer != nullptr ) \
331  { \
332  originalString = originalStringPointer; \
333  } \
334  object->Set##variable( "testing with a const char"); \
335  if( strcmp(object->Get##variable(), "testing with a const char") != 0) \
336  { \
337  std::cerr << "Error in Set/Get"#variable << " with a string literal" << std::endl; \
338  return EXIT_FAILURE; \
339  } \
340  std::string string1 = "testingIsGood"; \
341  object->Set##variable( string1.c_str() ); \
342  if( object->Get##variable() != string1 ) \
343  { \
344  std::cerr << "Error in Set/Get"#variable << ", tried to set to " << string1.c_str() << " but got " << (object->Get##variable() ? object->Get##variable() : "null") << std::endl; \
345  return EXIT_FAILURE; \
346  } \
347  std::string string2 = "moreTestingIsBetter"; \
348  object->Set##variable( string2.c_str() ); \
349  if( object->Get##variable() != string2 ) \
350  { \
351  std::cerr << "Error in Set/Get"#variable << ", tried to set to " << string2.c_str() << " but got " << (object->Get##variable() ? object->Get##variable() : "null") << std::endl; \
352  return EXIT_FAILURE; \
353  } \
354  if( originalStringPointer != nullptr ) \
355  { \
356  object->Set##variable( originalString.c_str() ); \
357  } \
358  else \
359  { \
360  object->Set##variable( nullptr ); \
361  } \
362  }
363 
364 #define EXERCISE_BASIC_OBJECT_METHODS( node ) \
365  { \
366  int result = vtkMRMLCoreTestingUtilities::ExerciseBasicObjectMethods(node); \
367  if (result != EXIT_SUCCESS) \
368  { \
369  return result; \
370  } \
371  }
372 
374 #define EXERCISE_ALL_BASIC_MRML_METHODS( node ) \
375  CHECK_EXIT_SUCCESS(vtkMRMLCoreTestingUtilities::ExerciseAllBasicMRMLMethods(node));
376 
377 //---------------------------------------------------------------------------
378 // Deprecated macros, for backward compatibility only
379 // (className is no longer needed; usually EXERCISE_ALL_BASIC_MRML_METHODS can
380 // be used instead of all the macros below)
381 
382 #define EXERCISE_BASIC_MRML_METHODS( className, node ) \
383  CHECK_EXIT_SUCCESS(vtkMRMLCoreTestingUtilities::ExerciseBasicMRMLMethods(node));
384 
385 #define EXERCISE_BASIC_STORABLE_MRML_METHODS( className, node ) \
386  CHECK_EXIT_SUCCESS(vtkMRMLCoreTestingUtilities::ExerciseBasicStorableMRMLMethods(node));
387 
388 #define EXERCISE_BASIC_TRANSFORMABLE_MRML_METHODS( className, node ) \
389  CHECK_EXIT_SUCCESS(vtkMRMLCoreTestingUtilities::ExerciseBasicTransformableMRMLMethods(node));
390 
391 #define EXERCISE_BASIC_DISPLAYABLE_MRML_METHODS( className, node ) \
392  CHECK_EXIT_SUCCESS(vtkMRMLCoreTestingUtilities::ExerciseBasicDisplayableMRMLMethods(node));
393 
394 #define EXERCISE_BASIC_DISPLAY_MRML_METHODS( className, node ) \
395  CHECK_EXIT_SUCCESS(vtkMRMLCoreTestingUtilities::ExerciseBasicDisplayMRMLMethods(node));
396 
397 #define EXERCISE_BASIC_STORAGE_MRML_METHODS( className, node ) \
398  CHECK_EXIT_SUCCESS(vtkMRMLCoreTestingUtilities::ExerciseBasicStorageMRMLMethods(node));
399 
400 #define EXERCISE_BASIC_TRANSFORM_MRML_METHODS( className, node ) \
401  CHECK_EXIT_SUCCESS(vtkMRMLCoreTestingUtilities::ExerciseBasicTransformMRMLMethods(node));
402 
403 // Need to include vtkMRMLCoreTestingUtilities.h here because
404 // vtkMRMLCoreTestingUtilities use some of the macros above, too.
406 #include "vtkMRMLNode.h"
407 
408 // Commonly used headers in tests
409 #include <vtkAddonTestingMacros.h>
410 
411 #endif