///------------------------------------------------------------------------------------------------- /// /// \file misc.hpp /// \brief Some constants and functions for google tests /// \author Thibaut Monseigne (Inria). /// \version 0.1. /// \date 26/10/2018. /// \copyright GNU Affero General Public License v3.0. /// \remarks /// - For this test I compare the results with the pyRiemann library (License) or sklearn if pyRiemman just redirect the function. /// ///------------------------------------------------------------------------------------------------- #pragma once #include #include #include #include #include const std::string SEP = "\n====================\n"; //********************************************************************************* //********** Comparison of values with epsilon tolerance for google test ********** //********************************************************************************* /// Check if two doubles are almost equal. /// The first value. /// The second value. /// (Optional) The epsilon tolerance. /// True if almost equal, false if not. inline bool isAlmostEqual(const double x, const double y, const double epsilon = 0.0001) { return std::abs(x - y) < epsilon; } /// Check if sum of two vectors are almost equal. /// Generic numeric type parameter. /// \copydetails isAlmostEqual(const double, const double, const double) template ::value, T>::type> bool isAlmostEqual(const std::vector& x, const std::vector& y, const double epsilon = 0.0001) { double xsum = 0.0, ysum = 0.0; for (const auto& n : x) { xsum += n; } for (const auto& n : y) { ysum += n; } return (x.size() == y.size() && isAlmostEqual(xsum, ysum, epsilon)); } /// Check if sum of two matrix are almost equal. /// \copydetails isAlmostEqual(const double, const double, const double) inline bool isAlmostEqual(const Eigen::MatrixXd& x, const Eigen::MatrixXd& y, const double epsilon = 0.0001) { return x.size() == y.size() && isAlmostEqual(x.cwiseAbs().sum(), y.cwiseAbs().sum(), epsilon); } //***************************************************************** //********** Error Message Standardization for googltest ********** //***************************************************************** /// Error message for size_t. /// The name of the test. /// The reference value. /// The calculate value. /// Error message. inline std::string ErrorMsg(const std::string& name, const size_t ref, const size_t calc) { std::stringstream ss; ss << SEP << name << " : Reference : " << ref << ", \tCompute : " << calc << SEP; return ss.str(); } /// Error message for doubles. /// \copydetails ErrorMsg(const std::string&, const size_t, const size_t) inline std::string ErrorMsg(const std::string& name, const double ref, const double calc) { std::stringstream ss; ss << SEP << name << " : Reference : " << ref << ", \tCompute : " << calc << SEP; return ss.str(); } /// Error message for numeric vector. /// Generic numeric type parameter. /// \copydetails ErrorMsg(const std::string&, const size_t, const size_t) template ::value, T>::type> std::string ErrorMsg(const std::string& name, const std::vector& ref, const std::vector& calc) { std::stringstream ss; ss << SEP << name << " : " << std::endl << " Reference : \t["; for (const T& t : ref) { ss << t << ", "; } if (!ref.empty()) { ss.seekp(ss.str().length() - 2); } ss << "] " << std::endl << " Compute : \t["; for (const T& t : calc) { ss << t << ", "; } if (!ref.empty()) { ss.seekp(ss.str().length() - 2); } ss << "] " << SEP; return ss.str(); } /// Error message for matrix. /// \copydetails ErrorMsg(const std::string&, const size_t, const size_t) inline std::string ErrorMsg(const std::string& name, const Eigen::MatrixXd& ref, const Eigen::MatrixXd& calc) { std::stringstream ss; ss << SEP << name << " : " << std::endl << "********** Reference **********\n" << ref << std::endl << "********** Compute **********\n" << calc << SEP; return ss.str(); } /// Error message for matrix Classifier. /// \copydetails ErrorMsg(const std::string&, const size_t, const size_t) inline std::string ErrorMsg(const std::string& name, const Geometry::IMatrixClassifier& ref, const Geometry::IMatrixClassifier& calc) { std::stringstream ss; ss << SEP << name << " : " << std::endl << "********** Reference **********\n" << ref << std::endl << "********** Compute **********\n" << calc << SEP; return ss.str(); } /// Error message for ASR. /// \copydetails ErrorMsg(const std::string&, const size_t, const size_t) inline std::string ErrorMsg(const std::string& name, const Geometry::CASR& ref, const Geometry::CASR& calc) { std::stringstream ss; ss << SEP << name << " : " << std::endl << "********** Reference **********\n" << ref << std::endl << "********** Compute **********\n" << calc << SEP; return ss.str(); }