1 /* Miniature re-implementation of the "check" library.
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.
13 #include "internal.h" /* for UNUSED_P only */
14 #include "minicheck.h"
17 suite_create(const char *name)
19 Suite *suite = (Suite *) calloc(1, sizeof(Suite));
27 tcase_create(const char *name)
29 TCase *tc = (TCase *) calloc(1, sizeof(TCase));
37 suite_add_tcase(Suite *suite, TCase *tc)
39 assert(suite != NULL);
41 assert(tc->next_tcase == NULL);
43 tc->next_tcase = suite->tests;
48 tcase_add_checked_fixture(TCase *tc,
49 tcase_setup_function setup,
50 tcase_teardown_function teardown)
54 tc->teardown = teardown;
58 tcase_add_test(TCase *tc, tcase_test_function test)
61 if (tc->allocated == tc->ntests) {
62 int nalloc = tc->allocated + 100;
63 size_t new_size = sizeof(tcase_test_function) * nalloc;
64 tcase_test_function *new_tests = realloc(tc->tests, new_size);
65 assert(new_tests != NULL);
66 if (new_tests != tc->tests) {
68 tc->tests = new_tests;
70 tc->allocated = nalloc;
72 tc->tests[tc->ntests] = test;
77 srunner_create(Suite *suite)
79 SRunner *runner = calloc(1, sizeof(SRunner));
81 runner->suite = suite;
88 static char const *_check_current_function = NULL;
89 static int _check_current_lineno = -1;
90 static char const *_check_current_filename = NULL;
93 _check_set_test_info(char const *function, char const *filename, int lineno)
95 _check_current_function = function;
96 _check_current_lineno = lineno;
97 _check_current_filename = filename;
102 add_failure(SRunner *runner, int verbosity)
105 if (verbosity >= CK_VERBOSE) {
106 printf("%s:%d: %s\n", _check_current_filename,
107 _check_current_lineno, _check_current_function);
112 srunner_run_all(SRunner *runner, int verbosity)
116 assert(runner != NULL);
117 suite = runner->suite;
121 for (i = 0; i < tc->ntests; ++i) {
124 if (tc->setup != NULL) {
127 add_failure(runner, verbosity);
134 add_failure(runner, verbosity);
140 if (tc->teardown != NULL) {
142 add_failure(runner, verbosity);
151 int passed = runner->nchecks - runner->nfailures;
152 double percentage = ((double) passed) / runner->nchecks;
153 int display = (int) (percentage * 100);
154 printf("%d%%: Checks: %d, Failed: %d\n",
155 display, runner->nchecks, runner->nfailures);
160 _fail_unless(int UNUSED_P(condition), const char *UNUSED_P(file), int UNUSED_P(line), const char *msg)
162 /* Always print the error message so it isn't lost. In this case,
163 we have a failure, so there's no reason to be quiet about what
172 srunner_ntests_failed(SRunner *runner)
174 assert(runner != NULL);
175 return runner->nfailures;
179 srunner_free(SRunner *runner)