4
0

tap.c 8.3 KB

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