minicheck.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* Miniature re-implementation of the "check" library.
  2. This is intended to support just enough of check to run the Expat
  3. tests. This interface is based entirely on the portion of the
  4. check library being used.
  5. __ __ _
  6. ___\ \/ /_ __ __ _| |_
  7. / _ \\ /| '_ \ / _` | __|
  8. | __// \| |_) | (_| | |_
  9. \___/_/\_\ .__/ \__,_|\__|
  10. |_| XML parser
  11. Copyright (c) 2004-2006 Fred L. Drake, Jr. <[email protected]>
  12. Copyright (c) 2016-2023 Sebastian Pipping <[email protected]>
  13. Copyright (c) 2017 Rhodri James <[email protected]>
  14. Copyright (c) 2018 Marco Maggi <[email protected]>
  15. Copyright (c) 2019 David Loffredo <[email protected]>
  16. Copyright (c) 2023-2024 Sony Corporation / Snild Dolkow <[email protected]>
  17. Licensed under the MIT license:
  18. Permission is hereby granted, free of charge, to any person obtaining
  19. a copy of this software and associated documentation files (the
  20. "Software"), to deal in the Software without restriction, including
  21. without limitation the rights to use, copy, modify, merge, publish,
  22. distribute, sublicense, and/or sell copies of the Software, and to permit
  23. persons to whom the Software is furnished to do so, subject to the
  24. following conditions:
  25. The above copyright notice and this permission notice shall be included
  26. in all copies or substantial portions of the Software.
  27. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  30. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  31. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  32. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  33. USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. */
  35. #if defined(NDEBUG)
  36. # undef NDEBUG /* because test suite relies on assert(...) at the moment */
  37. #endif
  38. #include <stdarg.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <setjmp.h>
  42. #include <assert.h>
  43. #include <string.h>
  44. #include "internal.h" /* for UNUSED_P only */
  45. #include "minicheck.h"
  46. Suite *
  47. suite_create(const char *name) {
  48. Suite *suite = (Suite *)calloc(1, sizeof(Suite));
  49. if (suite != NULL) {
  50. suite->name = name;
  51. }
  52. return suite;
  53. }
  54. TCase *
  55. tcase_create(const char *name) {
  56. TCase *tc = (TCase *)calloc(1, sizeof(TCase));
  57. if (tc != NULL) {
  58. tc->name = name;
  59. }
  60. return tc;
  61. }
  62. void
  63. suite_add_tcase(Suite *suite, TCase *tc) {
  64. assert(suite != NULL);
  65. assert(tc != NULL);
  66. assert(tc->next_tcase == NULL);
  67. tc->next_tcase = suite->tests;
  68. suite->tests = tc;
  69. }
  70. void
  71. tcase_add_checked_fixture(TCase *tc, tcase_setup_function setup,
  72. tcase_teardown_function teardown) {
  73. assert(tc != NULL);
  74. tc->setup = setup;
  75. tc->teardown = teardown;
  76. }
  77. void
  78. tcase_add_test(TCase *tc, tcase_test_function test) {
  79. assert(tc != NULL);
  80. if (tc->allocated == tc->ntests) {
  81. int nalloc = tc->allocated + 100;
  82. size_t new_size = sizeof(tcase_test_function) * nalloc;
  83. tcase_test_function *const new_tests
  84. = (tcase_test_function *)realloc(tc->tests, new_size);
  85. assert(new_tests != NULL);
  86. tc->tests = new_tests;
  87. tc->allocated = nalloc;
  88. }
  89. tc->tests[tc->ntests] = test;
  90. tc->ntests++;
  91. }
  92. static void
  93. tcase_free(TCase *tc) {
  94. if (! tc) {
  95. return;
  96. }
  97. free(tc->tests);
  98. free(tc);
  99. }
  100. static void
  101. suite_free(Suite *suite) {
  102. if (! suite) {
  103. return;
  104. }
  105. while (suite->tests != NULL) {
  106. TCase *next = suite->tests->next_tcase;
  107. tcase_free(suite->tests);
  108. suite->tests = next;
  109. }
  110. free(suite);
  111. }
  112. SRunner *
  113. srunner_create(Suite *suite) {
  114. SRunner *const runner = (SRunner *)calloc(1, sizeof(SRunner));
  115. if (runner != NULL) {
  116. runner->suite = suite;
  117. }
  118. return runner;
  119. }
  120. static jmp_buf env;
  121. #define SUBTEST_LEN (50) // informative, but not too long
  122. static char const *_check_current_function = NULL;
  123. static char _check_current_subtest[SUBTEST_LEN];
  124. static int _check_current_lineno = -1;
  125. static char const *_check_current_filename = NULL;
  126. void
  127. _check_set_test_info(char const *function, char const *filename, int lineno) {
  128. _check_current_function = function;
  129. set_subtest("%s", "");
  130. _check_current_lineno = lineno;
  131. _check_current_filename = filename;
  132. }
  133. void
  134. set_subtest(char const *fmt, ...) {
  135. va_list ap;
  136. va_start(ap, fmt);
  137. vsnprintf(_check_current_subtest, SUBTEST_LEN, fmt, ap);
  138. va_end(ap);
  139. // replace line feeds with spaces, for nicer error logs
  140. for (size_t i = 0; i < SUBTEST_LEN; ++i) {
  141. if (_check_current_subtest[i] == '\n') {
  142. _check_current_subtest[i] = ' ';
  143. }
  144. }
  145. _check_current_subtest[SUBTEST_LEN - 1] = '\0'; // ensure termination
  146. }
  147. static void
  148. handle_success(int verbosity) {
  149. if (verbosity >= CK_VERBOSE) {
  150. printf("PASS: %s\n", _check_current_function);
  151. }
  152. }
  153. static void
  154. handle_failure(SRunner *runner, int verbosity, const char *context,
  155. const char *phase_info) {
  156. runner->nfailures++;
  157. if (verbosity != CK_SILENT) {
  158. if (strlen(_check_current_subtest) != 0) {
  159. phase_info = _check_current_subtest;
  160. }
  161. printf("FAIL [%s]: %s (%s at %s:%d)\n", context, _check_current_function,
  162. phase_info, _check_current_filename, _check_current_lineno);
  163. }
  164. }
  165. void
  166. srunner_run_all(SRunner *runner, const char *context, int verbosity) {
  167. Suite *suite;
  168. TCase *volatile tc;
  169. assert(runner != NULL);
  170. suite = runner->suite;
  171. tc = suite->tests;
  172. while (tc != NULL) {
  173. volatile int i;
  174. for (i = 0; i < tc->ntests; ++i) {
  175. runner->nchecks++;
  176. set_subtest("%s", "");
  177. if (tc->setup != NULL) {
  178. /* setup */
  179. if (setjmp(env)) {
  180. handle_failure(runner, verbosity, context, "during setup");
  181. continue;
  182. }
  183. tc->setup();
  184. }
  185. /* test */
  186. if (setjmp(env)) {
  187. handle_failure(runner, verbosity, context, "during actual test");
  188. continue;
  189. }
  190. (tc->tests[i])();
  191. set_subtest("%s", "");
  192. /* teardown */
  193. if (tc->teardown != NULL) {
  194. if (setjmp(env)) {
  195. handle_failure(runner, verbosity, context, "during teardown");
  196. continue;
  197. }
  198. tc->teardown();
  199. }
  200. handle_success(verbosity);
  201. }
  202. tc = tc->next_tcase;
  203. }
  204. }
  205. void
  206. srunner_summarize(SRunner *runner, int verbosity) {
  207. if (verbosity != CK_SILENT) {
  208. int passed = runner->nchecks - runner->nfailures;
  209. double percentage = ((double)passed) / runner->nchecks;
  210. int display = (int)(percentage * 100);
  211. printf("%d%%: Checks: %d, Failed: %d\n", display, runner->nchecks,
  212. runner->nfailures);
  213. }
  214. }
  215. void
  216. _fail(const char *file, int line, const char *msg) {
  217. /* Always print the error message so it isn't lost. In this case,
  218. we have a failure, so there's no reason to be quiet about what
  219. it is.
  220. */
  221. _check_current_filename = file;
  222. _check_current_lineno = line;
  223. if (msg != NULL) {
  224. const int has_newline = (msg[strlen(msg) - 1] == '\n');
  225. fprintf(stderr, "ERROR: %s%s", msg, has_newline ? "" : "\n");
  226. }
  227. longjmp(env, 1);
  228. }
  229. int
  230. srunner_ntests_failed(SRunner *runner) {
  231. assert(runner != NULL);
  232. return runner->nfailures;
  233. }
  234. void
  235. srunner_free(SRunner *runner) {
  236. if (! runner) {
  237. return;
  238. }
  239. suite_free(runner->suite);
  240. free(runner);
  241. }