Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkTestingMacros.h"
18 
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 
31 #include <itkCommand.h>
32 
33 struct PropertyModifiedListener
34 {
35  typedef itk::SimpleMemberCommand<PropertyModifiedListener> CmdType;
36 
37  PropertyModifiedListener() : m_Modified(false), m_Cmd(CmdType::New())
38  {
39  m_Cmd->SetCallbackFunction(this, &PropertyModifiedListener::Modified);
40  }
41 
42  void Modified() { m_Modified = true; }
43  bool Pop()
44  {
45  bool b = m_Modified;
46  m_Modified = false;
47  return b;
48  }
49 
50  bool m_Modified;
51  CmdType::Pointer m_Cmd;
52 };
53 
54 template <class T>
55 void TestPropInequality(T prop, T prop2)
56 {
57  mitk::BaseProperty::Pointer baseProp2(prop2.GetPointer());
58  MITK_TEST_CONDITION_REQUIRED(!(*prop == *prop2), "Test inequality 1");
59  MITK_TEST_CONDITION_REQUIRED(!(*prop == *baseProp2), "Test polymorphic inequality 1");
60  MITK_TEST_CONDITION_REQUIRED(!(*baseProp2 == *prop), "Test polymorphic inequality 2");
61 }
62 
63 template <class T>
64 void TestPropAssignment(T prop, T prop2, const std::string &strProp)
65 {
66  PropertyModifiedListener l;
67  unsigned long tag = prop->AddObserver(itk::ModifiedEvent(), l.m_Cmd.GetPointer());
68 
69  mitk::BaseProperty::Pointer baseProp2(prop2.GetPointer());
70  *prop = *baseProp2;
71  MITK_TEST_CONDITION_REQUIRED(l.Pop(), "Test modified event");
72  std::string msg = std::string("Test assignment [") + prop->GetValueAsString() + " == " + strProp + "]";
73  MITK_TEST_CONDITION_REQUIRED(prop->GetValueAsString() == strProp, msg);
74  MITK_TEST_CONDITION_REQUIRED(*prop == *prop2, "Test equality");
75  MITK_TEST_CONDITION_REQUIRED(*prop == *baseProp2, "Test equality");
76  MITK_TEST_CONDITION_REQUIRED(*baseProp2 == *prop, "Test polymorphic equality");
77 
78  prop->RemoveObserver(tag);
79 }
80 
81 template <class T>
82 void TestPropPolymorphicAssignment(T prop, T prop2, const std::string &strProp)
83 {
84  mitk::BaseProperty::Pointer baseProp(prop.GetPointer());
85 
86  PropertyModifiedListener l;
87  unsigned long tag = baseProp->AddObserver(itk::ModifiedEvent(), l.m_Cmd.GetPointer());
88 
89  *baseProp = *prop2;
90  MITK_TEST_CONDITION_REQUIRED(l.Pop(), "Test modified event");
91  std::string msg =
92  std::string("Test polymorphic assignment [") + baseProp->GetValueAsString() + " == " + strProp + "]";
93  MITK_TEST_CONDITION_REQUIRED(baseProp->GetValueAsString() == strProp, msg);
94  MITK_TEST_CONDITION_REQUIRED(*prop == *prop2, "Test equality");
95  MITK_TEST_CONDITION_REQUIRED(*prop2 == *baseProp, "Test equality");
96  MITK_TEST_CONDITION_REQUIRED(*baseProp == *prop2, "Test polymorphic equality");
97 
98  baseProp->RemoveObserver(tag);
99 }
100 
101 template <class T>
102 void TestPropCloning(T prop)
103 {
104  T prop2 = prop->Clone();
105  MITK_TEST_CONDITION_REQUIRED(prop.GetPointer() != prop2.GetPointer(), "Test clone pointer")
106  MITK_TEST_CONDITION_REQUIRED(*prop == *prop2, "Test equality of the clone")
107 }
108 
109 template <class T>
110 void TestProperty(const typename T::ValueType &v1,
111  const typename T::ValueType &v2,
112  const std::string &strV1,
113  const std::string &strV2)
114 {
115  PropertyModifiedListener l;
116 
117  typename T::Pointer prop = T::New(v1);
118  MITK_TEST_OUTPUT(<< "**** Test [" << prop->GetNameOfClass() << "] ****");
119 
120  MITK_TEST_CONDITION_REQUIRED(prop->GetValue() == v1, "Test constructor");
121  std::string msg = std::string("Test GetValueAsString() [") + prop->GetValueAsString() + " == " + strV1 + "]";
122  MITK_TEST_CONDITION_REQUIRED(prop->GetValueAsString() == strV1, msg);
123 
124  TestPropCloning(prop);
125 
126  typename T::Pointer prop2 = T::New();
127  prop2->AddObserver(itk::ModifiedEvent(), l.m_Cmd.GetPointer());
128  MITK_TEST_CONDITION_REQUIRED(!l.m_Modified, "Test modified");
129  prop2->SetValue(v2);
130  MITK_TEST_CONDITION_REQUIRED(l.Pop(), "Test modified");
131  MITK_TEST_CONDITION_REQUIRED(prop2->GetValue() == v2, "Test SetValue()");
132 
133  prop2->SetValue(v2);
134  MITK_TEST_CONDITION_REQUIRED(!l.Pop(), "Test for no modification");
135 
136  TestPropInequality(prop, prop2);
137  TestPropAssignment(prop, prop2, strV2);
138 
139  prop->SetValue(v1);
140  TestPropPolymorphicAssignment(prop2, prop, strV1);
141 }
142 
144 {
145  TestProperty<mitk::BoolProperty>(false, true, "0", "1");
146  TestProperty<mitk::IntProperty>(3, 5, "3", "5");
147  TestProperty<mitk::FloatProperty>(0.3f, -23.5f, "0.3", "-23.5");
148  TestProperty<mitk::DoubleProperty>(64.1f, 2.34f, "64.1", "2.34");
149 
150  {
151  mitk::Vector3D p1;
152  p1[0] = 2.0;
153  p1[1] = 3.0;
154  p1[2] = 4.0;
155  mitk::Vector3D p2;
156  p2[0] = -1.0;
157  p2[1] = 2.0;
158  p2[2] = 3.0;
159  TestProperty<mitk::Vector3DProperty>(p1, p2, "[2, 3, 4]", "[-1, 2, 3]");
160  }
161 
162  {
163  mitk::Point3D p1;
164  p1[0] = 2.0;
165  p1[1] = 3.0;
166  p1[2] = 4.0;
167  mitk::Point3D p2;
168  p2[0] = -1.0;
169  p2[1] = 2.0;
170  p2[2] = 3.0;
171  TestProperty<mitk::Point3dProperty>(p1, p2, "[2, 3, 4]", "[-1, 2, 3]");
172  }
173 
174  {
175  mitk::Point4D p1;
176  p1[0] = 2.0;
177  p1[1] = 3.0;
178  p1[2] = 4.0;
179  p1[3] = -2.0;
180  mitk::Point4D p2;
181  p2[0] = -1.0;
182  p2[1] = 2.0;
183  p2[2] = 3.0;
184  p2[3] = 5.0;
185  TestProperty<mitk::Point4dProperty>(p1, p2, "[2, 3, 4, -2]", "[-1, 2, 3, 5]");
186  }
187 
188  {
189  mitk::Point3I p1;
190  p1[0] = 2;
191  p1[1] = 3;
192  p1[2] = 4;
193  mitk::Point3I p2;
194  p2[0] = 8;
195  p2[1] = 7;
196  p2[2] = 6;
197  TestProperty<mitk::Point3iProperty>(p1, p2, "[2, 3, 4]", "[8, 7, 6]");
198  }
199 
200  {
202  lut1.SetTableValue(1, 0.3f);
203  lut1.SetTableValue(4, 323.7f);
204 
206  lut2.SetTableValue(6, -0.3f);
207  lut2.SetTableValue(2, 25.7f);
208 
209  TestProperty<mitk::FloatLookupTableProperty>(lut1, lut2, "[1 -> 0.3, 4 -> 323.7]", "[2 -> 25.7, 6 -> -0.3]");
210  }
211 
212  {
214  lut1.SetTableValue(3, false);
215  lut1.SetTableValue(5, true);
216 
218  lut2.SetTableValue(1, false);
219  lut2.SetTableValue(2, false);
220  TestProperty<mitk::BoolLookupTableProperty>(lut1, lut2, "[3 -> 0, 5 -> 1]", "[1 -> 0, 2 -> 0]");
221  }
222 
223  {
225  lut1.SetTableValue(5, -12);
226  lut1.SetTableValue(7, 3);
227 
229  lut2.SetTableValue(4, -6);
230  lut2.SetTableValue(8, -45);
231 
232  TestProperty<mitk::IntLookupTableProperty>(lut1, lut2, "[5 -> -12, 7 -> 3]", "[4 -> -6, 8 -> -45]");
233  }
234 
235  {
237  lut1.SetTableValue(0, "a");
238  lut1.SetTableValue(2, "b");
239 
241  lut2.SetTableValue(0, "a");
242  lut2.SetTableValue(2, "c");
243 
244  TestProperty<mitk::StringLookupTableProperty>(lut1, lut2, "[0 -> a, 2 -> b]", "[0 -> a, 2 -> c]");
245  }
246 }
247 
249 {
250  PropertyModifiedListener l;
251 
252  std::string label1("Label1");
253  mitk::Point3D point1;
254  point1[0] = 3;
255  point1[1] = 5;
256  point1[2] = -4;
257  std::string str1 = "Label1[3, 5, -4]";
258 
259  std::string label2("Label2");
260  mitk::Point3D point2;
261  point2[0] = -2;
262  point2[1] = 8;
263  point2[2] = -4;
264  std::string str2 = "Label2[-2, 8, -4]";
265 
267  MITK_TEST_OUTPUT(<< "**** Test [" << prop->GetNameOfClass() << "] ****");
268 
269  MITK_TEST_CONDITION_REQUIRED(prop->GetLabel() == label1 && prop->GetPosition() == point1, "Test constructor");
270  std::string msg = std::string("Test GetValueAsString() [") + prop->GetValueAsString() + " == " + str1 + "]";
271  MITK_TEST_CONDITION_REQUIRED(prop->GetValueAsString() == str1, msg);
272 
274  prop2->AddObserver(itk::ModifiedEvent(), l.m_Cmd.GetPointer());
275  MITK_TEST_CONDITION_REQUIRED(!l.m_Modified, "Test not modified");
276  prop2->SetLabel(label2);
277  MITK_TEST_CONDITION_REQUIRED(l.Pop(), "Test modified");
278  prop2->SetPosition(point2);
279  MITK_TEST_CONDITION_REQUIRED(l.Pop(), "Test modified");
280  MITK_TEST_CONDITION_REQUIRED(prop2->GetLabel() == label2 && prop2->GetPosition() == point2, "Test Setter");
281 
282  prop2->SetLabel(label2);
283  MITK_TEST_CONDITION_REQUIRED(!l.Pop(), "Test for no modification");
284  prop2->SetPosition(point2);
285  MITK_TEST_CONDITION_REQUIRED(!l.Pop(), "Test for no modification");
286 
287  TestPropInequality(prop, prop2);
288  TestPropAssignment(prop, prop2, str2);
289 
290  prop->SetLabel(label1);
291  prop->SetPosition(point1);
292  TestPropPolymorphicAssignment(prop2, prop, str1);
293 }
294 
296 {
297  PropertyModifiedListener l;
298 
299  bool enabled1 = true;
300  mitk::Point3D point1;
301  point1[0] = 3;
302  point1[1] = 5;
303  point1[2] = -4;
304  mitk::Vector3D vec1;
305  vec1[0] = 0;
306  vec1[1] = 2;
307  vec1[2] = -1;
308  std::string str1 = "1[3, 5, -4][0, 2, -1]";
309 
310  bool enabled2 = false;
311  mitk::Point3D point2;
312  point2[0] = -2;
313  point2[1] = 8;
314  point2[2] = -4;
315  mitk::Vector3D vec2;
316  vec2[0] = 0;
317  vec2[1] = 2;
318  vec2[2] = 4;
319  std::string str2 = "0[-2, 8, -4][0, 2, 4]";
320 
322  MITK_TEST_OUTPUT(<< "**** Test [" << prop->GetNameOfClass() << "] ****");
323 
325  prop->GetClippingEnabled() == enabled1 && prop->GetOrigin() == point1 && prop->GetNormal() == vec1,
326  "Test constructor");
327  std::string msg = std::string("Test GetValueAsString() [") + prop->GetValueAsString() + " == " + str1 + "]";
328  MITK_TEST_CONDITION_REQUIRED(prop->GetValueAsString() == str1, msg);
329 
331  prop2->AddObserver(itk::ModifiedEvent(), l.m_Cmd.GetPointer());
332  MITK_TEST_CONDITION_REQUIRED(!l.m_Modified, "Test not modified");
333  prop2->SetClippingEnabled(enabled2);
334  MITK_TEST_CONDITION_REQUIRED(!l.Pop(), "Test not modified");
335  prop2->SetOrigin(point2);
336  MITK_TEST_CONDITION_REQUIRED(l.Pop(), "Test modified");
337  prop2->SetNormal(vec2);
338  MITK_TEST_CONDITION_REQUIRED(l.Pop(), "Test modified");
339 
341  prop2->GetClippingEnabled() == enabled2 && prop2->GetOrigin() == point2 && prop2->GetNormal() == vec2,
342  "Test Setter");
343 
344  prop2->SetClippingEnabled(enabled2);
345  MITK_TEST_CONDITION_REQUIRED(!l.Pop(), "Test for no modification");
346  prop2->SetOrigin(point2);
347  MITK_TEST_CONDITION_REQUIRED(!l.Pop(), "Test for no modification");
348  prop2->SetNormal(vec2);
349  MITK_TEST_CONDITION_REQUIRED(!l.Pop(), "Test for no modification");
350 
351  TestPropInequality(prop, prop2);
352  TestPropAssignment(prop, prop2, str2);
353 
354  prop->SetClippingEnabled(enabled1);
355  prop->SetOrigin(point1);
356  prop->SetNormal(vec1);
357  TestPropPolymorphicAssignment(prop2, prop, str1);
358 }
359 
360 int mitkPropertyTest(int /* argc */, char * /*argv*/ [])
361 {
362  MITK_TEST_BEGIN("Testing MITK Properties")
363 
365 
368 
369  mitk::Color c1;
370  c1[0] = 0.2;
371  c1[1] = 0.6;
372  c1[2] = 0.8;
373  mitk::Color c2;
374  c2[0] = 0.2;
375  c2[1] = 0.4;
376  c2[2] = 0.1;
377  TestProperty<mitk::ColorProperty>(c1, c2, "0.2 0.6 0.8", "0.2 0.4 0.1");
378 
379  mitk::LevelWindow lw1(50, 100);
380  mitk::LevelWindow lw2(120, 30);
381  TestProperty<mitk::LevelWindowProperty>(lw1, lw2, "L:50 W:100", "L:120 W:30");
382 
383  {
386  // to generate the UIDs, we set the smartpointers
389 
390  TestProperty<mitk::SmartPointerProperty>(sp1, sp2, spp1->GetReferenceUIDFor(sp1), spp2->GetReferenceUIDFor(sp2));
391  }
392 
393  TestProperty<mitk::StringProperty>("1", "2", "1", "2");
394 
395  {
398  tf2->AddScalarOpacityPoint(0.4, 0.8);
399  std::stringstream ss;
400  ss << tf1;
401  std::string strTF1 = ss.str();
402  ss.str("");
403  ss << tf2;
404  std::string strTF2 = ss.str();
405  TestProperty<mitk::TransferFunctionProperty>(tf1, tf2, strTF1, strTF2);
406  }
407 
408  {
411  mitk::WeakPointerProperty::ValueType wp1 = sp1.GetPointer();
412  mitk::WeakPointerProperty::ValueType wp2 = sp2.GetPointer();
413  std::stringstream ss;
414  ss << sp1.GetPointer();
415  std::string str1 = ss.str();
416  ss.str("");
417  ss << sp2.GetPointer();
418  std::string str2 = ss.str();
419 
420  TestProperty<mitk::WeakPointerProperty>(wp1, wp2, str1, str2);
421  }
422 
423  {
425  lut1->GetVtkLookupTable()->SetTableValue(0, 0.2, 0.3, 0.4);
427  lut2->GetVtkLookupTable()->SetTableValue(0, 0.2, 0.4, 0.4);
428  std::stringstream ss;
429  ss << lut1;
430  std::string strLUT1 = ss.str();
431  ss.str("");
432  ss << lut2;
433  std::string strLUT2 = ss.str();
434  TestProperty<mitk::LookupTableProperty>(lut1, lut2, strLUT1, strLUT2);
435  }
436 
437  MITK_TEST_END()
438 }
void TestProperty(const typename T::ValueType &v1, const typename T::ValueType &v2, const std::string &strV1, const std::string &strV2)
void TestPropPolymorphicAssignment(T prop, T prop2, const std::string &strProp)
itk::SmartPointer< Self > Pointer
void SetTableValue(IdentifierType id, ValueType value)
void TestPropInequality(T prop, T prop2)
void TestPropAssignment(T prop, T prop2, const std::string &strProp)
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
#define MITK_TEST_OUTPUT(x)
Output some text.
The LevelWindow class Class to store level/window values.
void TestPropCloning(T prop)
void TestClippingProperty()
static Pointer New()
ValueType
Type of the value held by a Value object.
Definition: jsoncpp.h:345
static Pointer New()
itk::WeakPointer< itk::Object > ValueType
void TestAnnotationProperty()
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
void TestGenericProperties()
static Pointer New()
static Pointer New()
specializations of GenericLookupTable
and MITK_TEST_END()
int mitkPropertyTest(int, char *[])
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.