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.

utils.hpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ///-------------------------------------------------------------------------------------------------
  2. ///
  3. /// \file utils.hpp
  4. /// \brief Some constants and functions for google tests
  5. /// \author Thibaut Monseigne (Inria).
  6. /// \version 0.1.
  7. /// \date 26/10/2018.
  8. /// \copyright <a href="https://choosealicense.com/licenses/agpl-3.0/">GNU Affero General Public License v3.0</a>.
  9. ///
  10. ///-------------------------------------------------------------------------------------------------
  11. #pragma once
  12. #include "openvibe/CIdentifier.hpp"
  13. #include "openvibe/CTime.hpp"
  14. #include <openvibe/CMatrix.hpp>
  15. #include <cmath>
  16. #include <sstream>
  17. const std::string SEP = "\n====================\n";
  18. //---------------------------------------------------------------------------------------------------
  19. /// <summary> Check if double are almost equals. </summary>
  20. /// <param name="a"> The first number. </param>
  21. /// <param name="b"> The second number. </param>
  22. /// <param name="epsilon"> The tolerance. </param>
  23. /// <returns> <c>true</c> if almmost equals, <c>false</c> otherwise. </returns>
  24. inline bool AlmostEqual(const double a, const double b, const double epsilon = OV_EPSILON) { return std::fabs(a - b) < std::fabs(epsilon); }
  25. //*****************************************************************
  26. //********** Error Message Standardization for googltest **********
  27. //*****************************************************************
  28. //---------------------------------------------------------------------------------------------------
  29. /// <summary> Error message for numeric value. </summary>
  30. /// <param name="name"> The name of the test. </param>
  31. /// <param name="ref"> The reference value. </param>
  32. /// <param name="calc"> The calculate value. </param>
  33. /// <returns> Error message. </returns>
  34. /// <typeparam name="T"> Generic numeric type parameter. </typeparam>
  35. template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
  36. std::string ErrorMsg(const std::string& name, const T ref, const T calc)
  37. {
  38. std::stringstream ss;
  39. ss << SEP << name << " : Reference : " << ref << ", \tCompute : " << calc << SEP;
  40. return ss.str();
  41. }
  42. //---------------------------------------------------------------------------------------------------
  43. /// <summary> Error message for string value. </summary>
  44. /// <inheritdoc cref="ErrorMsg(const std::string&, const T, const T)"/>
  45. inline std::string ErrorMsg(const std::string& name, const std::string& ref, const std::string& calc)
  46. {
  47. std::stringstream ss;
  48. ss << SEP << name << " : Reference : " << ref << ", \tCompute : " << calc << SEP;
  49. return ss.str();
  50. }
  51. //---------------------------------------------------------------------------------------------------
  52. /// <summary> Error message for CTime value. </summary>
  53. /// <inheritdoc cref="ErrorMsg(const std::string&, const T, const T)"/>
  54. inline std::string ErrorMsg(const std::string& name, const OpenViBE::CTime& ref, const OpenViBE::CTime& calc)
  55. {
  56. std::stringstream ss;
  57. ss << SEP << name << " : Reference : " << ref.str(true, true) << ", \tCompute : " << calc.str(true, true) << SEP;
  58. return ss.str();
  59. }
  60. //---------------------------------------------------------------------------------------------------
  61. /// <summary> Error message for CIdentifier value. </summary>
  62. /// <inheritdoc cref="ErrorMsg(const std::string&, const T, const T)"/>
  63. inline std::string ErrorMsg(const std::string& name, const OpenViBE::CIdentifier& ref, const OpenViBE::CIdentifier& calc)
  64. {
  65. std::stringstream ss;
  66. ss << SEP << name << " : Reference : " << ref << ", \tCompute : " << calc << SEP;
  67. return ss.str();
  68. }
  69. //---------------------------------------------------------------------------------------------------
  70. /// <summary> Error message for CIdentifier value. </summary>
  71. /// <inheritdoc cref="ErrorMsg(const std::string&, const T, const T)"/>
  72. inline std::string ErrorMsg(const std::string& name, const OpenViBE::CMatrix& ref, const OpenViBE::CMatrix& calc)
  73. {
  74. std::stringstream ss;
  75. ss << SEP << name << " : " << std::endl << "********** Reference **********\n" << ref << std::endl << "********** Compute **********\n" << calc << SEP;
  76. return ss.str();
  77. }