minicheck.c 6.1 KB

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