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.

ovtAssert.h 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*********************************************************************
  2. * Software License Agreement (AGPL-3 License)
  3. *
  4. * OpenViBE SDK Test Software
  5. * Based on OpenViBE V1.1.0, Copyright (C) Inria, 2006-2015
  6. * Copyright (C) Inria, 2015-2017,V1.0
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program.
  19. * If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #pragma once
  22. #include <string>
  23. namespace OpenViBE {
  24. namespace Test {
  25. void printError(const char* expression, const char* message, const char* file, const int line);
  26. void printError(const char* expression, const std::string& message, const char* file, const int line);
  27. void printError(const char* expression, const std::ostream& message, const char* file, const int line);
  28. void printExpressionPair(const char* str1, const char* str2);
  29. std::string buildExpressionFromPair(const char* str1, const char* str2);
  30. } // namespace Test
  31. } // namespace OpenViBE
  32. /**
  33. * OVT_ASSERT_PRIV: Assess expression and
  34. * return EXIT_FAILURE if expr is false
  35. * - expr: expression to assess
  36. * - origin: original assessed expression
  37. * - msg: custom error message
  38. * WARNING: SHOULD NOT BE USED DIRECTLY
  39. */
  40. #define OVT_ASSERT_PRIV(expr, origin, msg) \
  41. do { \
  42. if (!(expr)) \
  43. { \
  44. OpenViBE::Test::printError(#origin, (msg), __FILE__, __LINE__); \
  45. return EXIT_FAILURE; \
  46. } \
  47. } while (0)
  48. /**
  49. * OVT_ASSERT: Assess simple expression
  50. * - expr: expression to assess
  51. * - msg: custom error message
  52. */
  53. #define OVT_ASSERT(expr, msg) OVT_ASSERT_PRIV((expr), (expr), (msg))
  54. /**
  55. * OVT_ASSERT_STR: Assess string equality
  56. * - str1: reference string
  57. * - str2: compared string
  58. * - msg: custom error message
  59. */
  60. #define OVT_ASSERT_STREQ(str1, str2, msg) \
  61. do { \
  62. if (!((str1) == (str2))) \
  63. { \
  64. OpenViBE::Test::printError(OpenViBE::Test::buildExpressionFromPair(#str1, #str2).c_str(),(msg), __FILE__, __LINE__); \
  65. OpenViBE::Test::printExpressionPair((str1).c_str(),(str2).c_str());\
  66. return EXIT_FAILURE; \
  67. } \
  68. } while (0)
  69. /**
  70. * OVT_ASSERT_EX: Assess expr throws an
  71. * exception
  72. * - expr: expression to assess
  73. * - msg: custom error message
  74. */
  75. #define OVT_ASSERT_EX(expr, msg) \
  76. do { \
  77. bool hasTrown{ false }; \
  78. try { (expr); } \
  79. catch (...) { hasTrown = true; } \
  80. OVT_ASSERT_PRIV(hasTrown, (msg)); \
  81. } while (0)