26 #include <itksys/SystemTools.hxx> 29 #include <vtkDebugLeaks.h> 32 #include <vnl/vnl_vector_fixed.hxx> 36 CPPUNIT_TEST_SUITE(mitkTinyXMLTestSuite);
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);
44 CPPUNIT_TEST_SUITE_END();
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";
51 TiXmlDocument m_Document;
52 TiXmlElement *m_DoubleTest;
54 double calcPrecision(
const unsigned int requiredDecimalPlaces)
56 return pow(10.0, -1.0 * ((
double)requiredDecimalPlaces));
59 bool Setup(
double valueToWrite)
62 auto decl =
new TiXmlDeclaration(
"1.0",
"",
"");
63 m_Document.LinkEndChild(decl);
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);
72 auto vElement =
new TiXmlElement(m_ElementToStoreAttributeName);
73 vElement->SetDoubleAttribute(m_AttributeToStoreName, valueToWrite);
74 m_Document.LinkEndChild(vElement);
77 return m_Document.SaveFile(m_Filename);
81 void setUp()
override {}
83 void tearDown()
override {}
85 void TestingFunctionSetupWorks_Success()
87 CPPUNIT_ASSERT_MESSAGE(
"Test if Setup correctly writes data to file",
Setup(1.0));
90 int readValueFromSetupDocument(
double &readOutValue)
92 if (!m_Document.LoadFile(m_Filename))
94 CPPUNIT_ASSERT_MESSAGE(
"Test Setup failed, could not open file",
false);
95 return TIXML_NO_ATTRIBUTE;
99 m_DoubleTest = m_Document.FirstChildElement(m_ElementToStoreAttributeName);
100 return m_DoubleTest->QueryDoubleAttribute(m_AttributeToStoreName, &readOutValue);
104 void TestingReadValueFromSetupDocument_Success()
106 if (!m_Document.LoadFile(m_Filename))
108 CPPUNIT_ASSERT_MESSAGE(
"Test Setup failed, could not open file", !m_Document.LoadFile(m_Filename));
112 m_DoubleTest = m_Document.FirstChildElement(m_ElementToStoreAttributeName);
113 CPPUNIT_ASSERT_MESSAGE(
"Test Setup could open file", m_DoubleTest !=
nullptr);
121 void TestingReadOutValueWorks_Success()
125 CPPUNIT_ASSERT_MESSAGE(
"checking if readout mechanism works.",
126 TIXML_SUCCESS == readValueFromSetupDocument(readValue));
129 void TestDoubleValueWriteOut_Success()
131 const double valueToWrite = -1.123456;
132 const int validDigitsAfterComma = 6;
133 const double neededPrecision = calcPrecision(validDigitsAfterComma + 1);
137 readValueFromSetupDocument(readValue);
139 CPPUNIT_ASSERT_MESSAGE(
"Testing if value valueToWrite equals readValue which was retrieved from TinyXML document",
140 mitk::Equal(valueToWrite, readValue, neededPrecision));
143 void TestDoubleValueWriteOutManyDecimalPlaces_Success()
145 const double valueToWrite = -1.12345678910111;
146 const int validDigitsAfterComma = 14;
147 const double neededPrecision = calcPrecision(validDigitsAfterComma + 1);
152 readValueFromSetupDocument(readValue);
154 CPPUNIT_ASSERT_MESSAGE(
"Testing if value valueToWrite equals readValue which was retrieved from TinyXML document",
155 mitk::Equal(valueToWrite, readValue, neededPrecision));
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.