tap.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*-
  2. * Copyright (c) 2004 Nik Clayton
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. */
  26. /**
  27. * plan_tests - announce the number of tests you plan to run
  28. * @tests: the number of tests
  29. *
  30. * This should be the first call in your test program: it allows tracing
  31. * of failures which mean that not all tests are run.
  32. *
  33. * If you don't know how many tests will actually be run, assume all of them
  34. * and use skip() if you don't actually run some tests.
  35. *
  36. * Example:
  37. * plan_tests(13);
  38. */
  39. void plan_tests(unsigned int tests);
  40. #if (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L) && !defined(__GNUC__)
  41. # error "Needs gcc or C99 compiler for variadic macros."
  42. #else
  43. /**
  44. * ok1 - Simple conditional test
  45. * @e: the expression which we expect to be true.
  46. *
  47. * This is the simplest kind of test: if the expression is true, the
  48. * test passes. The name of the test which is printed will simply be
  49. * file name, line number, and the expression itself.
  50. *
  51. * Example:
  52. * ok1(init_subsystem() == 1);
  53. */
  54. # define ok1(e) ((e) ? \
  55. _gen_result(1, __func__, __FILE__, __LINE__, "%s", #e) : \
  56. _gen_result(0, __func__, __FILE__, __LINE__, "%s", #e))
  57. /**
  58. * ok - Conditional test with a name
  59. * @e: the expression which we expect to be true.
  60. * @...: the printf-style name of the test.
  61. *
  62. * If the expression is true, the test passes. The name of the test will be
  63. * the filename, line number, and the printf-style string. This can be clearer
  64. * than simply the expression itself.
  65. *
  66. * Example:
  67. * ok1(init_subsystem() == 1);
  68. * ok(init_subsystem() == 0, "Second initialization should fail");
  69. */
  70. # define ok(e, ...) ((e) ? \
  71. _gen_result(1, __func__, __FILE__, __LINE__, \
  72. __VA_ARGS__) : \
  73. _gen_result(0, __func__, __FILE__, __LINE__, \
  74. __VA_ARGS__))
  75. /**
  76. * pass - Note that a test passed
  77. * @...: the printf-style name of the test.
  78. *
  79. * For complicated code paths, it can be easiest to simply call pass() in one
  80. * branch and fail() in another.
  81. *
  82. * Example:
  83. * x = do_something();
  84. * if (!checkable(x) || check_value(x))
  85. * pass("do_something() returned a valid value");
  86. * else
  87. * fail("do_something() returned an invalid value");
  88. */
  89. # define pass(...) ok(1, __VA_ARGS__)
  90. /**
  91. * fail - Note that a test failed
  92. * @...: the printf-style name of the test.
  93. *
  94. * For complicated code paths, it can be easiest to simply call pass() in one
  95. * branch and fail() in another.
  96. */
  97. # define fail(...) ok(0, __VA_ARGS__)
  98. /* I don't find these to be useful. */
  99. # define skip_if(cond, n, ...) \
  100. if (cond) skip((n), __VA_ARGS__); \
  101. else
  102. # define skip_start(test, n, ...) \
  103. do { \
  104. if((test)) { \
  105. skip(n, __VA_ARGS__); \
  106. continue; \
  107. }
  108. # define skip_end } while(0)
  109. #ifndef PRINTF_ATTRIBUTE
  110. #ifdef __GNUC__
  111. #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
  112. #else
  113. #define PRINTF_ATTRIBUTE(a1, a2)
  114. #endif
  115. #endif
  116. unsigned int _gen_result(int, const char *, char *, unsigned int, char *, ...)
  117. PRINTF_ATTRIBUTE(5, 6);
  118. /**
  119. * diag - print a diagnostic message (use instead of printf/fprintf)
  120. * @fmt: the format of the printf-style message
  121. *
  122. * diag ensures that the output will not be considered to be a test
  123. * result by the TAP test harness. It will append '\n' for you.
  124. *
  125. * Example:
  126. * diag("Now running complex tests");
  127. */
  128. void diag(char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
  129. /**
  130. * skip - print a diagnostic message (use instead of printf/fprintf)
  131. * @n: number of tests you're skipping.
  132. * @fmt: the format of the reason you're skipping the tests.
  133. *
  134. * Sometimes tests cannot be run because the test system lacks some feature:
  135. * you should explicitly document that you're skipping tests using skip().
  136. *
  137. * From the Test::More documentation:
  138. * If it's something the user might not be able to do, use SKIP. This
  139. * includes optional modules that aren't installed, running under an OS that
  140. * doesn't have some feature (like fork() or symlinks), or maybe you need an
  141. * Internet connection and one isn't available.
  142. *
  143. * Example:
  144. * #ifdef HAVE_SOME_FEATURE
  145. * ok1(test_some_feature());
  146. * #else
  147. * skip(1, "Don't have SOME_FEATURE");
  148. * #endif
  149. */
  150. void skip(unsigned int n, char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
  151. /**
  152. * todo_start - mark tests that you expect to fail.
  153. * @fmt: the reason they currently fail.
  154. *
  155. * It's extremely useful to write tests before you implement the matching fix
  156. * or features: surround these tests by todo_start()/todo_end(). These tests
  157. * will still be run, but with additional output that indicates that they are
  158. * expected to fail.
  159. *
  160. * This way, should a test start to succeed unexpectedly, tools like prove(1)
  161. * will indicate this and you can move the test out of the todo block. This
  162. * is much more useful than simply commenting out (or '#if 0') the tests.
  163. *
  164. * From the Test::More documentation:
  165. * If it's something the programmer hasn't done yet, use TODO. This is for
  166. * any code you haven't written yet, or bugs you have yet to fix, but want to
  167. * put tests in your testing script (always a good idea).
  168. *
  169. * Example:
  170. * todo_start("dwim() not returning true yet");
  171. * ok(dwim(), "Did what the user wanted");
  172. * todo_end();
  173. */
  174. void todo_start(char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
  175. /**
  176. * todo_end - end of tests you expect to fail.
  177. *
  178. * See todo_start().
  179. */
  180. void todo_end(void);
  181. /**
  182. * exit_status - the value that main should return.
  183. *
  184. * For maximum compatability your test program should return a particular exit
  185. * code (ie. 0 if all tests were run, and every test which was expected to
  186. * succeed succeeded).
  187. *
  188. * Example:
  189. * exit(exit_status());
  190. */
  191. int exit_status(void);
  192. /**
  193. * plan_no_plan - I have no idea how many tests I'm going to run.
  194. *
  195. * In some situations you may not know how many tests you will be running, or
  196. * you are developing your test program, and do not want to update the
  197. * plan_tests() call every time you make a change. For those situations use
  198. * plan_no_plan() instead of plan_tests(). It indicates to the test harness
  199. * that an indeterminate number of tests will be run.
  200. *
  201. * Remember, if you fail to plan, you plan to fail.
  202. *
  203. * Example:
  204. * plan_no_plan();
  205. * while (random() % 2)
  206. * ok1(some_test());
  207. * exit(exit_status());
  208. */
  209. void plan_no_plan(void);
  210. /**
  211. * plan_skip_all - Indicate that you will skip all tests.
  212. * @reason: the string indicating why you can't run any tests.
  213. *
  214. * If your test program detects at run time that some required functionality
  215. * is missing (for example, it relies on a database connection which is not
  216. * present, or a particular configuration option that has not been included
  217. * in the running kernel) use plan_skip_all() instead of plan_tests().
  218. *
  219. * Example:
  220. * if (!have_some_feature) {
  221. * plan_skip_all("Need some_feature support");
  222. * exit(exit_status());
  223. * }
  224. * plan_tests(13);
  225. */
  226. void plan_skip_all(char *reason);
  227. #endif /* C99 or gcc */