libs/gst/check: Run gst-indent on libcheck.

This commit is contained in:
Edward Hervey 2009-10-12 13:49:35 +02:00
parent 7f68d7a6f8
commit bf3ab71a2b
9 changed files with 825 additions and 704 deletions

View file

@ -41,11 +41,11 @@ int check_micro_version = CHECK_MICRO_VERSION;
static int non_pass (int val); static int non_pass (int val);
static Fixture *fixture_create (SFun fun, int ischecked); static Fixture *fixture_create (SFun fun, int ischecked);
static void tcase_add_fixture (TCase *tc, SFun setup, SFun teardown, static void tcase_add_fixture (TCase * tc, SFun setup, SFun teardown,
int ischecked); int ischecked);
static void tr_init (TestResult *tr); static void tr_init (TestResult * tr);
static void suite_free (Suite *s); static void suite_free (Suite * s);
static void tcase_free (TCase *tc); static void tcase_free (TCase * tc);
#if HAVE_CONFIG_H #if HAVE_CONFIG_H
#include <config.h> #include <config.h>
@ -58,11 +58,11 @@ static void tcase_free (TCase *tc);
void *malloc (size_t n); void *malloc (size_t n);
void *realloc (void *p, size_t n); void *realloc (void *p, size_t n);
char *strsignal(int sig); char *strsignal (int sig);
void *rpl_malloc (size_t n); void *rpl_malloc (size_t n);
void *rpl_realloc (void *p, size_t n); void *rpl_realloc (void *p, size_t n);
static const char *rpl_strsignal(int sig); static const char *rpl_strsignal (int sig);
/* Allocate an N-byte block of memory from the heap. If N is zero, /* Allocate an N-byte block of memory from the heap. If N is zero,
allocate a 1-byte block. */ allocate a 1-byte block. */
@ -72,7 +72,7 @@ rpl_malloc (size_t n)
if (n == 0) if (n == 0)
n = 1; n = 1;
return malloc (n); return malloc (n);
} }
/* AC_FUNC_REALLOC in configure defines realloc to rpl_realloc if /* AC_FUNC_REALLOC in configure defines realloc to rpl_realloc if
realloc(0,0) is NULL to make it GNU compatible and always return a realloc(0,0) is NULL to make it GNU compatible and always return a
@ -101,189 +101,203 @@ static const char *
rpl_strsignal (int sig) rpl_strsignal (int sig)
{ {
static char signame[40]; static char signame[40];
sprintf(signame, "SIG #%d", sig); sprintf (signame, "SIG #%d", sig);
return signame; return signame;
} }
Suite *suite_create (const char *name) Suite *
suite_create (const char *name)
{ {
Suite *s; Suite *s;
s = emalloc (sizeof(Suite)); /* freed in suite_free */ s = emalloc (sizeof (Suite)); /* freed in suite_free */
if (name == NULL) if (name == NULL)
s->name = ""; s->name = "";
else else
s->name = name; s->name = name;
s->tclst = check_list_create(); s->tclst = check_list_create ();
return s; return s;
} }
static void suite_free (Suite *s) static void
suite_free (Suite * s)
{ {
List *l; List *l;
if (s == NULL) if (s == NULL)
return; return;
l = s->tclst; l = s->tclst;
for (list_front(l); !list_at_end(l); list_advance (l)) { for (list_front (l); !list_at_end (l); list_advance (l)) {
tcase_free (list_val(l)); tcase_free (list_val (l));
} }
list_free (s->tclst); list_free (s->tclst);
free(s); free (s);
} }
TCase *tcase_create (const char *name) TCase *
tcase_create (const char *name)
{ {
char *env; char *env;
int timeout = DEFAULT_TIMEOUT; int timeout = DEFAULT_TIMEOUT;
TCase *tc = emalloc (sizeof(TCase)); /*freed in tcase_free */ TCase *tc = emalloc (sizeof (TCase)); /*freed in tcase_free */
if (name == NULL) if (name == NULL)
tc->name = ""; tc->name = "";
else else
tc->name = name; tc->name = name;
env = getenv("CK_DEFAULT_TIMEOUT"); env = getenv ("CK_DEFAULT_TIMEOUT");
if (env != NULL) { if (env != NULL) {
int tmp = atoi(env); int tmp = atoi (env);
if (tmp >= 0) { if (tmp >= 0) {
timeout = tmp; timeout = tmp;
} }
} }
tc->timeout = timeout; tc->timeout = timeout;
tc->tflst = check_list_create(); tc->tflst = check_list_create ();
tc->unch_sflst = check_list_create(); tc->unch_sflst = check_list_create ();
tc->ch_sflst = check_list_create(); tc->ch_sflst = check_list_create ();
tc->unch_tflst = check_list_create(); tc->unch_tflst = check_list_create ();
tc->ch_tflst = check_list_create(); tc->ch_tflst = check_list_create ();
return tc; return tc;
} }
static void tcase_free (TCase *tc) static void
tcase_free (TCase * tc)
{ {
list_apply (tc->tflst, free); list_apply (tc->tflst, free);
list_apply (tc->unch_sflst, free); list_apply (tc->unch_sflst, free);
list_apply (tc->ch_sflst, free); list_apply (tc->ch_sflst, free);
list_apply (tc->unch_tflst, free); list_apply (tc->unch_tflst, free);
list_apply (tc->ch_tflst, free); list_apply (tc->ch_tflst, free);
list_free(tc->tflst); list_free (tc->tflst);
list_free(tc->unch_sflst); list_free (tc->unch_sflst);
list_free(tc->ch_sflst); list_free (tc->ch_sflst);
list_free(tc->unch_tflst); list_free (tc->unch_tflst);
list_free(tc->ch_tflst); list_free (tc->ch_tflst);
free(tc); free (tc);
} }
void suite_add_tcase (Suite *s, TCase *tc) void
suite_add_tcase (Suite * s, TCase * tc)
{ {
if (s == NULL || tc == NULL) if (s == NULL || tc == NULL)
return; return;
list_add_end (s->tclst, tc); list_add_end (s->tclst, tc);
} }
void _tcase_add_test (TCase *tc, TFun fn, const char *name, int _signal, int start, int end) void
_tcase_add_test (TCase * tc, TFun fn, const char *name, int _signal, int start,
int end)
{ {
TF * tf; TF *tf;
if (tc == NULL || fn == NULL || name == NULL) if (tc == NULL || fn == NULL || name == NULL)
return; return;
tf = emalloc (sizeof(TF)); /* freed in tcase_free */ tf = emalloc (sizeof (TF)); /* freed in tcase_free */
tf->fn = fn; tf->fn = fn;
tf->loop_start = start; tf->loop_start = start;
tf->loop_end = end; tf->loop_end = end;
tf->signal = _signal; /* 0 means no signal expected */ tf->signal = _signal; /* 0 means no signal expected */
tf->name = name; tf->name = name;
list_add_end (tc->tflst, tf); list_add_end (tc->tflst, tf);
} }
static Fixture *fixture_create (SFun fun, int ischecked) static Fixture *
fixture_create (SFun fun, int ischecked)
{ {
Fixture *f; Fixture *f;
f = emalloc (sizeof(Fixture)); f = emalloc (sizeof (Fixture));
f->fun = fun; f->fun = fun;
f->ischecked = ischecked; f->ischecked = ischecked;
return f; return f;
} }
void tcase_add_unchecked_fixture (TCase *tc, SFun setup, SFun teardown) void
tcase_add_unchecked_fixture (TCase * tc, SFun setup, SFun teardown)
{ {
tcase_add_fixture(tc,setup,teardown,0); tcase_add_fixture (tc, setup, teardown, 0);
} }
void tcase_add_checked_fixture (TCase *tc, SFun setup, SFun teardown) void
tcase_add_checked_fixture (TCase * tc, SFun setup, SFun teardown)
{ {
tcase_add_fixture (tc,setup,teardown,1); tcase_add_fixture (tc, setup, teardown, 1);
} }
static void tcase_add_fixture (TCase *tc, SFun setup, SFun teardown, static void
int ischecked) tcase_add_fixture (TCase * tc, SFun setup, SFun teardown, int ischecked)
{ {
if (setup) { if (setup) {
if (ischecked) if (ischecked)
list_add_end (tc->ch_sflst, fixture_create(setup, ischecked)); list_add_end (tc->ch_sflst, fixture_create (setup, ischecked));
else else
list_add_end (tc->unch_sflst, fixture_create(setup, ischecked)); list_add_end (tc->unch_sflst, fixture_create (setup, ischecked));
} }
/* Add teardowns at front so they are run in reverse order. */ /* Add teardowns at front so they are run in reverse order. */
if (teardown) { if (teardown) {
if (ischecked) if (ischecked)
list_add_front (tc->ch_tflst, fixture_create(teardown, ischecked)); list_add_front (tc->ch_tflst, fixture_create (teardown, ischecked));
else else
list_add_front (tc->unch_tflst, fixture_create(teardown, ischecked)); list_add_front (tc->unch_tflst, fixture_create (teardown, ischecked));
} }
} }
void tcase_set_timeout (TCase *tc, int timeout) void
tcase_set_timeout (TCase * tc, int timeout)
{ {
if (timeout >= 0) if (timeout >= 0)
tc->timeout = timeout; tc->timeout = timeout;
} }
void tcase_fn_start (const char *fname, const char *file, int line) void
tcase_fn_start (const char *fname, const char *file, int line)
{ {
send_ctx_info (CK_CTX_TEST); send_ctx_info (CK_CTX_TEST);
send_loc_info (file, line); send_loc_info (file, line);
} }
void _mark_point (const char *file, int line) void
_mark_point (const char *file, int line)
{ {
send_loc_info (file, line); send_loc_info (file, line);
} }
void _fail_unless (int result, const char *file, void
int line, const char *expr, ...) _fail_unless (int result, const char *file, int line, const char *expr, ...)
{ {
const char *msg; const char *msg;
send_loc_info (file, line); send_loc_info (file, line);
if (!result) { if (!result) {
va_list ap; va_list ap;
char buf[BUFSIZ]; char buf[BUFSIZ];
va_start(ap,expr); va_start (ap, expr);
msg = (const char*)va_arg(ap, char *); msg = (const char *) va_arg (ap, char *);
if (msg == NULL) if (msg == NULL)
msg = expr; msg = expr;
vsnprintf(buf, BUFSIZ, msg, ap); vsnprintf (buf, BUFSIZ, msg, ap);
va_end(ap); va_end (ap);
send_failure_info (buf); send_failure_info (buf);
if (cur_fork_status() == CK_FORK) if (cur_fork_status () == CK_FORK)
exit(1); exit (1);
} }
} }
SRunner *srunner_create (Suite *s) SRunner *
srunner_create (Suite * s)
{ {
SRunner *sr = emalloc (sizeof(SRunner)); /* freed in srunner_free */ SRunner *sr = emalloc (sizeof (SRunner)); /* freed in srunner_free */
sr->slst = check_list_create(); sr->slst = check_list_create ();
if (s != NULL) if (s != NULL)
list_add_end(sr->slst, s); list_add_end (sr->slst, s);
sr->stats = emalloc (sizeof(TestStats)); /* freed in srunner_free */ sr->stats = emalloc (sizeof (TestStats)); /* freed in srunner_free */
sr->stats->n_checked = sr->stats->n_failed = sr->stats->n_errors = 0; sr->stats->n_checked = sr->stats->n_failed = sr->stats->n_errors = 0;
sr->resultlst = check_list_create(); sr->resultlst = check_list_create ();
sr->log_fname = NULL; sr->log_fname = NULL;
sr->xml_fname = NULL; sr->xml_fname = NULL;
sr->loglst = NULL; sr->loglst = NULL;
@ -291,102 +305,112 @@ SRunner *srunner_create (Suite *s)
return sr; return sr;
} }
void srunner_add_suite (SRunner *sr, Suite *s) void
srunner_add_suite (SRunner * sr, Suite * s)
{ {
if (s == NULL) if (s == NULL)
return; return;
list_add_end(sr->slst, s); list_add_end (sr->slst, s);
} }
void srunner_free (SRunner *sr) void
srunner_free (SRunner * sr)
{ {
List *l; List *l;
TestResult *tr; TestResult *tr;
if (sr == NULL) if (sr == NULL)
return; return;
free (sr->stats); free (sr->stats);
l = sr->slst; l = sr->slst;
for (list_front(l); !list_at_end(l); list_advance(l)) { for (list_front (l); !list_at_end (l); list_advance (l)) {
suite_free(list_val(l)); suite_free (list_val (l));
} }
list_free(sr->slst); list_free (sr->slst);
l = sr->resultlst; l = sr->resultlst;
for (list_front(l); !list_at_end(l); list_advance(l)) { for (list_front (l); !list_at_end (l); list_advance (l)) {
tr = list_val(l); tr = list_val (l);
free(tr->file); free (tr->file);
free(tr->msg); free (tr->msg);
free(tr); free (tr);
} }
list_free (sr->resultlst); list_free (sr->resultlst);
free (sr); free (sr);
} }
int srunner_ntests_failed (SRunner *sr) int
srunner_ntests_failed (SRunner * sr)
{ {
return sr->stats->n_failed + sr->stats->n_errors; return sr->stats->n_failed + sr->stats->n_errors;
} }
int srunner_ntests_run (SRunner *sr) int
srunner_ntests_run (SRunner * sr)
{ {
return sr->stats->n_checked; return sr->stats->n_checked;
} }
TestResult **srunner_failures (SRunner *sr) TestResult **
srunner_failures (SRunner * sr)
{ {
int i = 0; int i = 0;
TestResult **trarray; TestResult **trarray;
List *rlst; List *rlst;
trarray = malloc (sizeof(trarray[0]) * srunner_ntests_failed (sr)); trarray = malloc (sizeof (trarray[0]) * srunner_ntests_failed (sr));
rlst = sr->resultlst; rlst = sr->resultlst;
for (list_front(rlst); !list_at_end(rlst); list_advance(rlst)) { for (list_front (rlst); !list_at_end (rlst); list_advance (rlst)) {
TestResult *tr = list_val(rlst); TestResult *tr = list_val (rlst);
if (non_pass(tr->rtype)) if (non_pass (tr->rtype))
trarray[i++] = tr; trarray[i++] = tr;
} }
return trarray; return trarray;
} }
TestResult **srunner_results (SRunner *sr) TestResult **
srunner_results (SRunner * sr)
{ {
int i = 0; int i = 0;
TestResult **trarray; TestResult **trarray;
List *rlst; List *rlst;
trarray = malloc (sizeof(trarray[0]) * srunner_ntests_run (sr)); trarray = malloc (sizeof (trarray[0]) * srunner_ntests_run (sr));
rlst = sr->resultlst; rlst = sr->resultlst;
for (list_front(rlst); !list_at_end(rlst); list_advance(rlst)) { for (list_front (rlst); !list_at_end (rlst); list_advance (rlst)) {
trarray[i++] = list_val(rlst); trarray[i++] = list_val (rlst);
} }
return trarray; return trarray;
} }
static int non_pass (int val) static int
non_pass (int val)
{ {
return val != CK_PASS; return val != CK_PASS;
} }
TestResult *tr_create(void) TestResult *
tr_create (void)
{ {
TestResult *tr; TestResult *tr;
tr = emalloc (sizeof(TestResult)); tr = emalloc (sizeof (TestResult));
tr_init (tr); tr_init (tr);
return tr; return tr;
} }
void tr_reset(TestResult *tr) void
tr_reset (TestResult * tr)
{ {
tr_init(tr); tr_init (tr);
} }
static void tr_init (TestResult *tr) static void
tr_init (TestResult * tr)
{ {
tr->ctx = CK_CTX_INVALID; tr->ctx = CK_CTX_INVALID;
tr->line = -1; tr->line = -1;
@ -398,39 +422,46 @@ static void tr_init (TestResult *tr)
} }
const char *tr_msg (TestResult *tr) const char *
tr_msg (TestResult * tr)
{ {
return tr->msg; return tr->msg;
} }
int tr_lno (TestResult *tr) int
tr_lno (TestResult * tr)
{ {
return tr->line; return tr->line;
} }
const char *tr_lfile (TestResult *tr) const char *
tr_lfile (TestResult * tr)
{ {
return tr->file; return tr->file;
} }
int tr_rtype (TestResult *tr) int
tr_rtype (TestResult * tr)
{ {
return tr->rtype; return tr->rtype;
} }
enum ck_result_ctx tr_ctx (TestResult *tr) enum ck_result_ctx
tr_ctx (TestResult * tr)
{ {
return tr->ctx; return tr->ctx;
} }
const char *tr_tcname (TestResult *tr) const char *
tr_tcname (TestResult * tr)
{ {
return tr->tcname; return tr->tcname;
} }
static int _fstat = CK_FORK; static int _fstat = CK_FORK;
void set_fork_status (enum fork_status fstat) void
set_fork_status (enum fork_status fstat)
{ {
if (fstat == CK_FORK || fstat == CK_NOFORK || fstat == CK_FORK_GETENV) if (fstat == CK_FORK || fstat == CK_NOFORK || fstat == CK_FORK_GETENV)
_fstat = fstat; _fstat = fstat;
@ -438,7 +469,8 @@ void set_fork_status (enum fork_status fstat)
eprintf ("Bad status in set_fork_status", __FILE__, __LINE__); eprintf ("Bad status in set_fork_status", __FILE__, __LINE__);
} }
enum fork_status cur_fork_status (void) enum fork_status
cur_fork_status (void)
{ {
return _fstat; return _fstat;
} }

View file

@ -30,38 +30,41 @@
/* FIXME: including a colon at the end is a bad way to indicate an error */ /* FIXME: including a colon at the end is a bad way to indicate an error */
void eprintf (const char *fmt, const char *file, int line, ...) void
eprintf (const char *fmt, const char *file, int line, ...)
{ {
va_list args; va_list args;
fflush(stderr); fflush (stderr);
fprintf(stderr,"%s:%d: ",file,line); fprintf (stderr, "%s:%d: ", file, line);
va_start(args, line); va_start (args, line);
vfprintf(stderr, fmt, args); vfprintf (stderr, fmt, args);
va_end(args); va_end (args);
/*include system error information if format ends in colon */ /*include system error information if format ends in colon */
if (fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') if (fmt[0] != '\0' && fmt[strlen (fmt) - 1] == ':')
fprintf(stderr, " %s", strerror(errno)); fprintf (stderr, " %s", strerror (errno));
fprintf(stderr, "\n"); fprintf (stderr, "\n");
exit(2); exit (2);
} }
void *emalloc (size_t n) void *
emalloc (size_t n)
{ {
void *p; void *p;
p = malloc(n); p = malloc (n);
if (p == NULL) if (p == NULL)
eprintf("malloc of %u bytes failed:", __FILE__, __LINE__ - 2, n); eprintf ("malloc of %u bytes failed:", __FILE__, __LINE__ - 2, n);
return p; return p;
} }
void *erealloc (void * ptr, size_t n) void *
erealloc (void *ptr, size_t n)
{ {
void *p; void *p;
p = realloc (ptr, n); p = realloc (ptr, n);
if (p == NULL) if (p == NULL)
eprintf("realloc of %u bytes failed:", __FILE__, __LINE__ - 2, n); eprintf ("realloc of %u bytes failed:", __FILE__, __LINE__ - 2, n);
return p; return p;
} }

View file

@ -27,62 +27,69 @@
#include "check_error.h" #include "check_error.h"
enum { enum
{
LINIT = 1, LINIT = 1,
LGROW = 2 LGROW = 2
}; };
struct List { struct List
{
int n_elts; int n_elts;
int max_elts; int max_elts;
int current; /* pointer to the current node */ int current; /* pointer to the current node */
int last; /* pointer to the node before END */ int last; /* pointer to the node before END */
const void **data; const void **data;
}; };
static void maybe_grow (List *lp) static void
maybe_grow (List * lp)
{ {
if (lp->n_elts >= lp->max_elts) { if (lp->n_elts >= lp->max_elts) {
lp->max_elts *= LGROW; lp->max_elts *= LGROW;
lp->data = erealloc (lp->data, lp->max_elts * sizeof(lp->data[0])); lp->data = erealloc (lp->data, lp->max_elts * sizeof (lp->data[0]));
} }
} }
List *check_list_create (void) List *
check_list_create (void)
{ {
List *lp; List *lp;
lp = emalloc (sizeof(List)); lp = emalloc (sizeof (List));
lp->n_elts = 0; lp->n_elts = 0;
lp->max_elts = LINIT; lp->max_elts = LINIT;
lp->data = emalloc(sizeof(lp->data[0]) * LINIT); lp->data = emalloc (sizeof (lp->data[0]) * LINIT);
lp->current = lp->last = -1; lp->current = lp->last = -1;
return lp; return lp;
} }
void list_add_front (List *lp, const void *val) void
list_add_front (List * lp, const void *val)
{ {
if (lp == NULL) if (lp == NULL)
return; return;
maybe_grow(lp); maybe_grow (lp);
memmove(lp->data + 1, lp->data, lp->n_elts * sizeof lp->data[0]); memmove (lp->data + 1, lp->data, lp->n_elts * sizeof lp->data[0]);
lp->last++; lp->last++;
lp->n_elts++; lp->n_elts++;
lp->current = 0; lp->current = 0;
lp->data[lp->current] = val; lp->data[lp->current] = val;
} }
void list_add_end (List *lp, const void *val) void
list_add_end (List * lp, const void *val)
{ {
if (lp == NULL) if (lp == NULL)
return; return;
maybe_grow(lp); maybe_grow (lp);
lp->last++; lp->last++;
lp->n_elts++; lp->n_elts++;
lp->current = lp->last; lp->current = lp->last;
lp->data[lp->current] = val; lp->data[lp->current] = val;
} }
int list_at_end (List *lp) int
list_at_end (List * lp)
{ {
if (lp->current == -1) if (lp->current == -1)
return 1; return 1;
@ -90,7 +97,8 @@ int list_at_end (List *lp)
return (lp->current > lp->last); return (lp->current > lp->last);
} }
void list_front (List *lp) void
list_front (List * lp)
{ {
if (lp->current == -1) if (lp->current == -1)
return; return;
@ -98,44 +106,45 @@ void list_front (List *lp)
} }
void list_free (List *lp) void
list_free (List * lp)
{ {
if (lp == NULL) if (lp == NULL)
return; return;
free(lp->data); free (lp->data);
free (lp); free (lp);
} }
void *list_val (List *lp) void *
list_val (List * lp)
{ {
if (lp == NULL) if (lp == NULL)
return NULL; return NULL;
if (lp->current == -1 || lp->current > lp->last) if (lp->current == -1 || lp->current > lp->last)
return NULL; return NULL;
return (void*) lp->data[lp->current]; return (void *) lp->data[lp->current];
} }
void list_advance (List *lp) void
list_advance (List * lp)
{ {
if (lp == NULL) if (lp == NULL)
return; return;
if (list_at_end(lp)) if (list_at_end (lp))
return; return;
lp->current++; lp->current++;
} }
void list_apply (List *lp, void (*fp) (void *)) void
list_apply (List * lp, void (*fp) (void *))
{ {
if (lp == NULL || fp == NULL) if (lp == NULL || fp == NULL)
return; return;
for (list_front(lp); !list_at_end(lp); list_advance(lp)) for (list_front (lp); !list_at_end (lp); list_advance (lp))
fp (list_val(lp)); fp (list_val (lp));
} }

View file

@ -33,50 +33,57 @@
#include "check_print.h" #include "check_print.h"
static void srunner_send_evt (SRunner *sr, void *obj, enum cl_event evt); static void srunner_send_evt (SRunner * sr, void *obj, enum cl_event evt);
void srunner_set_log (SRunner *sr, const char *fname) void
srunner_set_log (SRunner * sr, const char *fname)
{ {
if (sr->log_fname) if (sr->log_fname)
return; return;
sr->log_fname = fname; sr->log_fname = fname;
} }
int srunner_has_log (SRunner *sr) int
srunner_has_log (SRunner * sr)
{ {
return sr->log_fname != NULL; return sr->log_fname != NULL;
} }
const char *srunner_log_fname (SRunner *sr) const char *
srunner_log_fname (SRunner * sr)
{ {
return sr->log_fname; return sr->log_fname;
} }
void srunner_set_xml (SRunner *sr, const char *fname) void
srunner_set_xml (SRunner * sr, const char *fname)
{ {
if (sr->xml_fname) if (sr->xml_fname)
return; return;
sr->xml_fname = fname; sr->xml_fname = fname;
} }
int srunner_has_xml (SRunner *sr) int
srunner_has_xml (SRunner * sr)
{ {
return sr->xml_fname != NULL; return sr->xml_fname != NULL;
} }
const char *srunner_xml_fname (SRunner *sr) const char *
srunner_xml_fname (SRunner * sr)
{ {
return sr->xml_fname; return sr->xml_fname;
} }
void srunner_register_lfun (SRunner *sr, FILE *lfile, int close, void
LFun lfun, enum print_output printmode) srunner_register_lfun (SRunner * sr, FILE * lfile, int close,
LFun lfun, enum print_output printmode)
{ {
Log *l = emalloc (sizeof(Log)); Log *l = emalloc (sizeof (Log));
if (printmode == CK_ENV) { if (printmode == CK_ENV) {
printmode = get_env_printmode(); printmode = get_env_printmode ();
} }
l->lfile = lfile; l->lfile = lfile;
@ -87,208 +94,220 @@ void srunner_register_lfun (SRunner *sr, FILE *lfile, int close,
return; return;
} }
void log_srunner_start (SRunner *sr) void
log_srunner_start (SRunner * sr)
{ {
srunner_send_evt (sr, NULL, CLSTART_SR); srunner_send_evt (sr, NULL, CLSTART_SR);
} }
void log_srunner_end (SRunner *sr) void
log_srunner_end (SRunner * sr)
{ {
srunner_send_evt (sr, NULL, CLEND_SR); srunner_send_evt (sr, NULL, CLEND_SR);
} }
void log_suite_start (SRunner *sr, Suite *s) void
log_suite_start (SRunner * sr, Suite * s)
{ {
srunner_send_evt (sr, s, CLSTART_S); srunner_send_evt (sr, s, CLSTART_S);
} }
void log_suite_end (SRunner *sr, Suite *s) void
log_suite_end (SRunner * sr, Suite * s)
{ {
srunner_send_evt (sr, s, CLEND_S); srunner_send_evt (sr, s, CLEND_S);
} }
void log_test_end (SRunner *sr, TestResult *tr) void
log_test_end (SRunner * sr, TestResult * tr)
{ {
srunner_send_evt (sr, tr, CLEND_T); srunner_send_evt (sr, tr, CLEND_T);
} }
static void srunner_send_evt (SRunner *sr, void *obj, enum cl_event evt) static void
srunner_send_evt (SRunner * sr, void *obj, enum cl_event evt)
{ {
List *l; List *l;
Log *lg; Log *lg;
l = sr->loglst; l = sr->loglst;
for (list_front(l); !list_at_end(l); list_advance(l)) { for (list_front (l); !list_at_end (l); list_advance (l)) {
lg = list_val(l); lg = list_val (l);
fflush(lg->lfile); fflush (lg->lfile);
lg->lfun (sr, lg->lfile, lg->mode, obj, evt); lg->lfun (sr, lg->lfile, lg->mode, obj, evt);
fflush(lg->lfile); fflush (lg->lfile);
} }
} }
void stdout_lfun (SRunner *sr, FILE *file, enum print_output printmode, void
void *obj, enum cl_event evt) stdout_lfun (SRunner * sr, FILE * file, enum print_output printmode,
void *obj, enum cl_event evt)
{ {
TestResult *tr; TestResult *tr;
Suite *s; Suite *s;
if (printmode == CK_ENV) { if (printmode == CK_ENV) {
printmode = get_env_printmode(); printmode = get_env_printmode ();
} }
switch (evt) { switch (evt) {
case CLINITLOG_SR: case CLINITLOG_SR:
break; break;
case CLENDLOG_SR: case CLENDLOG_SR:
break; break;
case CLSTART_SR: case CLSTART_SR:
if (printmode > CK_SILENT) { if (printmode > CK_SILENT) {
fprintf(file, "Running suite(s):"); fprintf (file, "Running suite(s):");
} }
break; break;
case CLSTART_S: case CLSTART_S:
s = obj; s = obj;
if (printmode > CK_SILENT) { if (printmode > CK_SILENT) {
fprintf(file, " %s\n", s->name); fprintf (file, " %s\n", s->name);
} }
break; break;
case CLEND_SR: case CLEND_SR:
if (printmode > CK_SILENT) { if (printmode > CK_SILENT) {
/* we don't want a newline before printing here, newlines should /* we don't want a newline before printing here, newlines should
come after printing a string, not before. it's better to add come after printing a string, not before. it's better to add
the newline above in CLSTART_S. the newline above in CLSTART_S.
*/ */
srunner_fprint (file, sr, printmode); srunner_fprint (file, sr, printmode);
} }
break; break;
case CLEND_S: case CLEND_S:
s = obj; s = obj;
break; break;
case CLEND_T: case CLEND_T:
tr = obj; tr = obj;
break; break;
default: default:
eprintf("Bad event type received in stdout_lfun", __FILE__, __LINE__); eprintf ("Bad event type received in stdout_lfun", __FILE__, __LINE__);
} }
} }
void lfile_lfun (SRunner *sr, FILE *file, enum print_output printmode, void
void *obj, enum cl_event evt) lfile_lfun (SRunner * sr, FILE * file, enum print_output printmode,
void *obj, enum cl_event evt)
{ {
TestResult *tr; TestResult *tr;
Suite *s; Suite *s;
switch (evt) { switch (evt) {
case CLINITLOG_SR: case CLINITLOG_SR:
break; break;
case CLENDLOG_SR: case CLENDLOG_SR:
break; break;
case CLSTART_SR: case CLSTART_SR:
break; break;
case CLSTART_S: case CLSTART_S:
s = obj; s = obj;
fprintf(file, "Running suite %s\n", s->name); fprintf (file, "Running suite %s\n", s->name);
break; break;
case CLEND_SR: case CLEND_SR:
fprintf (file, "Results for all suites run:\n"); fprintf (file, "Results for all suites run:\n");
srunner_fprint (file, sr, CK_MINIMAL); srunner_fprint (file, sr, CK_MINIMAL);
break; break;
case CLEND_S: case CLEND_S:
s = obj; s = obj;
break; break;
case CLEND_T: case CLEND_T:
tr = obj; tr = obj;
tr_fprint(file, tr, CK_VERBOSE); tr_fprint (file, tr, CK_VERBOSE);
break; break;
default: default:
eprintf("Bad event type received in stdout_lfun", __FILE__, __LINE__); eprintf ("Bad event type received in stdout_lfun", __FILE__, __LINE__);
} }
} }
void xml_lfun (SRunner *sr, FILE *file, enum print_output printmode, void
void *obj, enum cl_event evt) xml_lfun (SRunner * sr, FILE * file, enum print_output printmode,
void *obj, enum cl_event evt)
{ {
TestResult *tr; TestResult *tr;
Suite *s; Suite *s;
static struct timeval inittv, endtv; static struct timeval inittv, endtv;
static char t[sizeof "yyyy-mm-dd hh:mm:ss"] = {0}; static char t[sizeof "yyyy-mm-dd hh:mm:ss"] = { 0 };
if (t[0] == 0) if (t[0] == 0) {
{
struct tm now; struct tm now;
gettimeofday(&inittv, NULL); gettimeofday (&inittv, NULL);
localtime_r(&(inittv.tv_sec), &now); localtime_r (&(inittv.tv_sec), &now);
strftime(t, sizeof("yyyy-mm-dd hh:mm:ss"), "%Y-%m-%d %H:%M:%S", &now); strftime (t, sizeof ("yyyy-mm-dd hh:mm:ss"), "%Y-%m-%d %H:%M:%S", &now);
} }
switch (evt) { switch (evt) {
case CLINITLOG_SR: case CLINITLOG_SR:
fprintf(file, "<?xml version=\"1.0\"?>\n"); fprintf (file, "<?xml version=\"1.0\"?>\n");
fprintf(file, "<testsuites xmlns=\"http://check.sourceforge.net/ns\">\n"); fprintf (file,
fprintf(file, " <datetime>%s</datetime>\n", t); "<testsuites xmlns=\"http://check.sourceforge.net/ns\">\n");
break; fprintf (file, " <datetime>%s</datetime>\n", t);
case CLENDLOG_SR: break;
gettimeofday(&endtv, NULL); case CLENDLOG_SR:
fprintf(file, " <duration>%f</duration>\n", gettimeofday (&endtv, NULL);
(endtv.tv_sec + (float)(endtv.tv_usec)/1000000) - \ fprintf (file, " <duration>%f</duration>\n",
(inittv.tv_sec + (float)(inittv.tv_usec/1000000))); (endtv.tv_sec + (float) (endtv.tv_usec) / 1000000) -
fprintf(file, "</testsuites>\n"); (inittv.tv_sec + (float) (inittv.tv_usec / 1000000)));
break; fprintf (file, "</testsuites>\n");
case CLSTART_SR: break;
break; case CLSTART_SR:
case CLSTART_S: break;
s = obj; case CLSTART_S:
fprintf(file, " <suite>\n"); s = obj;
fprintf(file, " <title>%s</title>\n", s->name); fprintf (file, " <suite>\n");
break; fprintf (file, " <title>%s</title>\n", s->name);
case CLEND_SR: break;
break; case CLEND_SR:
case CLEND_S: break;
fprintf(file, " </suite>\n"); case CLEND_S:
s = obj; fprintf (file, " </suite>\n");
break; s = obj;
case CLEND_T: break;
tr = obj; case CLEND_T:
tr_xmlprint(file, tr, CK_VERBOSE); tr = obj;
break; tr_xmlprint (file, tr, CK_VERBOSE);
default: break;
eprintf("Bad event type received in xml_lfun", __FILE__, __LINE__); default:
eprintf ("Bad event type received in xml_lfun", __FILE__, __LINE__);
} }
} }
FILE *srunner_open_lfile (SRunner *sr) FILE *
srunner_open_lfile (SRunner * sr)
{ {
FILE *f = NULL; FILE *f = NULL;
if (srunner_has_log (sr)) { if (srunner_has_log (sr)) {
f = fopen(sr->log_fname, "w"); f = fopen (sr->log_fname, "w");
if (f == NULL) if (f == NULL)
eprintf ("Error in call to fopen while opening log file %s:", __FILE__, __LINE__ - 2, eprintf ("Error in call to fopen while opening log file %s:", __FILE__,
sr->log_fname); __LINE__ - 2, sr->log_fname);
} }
return f; return f;
} }
FILE *srunner_open_xmlfile (SRunner *sr) FILE *
srunner_open_xmlfile (SRunner * sr)
{ {
FILE *f = NULL; FILE *f = NULL;
if (srunner_has_xml (sr)) { if (srunner_has_xml (sr)) {
f = fopen(sr->xml_fname, "w"); f = fopen (sr->xml_fname, "w");
if (f == NULL) if (f == NULL)
eprintf ("Error in call to fopen while opening xml file %s:", __FILE__, __LINE__ - 2, eprintf ("Error in call to fopen while opening xml file %s:", __FILE__,
sr->xml_fname); __LINE__ - 2, sr->xml_fname);
} }
return f; return f;
} }
void srunner_init_logging (SRunner *sr, enum print_output print_mode) void
srunner_init_logging (SRunner * sr, enum print_output print_mode)
{ {
FILE *f; FILE *f;
sr->loglst = check_list_create(); sr->loglst = check_list_create ();
srunner_register_lfun (sr, stdout, 0, stdout_lfun, print_mode); srunner_register_lfun (sr, stdout, 0, stdout_lfun, print_mode);
f = srunner_open_lfile (sr); f = srunner_open_lfile (sr);
if (f) { if (f) {
@ -301,7 +320,8 @@ void srunner_init_logging (SRunner *sr, enum print_output print_mode)
srunner_send_evt (sr, NULL, CLINITLOG_SR); srunner_send_evt (sr, NULL, CLINITLOG_SR);
} }
void srunner_end_logging (SRunner *sr) void
srunner_end_logging (SRunner * sr)
{ {
List *l; List *l;
int rval; int rval;
@ -309,15 +329,16 @@ void srunner_end_logging (SRunner *sr)
srunner_send_evt (sr, NULL, CLENDLOG_SR); srunner_send_evt (sr, NULL, CLENDLOG_SR);
l = sr->loglst; l = sr->loglst;
for (list_front(l); !list_at_end(l); list_advance(l)) { for (list_front (l); !list_at_end (l); list_advance (l)) {
Log *lg = list_val(l); Log *lg = list_val (l);
if (lg->close) { if (lg->close) {
rval = fclose (lg->lfile); rval = fclose (lg->lfile);
if (rval != 0) if (rval != 0)
eprintf ("Error in call to fclose while closing log file:", __FILE__, __LINE__ - 2); eprintf ("Error in call to fclose while closing log file:", __FILE__,
__LINE__ - 2);
} }
free (lg); free (lg);
} }
list_free(l); list_free (l);
sr->loglst = NULL; sr->loglst = NULL;
} }

View file

@ -57,73 +57,78 @@
static FILE *send_file1; static FILE *send_file1;
static FILE *send_file2; static FILE *send_file2;
static FILE * get_pipe(void); static FILE *get_pipe (void);
static void setup_pipe (void); static void setup_pipe (void);
static void teardown_pipe (void); static void teardown_pipe (void);
static TestResult *construct_test_result (RcvMsg *rmsg, int waserror); static TestResult *construct_test_result (RcvMsg * rmsg, int waserror);
static void tr_set_loc_by_ctx (TestResult *tr, enum ck_result_ctx ctx, static void tr_set_loc_by_ctx (TestResult * tr, enum ck_result_ctx ctx,
RcvMsg *rmsg); RcvMsg * rmsg);
static FILE * get_pipe(void) static FILE *
get_pipe (void)
{ {
if (send_file2 != 0) { if (send_file2 != 0) {
return send_file2; return send_file2;
} }
if (send_file1 != 0) { if (send_file1 != 0) {
return send_file1; return send_file1;
} }
printf ("send_file1=%p,send_file2=%p", send_file1, send_file2); printf ("send_file1=%p,send_file2=%p", send_file1, send_file2);
eprintf("No messaging setup", __FILE__, __LINE__); eprintf ("No messaging setup", __FILE__, __LINE__);
return NULL; return NULL;
} }
void send_failure_info(const char *msg) void
send_failure_info (const char *msg)
{ {
FailMsg fmsg; FailMsg fmsg;
fmsg.msg = (char *) msg; fmsg.msg = (char *) msg;
ppack(fileno(get_pipe()), CK_MSG_FAIL, (CheckMsg *) &fmsg); ppack (fileno (get_pipe ()), CK_MSG_FAIL, (CheckMsg *) & fmsg);
} }
void send_loc_info(const char * file, int line) void
send_loc_info (const char *file, int line)
{ {
LocMsg lmsg; LocMsg lmsg;
lmsg.file = (char *) file; lmsg.file = (char *) file;
lmsg.line = line; lmsg.line = line;
ppack(fileno(get_pipe()), CK_MSG_LOC, (CheckMsg *) &lmsg); ppack (fileno (get_pipe ()), CK_MSG_LOC, (CheckMsg *) & lmsg);
} }
void send_ctx_info(enum ck_result_ctx ctx) void
send_ctx_info (enum ck_result_ctx ctx)
{ {
CtxMsg cmsg; CtxMsg cmsg;
cmsg.ctx = ctx; cmsg.ctx = ctx;
ppack(fileno(get_pipe()), CK_MSG_CTX, (CheckMsg *) &cmsg); ppack (fileno (get_pipe ()), CK_MSG_CTX, (CheckMsg *) & cmsg);
} }
TestResult *receive_test_result (int waserror) TestResult *
receive_test_result (int waserror)
{ {
FILE *fp; FILE *fp;
RcvMsg *rmsg; RcvMsg *rmsg;
TestResult *result; TestResult *result;
fp = get_pipe(); fp = get_pipe ();
if (fp == NULL) if (fp == NULL)
eprintf ("Error in call to get_pipe",__FILE__, __LINE__ - 2); eprintf ("Error in call to get_pipe", __FILE__, __LINE__ - 2);
rewind(fp); rewind (fp);
rmsg = punpack (fileno(fp)); rmsg = punpack (fileno (fp));
teardown_pipe(); teardown_pipe ();
setup_pipe(); setup_pipe ();
result = construct_test_result (rmsg, waserror); result = construct_test_result (rmsg, waserror);
rcvmsg_free(rmsg); rcvmsg_free (rmsg);
return result; return result;
} }
static void tr_set_loc_by_ctx (TestResult *tr, enum ck_result_ctx ctx, static void
RcvMsg *rmsg) tr_set_loc_by_ctx (TestResult * tr, enum ck_result_ctx ctx, RcvMsg * rmsg)
{ {
if (ctx == CK_CTX_TEST) { if (ctx == CK_CTX_TEST) {
tr->file = rmsg->test_file; tr->file = rmsg->test_file;
@ -138,14 +143,15 @@ static void tr_set_loc_by_ctx (TestResult *tr, enum ck_result_ctx ctx,
} }
} }
static TestResult *construct_test_result (RcvMsg *rmsg, int waserror) static TestResult *
construct_test_result (RcvMsg * rmsg, int waserror)
{ {
TestResult *tr; TestResult *tr;
if (rmsg == NULL) if (rmsg == NULL)
return NULL; return NULL;
tr = tr_create(); tr = tr_create ();
if (rmsg->msg != NULL || waserror) { if (rmsg->msg != NULL || waserror) {
tr->ctx = (cur_fork_status () == CK_FORK) ? rmsg->lastctx : rmsg->failctx; tr->ctx = (cur_fork_status () == CK_FORK) ? rmsg->lastctx : rmsg->failctx;
@ -155,7 +161,7 @@ static TestResult *construct_test_result (RcvMsg *rmsg, int waserror)
} else if (rmsg->lastctx == CK_CTX_SETUP) { } else if (rmsg->lastctx == CK_CTX_SETUP) {
tr->ctx = CK_CTX_SETUP; tr->ctx = CK_CTX_SETUP;
tr->msg = NULL; tr->msg = NULL;
tr_set_loc_by_ctx (tr, CK_CTX_SETUP, rmsg); tr_set_loc_by_ctx (tr, CK_CTX_SETUP, rmsg);
} else { } else {
tr->ctx = CK_CTX_TEST; tr->ctx = CK_CTX_TEST;
tr->msg = NULL; tr->msg = NULL;
@ -165,37 +171,40 @@ static TestResult *construct_test_result (RcvMsg *rmsg, int waserror)
return tr; return tr;
} }
void setup_messaging(void) void
setup_messaging (void)
{ {
setup_pipe(); setup_pipe ();
} }
void teardown_messaging(void) void
teardown_messaging (void)
{ {
teardown_pipe(); teardown_pipe ();
} }
static void setup_pipe(void) static void
setup_pipe (void)
{ {
if (send_file1 != 0) { if (send_file1 != 0) {
if (send_file2 != 0) if (send_file2 != 0)
eprintf("Only one nesting of suite runs supported", __FILE__, __LINE__); eprintf ("Only one nesting of suite runs supported", __FILE__, __LINE__);
send_file2 = tmpfile(); send_file2 = tmpfile ();
} else { } else {
send_file1 = tmpfile(); send_file1 = tmpfile ();
} }
} }
static void teardown_pipe(void) static void
teardown_pipe (void)
{ {
if (send_file2 != 0) { if (send_file2 != 0) {
fclose(send_file2); fclose (send_file2);
send_file2 = 0; send_file2 = 0;
} else if (send_file1 != 0) { } else if (send_file1 != 0) {
fclose(send_file1); fclose (send_file1);
send_file1 = 0; send_file1 = 0;
} else { } else {
eprintf("No messaging setup", __FILE__, __LINE__); eprintf ("No messaging setup", __FILE__, __LINE__);
} }
} }

View file

@ -40,45 +40,46 @@
typedef uint32_t ck_uint32; typedef uint32_t ck_uint32;
static void pack_int (char **buf, int val); static void pack_int (char **buf, int val);
static int upack_int (char **buf); static int upack_int (char **buf);
static void pack_str (char **buf, const char *str); static void pack_str (char **buf, const char *str);
static char *upack_str (char **buf); static char *upack_str (char **buf);
static int pack_ctx (char **buf, CtxMsg *cmsg); static int pack_ctx (char **buf, CtxMsg * cmsg);
static int pack_loc (char **buf, LocMsg *lmsg); static int pack_loc (char **buf, LocMsg * lmsg);
static int pack_fail (char **buf, FailMsg *fmsg); static int pack_fail (char **buf, FailMsg * fmsg);
static void upack_ctx (char **buf, CtxMsg *cmsg); static void upack_ctx (char **buf, CtxMsg * cmsg);
static void upack_loc (char **buf, LocMsg *lmsg); static void upack_loc (char **buf, LocMsg * lmsg);
static void upack_fail (char **buf, FailMsg *fmsg); static void upack_fail (char **buf, FailMsg * fmsg);
static void check_type (int type, const char *file, int line); static void check_type (int type, const char *file, int line);
static enum ck_msg_type upack_type (char **buf); static enum ck_msg_type upack_type (char **buf);
static void pack_type (char **buf, enum ck_msg_type type); static void pack_type (char **buf, enum ck_msg_type type);
static int read_buf (int fdes, char **buf); static int read_buf (int fdes, char **buf);
static int get_result (char *buf, RcvMsg *rmsg); static int get_result (char *buf, RcvMsg * rmsg);
static void rcvmsg_update_ctx (RcvMsg *rmsg, enum ck_result_ctx ctx); static void rcvmsg_update_ctx (RcvMsg * rmsg, enum ck_result_ctx ctx);
static void rcvmsg_update_loc (RcvMsg *rmsg, const char *file, int line); static void rcvmsg_update_loc (RcvMsg * rmsg, const char *file, int line);
static RcvMsg *rcvmsg_create (void); static RcvMsg *rcvmsg_create (void);
void rcvmsg_free (RcvMsg *rmsg); void rcvmsg_free (RcvMsg * rmsg);
typedef int (*pfun) (char **, CheckMsg *); typedef int (*pfun) (char **, CheckMsg *);
typedef void (*upfun) (char **, CheckMsg *); typedef void (*upfun) (char **, CheckMsg *);
static pfun pftab [] = { static pfun pftab[] = {
(pfun) pack_ctx, (pfun) pack_ctx,
(pfun) pack_fail, (pfun) pack_fail,
(pfun) pack_loc (pfun) pack_loc
}; };
static upfun upftab [] = { static upfun upftab[] = {
(upfun) upack_ctx, (upfun) upack_ctx,
(upfun) upack_fail, (upfun) upack_fail,
(upfun) upack_loc (upfun) upack_loc
}; };
int pack (enum ck_msg_type type, char **buf, CheckMsg *msg) int
pack (enum ck_msg_type type, char **buf, CheckMsg * msg)
{ {
if (buf == NULL) if (buf == NULL)
return -1; return -1;
@ -90,7 +91,8 @@ int pack (enum ck_msg_type type, char **buf, CheckMsg *msg)
return pftab[type] (buf, msg); return pftab[type] (buf, msg);
} }
int upack (char *buf, CheckMsg *msg, enum ck_msg_type *type) int
upack (char *buf, CheckMsg * msg, enum ck_msg_type *type)
{ {
char *obuf; char *obuf;
int nread; int nread;
@ -103,27 +105,29 @@ int upack (char *buf, CheckMsg *msg, enum ck_msg_type *type)
*type = upack_type (&buf); *type = upack_type (&buf);
check_type (*type, __FILE__, __LINE__); check_type (*type, __FILE__, __LINE__);
upftab[*type] (&buf, msg); upftab[*type] (&buf, msg);
nread = buf - obuf; nread = buf - obuf;
return nread; return nread;
} }
static void pack_int (char **buf, int val) static void
pack_int (char **buf, int val)
{ {
unsigned char *ubuf = (unsigned char *) *buf; unsigned char *ubuf = (unsigned char *) *buf;
ck_uint32 uval = val; ck_uint32 uval = val;
ubuf[0] = (uval >> 24) & 0xFF; ubuf[0] = (uval >> 24) & 0xFF;
ubuf[1] = (uval >> 16) & 0xFF; ubuf[1] = (uval >> 16) & 0xFF;
ubuf[2] = (uval >> 8) & 0xFF; ubuf[2] = (uval >> 8) & 0xFF;
ubuf[3] = uval & 0xFF; ubuf[3] = uval & 0xFF;
*buf += 4; *buf += 4;
} }
static int upack_int (char **buf) static int
upack_int (char **buf)
{ {
unsigned char *ubuf = (unsigned char *) *buf; unsigned char *ubuf = (unsigned char *) *buf;
ck_uint32 uval; ck_uint32 uval;
@ -135,7 +139,8 @@ static int upack_int (char **buf)
return (int) uval; return (int) uval;
} }
static void pack_str (char **buf, const char *val) static void
pack_str (char **buf, const char *val)
{ {
int strsz; int strsz;
@ -144,15 +149,16 @@ static void pack_str (char **buf, const char *val)
else else
strsz = strlen (val); strsz = strlen (val);
pack_int (buf, strsz); pack_int (buf, strsz);
if (strsz > 0) { if (strsz > 0) {
memcpy (*buf, val, strsz); memcpy (*buf, val, strsz);
*buf += strsz; *buf += strsz;
} }
} }
static char *upack_str (char **buf) static char *
upack_str (char **buf)
{ {
char *val; char *val;
int strsz; int strsz;
@ -172,37 +178,42 @@ static char *upack_str (char **buf)
return val; return val;
} }
static void pack_type (char **buf, enum ck_msg_type type) static void
pack_type (char **buf, enum ck_msg_type type)
{ {
pack_int (buf, (int) type); pack_int (buf, (int) type);
} }
static enum ck_msg_type upack_type (char **buf) static enum ck_msg_type
upack_type (char **buf)
{ {
return (enum ck_msg_type) upack_int (buf); return (enum ck_msg_type) upack_int (buf);
} }
static int pack_ctx (char **buf, CtxMsg *cmsg) static int
pack_ctx (char **buf, CtxMsg * cmsg)
{ {
char *ptr; char *ptr;
int len; int len;
len = 4 + 4; len = 4 + 4;
*buf = ptr = emalloc (len); *buf = ptr = emalloc (len);
pack_type (&ptr, CK_MSG_CTX); pack_type (&ptr, CK_MSG_CTX);
pack_int (&ptr, (int) cmsg->ctx); pack_int (&ptr, (int) cmsg->ctx);
return len; return len;
} }
static void upack_ctx (char **buf, CtxMsg *cmsg) static void
upack_ctx (char **buf, CtxMsg * cmsg)
{ {
cmsg->ctx = upack_int (buf); cmsg->ctx = upack_int (buf);
} }
static int pack_loc (char **buf, LocMsg *lmsg) static int
pack_loc (char **buf, LocMsg * lmsg)
{ {
char *ptr; char *ptr;
int len; int len;
@ -217,13 +228,15 @@ static int pack_loc (char **buf, LocMsg *lmsg)
return len; return len;
} }
static void upack_loc (char **buf, LocMsg *lmsg) static void
upack_loc (char **buf, LocMsg * lmsg)
{ {
lmsg->file = upack_str (buf); lmsg->file = upack_str (buf);
lmsg->line = upack_int (buf); lmsg->line = upack_int (buf);
} }
static int pack_fail (char **buf, FailMsg *fmsg) static int
pack_fail (char **buf, FailMsg * fmsg)
{ {
char *ptr; char *ptr;
int len; int len;
@ -237,18 +250,21 @@ static int pack_fail (char **buf, FailMsg *fmsg)
return len; return len;
} }
static void upack_fail (char **buf, FailMsg *fmsg) static void
upack_fail (char **buf, FailMsg * fmsg)
{ {
fmsg->msg = upack_str (buf); fmsg->msg = upack_str (buf);
} }
static void check_type (int type, const char *file, int line) static void
check_type (int type, const char *file, int line)
{ {
if (type < 0 || type >= CK_MSG_LAST) if (type < 0 || type >= CK_MSG_LAST)
eprintf ("Bad message type arg", file, line); eprintf ("Bad message type arg", file, line);
} }
void ppack (int fdes, enum ck_msg_type type, CheckMsg *msg) void
ppack (int fdes, enum ck_msg_type type, CheckMsg * msg)
{ {
char *buf; char *buf;
int n; int n;
@ -262,15 +278,16 @@ void ppack (int fdes, enum ck_msg_type type, CheckMsg *msg)
free (buf); free (buf);
} }
static int read_buf (int fdes, char **buf) static int
read_buf (int fdes, char **buf)
{ {
char *readloc; char *readloc;
int n; int n;
int nread = 0; int nread = 0;
int size = 1; int size = 1;
int grow = 2; int grow = 2;
*buf = emalloc(size); *buf = emalloc (size);
readloc = *buf; readloc = *buf;
while (1) { while (1) {
n = read (fdes, readloc, size - nread); n = read (fdes, readloc, size - nread);
@ -281,15 +298,16 @@ static int read_buf (int fdes, char **buf)
nread += n; nread += n;
size *= grow; size *= grow;
*buf = erealloc (*buf,size); *buf = erealloc (*buf, size);
readloc = *buf + nread; readloc = *buf + nread;
} }
return nread; return nread;
} }
static int get_result (char *buf, RcvMsg *rmsg) static int
get_result (char *buf, RcvMsg * rmsg)
{ {
enum ck_msg_type type; enum ck_msg_type type;
CheckMsg msg; CheckMsg msg;
@ -298,27 +316,23 @@ static int get_result (char *buf, RcvMsg *rmsg)
n = upack (buf, &msg, &type); n = upack (buf, &msg, &type);
if (n == -1) if (n == -1)
eprintf ("Error in call to upack", __FILE__, __LINE__ - 2); eprintf ("Error in call to upack", __FILE__, __LINE__ - 2);
if (type == CK_MSG_CTX) { if (type == CK_MSG_CTX) {
CtxMsg *cmsg = (CtxMsg *) &msg; CtxMsg *cmsg = (CtxMsg *) & msg;
rcvmsg_update_ctx (rmsg, cmsg->ctx); rcvmsg_update_ctx (rmsg, cmsg->ctx);
} else if (type == CK_MSG_LOC) { } else if (type == CK_MSG_LOC) {
LocMsg *lmsg = (LocMsg *) &msg; LocMsg *lmsg = (LocMsg *) & msg;
if (rmsg->failctx == CK_CTX_INVALID) if (rmsg->failctx == CK_CTX_INVALID) {
{
rcvmsg_update_loc (rmsg, lmsg->file, lmsg->line); rcvmsg_update_loc (rmsg, lmsg->file, lmsg->line);
} }
free (lmsg->file); free (lmsg->file);
} else if (type == CK_MSG_FAIL) { } else if (type == CK_MSG_FAIL) {
FailMsg *fmsg = (FailMsg *) &msg; FailMsg *fmsg = (FailMsg *) & msg;
if (rmsg->msg == NULL) if (rmsg->msg == NULL) {
{
rmsg->msg = emalloc (strlen (fmsg->msg) + 1); rmsg->msg = emalloc (strlen (fmsg->msg) + 1);
strcpy (rmsg->msg, fmsg->msg); strcpy (rmsg->msg, fmsg->msg);
rmsg->failctx = rmsg->lastctx; rmsg->failctx = rmsg->lastctx;
} } else {
else
{
/* Skip subsequent failure messages, only happens for CK_NOFORK */ /* Skip subsequent failure messages, only happens for CK_NOFORK */
} }
free (fmsg->msg); free (fmsg->msg);
@ -328,19 +342,22 @@ static int get_result (char *buf, RcvMsg *rmsg)
return n; return n;
} }
static void reset_rcv_test (RcvMsg *rmsg) static void
reset_rcv_test (RcvMsg * rmsg)
{ {
rmsg->test_line = -1; rmsg->test_line = -1;
rmsg->test_file = NULL; rmsg->test_file = NULL;
} }
static void reset_rcv_fixture (RcvMsg *rmsg) static void
reset_rcv_fixture (RcvMsg * rmsg)
{ {
rmsg->fixture_line = -1; rmsg->fixture_line = -1;
rmsg->fixture_file = NULL; rmsg->fixture_file = NULL;
} }
static RcvMsg *rcvmsg_create (void) static RcvMsg *
rcvmsg_create (void)
{ {
RcvMsg *rmsg; RcvMsg *rmsg;
@ -353,42 +370,45 @@ static RcvMsg *rcvmsg_create (void)
return rmsg; return rmsg;
} }
void rcvmsg_free (RcvMsg *rmsg) void
rcvmsg_free (RcvMsg * rmsg)
{ {
free(rmsg->fixture_file); free (rmsg->fixture_file);
free(rmsg->test_file); free (rmsg->test_file);
free(rmsg->msg); free (rmsg->msg);
free(rmsg); free (rmsg);
} }
static void rcvmsg_update_ctx (RcvMsg *rmsg, enum ck_result_ctx ctx) static void
rcvmsg_update_ctx (RcvMsg * rmsg, enum ck_result_ctx ctx)
{ {
if (rmsg->lastctx != CK_CTX_INVALID) if (rmsg->lastctx != CK_CTX_INVALID) {
{ free (rmsg->fixture_file);
free(rmsg->fixture_file);
reset_rcv_fixture (rmsg); reset_rcv_fixture (rmsg);
} }
rmsg->lastctx = ctx; rmsg->lastctx = ctx;
} }
static void rcvmsg_update_loc (RcvMsg *rmsg, const char *file, int line) static void
rcvmsg_update_loc (RcvMsg * rmsg, const char *file, int line)
{ {
int flen = strlen(file); int flen = strlen (file);
if (rmsg->lastctx == CK_CTX_TEST) { if (rmsg->lastctx == CK_CTX_TEST) {
free(rmsg->test_file); free (rmsg->test_file);
rmsg->test_line = line; rmsg->test_line = line;
rmsg->test_file = emalloc (flen + 1); rmsg->test_file = emalloc (flen + 1);
strcpy (rmsg->test_file, file); strcpy (rmsg->test_file, file);
} else { } else {
free(rmsg->fixture_file); free (rmsg->fixture_file);
rmsg->fixture_line = line; rmsg->fixture_line = line;
rmsg->fixture_file = emalloc (flen + 1); rmsg->fixture_file = emalloc (flen + 1);
strcpy (rmsg->fixture_file, file); strcpy (rmsg->fixture_file, file);
} }
} }
RcvMsg *punpack (int fdes) RcvMsg *
punpack (int fdes)
{ {
int nread, n; int nread, n;
char *buf; char *buf;
@ -398,7 +418,7 @@ RcvMsg *punpack (int fdes)
nread = read_buf (fdes, &buf); nread = read_buf (fdes, &buf);
obuf = buf; obuf = buf;
rmsg = rcvmsg_create (); rmsg = rcvmsg_create ();
while (nread > 0) { while (nread > 0) {
n = get_result (buf, rmsg); n = get_result (buf, rmsg);
nread -= n; nread -= n;

View file

@ -31,69 +31,74 @@
#include "check_str.h" #include "check_str.h"
#include "check_print.h" #include "check_print.h"
static void srunner_fprint_summary (FILE *file, SRunner *sr, static void srunner_fprint_summary (FILE * file, SRunner * sr,
enum print_output print_mode); enum print_output print_mode);
static void srunner_fprint_results (FILE *file, SRunner *sr, static void srunner_fprint_results (FILE * file, SRunner * sr,
enum print_output print_mode); enum print_output print_mode);
void srunner_print (SRunner *sr, enum print_output print_mode) void
srunner_print (SRunner * sr, enum print_output print_mode)
{ {
srunner_fprint (stdout, sr, print_mode); srunner_fprint (stdout, sr, print_mode);
} }
void srunner_fprint (FILE *file, SRunner *sr, enum print_output print_mode) void
srunner_fprint (FILE * file, SRunner * sr, enum print_output print_mode)
{ {
if (print_mode == CK_ENV) { if (print_mode == CK_ENV) {
print_mode = get_env_printmode(); print_mode = get_env_printmode ();
} }
srunner_fprint_summary (file, sr, print_mode); srunner_fprint_summary (file, sr, print_mode);
srunner_fprint_results (file, sr, print_mode); srunner_fprint_results (file, sr, print_mode);
} }
static void srunner_fprint_summary (FILE *file, SRunner *sr, static void
enum print_output print_mode) srunner_fprint_summary (FILE * file, SRunner * sr, enum print_output print_mode)
{ {
if (print_mode >= CK_MINIMAL) { if (print_mode >= CK_MINIMAL) {
char *str; char *str;
str = sr_stat_str (sr); str = sr_stat_str (sr);
fprintf (file, "%s\n", str); fprintf (file, "%s\n", str);
free(str); free (str);
} }
return; return;
} }
static void srunner_fprint_results (FILE *file, SRunner *sr, static void
enum print_output print_mode) srunner_fprint_results (FILE * file, SRunner * sr, enum print_output print_mode)
{ {
List *resultlst; List *resultlst;
resultlst = sr->resultlst; resultlst = sr->resultlst;
for (list_front(resultlst); !list_at_end(resultlst); list_advance(resultlst)) { for (list_front (resultlst); !list_at_end (resultlst);
TestResult *tr = list_val(resultlst); list_advance (resultlst)) {
TestResult *tr = list_val (resultlst);
tr_fprint (file, tr, print_mode); tr_fprint (file, tr, print_mode);
} }
return; return;
} }
void tr_fprint (FILE *file, TestResult *tr, enum print_output print_mode) void
tr_fprint (FILE * file, TestResult * tr, enum print_output print_mode)
{ {
if (print_mode == CK_ENV) { if (print_mode == CK_ENV) {
print_mode = get_env_printmode(); print_mode = get_env_printmode ();
} }
if ((print_mode >= CK_VERBOSE && tr->rtype == CK_PASS) || if ((print_mode >= CK_VERBOSE && tr->rtype == CK_PASS) ||
(tr->rtype != CK_PASS && print_mode >= CK_NORMAL)) { (tr->rtype != CK_PASS && print_mode >= CK_NORMAL)) {
char *trstr = tr_str (tr); char *trstr = tr_str (tr);
fprintf (file,"%s\n", trstr); fprintf (file, "%s\n", trstr);
free(trstr); free (trstr);
} }
} }
void tr_xmlprint (FILE *file, TestResult *tr, enum print_output print_mode) void
tr_xmlprint (FILE * file, TestResult * tr, enum print_output print_mode)
{ {
char result[10]; char result[10];
char *path_name; char *path_name;
@ -101,47 +106,48 @@ void tr_xmlprint (FILE *file, TestResult *tr, enum print_output print_mode)
char *slash; char *slash;
switch (tr->rtype) { switch (tr->rtype) {
case CK_PASS: case CK_PASS:
strcpy(result, "success"); strcpy (result, "success");
break; break;
case CK_FAILURE: case CK_FAILURE:
strcpy(result, "failure"); strcpy (result, "failure");
break; break;
case CK_ERROR: case CK_ERROR:
strcpy(result, "error"); strcpy (result, "error");
break; break;
case CK_TEST_RESULT_INVALID: case CK_TEST_RESULT_INVALID:
default: default:
abort (); abort ();
break; break;
} }
slash = strrchr(tr->file, '/'); slash = strrchr (tr->file, '/');
if (slash == NULL) { if (slash == NULL) {
path_name = (char*)"."; path_name = (char *) ".";
file_name = tr->file; file_name = tr->file;
} else { } else {
path_name = strdup(tr->file); path_name = strdup (tr->file);
path_name[slash - tr->file] = 0; /* Terminate the temporary string. */ path_name[slash - tr->file] = 0; /* Terminate the temporary string. */
file_name = slash + 1; file_name = slash + 1;
} }
fprintf(file, " <test result=\"%s\">\n", result);
fprintf(file, " <path>%s</path>\n", path_name); fprintf (file, " <test result=\"%s\">\n", result);
fprintf(file, " <fn>%s:%d</fn>\n", file_name, tr->line); fprintf (file, " <path>%s</path>\n", path_name);
fprintf(file, " <id>%s</id>\n", tr->tname); fprintf (file, " <fn>%s:%d</fn>\n", file_name, tr->line);
fprintf(file, " <iteration>%d</iteration>\n", tr->iter); fprintf (file, " <id>%s</id>\n", tr->tname);
fprintf(file, " <description>%s</description>\n", tr->tcname); fprintf (file, " <iteration>%d</iteration>\n", tr->iter);
fprintf(file, " <message>%s</message>\n", tr->msg); fprintf (file, " <description>%s</description>\n", tr->tcname);
fprintf(file, " </test>\n"); fprintf (file, " <message>%s</message>\n", tr->msg);
fprintf (file, " </test>\n");
if (slash != NULL) { if (slash != NULL) {
free(path_name); free (path_name);
} }
} }
enum print_output get_env_printmode (void) enum print_output
get_env_printmode (void)
{ {
char *env = getenv ("CK_VERBOSITY"); char *env = getenv ("CK_VERBOSITY");
if (env == NULL) if (env == NULL)

View file

@ -37,7 +37,8 @@
#include "check_msg.h" #include "check_msg.h"
#include "check_log.h" #include "check_log.h"
enum rinfo { enum rinfo
{
CK_R_SIG, CK_R_SIG,
CK_R_PASS, CK_R_PASS,
CK_R_EXIT, CK_R_EXIT,
@ -45,34 +46,33 @@ enum rinfo {
CK_R_FAIL_FIXTURE CK_R_FAIL_FIXTURE
}; };
enum tf_type { enum tf_type
{
CK_FORK_TEST, CK_FORK_TEST,
CK_NOFORK_TEST, CK_NOFORK_TEST,
CK_NOFORK_FIXTURE CK_NOFORK_FIXTURE
}; };
static void srunner_run_init (SRunner *sr, enum print_output print_mode); static void srunner_run_init (SRunner * sr, enum print_output print_mode);
static void srunner_run_end (SRunner *sr, enum print_output print_mode); static void srunner_run_end (SRunner * sr, enum print_output print_mode);
static void srunner_iterate_suites (SRunner *sr, static void srunner_iterate_suites (SRunner * sr, enum print_output print_mode);
enum print_output print_mode); static void srunner_run_tcase (SRunner * sr, TCase * tc);
static void srunner_run_tcase (SRunner *sr, TCase *tc); static int srunner_run_unchecked_setup (SRunner * sr, TCase * tc);
static int srunner_run_unchecked_setup (SRunner *sr, TCase *tc); static void srunner_run_unchecked_teardown (SRunner * sr, TCase * tc);
static void srunner_run_unchecked_teardown (SRunner *sr, TCase *tc); static TestResult *tcase_run_checked_setup (SRunner * sr, TCase * tc);
static TestResult * tcase_run_checked_setup (SRunner *sr, TCase *tc); static void tcase_run_checked_teardown (TCase * tc);
static void tcase_run_checked_teardown (TCase *tc); static void srunner_iterate_tcase_tfuns (SRunner * sr, TCase * tc);
static void srunner_iterate_tcase_tfuns (SRunner *sr, TCase *tc); static void srunner_add_failure (SRunner * sr, TestResult * tf);
static void srunner_add_failure (SRunner *sr, TestResult *tf); static TestResult *tcase_run_tfun_fork (SRunner * sr, TCase * tc, TF * tf,
static TestResult *tcase_run_tfun_fork (SRunner *sr, TCase *tc, TF *tf, int i); int i);
static TestResult *tcase_run_tfun_nofork (SRunner *sr, TCase *tc, TF *tf, int i); static TestResult *tcase_run_tfun_nofork (SRunner * sr, TCase * tc, TF * tf,
int i);
static TestResult *receive_result_info_fork (const char *tcname, static TestResult *receive_result_info_fork (const char *tcname,
const char *tname, const char *tname, int iter, int status, int expected_signal);
int iter,
int status, int expected_signal);
static TestResult *receive_result_info_nofork (const char *tcname, static TestResult *receive_result_info_nofork (const char *tcname,
const char *tname, const char *tname, int iter);
int iter); static void set_fork_info (TestResult * tr, int status, int expected_signal);
static void set_fork_info (TestResult *tr, int status, int expected_signal); static void set_nofork_info (TestResult * tr);
static void set_nofork_info (TestResult *tr);
static char *signal_msg (int sig); static char *signal_msg (int sig);
static char *signal_error_msg (int signal_received, int signal_expected); static char *signal_error_msg (int signal_received, int signal_expected);
static char *pass_msg (void); static char *pass_msg (void);
@ -84,173 +84,178 @@ static int waserror (int status, int expected_signal);
static int alarm_received; static int alarm_received;
static pid_t group_pid; static pid_t group_pid;
static void sig_handler(int sig_nr) static void
sig_handler (int sig_nr)
{ {
switch (sig_nr) { switch (sig_nr) {
case SIGALRM: case SIGALRM:
alarm_received = 1; alarm_received = 1;
killpg(group_pid, SIGKILL); killpg (group_pid, SIGKILL);
break; break;
default: default:
eprintf("Unhandled signal: %d", __FILE__, __LINE__, sig_nr); eprintf ("Unhandled signal: %d", __FILE__, __LINE__, sig_nr);
break; break;
} }
} }
static void srunner_run_init (SRunner *sr, enum print_output print_mode) static void
srunner_run_init (SRunner * sr, enum print_output print_mode)
{ {
set_fork_status(srunner_fork_status(sr)); set_fork_status (srunner_fork_status (sr));
setup_messaging(); setup_messaging ();
srunner_init_logging (sr, print_mode); srunner_init_logging (sr, print_mode);
log_srunner_start (sr); log_srunner_start (sr);
} }
static void srunner_run_end (SRunner *sr, enum print_output print_mode) static void
srunner_run_end (SRunner * sr, enum print_output print_mode)
{ {
log_srunner_end (sr); log_srunner_end (sr);
srunner_end_logging (sr); srunner_end_logging (sr);
teardown_messaging(); teardown_messaging ();
set_fork_status(CK_FORK); set_fork_status (CK_FORK);
} }
static void srunner_iterate_suites (SRunner *sr, static void
enum print_output print_mode) srunner_iterate_suites (SRunner * sr, enum print_output print_mode)
{ {
List *slst; List *slst;
List *tcl; List *tcl;
TCase *tc; TCase *tc;
slst = sr->slst; slst = sr->slst;
for (list_front(slst); !list_at_end(slst); list_advance(slst)) { for (list_front (slst); !list_at_end (slst); list_advance (slst)) {
Suite *s = list_val(slst); Suite *s = list_val (slst);
log_suite_start (sr, s); log_suite_start (sr, s);
tcl = s->tclst; tcl = s->tclst;
for (list_front(tcl);!list_at_end (tcl); list_advance (tcl)) { for (list_front (tcl); !list_at_end (tcl); list_advance (tcl)) {
tc = list_val (tcl); tc = list_val (tcl);
srunner_run_tcase (sr, tc); srunner_run_tcase (sr, tc);
} }
log_suite_end (sr, s); log_suite_end (sr, s);
} }
} }
void srunner_run_all (SRunner *sr, enum print_output print_mode) void
srunner_run_all (SRunner * sr, enum print_output print_mode)
{ {
struct sigaction old_action; struct sigaction old_action;
struct sigaction new_action; struct sigaction new_action;
if (sr == NULL) if (sr == NULL)
return; return;
if (print_mode >= CK_LAST) if (print_mode >= CK_LAST) {
{ eprintf ("Bad print_mode argument to srunner_run_all: %d",
eprintf ("Bad print_mode argument to srunner_run_all: %d", __FILE__, __LINE__, print_mode);
__FILE__, __LINE__, print_mode); }
} memset (&new_action, 0, sizeof new_action);
memset(&new_action, 0, sizeof new_action);
new_action.sa_handler = sig_handler; new_action.sa_handler = sig_handler;
sigaction(SIGALRM, &new_action, &old_action); sigaction (SIGALRM, &new_action, &old_action);
srunner_run_init (sr, print_mode); srunner_run_init (sr, print_mode);
srunner_iterate_suites (sr, print_mode); srunner_iterate_suites (sr, print_mode);
srunner_run_end (sr, print_mode); srunner_run_end (sr, print_mode);
sigaction(SIGALRM, &old_action, NULL); sigaction (SIGALRM, &old_action, NULL);
} }
static void srunner_add_failure (SRunner *sr, TestResult *tr) static void
{ srunner_add_failure (SRunner * sr, TestResult * tr)
{
list_add_end (sr->resultlst, tr); list_add_end (sr->resultlst, tr);
sr->stats->n_checked++; /* count checks during setup, test, and teardown */ sr->stats->n_checked++; /* count checks during setup, test, and teardown */
if (tr->rtype == CK_FAILURE) if (tr->rtype == CK_FAILURE)
sr->stats->n_failed++; sr->stats->n_failed++;
else if (tr->rtype == CK_ERROR) else if (tr->rtype == CK_ERROR)
sr->stats->n_errors++; sr->stats->n_errors++;
} }
static void srunner_iterate_tcase_tfuns (SRunner *sr, TCase *tc) static void
srunner_iterate_tcase_tfuns (SRunner * sr, TCase * tc)
{ {
List *tfl; List *tfl;
TF *tfun; TF *tfun;
TestResult *tr = NULL; TestResult *tr = NULL;
tfl = tc->tflst; tfl = tc->tflst;
for (list_front(tfl); !list_at_end (tfl); list_advance (tfl)) { for (list_front (tfl); !list_at_end (tfl); list_advance (tfl)) {
int i; int i;
tfun = list_val (tfl); tfun = list_val (tfl);
for (i = tfun->loop_start; i < tfun->loop_end; i++) for (i = tfun->loop_start; i < tfun->loop_end; i++) {
{ switch (srunner_fork_status (sr)) {
switch (srunner_fork_status(sr)) { case CK_FORK:
case CK_FORK: tr = tcase_run_tfun_fork (sr, tc, tfun, i);
tr = tcase_run_tfun_fork (sr, tc, tfun, i); break;
break; case CK_NOFORK:
case CK_NOFORK: tr = tcase_run_tfun_nofork (sr, tc, tfun, i);
tr = tcase_run_tfun_nofork (sr, tc, tfun, i); break;
break; default:
default: eprintf ("Bad fork status in SRunner", __FILE__, __LINE__);
eprintf("Bad fork status in SRunner", __FILE__, __LINE__);
} }
srunner_add_failure (sr, tr); srunner_add_failure (sr, tr);
log_test_end(sr, tr); log_test_end (sr, tr);
} }
} }
} }
static int srunner_run_unchecked_setup (SRunner *sr, TCase *tc) static int
srunner_run_unchecked_setup (SRunner * sr, TCase * tc)
{ {
TestResult *tr; TestResult *tr;
List *l; List *l;
Fixture *f; Fixture *f;
int rval = 1; int rval = 1;
set_fork_status(CK_NOFORK); set_fork_status (CK_NOFORK);
l = tc->unch_sflst; l = tc->unch_sflst;
for (list_front(l); !list_at_end(l); list_advance(l)) { for (list_front (l); !list_at_end (l); list_advance (l)) {
send_ctx_info(CK_CTX_SETUP); send_ctx_info (CK_CTX_SETUP);
f = list_val(l); f = list_val (l);
f->fun(); f->fun ();
tr = receive_result_info_nofork (tc->name, "unchecked_setup", 0); tr = receive_result_info_nofork (tc->name, "unchecked_setup", 0);
if (tr->rtype != CK_PASS) { if (tr->rtype != CK_PASS) {
srunner_add_failure(sr, tr); srunner_add_failure (sr, tr);
rval = 0; rval = 0;
break; break;
} }
free(tr->file); free (tr->file);
free(tr->msg); free (tr->msg);
free(tr); free (tr);
} }
set_fork_status(srunner_fork_status(sr)); set_fork_status (srunner_fork_status (sr));
return rval; return rval;
} }
static TestResult * tcase_run_checked_setup (SRunner *sr, TCase *tc) static TestResult *
tcase_run_checked_setup (SRunner * sr, TCase * tc)
{ {
TestResult *tr = NULL; TestResult *tr = NULL;
List *l; List *l;
Fixture *f; Fixture *f;
enum fork_status fstat = srunner_fork_status(sr); enum fork_status fstat = srunner_fork_status (sr);
l = tc->ch_sflst; l = tc->ch_sflst;
if (fstat == CK_FORK) { if (fstat == CK_FORK) {
send_ctx_info(CK_CTX_SETUP); send_ctx_info (CK_CTX_SETUP);
} }
for (list_front(l); !list_at_end(l); list_advance(l)) { for (list_front (l); !list_at_end (l); list_advance (l)) {
if (fstat == CK_NOFORK) { if (fstat == CK_NOFORK) {
send_ctx_info(CK_CTX_SETUP); send_ctx_info (CK_CTX_SETUP);
} }
f = list_val(l); f = list_val (l);
f->fun(); f->fun ();
/* Stop the setup and return the failure if nofork mode. */ /* Stop the setup and return the failure if nofork mode. */
if (fstat == CK_NOFORK) { if (fstat == CK_NOFORK) {
@ -259,9 +264,9 @@ static TestResult * tcase_run_checked_setup (SRunner *sr, TCase *tc)
break; break;
} }
free(tr->file); free (tr->file);
free(tr->msg); free (tr->msg);
free(tr); free (tr);
tr = NULL; tr = NULL;
} }
} }
@ -269,238 +274,248 @@ static TestResult * tcase_run_checked_setup (SRunner *sr, TCase *tc)
return tr; return tr;
} }
static void tcase_run_checked_teardown (TCase *tc) static void
tcase_run_checked_teardown (TCase * tc)
{ {
List *l; List *l;
Fixture *f; Fixture *f;
l = tc->ch_tflst; l = tc->ch_tflst;
send_ctx_info(CK_CTX_TEARDOWN);
for (list_front(l); !list_at_end(l); list_advance(l)) { send_ctx_info (CK_CTX_TEARDOWN);
f = list_val(l);
f->fun(); for (list_front (l); !list_at_end (l); list_advance (l)) {
f = list_val (l);
f->fun ();
} }
} }
static void srunner_run_unchecked_teardown (SRunner *sr, TCase *tc) static void
srunner_run_unchecked_teardown (SRunner * sr, TCase * tc)
{ {
List *l; List *l;
Fixture *f; Fixture *f;
set_fork_status(CK_NOFORK); set_fork_status (CK_NOFORK);
l = tc->unch_tflst; l = tc->unch_tflst;
for (list_front(l); !list_at_end(l); list_advance(l)) { for (list_front (l); !list_at_end (l); list_advance (l)) {
f = list_val(l); f = list_val (l);
send_ctx_info(CK_CTX_TEARDOWN); send_ctx_info (CK_CTX_TEARDOWN);
f->fun (); f->fun ();
} }
set_fork_status(srunner_fork_status(sr)); set_fork_status (srunner_fork_status (sr));
} }
static void srunner_run_tcase (SRunner *sr, TCase *tc) static void
srunner_run_tcase (SRunner * sr, TCase * tc)
{ {
if (srunner_run_unchecked_setup(sr,tc)) { if (srunner_run_unchecked_setup (sr, tc)) {
srunner_iterate_tcase_tfuns(sr,tc); srunner_iterate_tcase_tfuns (sr, tc);
srunner_run_unchecked_teardown(sr, tc); srunner_run_unchecked_teardown (sr, tc);
} }
} }
static TestResult *receive_result_info_fork (const char *tcname, static TestResult *
const char *tname, receive_result_info_fork (const char *tcname,
int iter, const char *tname, int iter, int status, int expected_signal)
int status, int expected_signal)
{ {
TestResult *tr; TestResult *tr;
tr = receive_test_result(waserror(status, expected_signal)); tr = receive_test_result (waserror (status, expected_signal));
if (tr == NULL) if (tr == NULL)
eprintf("Failed to receive test result", __FILE__, __LINE__); eprintf ("Failed to receive test result", __FILE__, __LINE__);
tr->tcname = tcname; tr->tcname = tcname;
tr->tname = tname; tr->tname = tname;
tr->iter = iter; tr->iter = iter;
set_fork_info(tr, status, expected_signal); set_fork_info (tr, status, expected_signal);
return tr; return tr;
} }
static TestResult *receive_result_info_nofork (const char *tcname, static TestResult *
const char *tname, receive_result_info_nofork (const char *tcname, const char *tname, int iter)
int iter)
{ {
TestResult *tr; TestResult *tr;
tr = receive_test_result(0); tr = receive_test_result (0);
if (tr == NULL) if (tr == NULL)
eprintf("Failed to receive test result", __FILE__, __LINE__); eprintf ("Failed to receive test result", __FILE__, __LINE__);
tr->tcname = tcname; tr->tcname = tcname;
tr->tname = tname; tr->tname = tname;
tr->iter = iter; tr->iter = iter;
set_nofork_info(tr); set_nofork_info (tr);
return tr; return tr;
} }
static void set_fork_info (TestResult *tr, int status, int signal_expected) static void
set_fork_info (TestResult * tr, int status, int signal_expected)
{ {
int was_sig = WIFSIGNALED(status); int was_sig = WIFSIGNALED (status);
int was_exit = WIFEXITED(status); int was_exit = WIFEXITED (status);
int exit_status = WEXITSTATUS(status); int exit_status = WEXITSTATUS (status);
int signal_received = WTERMSIG(status); int signal_received = WTERMSIG (status);
if (was_sig) { if (was_sig) {
if (signal_expected == signal_received) { if (signal_expected == signal_received) {
if (alarm_received) { if (alarm_received) {
/* Got alarm instead of signal */ /* Got alarm instead of signal */
tr->rtype = CK_ERROR; tr->rtype = CK_ERROR;
tr->msg = signal_error_msg(signal_received, signal_expected); tr->msg = signal_error_msg (signal_received, signal_expected);
} else { } else {
tr->rtype = CK_PASS; tr->rtype = CK_PASS;
tr->msg = pass_msg(); tr->msg = pass_msg ();
} }
} else if (signal_expected != 0) { } else if (signal_expected != 0) {
/* signal received, but not the expected one */ /* signal received, but not the expected one */
tr->rtype = CK_ERROR; tr->rtype = CK_ERROR;
tr->msg = signal_error_msg(signal_received, signal_expected); tr->msg = signal_error_msg (signal_received, signal_expected);
} else { } else {
/* signal received and none expected */ /* signal received and none expected */
tr->rtype = CK_ERROR; tr->rtype = CK_ERROR;
tr->msg = signal_msg(signal_received); tr->msg = signal_msg (signal_received);
} }
} else if (signal_expected == 0) { } else if (signal_expected == 0) {
if (was_exit && exit_status == 0) { if (was_exit && exit_status == 0) {
tr->rtype = CK_PASS; tr->rtype = CK_PASS;
tr->msg = pass_msg(); tr->msg = pass_msg ();
} else if (was_exit && exit_status != 0) { } else if (was_exit && exit_status != 0) {
if (tr->msg == NULL) { /* early exit */ if (tr->msg == NULL) { /* early exit */
tr->rtype = CK_ERROR; tr->rtype = CK_ERROR;
tr->msg = exit_msg(exit_status); tr->msg = exit_msg (exit_status);
} else { } else {
tr->rtype = CK_FAILURE; tr->rtype = CK_FAILURE;
} }
} }
} else { /* a signal was expected and none raised */ } else { /* a signal was expected and none raised */
if (was_exit) { if (was_exit) {
tr->msg = exit_msg(exit_status); tr->msg = exit_msg (exit_status);
if (exit_status == 0) if (exit_status == 0)
tr->rtype = CK_FAILURE; /* normal exit status */ tr->rtype = CK_FAILURE; /* normal exit status */
else else
tr->rtype = CK_FAILURE; /* early exit */ tr->rtype = CK_FAILURE; /* early exit */
} }
} }
} }
static void set_nofork_info (TestResult *tr) static void
set_nofork_info (TestResult * tr)
{ {
if (tr->msg == NULL) { if (tr->msg == NULL) {
tr->rtype = CK_PASS; tr->rtype = CK_PASS;
tr->msg = pass_msg(); tr->msg = pass_msg ();
} else { } else {
tr->rtype = CK_FAILURE; tr->rtype = CK_FAILURE;
} }
} }
static TestResult *tcase_run_tfun_nofork (SRunner *sr, TCase *tc, TF *tfun, int i) static TestResult *
tcase_run_tfun_nofork (SRunner * sr, TCase * tc, TF * tfun, int i)
{ {
TestResult *tr; TestResult *tr;
tr = tcase_run_checked_setup(sr, tc); tr = tcase_run_checked_setup (sr, tc);
if (tr == NULL) { if (tr == NULL) {
tfun->fn(i); tfun->fn (i);
tcase_run_checked_teardown(tc); tcase_run_checked_teardown (tc);
return receive_result_info_nofork(tc->name, tfun->name, i); return receive_result_info_nofork (tc->name, tfun->name, i);
} }
return tr; return tr;
} }
static TestResult *tcase_run_tfun_fork (SRunner *sr, TCase *tc, TF *tfun, int i) static TestResult *
tcase_run_tfun_fork (SRunner * sr, TCase * tc, TF * tfun, int i)
{ {
pid_t pid_w; pid_t pid_w;
pid_t pid; pid_t pid;
int status = 0; int status = 0;
pid = fork(); pid = fork ();
if (pid == -1) if (pid == -1)
eprintf("Error in call to fork:", __FILE__, __LINE__ - 2); eprintf ("Error in call to fork:", __FILE__, __LINE__ - 2);
if (pid == 0) { if (pid == 0) {
setpgid(0, 0); setpgid (0, 0);
group_pid = getpgrp(); group_pid = getpgrp ();
tcase_run_checked_setup(sr, tc); tcase_run_checked_setup (sr, tc);
tfun->fn(i); tfun->fn (i);
tcase_run_checked_teardown(tc); tcase_run_checked_teardown (tc);
exit(EXIT_SUCCESS); exit (EXIT_SUCCESS);
} else { } else {
group_pid = pid; group_pid = pid;
} }
alarm_received = 0; alarm_received = 0;
alarm(tc->timeout); alarm (tc->timeout);
do { do {
pid_w = waitpid(pid, &status, 0); pid_w = waitpid (pid, &status, 0);
} while (pid_w == -1); } while (pid_w == -1);
killpg(pid, SIGKILL); /* Kill remaining processes. */
return receive_result_info_fork(tc->name, tfun->name, i, status, tfun->signal); killpg (pid, SIGKILL); /* Kill remaining processes. */
return receive_result_info_fork (tc->name, tfun->name, i, status,
tfun->signal);
} }
static char *signal_error_msg (int signal_received, int signal_expected) static char *
signal_error_msg (int signal_received, int signal_expected)
{ {
char *sig_r_str; char *sig_r_str;
char *sig_e_str; char *sig_e_str;
char *msg = emalloc (MSG_LEN); /* free'd by caller */ char *msg = emalloc (MSG_LEN); /* free'd by caller */
sig_r_str = strdup(strsignal(signal_received)); sig_r_str = strdup (strsignal (signal_received));
sig_e_str = strdup(strsignal(signal_expected)); sig_e_str = strdup (strsignal (signal_expected));
if (alarm_received) { if (alarm_received) {
snprintf (msg, MSG_LEN, "Test timeout expired, expected signal %d (%s)", snprintf (msg, MSG_LEN, "Test timeout expired, expected signal %d (%s)",
signal_expected, sig_e_str); signal_expected, sig_e_str);
} else { } else {
snprintf (msg, MSG_LEN, "Received signal %d (%s), expected %d (%s)", snprintf (msg, MSG_LEN, "Received signal %d (%s), expected %d (%s)",
signal_received, sig_r_str, signal_expected, sig_e_str); signal_received, sig_r_str, signal_expected, sig_e_str);
} }
free(sig_r_str); free (sig_r_str);
free(sig_e_str); free (sig_e_str);
return msg; return msg;
} }
static char *signal_msg (int signal) static char *
signal_msg (int signal)
{ {
char *msg = emalloc(MSG_LEN); /* free'd by caller */ char *msg = emalloc (MSG_LEN); /* free'd by caller */
if (alarm_received) { if (alarm_received) {
snprintf(msg, MSG_LEN, "Test timeout expired"); snprintf (msg, MSG_LEN, "Test timeout expired");
} else { } else {
snprintf(msg, MSG_LEN, "Received signal %d (%s)", snprintf (msg, MSG_LEN, "Received signal %d (%s)",
signal, strsignal(signal)); signal, strsignal (signal));
} }
return msg; return msg;
} }
static char *exit_msg (int exitval) static char *
exit_msg (int exitval)
{ {
char *msg = emalloc(MSG_LEN); /* free'd by caller */ char *msg = emalloc (MSG_LEN); /* free'd by caller */
snprintf (msg, MSG_LEN, snprintf (msg, MSG_LEN, "Early exit with return value %d", exitval);
"Early exit with return value %d", exitval);
return msg; return msg;
} }
static char *pass_msg (void) static char *
pass_msg (void)
{ {
char *msg = emalloc(sizeof("Passed")); char *msg = emalloc (sizeof ("Passed"));
strcpy (msg, "Passed"); strcpy (msg, "Passed");
return msg; return msg;
} }
enum fork_status srunner_fork_status (SRunner *sr) enum fork_status
srunner_fork_status (SRunner * sr)
{ {
if (sr->fstat == CK_FORK_GETENV) { if (sr->fstat == CK_FORK_GETENV) {
char *env = getenv ("CK_FORK"); char *env = getenv ("CK_FORK");
if (env == NULL) if (env == NULL)
return CK_FORK; return CK_FORK;
if (strcmp (env,"no") == 0) if (strcmp (env, "no") == 0)
return CK_NOFORK; return CK_NOFORK;
else else
return CK_FORK; return CK_FORK;
@ -508,41 +523,45 @@ enum fork_status srunner_fork_status (SRunner *sr)
return sr->fstat; return sr->fstat;
} }
void srunner_set_fork_status (SRunner *sr, enum fork_status fstat) void
srunner_set_fork_status (SRunner * sr, enum fork_status fstat)
{ {
sr->fstat = fstat; sr->fstat = fstat;
} }
pid_t check_fork (void) pid_t
check_fork (void)
{ {
pid_t pid = fork(); pid_t pid = fork ();
/* Set the process to a process group to be able to kill it easily. */ /* Set the process to a process group to be able to kill it easily. */
setpgid(pid, group_pid); setpgid (pid, group_pid);
return pid; return pid;
} }
void check_waitpid_and_exit (pid_t pid) void
check_waitpid_and_exit (pid_t pid)
{ {
pid_t pid_w; pid_t pid_w;
int status; int status;
if (pid > 0) { if (pid > 0) {
do { do {
pid_w = waitpid(pid, &status, 0); pid_w = waitpid (pid, &status, 0);
} while (pid_w == -1); } while (pid_w == -1);
if (waserror(status, 0)) if (waserror (status, 0))
exit(EXIT_FAILURE); exit (EXIT_FAILURE);
} }
exit(EXIT_SUCCESS); exit (EXIT_SUCCESS);
} }
static int waserror (int status, int signal_expected) static int
waserror (int status, int signal_expected)
{ {
int was_sig = WIFSIGNALED (status); int was_sig = WIFSIGNALED (status);
int was_exit = WIFEXITED (status); int was_exit = WIFEXITED (status);
int exit_status = WEXITSTATUS (status); int exit_status = WEXITSTATUS (status);
int signal_received = WTERMSIG(status); int signal_received = WTERMSIG (status);
return ((was_sig && (signal_received != signal_expected)) || return ((was_sig && (signal_received != signal_expected)) ||
(was_exit && exit_status != 0)); (was_exit && exit_status != 0));
} }

View file

@ -29,39 +29,40 @@
#include "check_impl.h" #include "check_impl.h"
#include "check_str.h" #include "check_str.h"
static const char *tr_type_str (TestResult *tr); static const char *tr_type_str (TestResult * tr);
static int percent_passed (TestStats *t); static int percent_passed (TestStats * t);
char *tr_str (TestResult *tr) char *
tr_str (TestResult * tr)
{ {
const char *exact_msg; const char *exact_msg;
char *rstr; char *rstr;
exact_msg = (tr->rtype == CK_ERROR) ? "(after this point) ": ""; exact_msg = (tr->rtype == CK_ERROR) ? "(after this point) " : "";
rstr = ck_strdup_printf ("%s:%d:%s:%s:%s:%d: %s%s", rstr = ck_strdup_printf ("%s:%d:%s:%s:%s:%d: %s%s",
tr->file, tr->line, tr->file, tr->line,
tr_type_str(tr), tr->tcname, tr->tname, tr->iter, tr_type_str (tr), tr->tcname, tr->tname, tr->iter, exact_msg, tr->msg);
exact_msg, tr->msg);
return rstr; return rstr;
} }
char *sr_stat_str (SRunner *sr) char *
sr_stat_str (SRunner * sr)
{ {
char *str; char *str;
TestStats *ts; TestStats *ts;
ts = sr->stats; ts = sr->stats;
str = ck_strdup_printf ("%d%%: Checks: %d, Failures: %d, Errors: %d", str = ck_strdup_printf ("%d%%: Checks: %d, Failures: %d, Errors: %d",
percent_passed (ts), ts->n_checked, ts->n_failed, percent_passed (ts), ts->n_checked, ts->n_failed, ts->n_errors);
ts->n_errors);
return str; return str;
} }
char *ck_strdup_printf (const char *fmt, ...) char *
ck_strdup_printf (const char *fmt, ...)
{ {
/* Guess we need no more than 100 bytes. */ /* Guess we need no more than 100 bytes. */
int n, size = 100; int n, size = 100;
@ -70,27 +71,27 @@ char *ck_strdup_printf (const char *fmt, ...)
p = emalloc (size); p = emalloc (size);
while (1) while (1) {
{ /* Try to print in the allocated space. */
/* Try to print in the allocated space. */ va_start (ap, fmt);
va_start(ap, fmt); n = vsnprintf (p, size, fmt, ap);
n = vsnprintf (p, size, fmt, ap); va_end (ap);
va_end(ap); /* If that worked, return the string. */
/* If that worked, return the string. */ if (n > -1 && n < size)
if (n > -1 && n < size) return p;
return p;
/* Else try again with more space. */ /* Else try again with more space. */
if (n > -1) /* C99 conform vsnprintf() */ if (n > -1) /* C99 conform vsnprintf() */
size = n+1; /* precisely what is needed */ size = n + 1; /* precisely what is needed */
else /* glibc 2.0 */ else /* glibc 2.0 */
size *= 2; /* twice the old size */ size *= 2; /* twice the old size */
p = erealloc (p, size); p = erealloc (p, size);
} }
} }
static const char *tr_type_str (TestResult *tr) static const char *
tr_type_str (TestResult * tr)
{ {
const char *str = NULL; const char *str = NULL;
if (tr->ctx == CK_CTX_TEST) { if (tr->ctx == CK_CTX_TEST) {
@ -106,13 +107,14 @@ static const char *tr_type_str (TestResult *tr)
return str; return str;
} }
static int percent_passed (TestStats *t) static int
percent_passed (TestStats * t)
{ {
if (t->n_failed == 0 && t->n_errors == 0) if (t->n_failed == 0 && t->n_errors == 0)
return 100; return 100;
else if (t->n_checked == 0) else if (t->n_checked == 0)
return 0; return 0;
else else
return (int) ( (float) (t->n_checked - (t->n_failed + t->n_errors)) / return (int) ((float) (t->n_checked - (t->n_failed + t->n_errors)) /
(float) t->n_checked * 100); (float) t->n_checked * 100);
} }