Medical Imaging Interaction Toolkit  2016.11.0
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,
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 "mitkTestingMacros.h"
18 
19 #include <mitkNumericTypes.h>
20 
21 #include <cmath>
22 #include <iomanip>
23 #include <string>
24 #include <tinyxml.h>
25 
26 #include <itksys/SystemTools.hxx>
27 
28 static const std::string filename = itksys::SystemTools::GetCurrentWorkingDirectory() + "/TinyXMLTest.txt";
29 static const std::string elementToStoreAttributeName = "DoubleTest";
30 static const std::string attributeToStoreName = "CommaValue";
31 
32 static double calcPrecision(const unsigned int requiredDecimalPlaces)
33 {
34  return pow(10.0, -1.0 * ((double)requiredDecimalPlaces));
35 }
36 
42 static bool Setup(double valueToWrite)
43 {
44  // 1. create simple document
45  TiXmlDocument document;
46  auto decl = new TiXmlDeclaration("1.0", "", ""); // TODO what to write here? encoding? etc....
47  document.LinkEndChild(decl);
48 
49  auto version = new TiXmlElement("Version");
50  version->SetAttribute("Writer", __FILE__);
51  version->SetAttribute("CVSRevision", "$Revision: 17055 $");
52  version->SetAttribute("FileVersion", 1);
53  document.LinkEndChild(version);
54 
55  // 2. store one element containing a double value with potentially many after comma digits.
56  auto vElement = new TiXmlElement(elementToStoreAttributeName);
57  vElement->SetDoubleAttribute(attributeToStoreName, valueToWrite);
58  document.LinkEndChild(vElement);
59 
60  // 3. store in file.
61  return document.SaveFile(filename);
62 }
63 
64 static int readValueFromSetupDocument(double &readOutValue)
65 {
66  TiXmlDocument document;
67 
68  if (!document.LoadFile(filename))
69  {
70  MITK_TEST_CONDITION_REQUIRED(false, "Test Setup failed, could not open " << filename);
71  return TIXML_NO_ATTRIBUTE;
72  }
73  else
74  {
75  TiXmlElement *doubleTest = document.FirstChildElement(elementToStoreAttributeName);
76  return doubleTest->QueryDoubleAttribute(attributeToStoreName, &readOutValue);
77  }
78 }
79 
84 static bool TearDown()
85 {
86  return !remove(filename.c_str());
87 }
88 
89 static void Test_Setup_works()
90 {
92  Setup(1.0) && TearDown(),
93  "Test if setup and teardown correctly writes data to " << filename << " and deletes the file after the test");
94 }
95 
101 {
102  Setup(1.0);
103 
104  double readValue;
105 
106  MITK_TEST_CONDITION_REQUIRED(TIXML_SUCCESS == readValueFromSetupDocument(readValue),
107  "checking if readout mechanism works.");
108 }
109 
111 {
112  const double valueToWrite = -1.123456;
113  const int validDigitsAfterComma = 6; // indicates the number of valid digits after comma of valueToWrite
114  const double neededPrecision = calcPrecision(validDigitsAfterComma + 1);
115  double readValue;
116 
117  Setup(valueToWrite);
118 
119  readValueFromSetupDocument(readValue);
120 
122  mitk::Equal(valueToWrite, readValue, neededPrecision),
123  std::setprecision(validDigitsAfterComma) << "Testing if value " << valueToWrite << " equals " << readValue
124  << " which was retrieved from TinyXML document");
125 
126  TearDown();
127 }
128 
130 {
131  const double valueToWrite = -1.12345678910111;
132  const int validDigitsAfterComma = 14; // 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 
138  readValueFromSetupDocument(readValue);
139 
141  mitk::Equal(valueToWrite, readValue, neededPrecision),
142  std::setprecision(validDigitsAfterComma) << "Testing if value " << valueToWrite << " equals " << readValue
143  << " which was retrieved from TinyXML document");
144 
145  TearDown();
146 }
147 
148 int mitkTinyXMLTest(int /* argc */, char * /*argv*/ [])
149 {
150  MITK_TEST_BEGIN("TinyXMLTest");
151 
156 
157  MITK_TEST_END()
158 }
static void Test_Setup_works()
static void Test_DoubleValueWriteOut()
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
static const std::string elementToStoreAttributeName
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
static const std::string filename
static bool TearDown()
static double calcPrecision(const unsigned int requiredDecimalPlaces)
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 Test_ReadOutValue_works()
static int readValueFromSetupDocument(double &readOutValue)
static const std::string attributeToStoreName
static void Test_DoubleValueWriteOut_manyDecimalPlaces()
and MITK_TEST_END()
static bool Setup(double valueToWrite)
int mitkTinyXMLTest(int, char *[])