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 "minicheck.h"
16 suite_create(char *name)
18 Suite *suite = (Suite *) calloc(1, sizeof(Suite));
26 tcase_create(char *name)
28 TCase *tc = (TCase *) calloc(1, sizeof(TCase));
36 suite_add_tcase(Suite *suite, TCase *tc)
38 assert(suite != NULL);
40 assert(tc->next_tcase == NULL);
42 tc->next_tcase = suite->tests;
47 tcase_add_checked_fixture(TCase *tc,
48 tcase_setup_function setup,
49 tcase_teardown_function teardown)
53 tc->teardown = teardown;
57 tcase_add_test(TCase *tc, tcase_test_function test)
60 if (tc->allocated == tc->ntests) {
61 int nalloc = tc->allocated + 100;
62 size_t new_size = sizeof(tcase_test_function) * nalloc;
63 tcase_test_function *new_tests = realloc(tc->tests, new_size);
64 assert(new_tests != NULL);
65 if (new_tests != tc->tests) {
67 tc->tests = new_tests;
69 tc->allocated = nalloc;
71 tc->tests[tc->ntests] = test;
76 srunner_create(Suite *suite)
78 SRunner *runner = calloc(1, sizeof(SRunner));
80 runner->suite = suite;
87 static char const *_check_current_function = NULL;
88 static int _check_current_lineno = -1;
89 static char const *_check_current_filename = NULL;
92 _check_set_test_info(char const *function, char const *filename, int lineno)
94 _check_current_function = function;
95 _check_current_lineno = lineno;
96 _check_current_filename = filename;
101 add_failure(SRunner *runner, int verbosity)
104 if (verbosity >= CK_VERBOSE) {
105 printf("%s:%d: %s\n", _check_current_filename,
106 _check_current_lineno, _check_current_function);
111 srunner_run_all(SRunner *runner, int verbosity)
115 assert(runner != NULL);
116 suite = runner->suite;
120 for (i = 0; i < tc->ntests; ++i) {
123 if (tc->setup != NULL) {
126 add_failure(runner, verbosity);
133 add_failure(runner, verbosity);
139 if (tc->teardown != NULL) {
141 add_failure(runner, verbosity);
150 int passed = runner->nchecks - runner->nfailures;
151 double percentage = ((double) passed) / runner->nchecks;
152 int display = (int) (percentage * 100);
153 printf("%d%%: Checks: %d, Failed: %d\n",
154 display, runner->nchecks, runner->nfailures);
159 _fail_unless(int condition, const char *file, int line, char *msg)
161 /* Always print the error message so it isn't lost. In this case,
162 we have a failure, so there's no reason to be quiet about what
171 srunner_ntests_failed(SRunner *runner)
173 assert(runner != NULL);
174 return runner->nfailures;
178 srunner_free(SRunner *runner)