minicheck.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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-2020 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. Licensed under the MIT license:
  17. Permission is hereby granted, free of charge, to any person obtaining
  18. a copy of this software and associated documentation files (the
  19. "Software"), to deal in the Software without restriction, including
  20. without limitation the rights to use, copy, modify, merge, publish,
  21. distribute, sublicense, and/or sell copies of the Software, and to permit
  22. persons to whom the Software is furnished to do so, subject to the
  23. following conditions:
  24. The above copyright notice and this permission notice shall be included
  25. in all copies or substantial portions of the Software.
  26. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  29. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  30. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  31. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  32. USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <setjmp.h>
  37. #include <assert.h>
  38. #include <string.h>
  39. #include "internal.h" /* for UNUSED_P only */
  40. #include "minicheck.h"
  41. Suite *
  42. suite_create(const char *name) {
  43. Suite *suite = (Suite *)calloc(1, sizeof(Suite));
  44. if (suite != NULL) {
  45. suite->name = name;
  46. }
  47. return suite;
  48. }
  49. TCase *
  50. tcase_create(const char *name) {
  51. TCase *tc = (TCase *)calloc(1, sizeof(TCase));
  52. if (tc != NULL) {
  53. tc->name = name;
  54. }
  55. return tc;
  56. }
  57. void
  58. suite_add_tcase(Suite *suite, TCase *tc) {
  59. assert(suite != NULL);
  60. assert(tc != NULL);
  61. assert(tc->next_tcase == NULL);
  62. tc->next_tcase = suite->tests;
  63. suite->tests = tc;
  64. }
  65. void
  66. tcase_add_checked_fixture(TCase *tc, tcase_setup_function setup,
  67. tcase_teardown_function teardown) {
  68. assert(tc != NULL);
  69. tc->setup = setup;
  70. tc->teardown = teardown;
  71. }
  72. void
  73. tcase_add_test(TCase *tc, tcase_test_function test) {
  74. assert(tc != NULL);
  75. if (tc->allocated == tc->ntests) {
  76. int nalloc = tc->allocated + 100;
  77. size_t new_size = sizeof(tcase_test_function) * nalloc;
  78. tcase_test_function *new_tests = realloc(tc->tests, new_size);
  79. assert(new_tests != NULL);
  80. tc->tests = new_tests;
  81. tc->allocated = nalloc;
  82. }
  83. tc->tests[tc->ntests] = test;
  84. tc->ntests++;
  85. }
  86. static void
  87. tcase_free(TCase *tc) {
  88. if (! tc) {
  89. return;
  90. }
  91. free(tc->tests);
  92. free(tc);
  93. }
  94. static void
  95. suite_free(Suite *suite) {
  96. if (! suite) {
  97. return;
  98. }
  99. while (suite->tests != NULL) {
  100. TCase *next = suite->tests->next_tcase;
  101. tcase_free(suite->tests);
  102. suite->tests = next;
  103. }
  104. free(suite);
  105. }
  106. SRunner *
  107. srunner_create(Suite *suite) {
  108. SRunner *runner = calloc(1, sizeof(SRunner));
  109. if (runner != NULL) {
  110. runner->suite = suite;
  111. }
  112. return runner;
  113. }
  114. static jmp_buf env;
  115. static char const *_check_current_function = NULL;
  116. static int _check_current_lineno = -1;
  117. static char const *_check_current_filename = NULL;
  118. void
  119. _check_set_test_info(char const *function, char const *filename, int lineno) {
  120. _check_current_function = function;
  121. _check_current_lineno = lineno;
  122. _check_current_filename = filename;
  123. }
  124. static void
  125. handle_success(int verbosity) {
  126. if (verbosity >= CK_VERBOSE) {
  127. printf("PASS: %s\n", _check_current_function);
  128. }
  129. }
  130. static void
  131. handle_failure(SRunner *runner, int verbosity, const char *phase_info) {
  132. runner->nfailures++;
  133. if (verbosity != CK_SILENT) {
  134. printf("FAIL: %s (%s at %s:%d)\n", _check_current_function, phase_info,
  135. _check_current_filename, _check_current_lineno);
  136. }
  137. }
  138. void
  139. srunner_run_all(SRunner *runner, int verbosity) {
  140. Suite *suite;
  141. TCase *volatile tc;
  142. assert(runner != NULL);
  143. suite = runner->suite;
  144. tc = suite->tests;
  145. while (tc != NULL) {
  146. volatile int i;
  147. for (i = 0; i < tc->ntests; ++i) {
  148. runner->nchecks++;
  149. if (tc->setup != NULL) {
  150. /* setup */
  151. if (setjmp(env)) {
  152. handle_failure(runner, verbosity, "during setup");
  153. continue;
  154. }
  155. tc->setup();
  156. }
  157. /* test */
  158. if (setjmp(env)) {
  159. handle_failure(runner, verbosity, "during actual test");
  160. continue;
  161. }
  162. (tc->tests[i])();
  163. /* teardown */
  164. if (tc->teardown != NULL) {
  165. if (setjmp(env)) {
  166. handle_failure(runner, verbosity, "during teardown");
  167. continue;
  168. }
  169. tc->teardown();
  170. }
  171. handle_success(verbosity);
  172. }
  173. tc = tc->next_tcase;
  174. }
  175. if (verbosity != CK_SILENT) {
  176. int passed = runner->nchecks - runner->nfailures;
  177. double percentage = ((double)passed) / runner->nchecks;
  178. int display = (int)(percentage * 100);
  179. printf("%d%%: Checks: %d, Failed: %d\n", display, runner->nchecks,
  180. runner->nfailures);
  181. }
  182. }
  183. void
  184. _fail_unless(int condition, const char *file, int line, const char *msg) {
  185. /* Always print the error message so it isn't lost. In this case,
  186. we have a failure, so there's no reason to be quiet about what
  187. it is.
  188. */
  189. UNUSED_P(condition);
  190. _check_current_filename = file;
  191. _check_current_lineno = line;
  192. if (msg != NULL) {
  193. const int has_newline = (msg[strlen(msg) - 1] == '\n');
  194. fprintf(stderr, "ERROR: %s%s", msg, has_newline ? "" : "\n");
  195. }
  196. longjmp(env, 1);
  197. }
  198. int
  199. srunner_ntests_failed(SRunner *runner) {
  200. assert(runner != NULL);
  201. return runner->nfailures;
  202. }
  203. void
  204. srunner_free(SRunner *runner) {
  205. if (! runner) {
  206. return;
  207. }
  208. suite_free(runner->suite);
  209. free(runner);
  210. }