<commit_before>/*===================================================================

The Medical Imaging Interaction Toolkit (MITK)

Copyright (c) German Cancer Research Center,
Division of Medical and Biological Informatics.
All rights reserved.

This software is distributed WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.

See LICENSE.txt or http://www.mitk.org for details.

===================================================================*/

#include "mitkTestingMacros.h"

#include <tinyxml.h>
#include <string.h>
#include <itksys/SystemTools.hxx>


static const std::string filename = itksys::SystemTools::GetCurrentWorkingDirectory() + "/TinyXMLTest.txt";
static const std::string elementToStoreAttributeName = "DoubleTest";
static const std::string attributeToStoreName        = "CommaValue";

/**
 * create a simple xml document which stores the values
 * @param valueToWrite  value which should be stored
 * @return  true, if document was successfully created.
 */
static bool Setup(double valueToWrite)
{
  // 1. create simple document
  TiXmlDocument document;
  TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" ); // TODO what to write here? encoding? etc....
  document.LinkEndChild( decl );

  TiXmlElement* version = new TiXmlElement("Version");
  version->SetAttribute("Writer",  __FILE__ );
  version->SetAttribute("CVSRevision",  "$Revision: 17055 $" );
  version->SetAttribute("FileVersion",  1 );
  document.LinkEndChild(version);

  // 2. store one element containing a double value with potentially many after comma digits.
  TiXmlElement* vElement = new TiXmlElement( elementToStoreAttributeName );
  vElement->SetDoubleAttribute( attributeToStoreName, valueToWrite );
  document.LinkEndChild(vElement);

  // 3. store in file.
  return document.SaveFile( filename );
}

static int readValueFromSetupDocument(double& readOutValue)
{
  TiXmlDocument document;

  if (!document.LoadFile(filename))
  {
    MITK_TEST_CONDITION_REQUIRED(false, "Test Setup failed, could not open " << filename);
    return TIXML_NO_ATTRIBUTE;
  }
  else
  {
    TiXmlElement* doubleTest = document.FirstChildElement(elementToStoreAttributeName);
    return doubleTest->QueryDoubleAttribute(attributeToStoreName, &readOutValue);
  }
}

static void TearDown()
{
  // TODO delete created file, ...
}

static void Test_Setup_works()
{
  MITK_TEST_CONDITION_REQUIRED(Setup(1.0), "Test if setup correctly writes data to " << filename);
}

/**
 * this first test ensures we can correctly readout values from the
 * TinyXMLDocument.
 */
static void Test_ReadOutValue_works()
{
  Setup(1.0);

  double readValue;

  MITK_TEST_CONDITION_REQUIRED(TIXML_SUCCESS == readValueFromSetupDocument(readValue),
      "checking if readout mechanism works.");
}



static void Test_DoubleValueWriteOut()
{
  const double valueToWrite          = -1.123456;
  const int    validDigitsAfterComma = 6; // indicates the number of valid digits after comma of valueToWrite
  const double neededPrecision       = 1.0 / ((double) validDigitsAfterComma + 1);
  double       readValue;

  Setup(valueToWrite);

  TiXmlBase::SetRequiredDecimalPlaces(validDigitsAfterComma);

  readValueFromSetupDocument(readValue);

  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(valueToWrite, readValue, neededPrecision),
      std::setprecision(validDigitsAfterComma) <<
      "Testing if value " << valueToWrite << " equals " << readValue
      << " which was retrieved from TinyXML document");

  TearDown();
}

static void Test_DoubleValueWriteOut_manyDecimalPlaces()
{
  const double valueToWrite          = -1.12345678910111;
  const int    validDigitsAfterComma = 14; // indicates the number of valid digits after comma of valueToWrite
  const double neededPrecision       = 1.0 / ((double) validDigitsAfterComma + 1);
  double       readValue;

  Setup(valueToWrite);

  TiXmlBase::SetRequiredDecimalPlaces(validDigitsAfterComma);

  readValueFromSetupDocument(readValue);

  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(valueToWrite, readValue, neededPrecision),
      std::setprecision(validDigitsAfterComma) <<
      "Testing if value " << valueToWrite << " equals " << readValue
      << " which was retrieved from TinyXML document");

  TearDown();
}

static void Test_DoubleValueWriteOut_tooLittlePrecision()
{
  const double valueToWrite          = -1.12345678910111;
  const int    validDigitsAfterComma = 14; // indicates the number of valid digits after comma of valueToWrite
  const double neededPrecision       = 1.0 / ((double) validDigitsAfterComma + 1);
  double       readValue;

  Setup(valueToWrite);

  // this should lead the readout value to be different since
  // only the first 4 digits are written to xml.
  TiXmlBase::SetRequiredDecimalPlaces(4);

  readValueFromSetupDocument(readValue);

  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(valueToWrite, readValue, neededPrecision),
      std::setprecision(validDigitsAfterComma) <<
      "Testing if value " << valueToWrite << " equals " << readValue
      << " which was retrieved from TinyXML document");

  TearDown();
}

int mitkTinyXMLTest(int /* argc */, char* /*argv*/[])
{
  MITK_TEST_BEGIN("TinyXMLTest");

  Test_Setup_works();
  Test_ReadOutValue_works();
  Test_DoubleValueWriteOut();
  Test_DoubleValueWriteOut_manyDecimalPlaces();
  Test_DoubleValueWriteOut_tooLittlePrecision();

  MITK_TEST_END()
}
<commit_msg>changed interface.<commit_after>/*===================================================================

The Medical Imaging Interaction Toolkit (MITK)

Copyright (c) German Cancer Research Center,
Division of Medical and Biological Informatics.
All rights reserved.

This software is distributed WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.

See LICENSE.txt or http://www.mitk.org for details.

===================================================================*/

#include "mitkTestingMacros.h"

#include <tinyxml.h>
#include <string.h>
#include <itksys/SystemTools.hxx>


static const std::string  filename = itksys::SystemTools::GetCurrentWorkingDirectory() + "/TinyXMLTest.txt";
static const std::string  elementToStoreAttributeName = "DoubleTest";
static const std::string  attributeToStoreName        = "CommaValue";

/**
 * create a simple xml document which stores the values
 * @param valueToWrite  value which should be stored
 * @return  true, if document was successfully created.
 */
static bool Setup(double valueToWrite, const unsigned int requiredDecimalPlaces)
{
  // 1. create simple document
  TiXmlDocument document;
  TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" ); // TODO what to write here? encoding? etc....
  document.LinkEndChild( decl );

  TiXmlElement* version = new TiXmlElement("Version");
  version->SetAttribute("Writer",  __FILE__ );
  version->SetAttribute("CVSRevision",  "$Revision: 17055 $" );
  version->SetAttribute("FileVersion",  1 );
  document.LinkEndChild(version);

  // 2. store one element containing a double value with potentially many after comma digits.
  TiXmlElement* vElement = new TiXmlElement( elementToStoreAttributeName );
  vElement->SetDoubleAttribute( attributeToStoreName, valueToWrite, requiredDecimalPlaces );
  document.LinkEndChild(vElement);

  // 3. store in file.
  return document.SaveFile( filename );
}

static int readValueFromSetupDocument(double& readOutValue)
{
  TiXmlDocument document;

  if (!document.LoadFile(filename))
  {
    MITK_TEST_CONDITION_REQUIRED(false, "Test Setup failed, could not open " << filename);
    return TIXML_NO_ATTRIBUTE;
  }
  else
  {
    TiXmlElement* doubleTest = document.FirstChildElement(elementToStoreAttributeName);
    return doubleTest->QueryDoubleAttribute(attributeToStoreName, &readOutValue);
  }
}

/**
 *
 * @return true if TearDown was successful.
 */
static bool TearDown()
{
  return !remove(filename.c_str());
}

static void Test_Setup_works()
{
  MITK_TEST_CONDITION_REQUIRED(Setup(1.0, 1) && TearDown(),
      "Test if setup and teardown correctly writes data to " << filename << " and deletes the file after the test");
}

/**
 * this first test ensures we can correctly readout values from the
 * TinyXMLDocument.
 */
static void Test_ReadOutValue_works()
{
  Setup(1.0, 1);

  double readValue;

  MITK_TEST_CONDITION_REQUIRED(TIXML_SUCCESS == readValueFromSetupDocument(readValue),
      "checking if readout mechanism works.");
}



static void Test_DoubleValueWriteOut()
{
  const double valueToWrite          = -1.123456;
  const int    validDigitsAfterComma = 6; // indicates the number of valid digits after comma of valueToWrite
  const double neededPrecision       = 1.0 / ((double) validDigitsAfterComma + 1);
  double       readValue;

  Setup(valueToWrite, validDigitsAfterComma);

  readValueFromSetupDocument(readValue);

  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(valueToWrite, readValue, neededPrecision),
      std::setprecision(validDigitsAfterComma) <<
      "Testing if value " << valueToWrite << " equals " << readValue
      << " which was retrieved from TinyXML document");

  TearDown();
}

static void Test_DoubleValueWriteOut_manyDecimalPlaces()
{
  const double valueToWrite          = -1.12345678910111;
  const int    validDigitsAfterComma = 14; // indicates the number of valid digits after comma of valueToWrite
  const double neededPrecision       = 1.0 / ((double) validDigitsAfterComma + 1);
  double       readValue;

  Setup(valueToWrite, validDigitsAfterComma);

  readValueFromSetupDocument(readValue);

  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(valueToWrite, readValue, neededPrecision),
      std::setprecision(validDigitsAfterComma) <<
      "Testing if value " << valueToWrite << " equals " << readValue
      << " which was retrieved from TinyXML document");

  TearDown();
}


static void Test_DoubleValueWriteOut_tooLittlePrecision()
{
  const double valueToWrite          = -1.12345678910111;
  const int    validDigitsAfterComma = 14; // indicates the number of valid digits after comma of valueToWrite
  const double neededPrecision       = 1.0 / ((double) validDigitsAfterComma + 1);
  double       readValue;

  Setup(valueToWrite, validDigitsAfterComma);

  readValueFromSetupDocument(readValue);

  MITK_TEST_CONDITION_REQUIRED(!mitk::Equal(valueToWrite, readValue, neededPrecision),
      std::setprecision(validDigitsAfterComma) <<
      "Testing if value " << valueToWrite << " doesn't equal " << readValue
      << " which was retrieved from TinyXML document because decimal places are set too low.");

  TearDown();
}

int mitkTinyXMLTest(int /* argc */, char* /*argv*/[])
{
  MITK_TEST_BEGIN("TinyXMLTest");

  Test_Setup_works();
  Test_ReadOutValue_works();
  Test_DoubleValueWriteOut();
  Test_DoubleValueWriteOut_manyDecimalPlaces();
  Test_DoubleValueWriteOut_tooLittlePrecision();

  MITK_TEST_END()
}

{'fraction_non_alphanumeric': 0.11938794209272494, 'fraction_numerical': 0.012461059190031152, 'mean_word_length': 4.7356805044666315, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 5}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2348492', 'n_tokens_mistral': 3296, 'n_tokens_neox': 3083, 'n_words': 833}