Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkPropertyTest.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 // Testing
14 #include "mitkTestFixture.h"
15 #include "mitkTestingMacros.h"
16 // std includes
17 #include <string>
18 // MITK includes
19 #include <mitkAnnotationProperty.h>
20 #include <mitkClippingProperty.h>
21 #include <mitkColorProperty.h>
24 #include <mitkLookupTables.h>
25 #include <mitkProperties.h>
27 #include <mitkStringProperty.h>
30 #include <mitkLogMacros.h>
31 // ITK includes
32 #include <itkCommand.h>
33 // VTK includes
34 #include <vtkDebugLeaks.h>
35 
36 struct PropertyModifiedListener
37 {
38  typedef itk::SimpleMemberCommand<PropertyModifiedListener> CmdType;
39 
40  PropertyModifiedListener() : m_Modified(false), m_Cmd(CmdType::New())
41  {
42  m_Cmd->SetCallbackFunction(this, &PropertyModifiedListener::Modified);
43  }
44 
45  void Modified() { m_Modified = true; }
46  bool Pop()
47  {
48  bool b = m_Modified;
49  m_Modified = false;
50  return b;
51  }
52 
53  bool m_Modified;
54  CmdType::Pointer m_Cmd;
55 };
56 
57 class mitkPropertyTestSuite : public mitk::TestFixture
58 {
59  CPPUNIT_TEST_SUITE(mitkPropertyTestSuite);
60  MITK_TEST(TestBoolProperty_Success);
61  MITK_TEST(TestIntProperty_Success);
62  MITK_TEST(TestFloatProperty_Success);
63  MITK_TEST(TestDoubleProperty_Success);
64  MITK_TEST(TestVector3DProperty_Success);
65  MITK_TEST(TestPoint3D_Success);
66  MITK_TEST(TestPoint4D_Success);
67  MITK_TEST(TestPoint3I_Success);
68  MITK_TEST(TestFloatLookupTable_Success);
69  MITK_TEST(TestBoolLookupTable_Success);
70  MITK_TEST(TestIntLookupTable_Success);
71  MITK_TEST(TestStringLookupTable_Success);
72  MITK_TEST(TestAnnotationProperty_Success);
73  MITK_TEST(TestClippingProperty_Success);
74  MITK_TEST(TestColorProperty_Success);
75  MITK_TEST(TestLevelWindowProperty_Success);
76  MITK_TEST(TestSmartPointerProperty_Success);
77  MITK_TEST(TestStringProperty_Success);
78  MITK_TEST(TestTransferFunctionProperty_Success);
79  MITK_TEST(TestWeakPointerProperty_Success);
80  MITK_TEST(TestLookupTablePropertyProperty_Success);
81  CPPUNIT_TEST_SUITE_END();
82 
83 private:
84  PropertyModifiedListener m_L;
85 public:
86  void setUp()
87  {
88  }
89  void tearDown()
90  {
91  }
92 
93  template <class T>
94  void TestPropInequality(T prop, T prop2)
95  {
96  mitk::BaseProperty::Pointer baseProp2(prop2.GetPointer());
97  CPPUNIT_ASSERT_MESSAGE("Test inequality 1", !(*prop == *prop2));
98  CPPUNIT_ASSERT_MESSAGE("Test polymorphic inequality 1", !(*prop == *baseProp2));
99  CPPUNIT_ASSERT_MESSAGE("Test polymorphic inequality 2", !(*baseProp2 == *prop));
100  }
101 
102  template <class T>
103  void TestPropAssignment(T prop, T prop2, const std::string &strProp)
104  {
105  unsigned long tag = prop->AddObserver(itk::ModifiedEvent(), m_L.m_Cmd.GetPointer());
106 
107  mitk::BaseProperty::Pointer baseProp2(prop2.GetPointer());
108  *prop = *baseProp2;
109  CPPUNIT_ASSERT_MESSAGE("Test modified event", m_L.Pop());
110  std::string msg = std::string("Test assignment [") + prop->GetValueAsString() + " == " + strProp + "]";
111  CPPUNIT_ASSERT_MESSAGE(msg, prop->GetValueAsString() == strProp);
112  CPPUNIT_ASSERT_MESSAGE("Test equality", *prop == *prop2);
113  CPPUNIT_ASSERT_MESSAGE("Test equality", *prop == *baseProp2);
114  CPPUNIT_ASSERT_MESSAGE("Test polymorphic equality", *baseProp2 == *prop);
115 
116  prop->RemoveObserver(tag);
117  }
118 
119  template <class T>
120  void TestPropPolymorphicAssignment(T prop, T prop2, const std::string &strProp)
121  {
122  mitk::BaseProperty::Pointer baseProp(prop.GetPointer());
123 
124  unsigned long tag = baseProp->AddObserver(itk::ModifiedEvent(), m_L.m_Cmd.GetPointer());
125 
126  *baseProp = *prop2;
127  CPPUNIT_ASSERT_MESSAGE("Test modified event", m_L.Pop());
128  std::string msg =
129  std::string("Test polymorphic assignment [") + baseProp->GetValueAsString() + " == " + strProp + "]";
130  CPPUNIT_ASSERT_MESSAGE(msg, baseProp->GetValueAsString() == strProp);
131  CPPUNIT_ASSERT_MESSAGE("Test equality", *prop == *prop2);
132  CPPUNIT_ASSERT_MESSAGE("Test equality", *prop2 == *baseProp);
133  CPPUNIT_ASSERT_MESSAGE("Test polymorphic equality", *baseProp == *prop2);
134  baseProp->RemoveObserver(tag);
135  }
136 
137  template <class T>
138  void TestPropCloning(T prop)
139  {
140  T prop2 = prop->Clone();
141  CPPUNIT_ASSERT_MESSAGE("Test clone pointer", prop.GetPointer() != prop2.GetPointer());
142  CPPUNIT_ASSERT_MESSAGE("Test equality of the clone", *prop == *prop2);
143  }
144 
145  template <class T>
146  void TestProperty(const typename T::ValueType &v1,
147  const typename T::ValueType &v2,
148  const std::string &strV1,
149  const std::string &strV2)
150  {
151  typename T::Pointer prop = T::New(v1);
152  MITK_TEST_OUTPUT(<< "**** Test [" << prop->GetNameOfClass() << "] ****");
153 
154  CPPUNIT_ASSERT_MESSAGE("Test constructor", prop->GetValue() == v1);
155  std::string msg = std::string("Test GetValueAsString() [") + prop->GetValueAsString() + " == " + strV1 + "]";
156  CPPUNIT_ASSERT_MESSAGE(msg, prop->GetValueAsString() == strV1);
157 
158  TestPropCloning(prop);
159 
160  typename T::Pointer prop2 = T::New();
161  prop2->AddObserver(itk::ModifiedEvent(), m_L.m_Cmd.GetPointer());
162  CPPUNIT_ASSERT_MESSAGE("Test modified", !m_L.m_Modified);
163  prop2->SetValue(v2);
164  CPPUNIT_ASSERT_MESSAGE("Test modified", m_L.Pop());
165  CPPUNIT_ASSERT_MESSAGE("Test SetValue()", prop2->GetValue() == v2);
166  prop2->SetValue(v2);
167  CPPUNIT_ASSERT_MESSAGE("Test for no modification", !m_L.Pop());
168 
169  TestPropInequality(prop, prop2);
170  TestPropAssignment(prop, prop2, strV2);
171 
172  prop->SetValue(v1);
173  TestPropPolymorphicAssignment(prop2, prop, strV1);
174  }
175 
176  void TestBoolProperty_Success()
177  {
178  TestProperty<mitk::BoolProperty>(false, true, "0", "1");
179  }
180 
181  void TestIntProperty_Success()
182  {
183  TestProperty<mitk::IntProperty>(3, 5, "3", "5");
184  }
185 
186  void TestFloatProperty_Success()
187  {
188  TestProperty<mitk::FloatProperty>(0.3f, -23.5f, "0.3", "-23.5");
189  }
190 
191  void TestDoubleProperty_Success()
192  {
193  TestProperty<mitk::DoubleProperty>(64.1f, 2.34f, "64.1", "2.34");
194  }
195 
196  void TestVector3DProperty_Success()
197  {
198  mitk::Vector3D p1;
199  p1[0] = 2.0;
200  p1[1] = 3.0;
201  p1[2] = 4.0;
202  mitk::Vector3D p2;
203  p2[0] = -1.0;
204  p2[1] = 2.0;
205  p2[2] = 3.0;
206  TestProperty<mitk::Vector3DProperty>(p1, p2, "[2, 3, 4]", "[-1, 2, 3]");
207  }
208 
209  void TestPoint3D_Success()
210  {
211  mitk::Point3D p1;
212  p1[0] = 2.0;
213  p1[1] = 3.0;
214  p1[2] = 4.0;
215  mitk::Point3D p2;
216  p2[0] = -1.0;
217  p2[1] = 2.0;
218  p2[2] = 3.0;
219  TestProperty<mitk::Point3dProperty>(p1, p2, "[2, 3, 4]", "[-1, 2, 3]");
220  }
221 
222  void TestPoint4D_Success()
223  {
224  mitk::Point4D p1;
225  p1[0] = 2.0;
226  p1[1] = 3.0;
227  p1[2] = 4.0;
228  p1[3] = -2.0;
229  mitk::Point4D p2;
230  p2[0] = -1.0;
231  p2[1] = 2.0;
232  p2[2] = 3.0;
233  p2[3] = 5.0;
234  TestProperty<mitk::Point4dProperty>(p1, p2, "[2, 3, 4, -2]", "[-1, 2, 3, 5]");
235  }
236 
237  void TestPoint3I_Success()
238  {
239  mitk::Point3I p1;
240  p1[0] = 2;
241  p1[1] = 3;
242  p1[2] = 4;
243  mitk::Point3I p2;
244  p2[0] = 8;
245  p2[1] = 7;
246  p2[2] = 6;
247  TestProperty<mitk::Point3iProperty>(p1, p2, "[2, 3, 4]", "[8, 7, 6]");
248  }
249 
250  void TestFloatLookupTable_Success()
251  {
253  lut1.SetTableValue(1, 0.3f);
254  lut1.SetTableValue(4, 323.7f);
255 
257  lut2.SetTableValue(6, -0.3f);
258  lut2.SetTableValue(2, 25.7f);
259 
260  TestProperty<mitk::FloatLookupTableProperty>(lut1, lut2, "[1 -> 0.3, 4 -> 323.7]", "[2 -> 25.7, 6 -> -0.3]");
261  }
262 
263  void TestBoolLookupTable_Success()
264  {
266  lut1.SetTableValue(3, false);
267  lut1.SetTableValue(5, true);
268 
270  lut2.SetTableValue(1, false);
271  lut2.SetTableValue(2, false);
272  TestProperty<mitk::BoolLookupTableProperty>(lut1, lut2, "[3 -> 0, 5 -> 1]", "[1 -> 0, 2 -> 0]");
273  }
274 
275  void TestIntLookupTable_Success()
276  {
278  lut1.SetTableValue(5, -12);
279  lut1.SetTableValue(7, 3);
280 
282  lut2.SetTableValue(4, -6);
283  lut2.SetTableValue(8, -45);
284 
285  TestProperty<mitk::IntLookupTableProperty>(lut1, lut2, "[5 -> -12, 7 -> 3]", "[4 -> -6, 8 -> -45]");
286  }
287 
288  void TestStringLookupTable_Success()
289  {
291  lut1.SetTableValue(0, "a");
292  lut1.SetTableValue(2, "b");
293 
295  lut2.SetTableValue(0, "a");
296  lut2.SetTableValue(2, "c");
297 
298  TestProperty<mitk::StringLookupTableProperty>(lut1, lut2, "[0 -> a, 2 -> b]", "[0 -> a, 2 -> c]");
299  }
300 
301  void TestAnnotationProperty_Success()
302  {
303  std::string label1("Label1");
304  mitk::Point3D point1;
305  point1[0] = 3;
306  point1[1] = 5;
307  point1[2] = -4;
308  std::string str1 = "Label1[3, 5, -4]";
309 
310  std::string label2("Label2");
311  mitk::Point3D point2;
312  point2[0] = -2;
313  point2[1] = 8;
314  point2[2] = -4;
315  std::string str2 = "Label2[-2, 8, -4]";
316 
318  MITK_TEST_OUTPUT(<< "**** Test [" << prop->GetNameOfClass() << "] ****");
319 
320  CPPUNIT_ASSERT_MESSAGE("Test constructor", prop->GetLabel() == label1 && prop->GetPosition() == point1);
321  std::string msg = std::string("Test GetValueAsString() [") + prop->GetValueAsString() + " == " + str1 + "]";
322  CPPUNIT_ASSERT_MESSAGE(msg, prop->GetValueAsString() == str1);
323 
325  prop2->AddObserver(itk::ModifiedEvent(), m_L.m_Cmd.GetPointer());
326  CPPUNIT_ASSERT_MESSAGE("Test not modified", !m_L.m_Modified);
327  prop2->SetLabel(label2);
328  CPPUNIT_ASSERT_MESSAGE("Test modified", m_L.Pop());
329  prop2->SetPosition(point2);
330  CPPUNIT_ASSERT_MESSAGE("Test modified", m_L.Pop());
331  CPPUNIT_ASSERT_MESSAGE("Test Setter", prop2->GetLabel() == label2 && prop2->GetPosition() == point2);
332 
333  prop2->SetLabel(label2);
334  CPPUNIT_ASSERT_MESSAGE("Test for no modification", !m_L.Pop());
335  prop2->SetPosition(point2);
336  CPPUNIT_ASSERT_MESSAGE("Test for no modification", !m_L.Pop());
337 
338  TestPropInequality(prop, prop2);
339  TestPropAssignment(prop, prop2, str2);
340 
341  prop->SetLabel(label1);
342  prop->SetPosition(point1);
343  TestPropPolymorphicAssignment(prop2, prop, str1);
344  }
345 
346  void TestClippingProperty_Success()
347  {
348  bool enabled1 = true;
349  mitk::Point3D point1;
350  point1[0] = 3;
351  point1[1] = 5;
352  point1[2] = -4;
353  mitk::Vector3D vec1;
354  vec1[0] = 0;
355  vec1[1] = 2;
356  vec1[2] = -1;
357  std::string str1 = "1[3, 5, -4][0, 2, -1]";
358 
359  bool enabled2 = false;
360  mitk::Point3D point2;
361  point2[0] = -2;
362  point2[1] = 8;
363  point2[2] = -4;
364  mitk::Vector3D vec2;
365  vec2[0] = 0;
366  vec2[1] = 2;
367  vec2[2] = 4;
368  std::string str2 = "0[-2, 8, -4][0, 2, 4]";
369 
371  MITK_TEST_OUTPUT(<< "**** Test [" << prop->GetNameOfClass() << "] ****");
372 
373  CPPUNIT_ASSERT_MESSAGE("Test constructor",
374  prop->GetClippingEnabled() == enabled1 && prop->GetOrigin() == point1 && prop->GetNormal() == vec1);
375  std::string msg = std::string("Test GetValueAsString() [") + prop->GetValueAsString() + " == " + str1 + "]";
376  CPPUNIT_ASSERT_MESSAGE(msg, prop->GetValueAsString() == str1);
377 
379  prop2->AddObserver(itk::ModifiedEvent(), m_L.m_Cmd.GetPointer());
380  CPPUNIT_ASSERT_MESSAGE("Test not modified", !m_L.m_Modified);
381  prop2->SetClippingEnabled(enabled2);
382  CPPUNIT_ASSERT_MESSAGE("Test not modified", !m_L.Pop());
383  prop2->SetOrigin(point2);
384  CPPUNIT_ASSERT_MESSAGE("Test modified", m_L.Pop());
385  prop2->SetNormal(vec2);
386  CPPUNIT_ASSERT_MESSAGE("Test modified", m_L.Pop());
387 
388  CPPUNIT_ASSERT_MESSAGE("Test Setter",
389  prop2->GetClippingEnabled() == enabled2 && prop2->GetOrigin() == point2 && prop2->GetNormal() == vec2);
390 
391  prop2->SetClippingEnabled(enabled2);
392  CPPUNIT_ASSERT_MESSAGE("Test for no modification", !m_L.Pop());
393  prop2->SetOrigin(point2);
394  CPPUNIT_ASSERT_MESSAGE("Test for no modification", !m_L.Pop());
395  prop2->SetNormal(vec2);
396  CPPUNIT_ASSERT_MESSAGE("Test for no modification", !m_L.Pop());
397 
398  TestPropInequality(prop, prop2);
399  TestPropAssignment(prop, prop2, str2);
400 
401  prop->SetClippingEnabled(enabled1);
402  prop->SetOrigin(point1);
403  prop->SetNormal(vec1);
404  TestPropPolymorphicAssignment(prop2, prop, str1);
405  }
406 
407  void TestColorProperty_Success()
408  {
409  mitk::Color c1;
410  c1[0] = 0.2;
411  c1[1] = 0.6;
412  c1[2] = 0.8;
413  mitk::Color c2;
414  c2[0] = 0.2;
415  c2[1] = 0.4;
416  c2[2] = 0.1;
417  TestProperty<mitk::ColorProperty>(c1, c2, "0.2 0.6 0.8", "0.2 0.4 0.1");
418  }
419 
420  void TestLevelWindowProperty_Success()
421  {
422  mitk::LevelWindow lw1(50, 100);
423  mitk::LevelWindow lw2(120, 30);
424  TestProperty<mitk::LevelWindowProperty>(lw1, lw2, "L:50 W:100", "L:120 W:30");
425  }
426 
427  void TestSmartPointerProperty_Success()
428  {
429  itk::Object::Pointer sp1 = itk::Object::New();
430  itk::Object::Pointer sp2 = itk::Object::New();
431  // to generate the UIDs, we set the smartpointers
434 
435  TestProperty<mitk::SmartPointerProperty>(sp1, sp2, spp1->GetReferenceUIDFor(sp1), spp2->GetReferenceUIDFor(sp2));
436  }
437 
438  void TestStringProperty_Success()
439  {
440  TestProperty<mitk::StringProperty>("1", "2", "1", "2");
441  }
442 
443  void TestTransferFunctionProperty_Success()
444  {
447  tf2->AddScalarOpacityPoint(0.4, 0.8);
448  std::stringstream ss;
449  ss << tf1;
450  std::string strTF1 = ss.str();
451  ss.str("");
452  ss << tf2;
453  std::string strTF2 = ss.str();
454  TestProperty<mitk::TransferFunctionProperty>(tf1, tf2, strTF1, strTF2);
455  }
456 
457  void TestWeakPointerProperty_Success()
458  {
459  itk::Object::Pointer sp1 = itk::Object::New();
460  itk::Object::Pointer sp2 = itk::Object::New();
461  mitk::WeakPointerProperty::ValueType wp1 = sp1.GetPointer();
462  mitk::WeakPointerProperty::ValueType wp2 = sp2.GetPointer();
463  std::stringstream ss;
464  ss << sp1.GetPointer();
465  std::string str1 = ss.str();
466  ss.str("");
467  ss << sp2.GetPointer();
468  std::string str2 = ss.str();
469 
470  TestProperty<mitk::WeakPointerProperty>(wp1, wp2, str1, str2);
471  }
472 
473  void TestLookupTablePropertyProperty_Success()
474  {
476  lut1->GetVtkLookupTable()->SetTableValue(0, 0.2, 0.3, 0.4);
478  lut2->GetVtkLookupTable()->SetTableValue(0, 0.2, 0.4, 0.4);
479  std::stringstream ss;
480  ss << lut1;
481  std::string strLUT1 = ss.str();
482  ss.str("");
483  ss << lut2;
484  std::string strLUT2 = ss.str();
485  TestProperty<mitk::LookupTableProperty>(lut1, lut2, strLUT1, strLUT2);
486  }
487 };
488 MITK_TEST_SUITE_REGISTRATION(mitkProperty)
489 
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
void SetTableValue(IdentifierType id, ValueType value)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
#define MITK_TEST_OUTPUT(x)
Output some text.
The LevelWindow class Class to store level/window values.
static Pointer New()
ValueType
Type of the value held by a Value object.
Definition: jsoncpp.h:345
Test fixture for parameterized tests.
static Pointer New()
itk::WeakPointer< itk::Object > ValueType
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
static Pointer New()
static Pointer New()
specializations of GenericLookupTable