You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ///-------------------------------------------------------------------------------------------------
  2. ///
  3. /// \brief Classes of the Box Features Selection Trainer.
  4. /// \author Thibaut Monseigne (Inria).
  5. /// \version 1.0.
  6. /// \date 14/02/2020.
  7. /// \copyright <a href="https://choosealicense.com/licenses/agpl-3.0/">GNU Affero General Public License v3.0</a>.
  8. ///
  9. ///-------------------------------------------------------------------------------------------------
  10. #include <fstream>
  11. #include <iostream>
  12. #include <string>
  13. #include "tinyxml2.h"
  14. #include <algorithm>
  15. // validation Test take in the algorithm output file, the expected output file (reference) and a tolerance threshold
  16. bool comparison(const char* file1, const char* file2, bool removeSpace)
  17. {
  18. // Load Files
  19. tinyxml2::XMLDocument xmlDoc1, xmlDoc2;
  20. if (xmlDoc1.LoadFile(file1) != 0) // Check File 1 Exist and Loading
  21. {
  22. std::cerr << "Error during loading File." << std::endl;
  23. return false;
  24. }
  25. if (xmlDoc2.LoadFile(file2) != 0) // Check File 2 Exist and Loading
  26. {
  27. std::cerr << "Error during loading Reference." << std::endl;
  28. return false;
  29. }
  30. // Load Roots
  31. tinyxml2::XMLNode *root1 = xmlDoc1.FirstChild(), *root2 = xmlDoc2.FirstChild(); // Get Root Node
  32. if (root1 == nullptr) // Check Root Node Exist
  33. {
  34. std::cerr << "File format not compatible." << std::endl;
  35. return false;
  36. }
  37. if (root2 == nullptr) // Check Root Node Exist
  38. {
  39. std::cerr << "Reference format not compatible." << std::endl;
  40. return false;
  41. }
  42. // Count Settings
  43. size_t n1 = 0, n2 = 0;
  44. for (tinyxml2::XMLElement* e = root1->FirstChildElement(); e != nullptr; e = e->NextSiblingElement()) { n1++; }
  45. for (tinyxml2::XMLElement* e = root2->FirstChildElement(); e != nullptr; e = e->NextSiblingElement()) { n2++; }
  46. if (n1 != n2)
  47. {
  48. std::cerr << "File and Reference are not same number of features." << std::endl;
  49. return false;
  50. }
  51. // Load settings
  52. tinyxml2::XMLElement *e1 = root1->FirstChildElement(), *e2 = root2->FirstChildElement();
  53. for (size_t i = 0; i < n1; ++i, e1 = e1->NextSiblingElement(), e2 = e2->NextSiblingElement())
  54. {
  55. std::string s1(e1->GetText()), s2(e2->GetText());
  56. std::cout << s1 << std::endl << s2 << std::endl;
  57. if (removeSpace)
  58. {
  59. s1.erase(std::remove_if(s1.begin(), s1.end(), [](const unsigned char x) { return std::isspace(x); }), s1.end());
  60. s2.erase(std::remove_if(s2.begin(), s2.end(), [](const unsigned char x) { return std::isspace(x); }), s2.end());
  61. }
  62. std::cout << s1 << std::endl << s2 << std::endl;
  63. if (s1 != s2)
  64. {
  65. std::cerr << "Setting number " << i << " is different, For File : \n" << s1 << "\nfor Reference : \n" << s2 << std::endl;
  66. return false;
  67. }
  68. }
  69. return true;
  70. }
  71. int main(const int argc, char* argv[])
  72. {
  73. if (argc != 4)
  74. {
  75. std::cout << "Usage: " << argv[0] << " <file> <reference> <remove space (true or false)>\n";
  76. return -1;
  77. }
  78. bool remove;
  79. if (strcmp(argv[3], "true") == 0) { remove = true; }
  80. else if (strcmp(argv[3], "false") == 0) { remove = false; }
  81. else
  82. {
  83. std::cout << "Usage: <remove space> must be true or false not " << argv[3] << std::endl;
  84. return -1;
  85. }
  86. if (!comparison(argv[1], argv[2], remove))
  87. {
  88. std::cout << "Algorithm failed validation test \n" << std::endl;
  89. return 1;
  90. }
  91. std::cout << "Test passed\n" << std::endl;
  92. return 0;
  93. }