tinytest_macros.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* tinytest_macros.h -- Copyright 2009-2012 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_INCLUDED_
  26. #define TINYTEST_MACROS_H_INCLUDED_
  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_failprint_f(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(("%s",msg)); \
  82. fail; \
  83. } else { \
  84. TT_BLATHER(("%s",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,die_on_fail) \
  99. TT_STMT_BEGIN \
  100. type val1_ = (a); \
  101. type val2_ = (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. die_on_fail ; \
  123. } \
  124. } \
  125. TT_STMT_END
  126. #define tt_assert_test_type(a,b,str_test,type,test,fmt,die_on_fail) \
  127. tt_assert_test_fmt_type(a,b,str_test,type,test,type,fmt, \
  128. {print_=value_;},{},die_on_fail)
  129. #define tt_assert_test_type_opt(a,b,str_test,type,test,fmt,die_on_fail) \
  130. tt_assert_test_fmt_type(a,b,str_test,type,test,type,fmt, \
  131. {print_=value_?value_:"<NULL>";},{},die_on_fail)
  132. /* Helper: assert that a op b, when cast to type. Format the values with
  133. * printf format fmt on failure. */
  134. #define tt_assert_op_type(a,op,b,type,fmt) \
  135. tt_assert_test_type(a,b,#a" "#op" "#b,type,(val1_ op val2_),fmt, \
  136. TT_EXIT_TEST_FUNCTION)
  137. #define tt_int_op(a,op,b) \
  138. tt_assert_test_type(a,b,#a" "#op" "#b,long,(val1_ op val2_), \
  139. "%ld",TT_EXIT_TEST_FUNCTION)
  140. #define tt_uint_op(a,op,b) \
  141. tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long, \
  142. (val1_ op val2_),"%lu",TT_EXIT_TEST_FUNCTION)
  143. #define tt_ptr_op(a,op,b) \
  144. tt_assert_test_type(a,b,#a" "#op" "#b,const void*, \
  145. (val1_ op val2_),"%p",TT_EXIT_TEST_FUNCTION)
  146. #define tt_str_op(a,op,b) \
  147. tt_assert_test_type_opt(a,b,#a" "#op" "#b,const char *, \
  148. (val1_ && val2_ && strcmp(val1_,val2_) op 0),"<%s>", \
  149. TT_EXIT_TEST_FUNCTION)
  150. #define tt_mem_op(expr1, op, expr2, len) \
  151. tt_assert_test_fmt_type(expr1,expr2,#expr1" "#op" "#expr2, \
  152. const void *, \
  153. (val1_ && val2_ && memcmp(val1_, val2_, len) op 0), \
  154. char *, "%s", \
  155. { print_ = tinytest_format_hex_(value_, (len)); }, \
  156. { if (print_) free(print_); }, \
  157. TT_EXIT_TEST_FUNCTION \
  158. );
  159. #define tt_want_int_op(a,op,b) \
  160. tt_assert_test_type(a,b,#a" "#op" "#b,long,(val1_ op val2_),"%ld",(void)0)
  161. #define tt_want_uint_op(a,op,b) \
  162. tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long, \
  163. (val1_ op val2_),"%lu",(void)0)
  164. #define tt_want_ptr_op(a,op,b) \
  165. tt_assert_test_type(a,b,#a" "#op" "#b,const void*, \
  166. (val1_ op val2_),"%p",(void)0)
  167. #define tt_want_str_op(a,op,b) \
  168. tt_assert_test_type(a,b,#a" "#op" "#b,const char *, \
  169. (strcmp(val1_,val2_) op 0),"<%s>",(void)0)
  170. #endif