Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkTinyXMLTest.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 // Testing
13 #include "mitkTestFixture.h"
14 #include <mitkTestingMacros.h>
15 
16 // std includes
17 #include <cmath>
18 #include <iomanip>
19 #include <tinyxml.h>
20 
21 // MITK includes
22 #include "mitkStringProperty.h"
23 #include <mitkNumericTypes.h>
24 
25 // itksys
26 #include <itksys/SystemTools.hxx>
27 
28 // VTK includes
29 #include <vtkDebugLeaks.h>
30 
31 // vnl includes
32 #include <vnl/vnl_vector_fixed.hxx>
33 
34 class mitkTinyXMLTestSuite : public mitk::TestFixture
35 {
36  CPPUNIT_TEST_SUITE(mitkTinyXMLTestSuite);
37 
38  MITK_TEST(TestingFunctionSetupWorks_Success);
39  MITK_TEST(TestingReadValueFromSetupDocument_Success);
40  MITK_TEST(TestingReadOutValueWorks_Success);
41  MITK_TEST(TestDoubleValueWriteOut_Success);
42  MITK_TEST(TestDoubleValueWriteOutManyDecimalPlaces_Success);
43 
44  CPPUNIT_TEST_SUITE_END();
45 
46 private:
47  const std::string m_Filename = itksys::SystemTools::GetCurrentWorkingDirectory() + "/TinyXMLTest.txt";
48  const std::string m_ElementToStoreAttributeName = "DoubleTest";
49  const std::string m_AttributeToStoreName = "CommaValue";
50 
51  TiXmlDocument m_Document;
52  TiXmlElement *m_DoubleTest;
53 
54  double calcPrecision(const unsigned int requiredDecimalPlaces)
55  {
56  return pow(10.0, -1.0 * ((double)requiredDecimalPlaces));
57  }
58 
59  bool Setup(double valueToWrite)
60  {
61  // 1. create simple document
62  auto decl = new TiXmlDeclaration("1.0", "", ""); // TODO what to write here? encoding? etc....
63  m_Document.LinkEndChild(decl);
64 
65  auto version = new TiXmlElement("Version");
66  version->SetAttribute("Writer", __FILE__);
67  version->SetAttribute("CVSRevision", "$Revision: 17055 $");
68  version->SetAttribute("FileVersion", 1);
69  m_Document.LinkEndChild(version);
70 
71  // 2. store one element containing a double value with potentially many after comma digits.
72  auto vElement = new TiXmlElement(m_ElementToStoreAttributeName);
73  vElement->SetDoubleAttribute(m_AttributeToStoreName, valueToWrite);
74  m_Document.LinkEndChild(vElement);
75 
76  // 3. store in file.
77  return m_Document.SaveFile(m_Filename);
78  }
79 
80 public:
81  void setUp() override {}
82 
83  void tearDown() override {}
84 
85  void TestingFunctionSetupWorks_Success()
86  {
87  CPPUNIT_ASSERT_MESSAGE("Test if Setup correctly writes data to file", Setup(1.0));
88  }
89 
90  int readValueFromSetupDocument(double &readOutValue)
91  {
92  if (!m_Document.LoadFile(m_Filename))
93  {
94  CPPUNIT_ASSERT_MESSAGE("Test Setup failed, could not open file", false);
95  return TIXML_NO_ATTRIBUTE;
96  }
97  else
98  {
99  m_DoubleTest = m_Document.FirstChildElement(m_ElementToStoreAttributeName);
100  return m_DoubleTest->QueryDoubleAttribute(m_AttributeToStoreName, &readOutValue);
101  }
102  }
103 
104  void TestingReadValueFromSetupDocument_Success()
105  {
106  if (!m_Document.LoadFile(m_Filename))
107  {
108  CPPUNIT_ASSERT_MESSAGE("Test Setup failed, could not open file", !m_Document.LoadFile(m_Filename));
109  }
110  else
111  {
112  m_DoubleTest = m_Document.FirstChildElement(m_ElementToStoreAttributeName);
113  CPPUNIT_ASSERT_MESSAGE("Test Setup could open file", m_DoubleTest != nullptr);
114  }
115  }
116 
121  void TestingReadOutValueWorks_Success()
122  {
123  double readValue;
124 
125  CPPUNIT_ASSERT_MESSAGE("checking if readout mechanism works.",
126  TIXML_SUCCESS == readValueFromSetupDocument(readValue));
127  }
128 
129  void TestDoubleValueWriteOut_Success()
130  {
131  const double valueToWrite = -1.123456;
132  const int validDigitsAfterComma = 6; // indicates the number of valid digits after comma of valueToWrite
133  const double neededPrecision = calcPrecision(validDigitsAfterComma + 1);
134  double readValue;
135 
136  Setup(valueToWrite);
137  readValueFromSetupDocument(readValue);
138 
139  CPPUNIT_ASSERT_MESSAGE("Testing if value valueToWrite equals readValue which was retrieved from TinyXML document",
140  mitk::Equal(valueToWrite, readValue, neededPrecision));
141  }
142 
143  void TestDoubleValueWriteOutManyDecimalPlaces_Success()
144  {
145  const double valueToWrite = -1.12345678910111;
146  const int validDigitsAfterComma = 14; // indicates the number of valid digits after comma of valueToWrite
147  const double neededPrecision = calcPrecision(validDigitsAfterComma + 1);
148  double readValue;
149 
150  Setup(valueToWrite);
151 
152  readValueFromSetupDocument(readValue);
153 
154  CPPUNIT_ASSERT_MESSAGE("Testing if value valueToWrite equals readValue which was retrieved from TinyXML document",
155  mitk::Equal(valueToWrite, readValue, neededPrecision));
156  }
157 };
158 
159 MITK_TEST_SUITE_REGISTRATION(mitkTinyXML)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
Test fixture for parameterized tests.
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 void Setup()