105 lines
3.2 KiB
C++
105 lines
3.2 KiB
C++
|
///-------------------------------------------------------------------------------------------------
|
||
|
///
|
||
|
/// \brief Classes of the Box Features Selection Trainer.
|
||
|
/// \author Thibaut Monseigne (Inria).
|
||
|
/// \version 1.0.
|
||
|
/// \date 14/02/2020.
|
||
|
/// \copyright <a href="https://choosealicense.com/licenses/agpl-3.0/">GNU Affero General Public License v3.0</a>.
|
||
|
///
|
||
|
///-------------------------------------------------------------------------------------------------
|
||
|
|
||
|
#include <fstream>
|
||
|
#include <iostream>
|
||
|
#include <string>
|
||
|
#include "tinyxml2.h"
|
||
|
#include <algorithm>
|
||
|
|
||
|
// validation Test take in the algorithm output file, the expected output file (reference) and a tolerance threshold
|
||
|
bool comparison(const char* file1, const char* file2, bool removeSpace)
|
||
|
{
|
||
|
// Load Files
|
||
|
tinyxml2::XMLDocument xmlDoc1, xmlDoc2;
|
||
|
if (xmlDoc1.LoadFile(file1) != 0) // Check File 1 Exist and Loading
|
||
|
{
|
||
|
std::cerr << "Error during loading File." << std::endl;
|
||
|
return false;
|
||
|
}
|
||
|
if (xmlDoc2.LoadFile(file2) != 0) // Check File 2 Exist and Loading
|
||
|
{
|
||
|
std::cerr << "Error during loading Reference." << std::endl;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// Load Roots
|
||
|
tinyxml2::XMLNode *root1 = xmlDoc1.FirstChild(), *root2 = xmlDoc2.FirstChild(); // Get Root Node
|
||
|
if (root1 == nullptr) // Check Root Node Exist
|
||
|
{
|
||
|
std::cerr << "File format not compatible." << std::endl;
|
||
|
return false;
|
||
|
}
|
||
|
if (root2 == nullptr) // Check Root Node Exist
|
||
|
{
|
||
|
std::cerr << "Reference format not compatible." << std::endl;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// Count Settings
|
||
|
size_t n1 = 0, n2 = 0;
|
||
|
for (tinyxml2::XMLElement* e = root1->FirstChildElement(); e != nullptr; e = e->NextSiblingElement()) { n1++; }
|
||
|
for (tinyxml2::XMLElement* e = root2->FirstChildElement(); e != nullptr; e = e->NextSiblingElement()) { n2++; }
|
||
|
if (n1 != n2)
|
||
|
{
|
||
|
std::cerr << "File and Reference are not same number of features." << std::endl;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// Load settings
|
||
|
tinyxml2::XMLElement *e1 = root1->FirstChildElement(), *e2 = root2->FirstChildElement();
|
||
|
for (size_t i = 0; i < n1; ++i, e1 = e1->NextSiblingElement(), e2 = e2->NextSiblingElement())
|
||
|
{
|
||
|
std::string s1(e1->GetText()), s2(e2->GetText());
|
||
|
std::cout << s1 << std::endl << s2 << std::endl;
|
||
|
if (removeSpace)
|
||
|
{
|
||
|
s1.erase(std::remove_if(s1.begin(), s1.end(), [](const unsigned char x) { return std::isspace(x); }), s1.end());
|
||
|
s2.erase(std::remove_if(s2.begin(), s2.end(), [](const unsigned char x) { return std::isspace(x); }), s2.end());
|
||
|
}
|
||
|
std::cout << s1 << std::endl << s2 << std::endl;
|
||
|
|
||
|
if (s1 != s2)
|
||
|
{
|
||
|
std::cerr << "Setting number " << i << " is different, For File : \n" << s1 << "\nfor Reference : \n" << s2 << std::endl;
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
int main(const int argc, char* argv[])
|
||
|
{
|
||
|
if (argc != 4)
|
||
|
{
|
||
|
std::cout << "Usage: " << argv[0] << " <file> <reference> <remove space (true or false)>\n";
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
bool remove;
|
||
|
if (strcmp(argv[3], "true") == 0) { remove = true; }
|
||
|
else if (strcmp(argv[3], "false") == 0) { remove = false; }
|
||
|
else
|
||
|
{
|
||
|
std::cout << "Usage: <remove space> must be true or false not " << argv[3] << std::endl;
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if (!comparison(argv[1], argv[2], remove))
|
||
|
{
|
||
|
std::cout << "Algorithm failed validation test \n" << std::endl;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
std::cout << "Test passed\n" << std::endl;
|
||
|
return 0;
|
||
|
}
|