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.
8 / _ \\ /| '_ \ / _` | __|
9 | __// \| |_) | (_| | |_
10 \___/_/\_\ .__/ \__,_|\__|
13 Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
14 Copyright (c) 2000-2017 Expat development team
15 Licensed under the MIT license:
17 Permission is hereby granted, free of charge, to any person obtaining
18 a copy of this software and associated documentation files (the
19 "Software"), to deal in the Software without restriction, including
20 without limitation the rights to use, copy, modify, merge, publish,
21 distribute, sublicense, and/or sell copies of the Software, and to permit
22 persons to whom the Software is furnished to do so, subject to the
25 The above copyright notice and this permission notice shall be included
26 in all copies or substantial portions of the Software.
28 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
31 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
32 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
33 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
34 USE OR OTHER DEALINGS IN THE SOFTWARE.
43 #include "internal.h" /* for UNUSED_P only */
44 #include "minicheck.h"
47 suite_create(const char *name)
49 Suite *suite = (Suite *) calloc(1, sizeof(Suite));
57 tcase_create(const char *name)
59 TCase *tc = (TCase *) calloc(1, sizeof(TCase));
67 suite_add_tcase(Suite *suite, TCase *tc)
69 assert(suite != NULL);
71 assert(tc->next_tcase == NULL);
73 tc->next_tcase = suite->tests;
78 tcase_add_checked_fixture(TCase *tc,
79 tcase_setup_function setup,
80 tcase_teardown_function teardown)
84 tc->teardown = teardown;
88 tcase_add_test(TCase *tc, tcase_test_function test)
91 if (tc->allocated == tc->ntests) {
92 int nalloc = tc->allocated + 100;
93 size_t new_size = sizeof(tcase_test_function) * nalloc;
94 tcase_test_function *new_tests = realloc(tc->tests, new_size);
95 assert(new_tests != NULL);
96 tc->tests = new_tests;
97 tc->allocated = nalloc;
99 tc->tests[tc->ntests] = test;
104 tcase_free(TCase *tc)
115 suite_free(Suite *suite)
121 while (suite->tests != NULL) {
122 TCase *next = suite->tests->next_tcase;
123 tcase_free(suite->tests);
130 srunner_create(Suite *suite)
132 SRunner *runner = calloc(1, sizeof(SRunner));
133 if (runner != NULL) {
134 runner->suite = suite;
141 static char const *_check_current_function = NULL;
142 static int _check_current_lineno = -1;
143 static char const *_check_current_filename = NULL;
146 _check_set_test_info(char const *function, char const *filename, int lineno)
148 _check_current_function = function;
149 _check_current_lineno = lineno;
150 _check_current_filename = filename;
155 add_failure(SRunner *runner, int verbosity)
158 if (verbosity >= CK_VERBOSE) {
159 printf("%s:%d: %s\n", _check_current_filename,
160 _check_current_lineno, _check_current_function);
165 srunner_run_all(SRunner *runner, int verbosity)
169 assert(runner != NULL);
170 suite = runner->suite;
174 for (i = 0; i < tc->ntests; ++i) {
177 if (tc->setup != NULL) {
180 add_failure(runner, verbosity);
187 add_failure(runner, verbosity);
193 if (tc->teardown != NULL) {
195 add_failure(runner, verbosity);
204 int passed = runner->nchecks - runner->nfailures;
205 double percentage = ((double) passed) / runner->nchecks;
206 int display = (int) (percentage * 100);
207 printf("%d%%: Checks: %d, Failed: %d\n",
208 display, runner->nchecks, runner->nfailures);
213 _fail_unless(int UNUSED_P(condition), const char *UNUSED_P(file), int UNUSED_P(line), const char *msg)
215 /* Always print the error message so it isn't lost. In this case,
216 we have a failure, so there's no reason to be quiet about what
220 const int has_newline = (msg[strlen(msg) - 1] == '\n');
221 fprintf(stderr, "ERROR: %s%s", msg, has_newline ? "" : "\n");
227 srunner_ntests_failed(SRunner *runner)
229 assert(runner != NULL);
230 return runner->nfailures;
234 srunner_free(SRunner *runner)
240 suite_free(runner->suite);