test.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Data Differential YATL (i.e. libtest) library
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #pragma once
  37. #ifndef __INTEL_COMPILER
  38. #pragma GCC diagnostic ignored "-Wold-style-cast"
  39. #endif
  40. #include <libtest/lite.h>
  41. /**
  42. A structure describing the test case.
  43. */
  44. struct test_st {
  45. const char *name;
  46. bool requires_flush;
  47. test_callback_fn *test_fn;
  48. };
  49. namespace libtest {
  50. class TestCase;
  51. typedef std::vector<libtest::TestCase*> TestCases;
  52. class TestCase {
  53. public:
  54. TestCase(const struct test_st* test_):
  55. _result(TEST_FAILURE)
  56. {
  57. _test.name= test_->name;
  58. _test.requires_flush= test_->requires_flush;
  59. _test.test_fn= test_->test_fn;
  60. }
  61. const char* name() const
  62. {
  63. return _test.name;
  64. }
  65. test_return_t result() const
  66. {
  67. return _result;
  68. }
  69. void result(test_return_t result_)
  70. {
  71. _result= result_;
  72. }
  73. void result(test_return_t result_, const libtest::Timer& timer_)
  74. {
  75. _result= result_;
  76. _timer= timer_;
  77. }
  78. const libtest::Timer& timer() const
  79. {
  80. return _timer;
  81. }
  82. void timer(libtest::Timer& timer_)
  83. {
  84. _timer= timer_;
  85. }
  86. void skipped()
  87. {
  88. _result= TEST_SKIPPED;
  89. }
  90. void failed()
  91. {
  92. _result= TEST_FAILURE;
  93. }
  94. void success(const libtest::Timer& timer_)
  95. {
  96. _result= TEST_SUCCESS;
  97. _timer= timer_;
  98. }
  99. const struct test_st* test() const
  100. {
  101. return &_test;
  102. }
  103. bool requires_flush() const
  104. {
  105. return _test.requires_flush;
  106. }
  107. private:
  108. struct test_st _test;
  109. test_return_t _result;
  110. libtest::Timer _timer;
  111. };
  112. } // namespace libtest
  113. #define test_assert_errno(A) \
  114. do \
  115. { \
  116. if ((A)) { \
  117. fprintf(stderr, "\n%s:%d: Assertion failed for %s: ", __FILE__, __LINE__, __func__);\
  118. perror(#A); \
  119. fprintf(stderr, "\n"); \
  120. libtest::create_core(); \
  121. assert((A)); \
  122. } \
  123. } while (0)
  124. #define test_true_got(A, B) ASSERT_TRUE(A);
  125. #define test_true_hint(A, B) ASSERT_TRUE(A);
  126. #define test_compare_hint(A, B, C) test_compare(A, B);
  127. #define test_compare_got(A, B, C) test_compare(A, B);
  128. #define test_skip(__expected, __actual) \
  129. do \
  130. { \
  131. if (libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), false) == false) \
  132. { \
  133. return TEST_SKIPPED; \
  134. } \
  135. } while (0)
  136. #define test_skip_valgrind() \
  137. do \
  138. { \
  139. if (libtest::_in_valgrind(__FILE__, __LINE__, __func__)) \
  140. { \
  141. return TEST_SKIPPED; \
  142. } \
  143. } while (0)
  144. #define test_fail(A) \
  145. do \
  146. { \
  147. if (1) { \
  148. fprintf(stderr, "\n%s:%d: Failed with %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
  149. libtest::create_core(); \
  150. return TEST_FAILURE; \
  151. } \
  152. } while (0)
  153. #define test_false(A) \
  154. do \
  155. { \
  156. if ((A)) { \
  157. fprintf(stderr, "\n%s:%d: Assertion failed %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
  158. libtest::create_core(); \
  159. return TEST_FAILURE; \
  160. } \
  161. } while (0)
  162. #define test_false_with(A,B) \
  163. do \
  164. { \
  165. if ((A)) { \
  166. fprintf(stderr, "\n%s:%d: Assertion failed %s with %s\n", __FILE__, __LINE__, #A, (B));\
  167. libtest::create_core(); \
  168. return TEST_FAILURE; \
  169. } \
  170. } while (0)
  171. #define test_ne_compare(__expected, __actual) \
  172. do \
  173. { \
  174. if (libtest::_ne_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), true) == false) \
  175. { \
  176. libtest::create_core(); \
  177. return TEST_FAILURE; \
  178. } \
  179. } while (0)
  180. #define test_compare(__expected, __actual) \
  181. do \
  182. { \
  183. if (libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), true) == false) \
  184. { \
  185. libtest::create_core(); \
  186. return TEST_FAILURE; \
  187. } \
  188. } while (0)
  189. #define test_zero(__actual) \
  190. do \
  191. { \
  192. if (libtest::_compare_zero(__FILE__, __LINE__, __func__, ((__actual))) == false) \
  193. { \
  194. libtest::create_core(); \
  195. return TEST_FAILURE; \
  196. } \
  197. } while (0)
  198. #define test_null test_zero
  199. #define test_compare_warn(__expected, __actual) \
  200. do \
  201. { \
  202. void(libtest::_compare(__FILE__, __LINE__, __func__, (__expected), (__actual)), true); \
  203. } while (0)
  204. #define test_warn(__truth, __explain) \
  205. do \
  206. { \
  207. void(libtest::_assert_truth(__FILE__, __LINE__, __func__, bool((__truth)), #__truth, __explain)); \
  208. } while (0)
  209. #define test_strcmp(__expected, __actual) \
  210. do \
  211. { \
  212. void(libtest::_compare_strcmp(__FILE__, __LINE__, __func__, (__expected), (__actual))); \
  213. } while (0)
  214. #define test_memcmp(A,B,C) \
  215. do \
  216. { \
  217. if ((A) == NULL or (B) == NULL or memcmp((A), (B), (C))) \
  218. { \
  219. fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
  220. libtest::create_core(); \
  221. return TEST_FAILURE; \
  222. } \
  223. } while (0)
  224. #define test_return_if(__test_return_t) \
  225. do \
  226. { \
  227. if ((__test_return_t) != TEST_SUCCESS) \
  228. { \
  229. return __test_return_t; \
  230. } \
  231. } while (0)