minicheck.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. {
  41. Suite *suite = (Suite *) calloc(1, sizeof(Suite));
  42. if (suite != NULL) {
  43. suite->name = name;
  44. }
  45. return suite;
  46. }
  47. TCase *
  48. tcase_create(const char *name)
  49. {
  50. TCase *tc = (TCase *) calloc(1, sizeof(TCase));
  51. if (tc != NULL) {
  52. tc->name = name;
  53. }
  54. return tc;
  55. }
  56. void
  57. suite_add_tcase(Suite *suite, TCase *tc)
  58. {
  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,
  67. tcase_setup_function setup,
  68. tcase_teardown_function teardown)
  69. {
  70. assert(tc != NULL);
  71. tc->setup = setup;
  72. tc->teardown = teardown;
  73. }
  74. void
  75. tcase_add_test(TCase *tc, tcase_test_function test)
  76. {
  77. assert(tc != NULL);
  78. if (tc->allocated == tc->ntests) {
  79. int nalloc = tc->allocated + 100;
  80. size_t new_size = sizeof(tcase_test_function) * nalloc;
  81. tcase_test_function *new_tests = realloc(tc->tests, new_size);
  82. assert(new_tests != NULL);
  83. tc->tests = new_tests;
  84. tc->allocated = nalloc;
  85. }
  86. tc->tests[tc->ntests] = test;
  87. tc->ntests++;
  88. }
  89. static void
  90. tcase_free(TCase *tc)
  91. {
  92. if (! tc) {
  93. return;
  94. }
  95. free(tc->tests);
  96. free(tc);
  97. }
  98. static void
  99. suite_free(Suite *suite)
  100. {
  101. if (! suite) {
  102. return;
  103. }
  104. while (suite->tests != NULL) {
  105. TCase *next = suite->tests->next_tcase;
  106. tcase_free(suite->tests);
  107. suite->tests = next;
  108. }
  109. free(suite);
  110. }
  111. SRunner *
  112. srunner_create(Suite *suite)
  113. {
  114. SRunner *runner = calloc(1, sizeof(SRunner));
  115. if (runner != NULL) {
  116. runner->suite = suite;
  117. }
  118. return runner;
  119. }
  120. static jmp_buf env;
  121. static char const *_check_current_function = NULL;
  122. static int _check_current_lineno = -1;
  123. static char const *_check_current_filename = NULL;
  124. void
  125. _check_set_test_info(char const *function, char const *filename, int lineno)
  126. {
  127. _check_current_function = function;
  128. _check_current_lineno = lineno;
  129. _check_current_filename = filename;
  130. }
  131. static void
  132. add_failure(SRunner *runner, int verbosity)
  133. {
  134. runner->nfailures++;
  135. if (verbosity >= CK_VERBOSE) {
  136. printf("%s:%d: %s\n", _check_current_filename,
  137. _check_current_lineno, _check_current_function);
  138. }
  139. }
  140. void
  141. srunner_run_all(SRunner *runner, int verbosity)
  142. {
  143. Suite *suite;
  144. TCase *tc;
  145. assert(runner != NULL);
  146. suite = runner->suite;
  147. tc = suite->tests;
  148. while (tc != NULL) {
  149. int i;
  150. for (i = 0; i < tc->ntests; ++i) {
  151. runner->nchecks++;
  152. if (tc->setup != NULL) {
  153. /* setup */
  154. if (setjmp(env)) {
  155. add_failure(runner, verbosity);
  156. continue;
  157. }
  158. tc->setup();
  159. }
  160. /* test */
  161. if (setjmp(env)) {
  162. add_failure(runner, verbosity);
  163. continue;
  164. }
  165. (tc->tests[i])();
  166. /* teardown */
  167. if (tc->teardown != NULL) {
  168. if (setjmp(env)) {
  169. add_failure(runner, verbosity);
  170. continue;
  171. }
  172. tc->teardown();
  173. }
  174. }
  175. tc = tc->next_tcase;
  176. }
  177. if (verbosity) {
  178. int passed = runner->nchecks - runner->nfailures;
  179. double percentage = ((double) passed) / runner->nchecks;
  180. int display = (int) (percentage * 100);
  181. printf("%d%%: Checks: %d, Failed: %d\n",
  182. display, runner->nchecks, runner->nfailures);
  183. }
  184. }
  185. void
  186. _fail_unless(int UNUSED_P(condition), const char *UNUSED_P(file), int UNUSED_P(line), const char *msg)
  187. {
  188. /* Always print the error message so it isn't lost. In this case,
  189. we have a failure, so there's no reason to be quiet about what
  190. it is.
  191. */
  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. {
  201. assert(runner != NULL);
  202. return runner->nfailures;
  203. }
  204. void
  205. srunner_free(SRunner *runner)
  206. {
  207. if (! runner) {
  208. return;
  209. }
  210. suite_free(runner->suite);
  211. free(runner);
  212. }