Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkVectorPropertySerializerTest.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 "mitkTestFixture.h"
14 #include "mitkTestingMacros.h"
15 
17 #include "mitkVectorProperty.h"
18 #include <mitkLexicalCast.h>
19 
20 #include <limits>
21 #include <cmath>
22 
23 #include "mitkEqual.h"
24 
39 class mitkVectorPropertySerializerTestSuite : public mitk::TestFixture
40 {
41  CPPUNIT_TEST_SUITE(mitkVectorPropertySerializerTestSuite);
42  MITK_TEST(TestSerialize<int>);
43  MITK_TEST(TestSerialize<double>);
44  MITK_TEST(TestSerializeIntTypedef);
45  MITK_TEST(TestSerializeDoubleTypedef);
46  CPPUNIT_TEST_SUITE_END();
47 
48 public:
49  void setUp() override {}
50  void tearDown() override {}
51  template <typename DATATYPE>
52  typename mitk::VectorProperty<DATATYPE>::Pointer MakeExampleProperty()
53  {
55  std::vector<DATATYPE> data;
56  data.push_back(static_cast<DATATYPE>(-918273674.6172838));
57  data.push_back(0);
58  data.push_back(static_cast<DATATYPE>(+6172838.918273674));
59  data.push_back(sqrt(2));
60  if (std::numeric_limits<DATATYPE>::has_infinity)
61  {
62  data.push_back(std::numeric_limits<DATATYPE>::infinity());
63  data.push_back(-std::numeric_limits<DATATYPE>::infinity());
64  }
65  // do NOT test NaN: cannot be == to itself, so cannot be tested like the others
66  // NaN is covered in a different test (FloatToStringTest at the time of writing this)
67  // data.push_back( std::numeric_limits<DATATYPE>::quiet_NaN() );
68  vectorProperty->SetValue(data);
69 
70  return vectorProperty;
71  }
72 
73  mitk::BaseProperty::Pointer TestSerialize(mitk::BaseProperty *property)
74  {
75  std::string serializername = std::string(property->GetNameOfClass()) + "Serializer";
76 
77  std::list<itk::LightObject::Pointer> allSerializers =
78  itk::ObjectFactoryBase::CreateAllInstance(serializername.c_str());
79  CPPUNIT_ASSERT_EQUAL(size_t(1), allSerializers.size());
80 
81  auto *serializer =
82  dynamic_cast<mitk::BasePropertySerializer *>(allSerializers.begin()->GetPointer());
83  CPPUNIT_ASSERT(serializer != nullptr);
84  if (!serializer)
85  return nullptr;
86 
87  serializer->SetProperty(property);
88  TiXmlElement *serialization(nullptr);
89  try
90  {
91  serialization = serializer->Serialize();
92  }
93  catch (...)
94  {
95  }
96  CPPUNIT_ASSERT(serialization != nullptr);
97  if (!serialization)
98  return nullptr;
99 
100  mitk::BaseProperty::Pointer restoredProperty = serializer->Deserialize(serialization);
101  CPPUNIT_ASSERT(restoredProperty.IsNotNull());
102  return restoredProperty;
103  }
104 
105  template <typename DATATYPE>
106  void TestSerialize()
107  {
108  auto property = MakeExampleProperty<DATATYPE>();
109  mitk::BaseProperty::Pointer restored_property = TestSerialize(property);
110 
111  typename mitk::VectorProperty<DATATYPE>::Pointer restored_vector_property =
112  dynamic_cast<mitk::VectorProperty<DATATYPE> *>(restored_property.GetPointer());
113  CPPUNIT_ASSERT(restored_vector_property.IsNotNull());
114 
115  auto orig_vector = property->GetValue();
116  auto restored_vector = restored_vector_property->GetValue();
117 
118  CPPUNIT_ASSERT_EQUAL(orig_vector.size(), restored_vector.size());
119  for (unsigned int i = 0; i < orig_vector.size(); ++i)
120  {
121  // compare using Equal, i.e. with tolerance of mitk::eps
122  CPPUNIT_ASSERT_MESSAGE(std::string("Verifying element ") + boost::lexical_cast<std::string>(i),
123  mitk::Equal(orig_vector[i], restored_vector[i]));
124  }
125  }
126 
127  void TestSerializeIntTypedef()
128  {
129  mitk::IntVectorProperty::Pointer intVectorProperty = MakeExampleProperty<int>().GetPointer();
130  TestSerialize(intVectorProperty.GetPointer());
131  }
132 
133  void TestSerializeDoubleTypedef()
134  {
135  mitk::DoubleVectorProperty::Pointer doubleVectorProperty = MakeExampleProperty<double>().GetPointer();
136  TestSerialize(doubleVectorProperty.GetPointer());
137  }
138 
139 }; // class
140 
141 MITK_TEST_SUITE_REGISTRATION(mitkVectorPropertySerializer)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
virtual const VectorType & GetValue() const
returns a const reference to the contained vector
Abstract base class for properties.
Test fixture for parameterized tests.
Base class for objects that serialize BaseProperty types.
Providing a std::vector as property.
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
static Pointer New()