test.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_truth(A) \
  125. do \
  126. { \
  127. if (! (A)) { \
  128. fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
  129. libtest::create_core(); \
  130. return TEST_FAILURE; \
  131. } \
  132. } while (0)
  133. #define test_true(A) \
  134. do \
  135. { \
  136. if (! (A)) { \
  137. fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
  138. libtest::create_core(); \
  139. return TEST_FAILURE; \
  140. } \
  141. } while (0)
  142. #define test_true_got(A, B) test_true(A);
  143. #define test_true_hint(A, B) test_true(A);
  144. #define test_compare_hint(A, B, C) test_compare(A, B);
  145. #define test_compare_got(A, B, C) test_compare(A, B);
  146. #define test_skip(__expected, __actual) \
  147. do \
  148. { \
  149. if (libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), false) == false) \
  150. { \
  151. return TEST_SKIPPED; \
  152. } \
  153. } while (0)
  154. #define test_skip_valgrind() \
  155. do \
  156. { \
  157. if (libtest::_in_valgrind(__FILE__, __LINE__, __func__)) \
  158. { \
  159. return TEST_SKIPPED; \
  160. } \
  161. } while (0)
  162. #define test_fail(A) \
  163. do \
  164. { \
  165. if (1) { \
  166. fprintf(stderr, "\n%s:%d: Failed with %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
  167. libtest::create_core(); \
  168. return TEST_FAILURE; \
  169. } \
  170. } while (0)
  171. #define test_false(A) \
  172. do \
  173. { \
  174. if ((A)) { \
  175. fprintf(stderr, "\n%s:%d: Assertion failed %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
  176. libtest::create_core(); \
  177. return TEST_FAILURE; \
  178. } \
  179. } while (0)
  180. #define test_false_with(A,B) \
  181. do \
  182. { \
  183. if ((A)) { \
  184. fprintf(stderr, "\n%s:%d: Assertion failed %s with %s\n", __FILE__, __LINE__, #A, (B));\
  185. libtest::create_core(); \
  186. return TEST_FAILURE; \
  187. } \
  188. } while (0)
  189. #define test_ne_compare(__expected, __actual) \
  190. do \
  191. { \
  192. if (libtest::_ne_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), true) == false) \
  193. { \
  194. libtest::create_core(); \
  195. return TEST_FAILURE; \
  196. } \
  197. } while (0)
  198. #define test_compare(__expected, __actual) \
  199. do \
  200. { \
  201. if (libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), true) == false) \
  202. { \
  203. libtest::create_core(); \
  204. return TEST_FAILURE; \
  205. } \
  206. } while (0)
  207. #define test_zero(__actual) \
  208. do \
  209. { \
  210. if (libtest::_compare_zero(__FILE__, __LINE__, __func__, ((__actual))) == false) \
  211. { \
  212. libtest::create_core(); \
  213. return TEST_FAILURE; \
  214. } \
  215. } while (0)
  216. #define test_null test_zero
  217. #define test_compare_warn(__expected, __actual) \
  218. do \
  219. { \
  220. void(libtest::_compare(__FILE__, __LINE__, __func__, (__expected), (__actual)), true); \
  221. } while (0)
  222. #define test_warn(__truth, __explain) \
  223. do \
  224. { \
  225. void(libtest::_assert_truth(__FILE__, __LINE__, __func__, bool((__truth)), #__truth, __explain)); \
  226. } while (0)
  227. #define test_strcmp(__expected, __actual) \
  228. do \
  229. { \
  230. void(libtest::_compare_strcmp(__FILE__, __LINE__, __func__, (__expected), (__actual))); \
  231. } while (0)
  232. #define test_memcmp(A,B,C) \
  233. do \
  234. { \
  235. if ((A) == NULL or (B) == NULL or memcmp((A), (B), (C))) \
  236. { \
  237. fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
  238. libtest::create_core(); \
  239. return TEST_FAILURE; \
  240. } \
  241. } while (0)
  242. #define test_return_if(__test_return_t) \
  243. do \
  244. { \
  245. if ((__test_return_t) != TEST_SUCCESS) \
  246. { \
  247. return __test_return_t; \
  248. } \
  249. } while (0)