comparison.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * libtest
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or (at your option) any later version.
  11. *
  12. * This library 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 GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #pragma once
  22. #include <typeinfo>
  23. #include <libtest/strerror.h>
  24. #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
  25. #include <libmemcached/memcached.h>
  26. #endif
  27. #if defined(HAVE_LIBGEARMAN) && HAVE_LIBGEARMAN
  28. #include <libgearman/gearman.h>
  29. #endif
  30. namespace libtest {
  31. template <class T_comparable, class T_hint>
  32. bool _compare_true_hint(const char *file, int line, const char *func, T_comparable __expected, const char *assertation_label, T_hint __hint)
  33. {
  34. if (__expected == false)
  35. {
  36. libtest::stream::make_cerr(file, line, func) << "Assertation \"" << assertation_label << "\" failed, hint: " << __hint;
  37. return false;
  38. }
  39. return true;
  40. }
  41. template <class T_comparable>
  42. bool _compare(const char *file, int line, const char *func, const T_comparable __expected, const T_comparable __actual)
  43. {
  44. if (__expected != __actual)
  45. {
  46. if (typeid(__expected) == typeid(test_return_t))
  47. {
  48. const char *expected_str= test_strerror(test_return_t(__expected));
  49. const char *got_str= test_strerror(test_return_t(__actual));
  50. libtest::stream::make_cerr(file, line, func) << "Expected \""
  51. << expected_str
  52. << "\" got \""
  53. << got_str
  54. << "\"";
  55. }
  56. #if (defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED)
  57. else if (typeid(__expected) == typeid(memcached_return_t))
  58. {
  59. libtest::stream::make_cerr(file, line, func) << "Expected \""
  60. << memcached_strerror(NULL, memcached_return_t(__expected))
  61. << "\" got \""
  62. << memcached_strerror(NULL, memcached_return_t(__actual)) << "\"";
  63. }
  64. #endif
  65. #if defined(HAVE_LIBGEARMAN) && HAVE_LIBGEARMAN
  66. else if (typeid(__expected) == typeid(gearman_return_t))
  67. {
  68. libtest::stream::make_cerr(file, line, func) << "Expected \""
  69. << gearman_strerror(gearman_return_t(__expected))
  70. << "\" got \""
  71. << gearman_strerror(gearman_return_t(__actual)) << "\"";
  72. }
  73. #endif
  74. else
  75. {
  76. libtest::stream::make_cerr(file, line, func) << "Expected \"" << __expected << "\" got \"" << __actual << "\"";
  77. }
  78. return false;
  79. }
  80. return true;
  81. }
  82. template <class T_comparable>
  83. bool _compare_zero(const char *file, int line, const char *func, T_comparable __actual)
  84. {
  85. if (T_comparable(0) != __actual)
  86. {
  87. libtest::stream::make_cerr(file, line, func) << "Expected 0 got \"" << __actual << "\"";
  88. return false;
  89. }
  90. return true;
  91. }
  92. template <class T_comparable, class T_hint>
  93. bool _compare_hint(const char *file, int line, const char *func, T_comparable __expected, T_comparable __actual, T_hint __hint)
  94. {
  95. if (__expected != __actual)
  96. {
  97. libtest::stream::make_cerr(file, line, func) << "Expected \"" << __expected << "\" got \"" << __actual << "\" Additionally: \"" << __hint << "\"";
  98. return false;
  99. }
  100. return true;
  101. }
  102. } // namespace libtest