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