2004-04-29 23:18:11 -04:00
|
|
|
/* Miniature re-implementation of the "check" library.
|
|
|
|
*
|
|
|
|
* This is intended to support just enough of check to run the Expat
|
|
|
|
* tests. This interface is based entirely on the portion of the
|
|
|
|
* check library being used.
|
|
|
|
*/
|
|
|
|
|
2004-07-15 18:54:19 -04:00
|
|
|
#include <stdio.h>
|
2004-04-29 23:18:11 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2016-05-07 10:45:58 -04:00
|
|
|
#include "internal.h" /* for UNUSED_P only */
|
2004-04-29 23:18:11 -04:00
|
|
|
#include "minicheck.h"
|
|
|
|
|
|
|
|
Suite *
|
2016-03-28 15:18:29 -04:00
|
|
|
suite_create(const char *name)
|
2004-04-29 23:18:11 -04:00
|
|
|
{
|
|
|
|
Suite *suite = (Suite *) calloc(1, sizeof(Suite));
|
|
|
|
if (suite != NULL) {
|
|
|
|
suite->name = name;
|
|
|
|
}
|
|
|
|
return suite;
|
|
|
|
}
|
|
|
|
|
|
|
|
TCase *
|
2016-03-28 15:18:29 -04:00
|
|
|
tcase_create(const char *name)
|
2004-04-29 23:18:11 -04:00
|
|
|
{
|
|
|
|
TCase *tc = (TCase *) calloc(1, sizeof(TCase));
|
|
|
|
if (tc != NULL) {
|
|
|
|
tc->name = name;
|
|
|
|
}
|
|
|
|
return tc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
suite_add_tcase(Suite *suite, TCase *tc)
|
|
|
|
{
|
|
|
|
assert(suite != NULL);
|
|
|
|
assert(tc != NULL);
|
|
|
|
assert(tc->next_tcase == NULL);
|
|
|
|
|
|
|
|
tc->next_tcase = suite->tests;
|
|
|
|
suite->tests = tc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tcase_add_checked_fixture(TCase *tc,
|
|
|
|
tcase_setup_function setup,
|
|
|
|
tcase_teardown_function teardown)
|
|
|
|
{
|
|
|
|
assert(tc != NULL);
|
|
|
|
tc->setup = setup;
|
|
|
|
tc->teardown = teardown;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tcase_add_test(TCase *tc, tcase_test_function test)
|
|
|
|
{
|
|
|
|
assert(tc != NULL);
|
|
|
|
if (tc->allocated == tc->ntests) {
|
|
|
|
int nalloc = tc->allocated + 100;
|
|
|
|
size_t new_size = sizeof(tcase_test_function) * nalloc;
|
|
|
|
tcase_test_function *new_tests = realloc(tc->tests, new_size);
|
|
|
|
assert(new_tests != NULL);
|
2017-05-24 08:23:31 -04:00
|
|
|
tc->tests = new_tests;
|
2004-04-29 23:18:11 -04:00
|
|
|
tc->allocated = nalloc;
|
|
|
|
}
|
|
|
|
tc->tests[tc->ntests] = test;
|
|
|
|
tc->ntests++;
|
|
|
|
}
|
|
|
|
|
2017-07-14 12:53:56 -04:00
|
|
|
static void
|
2017-07-15 13:07:30 -04:00
|
|
|
tcase_free(TCase *tc)
|
2017-07-14 12:53:56 -04:00
|
|
|
{
|
2017-07-15 13:09:30 -04:00
|
|
|
if (! tc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-14 12:53:56 -04:00
|
|
|
free(tc->tests);
|
|
|
|
free(tc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-07-15 13:07:30 -04:00
|
|
|
suite_free(Suite *suite)
|
2017-07-14 12:53:56 -04:00
|
|
|
{
|
2017-07-15 13:09:30 -04:00
|
|
|
if (! suite) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-14 12:53:56 -04:00
|
|
|
while (suite->tests != NULL) {
|
|
|
|
TCase *next = suite->tests->next_tcase;
|
2017-07-15 13:07:30 -04:00
|
|
|
tcase_free(suite->tests);
|
2017-07-14 12:53:56 -04:00
|
|
|
suite->tests = next;
|
|
|
|
}
|
|
|
|
free(suite);
|
|
|
|
}
|
|
|
|
|
2004-04-29 23:18:11 -04:00
|
|
|
SRunner *
|
|
|
|
srunner_create(Suite *suite)
|
|
|
|
{
|
|
|
|
SRunner *runner = calloc(1, sizeof(SRunner));
|
|
|
|
if (runner != NULL) {
|
|
|
|
runner->suite = suite;
|
|
|
|
}
|
|
|
|
return runner;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jmp_buf env;
|
|
|
|
|
2005-01-28 01:32:31 -05:00
|
|
|
static char const *_check_current_function = NULL;
|
|
|
|
static int _check_current_lineno = -1;
|
|
|
|
static char const *_check_current_filename = NULL;
|
|
|
|
|
|
|
|
void
|
|
|
|
_check_set_test_info(char const *function, char const *filename, int lineno)
|
|
|
|
{
|
|
|
|
_check_current_function = function;
|
|
|
|
_check_current_lineno = lineno;
|
|
|
|
_check_current_filename = filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_failure(SRunner *runner, int verbosity)
|
|
|
|
{
|
|
|
|
runner->nfailures++;
|
|
|
|
if (verbosity >= CK_VERBOSE) {
|
2005-01-28 11:16:14 -05:00
|
|
|
printf("%s:%d: %s\n", _check_current_filename,
|
|
|
|
_check_current_lineno, _check_current_function);
|
2005-01-28 01:32:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-29 23:18:11 -04:00
|
|
|
void
|
|
|
|
srunner_run_all(SRunner *runner, int verbosity)
|
|
|
|
{
|
|
|
|
Suite *suite;
|
|
|
|
TCase *tc;
|
|
|
|
assert(runner != NULL);
|
|
|
|
suite = runner->suite;
|
|
|
|
tc = suite->tests;
|
|
|
|
while (tc != NULL) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < tc->ntests; ++i) {
|
|
|
|
runner->nchecks++;
|
|
|
|
|
|
|
|
if (tc->setup != NULL) {
|
|
|
|
/* setup */
|
|
|
|
if (setjmp(env)) {
|
2005-01-28 01:32:31 -05:00
|
|
|
add_failure(runner, verbosity);
|
2004-04-29 23:18:11 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tc->setup();
|
|
|
|
}
|
|
|
|
/* test */
|
|
|
|
if (setjmp(env)) {
|
2005-01-28 01:32:31 -05:00
|
|
|
add_failure(runner, verbosity);
|
2004-04-29 23:18:11 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(tc->tests[i])();
|
|
|
|
|
|
|
|
/* teardown */
|
|
|
|
if (tc->teardown != NULL) {
|
|
|
|
if (setjmp(env)) {
|
2005-01-28 01:32:31 -05:00
|
|
|
add_failure(runner, verbosity);
|
2004-04-29 23:18:11 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tc->teardown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tc = tc->next_tcase;
|
|
|
|
}
|
|
|
|
if (verbosity) {
|
|
|
|
int passed = runner->nchecks - runner->nfailures;
|
|
|
|
double percentage = ((double) passed) / runner->nchecks;
|
|
|
|
int display = (int) (percentage * 100);
|
|
|
|
printf("%d%%: Checks: %d, Failed: %d\n",
|
|
|
|
display, runner->nchecks, runner->nfailures);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-07 10:45:58 -04:00
|
|
|
_fail_unless(int UNUSED_P(condition), const char *UNUSED_P(file), int UNUSED_P(line), const char *msg)
|
2004-04-29 23:18:11 -04:00
|
|
|
{
|
2006-07-01 10:54:06 -04:00
|
|
|
/* Always print the error message so it isn't lost. In this case,
|
|
|
|
we have a failure, so there's no reason to be quiet about what
|
|
|
|
it is.
|
|
|
|
*/
|
|
|
|
if (msg != NULL)
|
|
|
|
printf("%s", msg);
|
2004-04-29 23:18:11 -04:00
|
|
|
longjmp(env, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
srunner_ntests_failed(SRunner *runner)
|
|
|
|
{
|
|
|
|
assert(runner != NULL);
|
|
|
|
return runner->nfailures;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
srunner_free(SRunner *runner)
|
|
|
|
{
|
2017-07-15 13:09:30 -04:00
|
|
|
if (! runner) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-15 13:07:30 -04:00
|
|
|
suite_free(runner->suite);
|
2004-04-29 23:18:11 -04:00
|
|
|
free(runner);
|
|
|
|
}
|