minicheck.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Miniature re-implementation of the "check" library.
  2. *
  3. * This is intended to support just enough of check to run the Expat
  4. * tests. This interface is based entirely on the portion of the
  5. * check library being used.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <setjmp.h>
  10. #include <assert.h>
  11. #include "internal.h" /* for UNUSED_P only */
  12. #include "minicheck.h"
  13. Suite *
  14. suite_create(const char *name)
  15. {
  16. Suite *suite = (Suite *) calloc(1, sizeof(Suite));
  17. if (suite != NULL) {
  18. suite->name = name;
  19. }
  20. return suite;
  21. }
  22. TCase *
  23. tcase_create(const char *name)
  24. {
  25. TCase *tc = (TCase *) calloc(1, sizeof(TCase));
  26. if (tc != NULL) {
  27. tc->name = name;
  28. }
  29. return tc;
  30. }
  31. void
  32. suite_add_tcase(Suite *suite, TCase *tc)
  33. {
  34. assert(suite != NULL);
  35. assert(tc != NULL);
  36. assert(tc->next_tcase == NULL);
  37. tc->next_tcase = suite->tests;
  38. suite->tests = tc;
  39. }
  40. void
  41. tcase_add_checked_fixture(TCase *tc,
  42. tcase_setup_function setup,
  43. tcase_teardown_function teardown)
  44. {
  45. assert(tc != NULL);
  46. tc->setup = setup;
  47. tc->teardown = teardown;
  48. }
  49. void
  50. tcase_add_test(TCase *tc, tcase_test_function test)
  51. {
  52. assert(tc != NULL);
  53. if (tc->allocated == tc->ntests) {
  54. int nalloc = tc->allocated + 100;
  55. size_t new_size = sizeof(tcase_test_function) * nalloc;
  56. tcase_test_function *new_tests = realloc(tc->tests, new_size);
  57. assert(new_tests != NULL);
  58. tc->tests = new_tests;
  59. tc->allocated = nalloc;
  60. }
  61. tc->tests[tc->ntests] = test;
  62. tc->ntests++;
  63. }
  64. SRunner *
  65. srunner_create(Suite *suite)
  66. {
  67. SRunner *runner = calloc(1, sizeof(SRunner));
  68. if (runner != NULL) {
  69. runner->suite = suite;
  70. }
  71. return runner;
  72. }
  73. static jmp_buf env;
  74. static char const *_check_current_function = NULL;
  75. static int _check_current_lineno = -1;
  76. static char const *_check_current_filename = NULL;
  77. void
  78. _check_set_test_info(char const *function, char const *filename, int lineno)
  79. {
  80. _check_current_function = function;
  81. _check_current_lineno = lineno;
  82. _check_current_filename = filename;
  83. }
  84. static void
  85. add_failure(SRunner *runner, int verbosity)
  86. {
  87. runner->nfailures++;
  88. if (verbosity >= CK_VERBOSE) {
  89. printf("%s:%d: %s\n", _check_current_filename,
  90. _check_current_lineno, _check_current_function);
  91. }
  92. }
  93. void
  94. srunner_run_all(SRunner *runner, int verbosity)
  95. {
  96. Suite *suite;
  97. TCase *tc;
  98. assert(runner != NULL);
  99. suite = runner->suite;
  100. tc = suite->tests;
  101. while (tc != NULL) {
  102. int i;
  103. for (i = 0; i < tc->ntests; ++i) {
  104. runner->nchecks++;
  105. if (tc->setup != NULL) {
  106. /* setup */
  107. if (setjmp(env)) {
  108. add_failure(runner, verbosity);
  109. continue;
  110. }
  111. tc->setup();
  112. }
  113. /* test */
  114. if (setjmp(env)) {
  115. add_failure(runner, verbosity);
  116. continue;
  117. }
  118. (tc->tests[i])();
  119. /* teardown */
  120. if (tc->teardown != NULL) {
  121. if (setjmp(env)) {
  122. add_failure(runner, verbosity);
  123. continue;
  124. }
  125. tc->teardown();
  126. }
  127. }
  128. tc = tc->next_tcase;
  129. }
  130. if (verbosity) {
  131. int passed = runner->nchecks - runner->nfailures;
  132. double percentage = ((double) passed) / runner->nchecks;
  133. int display = (int) (percentage * 100);
  134. printf("%d%%: Checks: %d, Failed: %d\n",
  135. display, runner->nchecks, runner->nfailures);
  136. }
  137. }
  138. void
  139. _fail_unless(int UNUSED_P(condition), const char *UNUSED_P(file), int UNUSED_P(line), const char *msg)
  140. {
  141. /* Always print the error message so it isn't lost. In this case,
  142. we have a failure, so there's no reason to be quiet about what
  143. it is.
  144. */
  145. if (msg != NULL)
  146. printf("%s", msg);
  147. longjmp(env, 1);
  148. }
  149. int
  150. srunner_ntests_failed(SRunner *runner)
  151. {
  152. assert(runner != NULL);
  153. return runner->nfailures;
  154. }
  155. void
  156. srunner_free(SRunner *runner)
  157. {
  158. free(runner->suite);
  159. free(runner);
  160. }