tap.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. #define _GNU_SOURCE
  27. #include <ctype.h>
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include "tap.h"
  32. static int no_plan = 0;
  33. static int skip_all = 0;
  34. static int have_plan = 0;
  35. static unsigned int test_count = 0; /* Number of tests that have been run */
  36. static unsigned int e_tests = 0; /* Expected number of tests to run */
  37. static unsigned int failures = 0; /* Number of tests that failed */
  38. static char *todo_msg = NULL;
  39. static char *todo_msg_fixed = "libtap malloc issue";
  40. static int todo = 0;
  41. static int test_died = 0;
  42. /* Encapsulate the pthread code in a conditional. In the absence of
  43. libpthread the code does nothing */
  44. #ifdef HAVE_LIBPTHREAD
  45. #include <pthread.h>
  46. static pthread_mutex_t M = PTHREAD_MUTEX_INITIALIZER;
  47. # define LOCK pthread_mutex_lock(&M)
  48. # define UNLOCK pthread_mutex_unlock(&M)
  49. #else
  50. # define LOCK
  51. # define UNLOCK
  52. #endif
  53. static void
  54. _expected_tests(unsigned int tests)
  55. {
  56. printf("1..%d\n", tests);
  57. e_tests = tests;
  58. }
  59. static void
  60. diagv(char *fmt, va_list ap)
  61. {
  62. fputs("# ", stderr);
  63. vfprintf(stderr, fmt, ap);
  64. fputs("\n", stderr);
  65. }
  66. static void
  67. _diag(char *fmt, ...)
  68. {
  69. va_list ap;
  70. va_start(ap, fmt);
  71. diagv(fmt, ap);
  72. va_end(ap);
  73. }
  74. /*
  75. * Generate a test result.
  76. *
  77. * ok -- boolean, indicates whether or not the test passed.
  78. * test_name -- the name of the test, may be NULL
  79. * test_comment -- a comment to print afterwards, may be NULL
  80. */
  81. unsigned int
  82. _gen_result(int ok, const char *func, char *file, unsigned int line,
  83. char *test_name, ...)
  84. {
  85. va_list ap;
  86. char *local_test_name = NULL;
  87. char *c;
  88. int name_is_digits;
  89. LOCK;
  90. test_count++;
  91. /* Start by taking the test name and performing any printf()
  92. expansions on it */
  93. if(test_name != NULL) {
  94. va_start(ap, test_name);
  95. vasprintf(&local_test_name, test_name, ap);
  96. va_end(ap);
  97. /* Make sure the test name contains more than digits
  98. and spaces. Emit an error message and exit if it
  99. does */
  100. if(local_test_name) {
  101. name_is_digits = 1;
  102. for(c = local_test_name; *c != '\0'; c++) {
  103. if(!isdigit(*c) && !isspace(*c)) {
  104. name_is_digits = 0;
  105. break;
  106. }
  107. }
  108. if(name_is_digits) {
  109. _diag(" You named your test '%s'. You shouldn't use numbers for your test names.", local_test_name);
  110. _diag(" Very confusing.");
  111. }
  112. }
  113. }
  114. if(!ok) {
  115. printf("not ");
  116. failures++;
  117. }
  118. printf("ok %d", test_count);
  119. if(test_name != NULL) {
  120. printf(" - ");
  121. /* Print the test name, escaping any '#' characters it
  122. might contain */
  123. if(local_test_name != NULL) {
  124. flockfile(stdout);
  125. for(c = local_test_name; *c != '\0'; c++) {
  126. if(*c == '#')
  127. fputc('\\', stdout);
  128. fputc((int)*c, stdout);
  129. }
  130. funlockfile(stdout);
  131. } else { /* vasprintf() failed, use a fixed message */
  132. printf("%s", todo_msg_fixed);
  133. }
  134. }
  135. /* If we're in a todo_start() block then flag the test as being
  136. TODO. todo_msg should contain the message to print at this
  137. point. If it's NULL then asprintf() failed, and we should
  138. use the fixed message.
  139. This is not counted as a failure, so decrement the counter if
  140. the test failed. */
  141. if(todo) {
  142. printf(" # TODO %s", todo_msg ? todo_msg : todo_msg_fixed);
  143. if(!ok)
  144. failures--;
  145. }
  146. printf("\n");
  147. if(!ok)
  148. _diag(" Failed %stest (%s:%s() at line %d)",
  149. todo ? "(TODO) " : "", file, func, line);
  150. free(local_test_name);
  151. UNLOCK;
  152. /* We only care (when testing) that ok is positive, but here we
  153. specifically only want to return 1 or 0 */
  154. return ok ? 1 : 0;
  155. }
  156. /*
  157. * Cleanup at the end of the run, produce any final output that might be
  158. * required.
  159. */
  160. static void
  161. _cleanup(void)
  162. {
  163. LOCK;
  164. /* If plan_no_plan() wasn't called, and we don't have a plan,
  165. and we're not skipping everything, then something happened
  166. before we could produce any output */
  167. if(!no_plan && !have_plan && !skip_all) {
  168. _diag("Looks like your test died before it could output anything.");
  169. UNLOCK;
  170. return;
  171. }
  172. if(test_died) {
  173. _diag("Looks like your test died just after %d.", test_count);
  174. UNLOCK;
  175. return;
  176. }
  177. /* No plan provided, but now we know how many tests were run, and can
  178. print the header at the end */
  179. if(!skip_all && (no_plan || !have_plan)) {
  180. printf("1..%d\n", test_count);
  181. }
  182. if((have_plan && !no_plan) && e_tests < test_count) {
  183. _diag("Looks like you planned %d tests but ran %d extra.",
  184. e_tests, test_count - e_tests);
  185. UNLOCK;
  186. return;
  187. }
  188. if((have_plan || !no_plan) && e_tests > test_count) {
  189. _diag("Looks like you planned %d tests but only ran %d.",
  190. e_tests, test_count);
  191. if(failures) {
  192. _diag("Looks like you failed %d tests of %d run.",
  193. failures, test_count);
  194. }
  195. UNLOCK;
  196. return;
  197. }
  198. if(failures)
  199. _diag("Looks like you failed %d tests of %d.",
  200. failures, test_count);
  201. UNLOCK;
  202. }
  203. /*
  204. * Initialise the TAP library. Will only do so once, however many times it's
  205. * called.
  206. */
  207. static void
  208. _tap_init(void)
  209. {
  210. static int run_once = 0;
  211. if(!run_once) {
  212. atexit(_cleanup);
  213. /* stdout needs to be unbuffered so that the output appears
  214. in the same place relative to stderr output as it does
  215. with Test::Harness */
  216. setbuf(stdout, 0);
  217. run_once = 1;
  218. }
  219. }
  220. /*
  221. * Note that there's no plan.
  222. */
  223. void
  224. plan_no_plan(void)
  225. {
  226. LOCK;
  227. _tap_init();
  228. if(have_plan != 0) {
  229. fprintf(stderr, "You tried to plan twice!\n");
  230. test_died = 1;
  231. UNLOCK;
  232. exit(255);
  233. }
  234. have_plan = 1;
  235. no_plan = 1;
  236. UNLOCK;
  237. }
  238. /*
  239. * Note that the plan is to skip all tests
  240. */
  241. void
  242. plan_skip_all(char *reason)
  243. {
  244. LOCK;
  245. _tap_init();
  246. skip_all = 1;
  247. printf("1..0");
  248. if(reason != NULL)
  249. printf(" # Skip %s", reason);
  250. printf("\n");
  251. UNLOCK;
  252. }
  253. /*
  254. * Note the number of tests that will be run.
  255. */
  256. void
  257. plan_tests(unsigned int tests)
  258. {
  259. LOCK;
  260. _tap_init();
  261. if(have_plan != 0) {
  262. fprintf(stderr, "You tried to plan twice!\n");
  263. test_died = 1;
  264. UNLOCK;
  265. exit(255);
  266. }
  267. if(tests == 0) {
  268. fprintf(stderr, "You said to run 0 tests! You've got to run something.\n");
  269. test_died = 1;
  270. UNLOCK;
  271. exit(255);
  272. }
  273. have_plan = 1;
  274. _expected_tests(tests);
  275. UNLOCK;
  276. }
  277. void
  278. diag(char *fmt, ...)
  279. {
  280. va_list ap;
  281. LOCK;
  282. va_start(ap, fmt);
  283. diagv(fmt, ap);
  284. va_end(ap);
  285. UNLOCK;
  286. }
  287. void
  288. skip(unsigned int n, char *fmt, ...)
  289. {
  290. va_list ap;
  291. char *skip_msg;
  292. LOCK;
  293. va_start(ap, fmt);
  294. vasprintf(&skip_msg, fmt, ap);
  295. va_end(ap);
  296. while(n-- > 0) {
  297. test_count++;
  298. printf("ok %d # skip %s\n", test_count,
  299. skip_msg != NULL ?
  300. skip_msg : "libtap():malloc() failed");
  301. }
  302. free(skip_msg);
  303. UNLOCK;
  304. }
  305. void
  306. todo_start(char *fmt, ...)
  307. {
  308. va_list ap;
  309. LOCK;
  310. va_start(ap, fmt);
  311. vasprintf(&todo_msg, fmt, ap);
  312. va_end(ap);
  313. todo = 1;
  314. UNLOCK;
  315. }
  316. void
  317. todo_end(void)
  318. {
  319. LOCK;
  320. todo = 0;
  321. free(todo_msg);
  322. UNLOCK;
  323. }
  324. int
  325. exit_status(void)
  326. {
  327. int r;
  328. LOCK;
  329. /* If there's no plan, just return the number of failures */
  330. if(no_plan || !have_plan) {
  331. UNLOCK;
  332. return failures;
  333. }
  334. /* Ran too many tests? Return the number of tests that were run
  335. that shouldn't have been */
  336. if(e_tests < test_count) {
  337. r = test_count - e_tests;
  338. UNLOCK;
  339. return r;
  340. }
  341. /* Return the number of tests that failed + the number of tests
  342. that weren't run */
  343. r = failures + e_tests - test_count;
  344. UNLOCK;
  345. return r;
  346. }