tinytest_macros.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* tinytest_macros.h -- Copyright 2009 Nick Mathewson
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions
  5. * are met:
  6. * 1. Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * 2. Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * 3. The name of the author may not be used to endorse or promote products
  12. * derived from this software without specific prior written permission.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef _TINYTEST_MACROS_H
  26. #define _TINYTEST_MACROS_H
  27. /* Helpers for defining statement-like macros */
  28. #define TT_STMT_BEGIN do {
  29. #define TT_STMT_END } while(0)
  30. /* Redefine this if your test functions want to abort with something besides
  31. * "goto end;" */
  32. #ifndef TT_EXIT_TEST_FUNCTION
  33. #define TT_EXIT_TEST_FUNCTION TT_STMT_BEGIN goto end; TT_STMT_END
  34. #endif
  35. /* Redefine this if you want to note success/failure in some different way. */
  36. #ifndef TT_DECLARE
  37. #define TT_DECLARE(prefix, args) \
  38. TT_STMT_BEGIN \
  39. printf("\n %s %s:%d: ",prefix,__FILE__,__LINE__); \
  40. printf args ; \
  41. TT_STMT_END
  42. #endif
  43. /* Announce a failure. Args are parenthesized printf args. */
  44. #define TT_GRIPE(args) TT_DECLARE("FAIL", args)
  45. /* Announce a non-failure if we're verbose. */
  46. #define TT_BLATHER(args) \
  47. TT_STMT_BEGIN \
  48. if (_tinytest_get_verbosity()>1) TT_DECLARE(" OK", args); \
  49. TT_STMT_END
  50. #define TT_DIE(args) \
  51. TT_STMT_BEGIN \
  52. _tinytest_set_test_failed(); \
  53. TT_GRIPE(args); \
  54. TT_EXIT_TEST_FUNCTION; \
  55. TT_STMT_END
  56. #define TT_FAIL(args) \
  57. TT_STMT_BEGIN \
  58. _tinytest_set_test_failed(); \
  59. TT_GRIPE(args); \
  60. TT_STMT_END
  61. /* Fail and abort the current test for the reason in msg */
  62. #define tt_abort_printf(msg) TT_DIE(msg)
  63. #define tt_abort_perror(op) TT_DIE(("%s: %s [%d]",(op),strerror(errno), errno))
  64. #define tt_abort_msg(msg) TT_DIE(("%s", msg))
  65. #define tt_abort() TT_DIE(("%s", "(Failed.)"))
  66. /* Fail but do not abort the current test for the reason in msg. */
  67. #define tt_fail_printf(msg) TT_FAIL(msg)
  68. #define tt_fail_perror(op) TT_FAIL(("%s: %s [%d]",(op),strerror(errno), errno))
  69. #define tt_fail_msg(msg) TT_FAIL(("%s", msg))
  70. #define tt_fail() TT_FAIL(("%s", "(Failed.)"))
  71. /* End the current test, and indicate we are skipping it. */
  72. #define tt_skip() \
  73. TT_STMT_BEGIN \
  74. _tinytest_set_test_skipped(); \
  75. TT_EXIT_TEST_FUNCTION; \
  76. TT_STMT_END
  77. #define _tt_want(b, msg, fail) \
  78. TT_STMT_BEGIN \
  79. if (!(b)) { \
  80. _tinytest_set_test_failed(); \
  81. TT_GRIPE((msg)); \
  82. fail; \
  83. } else { \
  84. TT_BLATHER((msg)); \
  85. } \
  86. TT_STMT_END
  87. /* Assert b, but do not stop the test if b fails. Log msg on failure. */
  88. #define tt_want_msg(b, msg) \
  89. _tt_want(b, msg, );
  90. /* Assert b and stop the test if b fails. Log msg on failure. */
  91. #define tt_assert_msg(b, msg) \
  92. _tt_want(b, msg, TT_EXIT_TEST_FUNCTION);
  93. /* Assert b, but do not stop the test if b fails. */
  94. #define tt_want(b) tt_want_msg( (b), "want("#b")")
  95. /* Assert b, and stop the test if b fails. */
  96. #define tt_assert(b) tt_assert_msg((b), "assert("#b")")
  97. #define tt_assert_test_fmt_type(a,b,str_test,type,test,printf_type,printf_fmt, \
  98. setup_block,cleanup_block) \
  99. TT_STMT_BEGIN \
  100. type _val1 = (type)(a); \
  101. type _val2 = (type)(b); \
  102. int _tt_status = (test); \
  103. if (!_tt_status || _tinytest_get_verbosity()>1) { \
  104. printf_type _print; \
  105. printf_type _print1; \
  106. printf_type _print2; \
  107. type _value = _val1; \
  108. setup_block; \
  109. _print1 = _print; \
  110. _value = _val2; \
  111. setup_block; \
  112. _print2 = _print; \
  113. TT_DECLARE(_tt_status?" OK":"FAIL", \
  114. ("assert(%s): "printf_fmt" vs "printf_fmt, \
  115. str_test, _print1, _print2)); \
  116. _print = _print1; \
  117. cleanup_block; \
  118. _print = _print2; \
  119. cleanup_block; \
  120. if (!_tt_status) { \
  121. _tinytest_set_test_failed(); \
  122. TT_EXIT_TEST_FUNCTION; \
  123. } \
  124. } \
  125. TT_STMT_END
  126. #define tt_assert_test_type(a,b,str_test,type,test,fmt) \
  127. tt_assert_test_fmt_type(a,b,str_test,type,test,type,fmt, \
  128. {_print=_value;},{})
  129. /* Helper: assert that a op b, when cast to type. Format the values with
  130. * printf format fmt on failure. */
  131. #define tt_assert_op_type(a,op,b,type,fmt) \
  132. tt_assert_test_type(a,b,#a" "#op" "#b,type,(_val1 op _val2),fmt)
  133. #define tt_int_op(a,op,b) \
  134. tt_assert_test_type(a,b,#a" "#op" "#b,long,(_val1 op _val2),"%ld")
  135. #define tt_uint_op(a,op,b) \
  136. tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long, \
  137. (_val1 op _val2),"%lu")
  138. #define tt_ptr_op(a,op,b) \
  139. tt_assert_test_type(a,b,#a" "#op" "#b,void*, \
  140. (_val1 op _val2),"%p")
  141. #define tt_str_op(a,op,b) \
  142. tt_assert_test_type(a,b,#a" "#op" "#b,const char *, \
  143. (strcmp(_val1,_val2) op 0),"<%s>")
  144. #endif