test.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * uTest Copyright (C) 2011 Data Differential, http://datadifferential.com/
  3. *
  4. * Use and distribution licensed under the BSD license. See
  5. * the COPYING file in the parent directory for full text.
  6. */
  7. #pragma once
  8. #ifndef __INTEL_COMPILER
  9. #pragma GCC diagnostic ignored "-Wold-style-cast"
  10. #endif
  11. #include <libtest/stream.h>
  12. #include <libtest/comparison.hpp>
  13. /**
  14. A structure describing the test case.
  15. */
  16. struct test_st {
  17. const char *name;
  18. bool requires_flush;
  19. test_callback_fn *test_fn;
  20. };
  21. #define test_assert_errno(A) \
  22. do \
  23. { \
  24. if ((A)) { \
  25. fprintf(stderr, "\n%s:%d: Assertion failed for %s: ", __FILE__, __LINE__, __func__);\
  26. perror(#A); \
  27. fprintf(stderr, "\n"); \
  28. create_core(); \
  29. assert((A)); \
  30. } \
  31. } while (0)
  32. #define test_assert(A, B) \
  33. do \
  34. { \
  35. if ((A)) { \
  36. fprintf(stderr, "\n%s:%d: Assertion failed %s, with message %s, in %s", __FILE__, __LINE__, (B), #A, __func__ );\
  37. fprintf(stderr, "\n"); \
  38. create_core(); \
  39. assert((A)); \
  40. } \
  41. } while (0)
  42. #define test_truth(A) \
  43. do \
  44. { \
  45. if (! (A)) { \
  46. fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
  47. create_core(); \
  48. return TEST_FAILURE; \
  49. } \
  50. } while (0)
  51. #define test_true(A) \
  52. do \
  53. { \
  54. if (! (A)) { \
  55. fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
  56. create_core(); \
  57. return TEST_FAILURE; \
  58. } \
  59. } while (0)
  60. #define test_true_got(__expected, __hint) \
  61. do \
  62. { \
  63. if (not libtest::_compare_true_hint(__FILE__, __LINE__, __func__, ((__expected)), #__expected, ((__hint)))) \
  64. { \
  65. create_core(); \
  66. return TEST_FAILURE; \
  67. } \
  68. } while (0)
  69. #define test_skip(A,B) \
  70. do \
  71. { \
  72. if ((A) != (B)) \
  73. { \
  74. return TEST_SKIPPED; \
  75. } \
  76. } while (0)
  77. #define test_fail(A) \
  78. do \
  79. { \
  80. if (1) { \
  81. fprintf(stderr, "\n%s:%d: Failed with %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
  82. create_core(); \
  83. return TEST_FAILURE; \
  84. } \
  85. } while (0)
  86. #define test_false(A) \
  87. do \
  88. { \
  89. if ((A)) { \
  90. fprintf(stderr, "\n%s:%d: Assertion failed %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
  91. create_core(); \
  92. return TEST_FAILURE; \
  93. } \
  94. } while (0)
  95. #define test_false_with(A,B) \
  96. do \
  97. { \
  98. if ((A)) { \
  99. fprintf(stderr, "\n%s:%d: Assertion failed %s with %s\n", __FILE__, __LINE__, #A, (B));\
  100. create_core(); \
  101. return TEST_FAILURE; \
  102. } \
  103. } while (0)
  104. #define test_compare(__expected, __actual) \
  105. do \
  106. { \
  107. if (not libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)))) \
  108. { \
  109. create_core(); \
  110. return TEST_FAILURE; \
  111. } \
  112. } while (0)
  113. #define test_zero(__actual) \
  114. do \
  115. { \
  116. if (not libtest::_compare_zero(__FILE__, __LINE__, __func__, ((__actual)))) \
  117. { \
  118. create_core(); \
  119. return TEST_FAILURE; \
  120. } \
  121. } while (0)
  122. #define test_compare_got(__expected, __actual, __hint) \
  123. do \
  124. { \
  125. if (not libtest::_compare_hint(__FILE__, __LINE__, __func__, (__expected), (__actual), (__hint))) \
  126. { \
  127. create_core(); \
  128. return TEST_FAILURE; \
  129. } \
  130. } while (0)
  131. #define test_strcmp(A,B) \
  132. do \
  133. { \
  134. if (strcmp((A), (B))) \
  135. { \
  136. fprintf(stderr, "\n%s:%d: Expected %s, got %s\n", __FILE__, __LINE__, (A), (B)); \
  137. create_core(); \
  138. return TEST_FAILURE; \
  139. } \
  140. } while (0)
  141. #define test_memcmp(A,B,C) \
  142. do \
  143. { \
  144. if (memcmp((A), (B), (C))) \
  145. { \
  146. fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
  147. create_core(); \
  148. return TEST_FAILURE; \
  149. } \
  150. } while (0)
  151. #define test_return_if(__test_return_t) \
  152. do \
  153. { \
  154. if ((__test_return_t) != TEST_SUCCESS) \
  155. { \
  156. return __test_return_t; \
  157. } \
  158. } while (0)