#ifndef __JCOMPAREHISTOGRAMS__JTESTSUMMARY_T__ #define __JCOMPAREHISTOGRAMS__JTESTSUMMARY_T__ #include #include #include #include "Jeep/JPrint.hh" #include "Jeep/JProperties.hh" #include "JLang/JColorFacet.hh" #include "JGizmo/JRootObjectID.hh" /** * \author rgruiz, bjung */ namespace JCOMPAREHISTOGRAMS {} namespace JPP { using namespace JCOMPAREHISTOGRAMS; } namespace JCOMPAREHISTOGRAMS { using JGIZMO::JRootObjectID; static const char* const PASSED_t = "PASSED"; static const char* const FAILED_t = "FAILED"; /** * Class dedicated to standardize the title of the graphical objects produced by the JTest_t() derived classes. */ class JTestSummary { public: /** * Default constructor. */ JTestSummary() : testName (""), histogramA (JRootObjectID()), histogramB (JRootObjectID()), parameterName (""), parameterValue (0.0), parameterThreshold(0.0), passed (false), passed_h (FAILED_t) {} /** * Constructor * * \param testName Test name * \param hA Name of the first histogram. * \param hB Name of the second histogram. * \param parameter Name of the parameter used for the test. * \param threshold Threshold value for the tested parameter. * \param value Value of the tested parameter. * \param passed true if the test is passed; else false. */ JTestSummary(const std::string& testName, const JRootObjectID& hA, const JRootObjectID& hB, const std::string& parameter, const double value, const double threshold, const bool passed): testName (testName), histogramA (hA), histogramB (hB), parameterName (parameter), parameterValue (value), parameterThreshold(threshold), passed (passed), passed_h (passed ? PASSED_t : FAILED_t) {} /** * Get equation parameters. * * \return equation parameters */ static inline JEquationParameters& getEquationParameters() { static JEquationParameters equation("=", "; ", "", "#"); return equation; } /** * Set equation parameters. * * \param equation equation parameters */ static inline void setEquationParameters(const JEquationParameters& equation) { getEquationParameters() = equation; } /** * Get properties of this class. * * \param equation equation parameters */ JProperties getProperties(const JEquationParameters& equation = JTestSummary::getEquationParameters()) { return JTestSummaryHelper(*this, equation); } /** * Get properties of this class. * * \param equation equation parameters */ JProperties getProperties(const JEquationParameters& equation = JTestSummary::getEquationParameters()) const { return JTestSummaryHelper(*this, equation); } /** * Read test summary from input. * * \param in input stream * \param object test summary * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JTestSummary& object) { using namespace std; in >> object.testName >> object.histogramA >> object.histogramB >> object.parameterName >> object.parameterThreshold >> object.passed_h; object.passed = (object.passed_h == PASSED_t) ? 1 : 0; return in; } /** * Write test summary to output. * * \param out output stream * \param object test summary * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, JTestSummary& object) { const JFormat format(out); out << object.testName << ' ' << object.histogramA << ' ' << object.histogramB << ' ' << object.parameterName << ' ' << object.parameterValue << ' ' << object.parameterThreshold << ' ' << object.passed_h; return out; } std::string testName; /*!< Test name. */ JRootObjectID histogramA; /*!< First histogram. */ JRootObjectID histogramB; /*!< Second histogram. */ std::string parameterName; /*!< Parameter evaluated by the test. */ double parameterValue; /*!< Value of the parameter evaluated by the test.*/ double parameterThreshold; /*!< Threshold to evaluate the parameter. */ bool passed; /*!< True if the test passed. */ std::string passed_h; /*!< Human readable version of passed. */ private: /** * Auxiliary class for I/O of test result message */ class JTestSummaryHelper : public JProperties { public: /** * Constructor. * * \param object test result message * \param equation equation parameters */ template JTestSummaryHelper(JTestSummary_t& object, const JEquationParameters& equation) : JProperties(equation, 1) { insert(gmake_property(object.testName)); insert(gmake_property(object.histogramA)); insert(gmake_property(object.histogramB)); insert(gmake_property(object.parameterName)); insert(gmake_property(object.parameterValue)); insert(gmake_property(object.parameterThreshold)); insert(gmake_property(object.passed_h)); } }; }; /** * Read test summary. * * \param in input stream * \param summary test summary * \param delimiter delimiter */ inline std::istream& read(std::istream& in, JTestSummary& summary, const char delimiter = ' ') { using namespace std; using namespace JPP; JProperties properties = summary.getProperties(); for (JProperties::iterator i = properties.begin(); i != properties.end(); ++i) { string buffer; getline(in, buffer, delimiter); istringstream iss(buffer); i->second->read(iss); } summary.passed = (summary.passed_h == PASSED_t); return in; } /** * Print test summary. * * \param out output stream * \param summary test summary * \param delimiter delimiter * \param useColors */ inline std::ostream& print(std::ostream& out, const JTestSummary& summary, const char delimiter = ' ', const bool useColors = true) { using namespace std; using namespace JPP; out << scientific << setprecision(2); if (useColors) { out << (summary.passed ? GREEN : RED); } const JProperties& properties = summary.getProperties(); for (JProperties::const_iterator i = properties.cbegin(); i != properties.cend(); ++i) { out << i->second.toString() << delimiter; } out << RESET << endl; return out; } /** * Print test summary. * * \param out output stream * \param summary test summary * \param __begin begin list of keys * \param __end end list of keys * \param delimiter delimiter * \param useColors */ template inline std::ostream& print(std::ostream& out, const JTestSummary& summary, T __begin, T __end, const char delimiter = ' ', const bool useColors = true) { using namespace std; using namespace JPP; out << scientific << setprecision(2); if (useColors) { out << (summary.passed ? GREEN : RED); } for (T i = __begin; i != __end; ++i) { summary.getProperties()[*i]->write(out); out << delimiter; } out << endl; return out; } } #endif