Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkVectorPropertyTest.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 
20 #include "mitkVectorProperty.h"
21 
22 CPPUNIT_NS_BEGIN
23 
27 template <typename DATA>
28 struct assertion_traits<std::vector<DATA>> // specialization for the std::string type
29 {
30  static bool equal(const std::vector<DATA> &x, const std::vector<DATA> &y) { return x == y; }
31  static std::string toString(const std::vector<DATA> &values)
32  {
33  OStringStream ost;
34  for (auto v : values)
35  ost << "'" << v << "' "; // adds quote around the string to see whitespace
36  return ost.str();
37  }
38 };
39 
43 template <typename DATA>
44 struct assertion_traits<mitk::VectorProperty<DATA>> // specialization for the std::string type
45 {
46  static bool equal(const mitk::VectorProperty<DATA> &x, const mitk::VectorProperty<DATA> &y)
47  {
48  return x == y; // use BaseProperty implementation of things
49  }
50 
51  static std::string toString(const mitk::VectorProperty<DATA> &prop) { return prop.GetValueAsString(); }
52 };
53 
54 CPPUNIT_NS_END
55 
59 class mitkVectorPropertyTestSuite : public mitk::TestFixture
60 {
61  CPPUNIT_TEST_SUITE(mitkVectorPropertyTestSuite);
62  MITK_TEST(Instantiate);
63  MITK_TEST(TestSetGet);
64  MITK_TEST(TestIsEqual);
65  MITK_TEST(TestClone);
66  CPPUNIT_TEST_SUITE_END();
67 
70 
71 public:
72  void setUp() override
73  {
74  m_DoubleData = mitk::DoubleVectorProperty::New();
75  m_IntData = mitk::IntVectorProperty::New();
76  }
77 
78  void tearDown() override
79  {
80  m_DoubleData = nullptr;
81  m_IntData = nullptr;
82  }
83 
85  void Instantiate() {}
86  template <typename T>
87  std::vector<T> MakeSimpleList()
88  {
89  std::vector<T> data;
90 
91  data.push_back(-2);
92  data.push_back(1);
93  data.push_back(3);
94 
95  return data;
96  }
97 
99  template <typename T>
100  void TestSetGet(mitk::VectorProperty<T> &prop)
101  {
102  std::vector<T> data = MakeSimpleList<T>();
103 
104  prop.SetValue(data);
105  std::vector<T> stored = prop.GetValue();
106 
107  CPPUNIT_ASSERT_EQUAL_MESSAGE("Result of GetValue() should equal parameter of SetValue(data)", data, stored);
108  }
109 
110  void TestSetGet()
111  {
112  TestSetGet(*m_DoubleData);
113  TestSetGet(*m_IntData);
114  }
115 
117  template <typename T>
118  void TestIsEqual(mitk::VectorProperty<T> &prop)
119  {
120  std::vector<T> data = MakeSimpleList<T>();
121  prop.SetValue(data);
122 
123  std::vector<T> modifiedData(data);
124  modifiedData.back() = -modifiedData.back(); // change last element
126  modifiedProperty->SetValue(modifiedData);
127 
128  CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE("Modified list shall be recognized by IsEqual()",
129  CPPUNIT_ASSERT_EQUAL(*modifiedProperty, prop));
130 
131  modifiedData.pop_back(); // remove last element
132  modifiedProperty->SetValue(modifiedData);
133  CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE("Removed element shall be recognized by IsEqual()",
134  CPPUNIT_ASSERT_EQUAL(*modifiedProperty, prop));
135  }
136 
137  void TestIsEqual()
138  {
139  TestIsEqual(*m_DoubleData);
140  TestIsEqual(*m_IntData);
141  }
142 
144  template <typename T>
145  void TestClone(mitk::VectorProperty<T> &prop)
146  {
147  std::vector<T> data = MakeSimpleList<T>();
148  prop.SetValue(data);
149 
150  typename mitk::VectorProperty<T>::Pointer clone = prop.Clone();
151  CPPUNIT_ASSERT(clone.IsNotNull());
152  CPPUNIT_ASSERT_EQUAL_MESSAGE("Result of Clone() shall equal original property", *clone, prop);
153 
154  std::vector<T> origData = prop.GetValue();
155  std::vector<T> cloneData = clone->GetValue();
156 
157  CPPUNIT_ASSERT_EQUAL_MESSAGE("Clone shall have a copy of original data", cloneData, origData);
158  }
159 
160  void TestClone()
161  {
162  TestClone(*m_DoubleData);
163  TestClone(*m_IntData);
164  }
165 
166 }; // class
167 
168 MITK_TEST_SUITE_REGISTRATION(mitkVectorProperty)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
virtual void SetValue(const VectorType &parameter_vector)
sets the content vector
STL namespace.
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
DataCollection - Class to facilitate loading/accessing structured data.
virtual std::string GetValueAsString() const override
Pointer Clone() const
static void clone(T *&dst, S *src, int n)
Definition: svm.cpp:73
virtual const VectorType & GetValue() const
returns a const reference to the contained vector
Test fixture for parameterized tests.
Providing a std::vector as property.
static Pointer New()