test.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #define test_assert_errno(A) \
  50. do \
  51. { \
  52. if ((A)) { \
  53. fprintf(stderr, "\n%s:%d: Assertion failed for %s: ", __FILE__, __LINE__, __func__);\
  54. perror(#A); \
  55. fprintf(stderr, "\n"); \
  56. libtest::create_core(); \
  57. assert((A)); \
  58. } \
  59. } while (0)
  60. #define test_truth(A) \
  61. do \
  62. { \
  63. if (! (A)) { \
  64. fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
  65. libtest::create_core(); \
  66. return TEST_FAILURE; \
  67. } \
  68. } while (0)
  69. #define test_true(A) \
  70. do \
  71. { \
  72. if (! (A)) { \
  73. fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
  74. libtest::create_core(); \
  75. return TEST_FAILURE; \
  76. } \
  77. } while (0)
  78. #define test_true_got(A, B) test_true(A);
  79. #define test_true_hint(A, B) test_true(A);
  80. #define test_compare_hint(A, B, C) test_compare(A, B);
  81. #define test_compare_got(A, B, C) test_compare(A, B);
  82. #define test_skip(__expected, __actual) \
  83. do \
  84. { \
  85. if (libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), false) == false) \
  86. { \
  87. return TEST_SKIPPED; \
  88. } \
  89. } while (0)
  90. #define test_skip_valgrind() \
  91. do \
  92. { \
  93. if (libtest::_in_valgrind(__FILE__, __LINE__, __func__)) \
  94. { \
  95. return TEST_SKIPPED; \
  96. } \
  97. } while (0)
  98. #define test_fail(A) \
  99. do \
  100. { \
  101. if (1) { \
  102. fprintf(stderr, "\n%s:%d: Failed with %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
  103. libtest::create_core(); \
  104. return TEST_FAILURE; \
  105. } \
  106. } while (0)
  107. #define test_false(A) \
  108. do \
  109. { \
  110. if ((A)) { \
  111. fprintf(stderr, "\n%s:%d: Assertion failed %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
  112. libtest::create_core(); \
  113. return TEST_FAILURE; \
  114. } \
  115. } while (0)
  116. #define test_false_with(A,B) \
  117. do \
  118. { \
  119. if ((A)) { \
  120. fprintf(stderr, "\n%s:%d: Assertion failed %s with %s\n", __FILE__, __LINE__, #A, (B));\
  121. libtest::create_core(); \
  122. return TEST_FAILURE; \
  123. } \
  124. } while (0)
  125. #define test_ne_compare(__expected, __actual) \
  126. do \
  127. { \
  128. if (libtest::_ne_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), true) == false) \
  129. { \
  130. libtest::create_core(); \
  131. return TEST_FAILURE; \
  132. } \
  133. } while (0)
  134. #define test_compare(__expected, __actual) \
  135. do \
  136. { \
  137. if (libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), true) == false) \
  138. { \
  139. libtest::create_core(); \
  140. return TEST_FAILURE; \
  141. } \
  142. } while (0)
  143. #define test_zero(__actual) \
  144. do \
  145. { \
  146. if (libtest::_compare_zero(__FILE__, __LINE__, __func__, ((__actual))) == false) \
  147. { \
  148. libtest::create_core(); \
  149. return TEST_FAILURE; \
  150. } \
  151. } while (0)
  152. #define test_null test_zero
  153. #define test_compare_warn(__expected, __actual) \
  154. do \
  155. { \
  156. void(libtest::_compare(__FILE__, __LINE__, __func__, (__expected), (__actual)), true); \
  157. } while (0)
  158. #define test_warn(__truth, __explain) \
  159. do \
  160. { \
  161. void(libtest::_assert_truth(__FILE__, __LINE__, __func__, bool((__truth)), #__truth, __explain)); \
  162. } while (0)
  163. #define test_strcmp(__expected, __actual) \
  164. do \
  165. { \
  166. void(libtest::_compare_strcmp(__FILE__, __LINE__, __func__, (__expected), (__actual))); \
  167. } while (0)
  168. #define test_memcmp(A,B,C) \
  169. do \
  170. { \
  171. if ((A) == NULL or (B) == NULL or memcmp((A), (B), (C))) \
  172. { \
  173. fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
  174. libtest::create_core(); \
  175. return TEST_FAILURE; \
  176. } \
  177. } while (0)
  178. #define test_return_if(__test_return_t) \
  179. do \
  180. { \
  181. if ((__test_return_t) != TEST_SUCCESS) \
  182. { \
  183. return __test_return_t; \
  184. } \
  185. } while (0)