Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkPropertyListTest.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 #include "mitkLookupTables.h"
14 #include "mitkProperties.h"
15 #include "mitkPropertyList.h"
16 #include "mitkStringProperty.h"
17 #include "mitkExceptionMacro.h"
18 
19 #include <iostream>
20 
21 int mitkPropertyListTest(int /*argc*/, char * /*argv*/ [])
22 {
24  std::cout << "Testing mitk::PropertyList::New(): ";
25  propList = mitk::PropertyList::New();
26  if (propList.IsNull())
27  {
28  std::cout << "[FAILED]" << std::endl;
29  return EXIT_FAILURE;
30  }
31  else
32  {
33  std::cout << "[PASSED]" << std::endl;
34  }
37  std::cout << "Testing BoolProperty ==: ";
38  if (!(*boolProp2 == *boolProp))
39  {
40  std::cout << "[FAILED]" << std::endl;
41  return EXIT_FAILURE;
42  }
43  std::cout << "[PASSED]" << std::endl;
44  unsigned long tBefore, tAfter;
45 
46  std::cout << "Testing SetProperty() with new key value: ";
47  tBefore = propList->GetMTime();
48  propList->SetProperty("test", boolProp);
49  tAfter = propList->GetMTime();
50  if (!(tAfter > tBefore))
51  {
52  std::cout << "[FAILED]" << std::endl;
53  return EXIT_FAILURE;
54  }
55  std::cout << "[PASSED]" << std::endl;
56 
57  std::cout << "Testing SetProperty() with changed property value: ";
58  tBefore = propList->GetMTime();
59  propList->SetProperty("test", mitk::BoolProperty::New(true));
60  tAfter = propList->GetMTime();
61  if (!(tAfter > tBefore))
62  {
63  std::cout << "[FAILED]" << std::endl;
64  return EXIT_FAILURE;
65  }
66  std::cout << "[PASSED]" << std::endl;
67 
68  std::cout << "Testing SetProperty() with unchanged property value: ";
69  tBefore = propList->GetMTime();
70  propList->SetProperty("test", mitk::BoolProperty::New(true));
71  tAfter = propList->GetMTime();
72  if (tBefore != tAfter)
73  {
74  std::cout << "[FAILED]" << std::endl;
75  return EXIT_FAILURE;
76  }
77  std::cout << "[PASSED]" << std::endl;
78 
79  std::cout << "Testing MTime correctness when changing property value: ";
80  boolProp = mitk::BoolProperty::New(true);
81  propList->ReplaceProperty("test", boolProp);
82  tBefore = propList->GetMTime();
83  boolProp->SetValue(true);
84  tAfter = propList->GetMTime();
85  boolProp->SetValue(false);
86  unsigned long tAfterAll = propList->GetMTime();
87 
88  if (tBefore != tAfter || tAfterAll <= tAfter)
89  {
90  std::cout << "[FAILED]" << std::endl;
91  return EXIT_FAILURE;
92  }
93  std::cout << "[PASSED]" << std::endl;
94 
95  std::cout << "Testing MTime correctness when calling SetProperty twice: ";
96  boolProp = mitk::BoolProperty::New(true);
97  propList->SetProperty("test", boolProp);
98  tBefore = propList->GetMTime();
99  propList->SetProperty("test", boolProp);
100  tAfter = propList->GetMTime();
101 
102  if (tBefore != tAfter)
103  {
104  std::cout << "[FAILED]" << std::endl;
105  return EXIT_FAILURE;
106  }
107  std::cout << "[PASSED]" << std::endl;
108 
109  std::cout << "Testing if existing properties survive SetProperty: ";
110  propList->SetProperty("test", boolProp);
111  mitk::BaseProperty *bpBefore = propList->GetProperty("test");
112  propList->SetProperty("test", boolProp2);
113  mitk::BaseProperty *bpAfter = propList->GetProperty("test");
114 
115  if (bpBefore != bpAfter || bpAfter == nullptr)
116  {
117  std::cout << std::endl;
118  std::cout << "[FAILED]" << std::endl;
119  return EXIT_FAILURE;
120  }
121  std::cout << "[PASSED]" << std::endl;
122 
123  std::cout << "Testing if existing properties survive ReplaceProperty: ";
124  propList->SetProperty("test", boolProp);
125  bpBefore = propList->GetProperty("test");
126  propList->ReplaceProperty("test", boolProp2);
127  bpAfter = propList->GetProperty("test");
128 
129  if (bpBefore == bpAfter || bpAfter == nullptr)
130  {
131  std::cout << std::endl;
132  std::cout << "[FAILED]" << std::endl;
133  return EXIT_FAILURE;
134  }
135  std::cout << "[PASSED]" << std::endl;
136 
137  // std::cout << "Testing output of PropertyList to file: ";
138  // if ( TestXMLWriter() )
139  // std::cout << "[PASSED]" << std::endl;
140  // else
141  // return EXIT_FAILURE;
142 
143  std::cout << "Testing GetPropertyValue(bool): ";
145  propList->SetProperty("gpvBool", gpvTest);
146  bool b = false;
147  bool getPropertyValueReturnValue = propList->GetPropertyValue<bool>("gpvBool", b);
148  if ((getPropertyValueReturnValue == true) && (b == gpvTest->GetValue()))
149  std::cout << "[PASSED]" << std::endl;
150  else
151  {
152  std::cout << "Oh, not goot:"
153  "\nWe called propList->GetPropertyValue<bool>('gpvBool', b) and it returned "
154  << getPropertyValueReturnValue << "\nThen we compared b [" << b << "] and gpvTest->GetValue() ["
155  << gpvTest->GetValue() << "]" << std::endl;
156  std::cout << "[FAILED]" << std::endl;
157  return EXIT_FAILURE;
158  }
159 
160  std::cout << "Testing GetPropertyValue(float): ";
162  propList->SetProperty("gpvfloat", gpvTest2);
163  float v = -1.23;
164  if ((propList->GetPropertyValue<float>("gpvfloat", v) == true) && (v == gpvTest2->GetValue()))
165  std::cout << "[PASSED]" << std::endl;
166  else
167  {
168  std::cout << "[FAILED]" << std::endl;
169  return EXIT_FAILURE;
170  }
171 
172  std::cout << "Testing GetPropertyValue(BoolLookupTable): ";
174  blt.SetTableValue(17, true);
175  propList->SetProperty("blutprop", mitk::BoolLookupTableProperty::New(blt));
176  try
177  {
179  if ((propList->GetPropertyValue<mitk::BoolLookupTable>("blutprop", blut) == true) &&
180  (blut.GetTableValue(17) == true))
181  std::cout << "[PASSED]" << std::endl;
182  else
183  {
184  std::cout << "[FAILED]" << std::endl;
185  return EXIT_FAILURE;
186  }
187  }
188  catch (...)
189  {
190  std::cout << "Exception thrown! [FAILED]" << std::endl;
191  return EXIT_FAILURE;
192  }
193 
194  {
195  std::cout << "Testing GetBoolProperty(): ";
197  propList->ReplaceProperty("test", prop);
198  bool v = false;
199  if ((propList->GetBoolProperty("test", v) == true) && (v == prop->GetValue()))
200  std::cout << "[PASSED]" << std::endl;
201  else
202  {
203  std::cout << "[FAILED]" << std::endl;
204  return EXIT_FAILURE;
205  }
206  }
207  {
208  std::cout << "Testing GetIntProperty(): ";
210  propList->ReplaceProperty("test", prop);
211  int v = 1;
212  if ((propList->GetIntProperty("test", v) == true) && (v == prop->GetValue()))
213  std::cout << "[PASSED]" << std::endl;
214  else
215  {
216  std::cout << "[FAILED]" << std::endl;
217  return EXIT_FAILURE;
218  }
219  }
220  {
221  std::cout << "Testing GetFloatProperty(): ";
223  propList->ReplaceProperty("test", prop);
224  float v = 1.2;
225  if ((propList->GetFloatProperty("test", v) == true) && (v == prop->GetValue()))
226  std::cout << "[PASSED]" << std::endl;
227  else
228  {
229  std::cout << "[FAILED]" << std::endl;
230  return EXIT_FAILURE;
231  }
232  }
233  {
234  std::cout << "Testing GetStringProperty(): ";
236  propList->ReplaceProperty("test", prop);
237  std::string v = "";
238  if ((propList->GetStringProperty("test", v) == true) && (v == prop->GetValue()))
239  std::cout << "[PASSED]" << std::endl;
240  else
241  {
242  std::cout << "[FAILED]" << std::endl;
243  return EXIT_FAILURE;
244  }
245  }
246  {
247 
248  std::cout << "Testing RemoveProperty(): ";
249  propList->RemoveProperty("test");
250  if (propList->GetProperty("test")!=nullptr)
251  {
252  std::cout << "[FAILED]" << std::endl;
253  return EXIT_FAILURE;
254  }
255  std::cout << "[PASSED]" << std::endl;
256  }
257 
258  std::cout << "Testing SetProperty() with no property (nullptr): ";
259  tBefore = propList->GetMTime();
260  propList->SetProperty("nullprop", nullptr);
261  tAfter = propList->GetMTime();
262  if (!(tAfter == tBefore))
263  {
264  std::cout << "[FAILED]" << std::endl;
265  return EXIT_FAILURE;
266  }
267  std::cout << "[PASSED]" << std::endl;
268 
269  std::cout << "Testing SetProperty() with invalid (empty) name: ";
270  try
271  {
272  propList->SetProperty("", boolProp);
273  std::cout << "[FAILED]" << std::endl;
274  return EXIT_FAILURE;
275  }
276  catch (const mitk::Exception& /*e*/)
277  {
278  std::cout << "[PASSED]" << std::endl;
279  }
280  catch (...)
281  {
282  std::cout << "[FAILED]" << std::endl;
283  return EXIT_FAILURE;
284  }
285 
286  std::cout << "[TEST DONE]" << std::endl;
287  return EXIT_SUCCESS;
288 }
static Pointer New()
void SetTableValue(IdentifierType id, ValueType value)
static Pointer New()
Abstract base class for properties.
ValueType GetTableValue(IdentifierType id) const
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
static Pointer New()
static Pointer New()
int mitkPropertyListTest(int, char *[])
specializations of GenericLookupTable
static Pointer New()