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