/* Cesar project {{{ * * Copyright (C) 2007 Spidcom * * <<>> * * }}} */ /** * \file lib/src/test.c * \brief Test infrastructure. * \ingroup lib * * For the moment, only stdio implementation. */ #include "common/std.h" #include "lib/test.h" #include #include #include void test_sig_handler (int sig) { signal (sig, test_sig_handler); dbg_fatal ("Caught signal %d", sig); } void test_init (test_t t, int argc, char **argv) { int i; test_init_basic (t, 2); /* Parse command line. */ for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { const char *s = argv[i] + 1; while (*s) { if (*s == 'v') t->verbose++; else if (*s == 'q') t->verbose = 0; s++; } } } /* Install signal handler. */ signal (SIGBUS, test_sig_handler); signal (SIGILL, test_sig_handler); signal (SIGFPE, test_sig_handler); signal (SIGSEGV, test_sig_handler); } void test_init_basic (test_t t, uint verbose) { t->current_test_suite = NULL; t->current_test_case = NULL; t->current_test = NULL; t->test_nb = 0; t->fail_nb = 0; t->verbose = verbose; } void test_result (test_t t) { int percent; if (t->verbose >= 1) { percent = t->test_nb == 0 ? 100 : 100 * (t->test_nb - t->fail_nb) / t->test_nb; fprintf (stderr, "%d%%, tests: %d, failures: %d\n", percent, t->test_nb, t->fail_nb); } } void test_suite_begin (test_t t, const char *name) { t->current_test_suite = name; if (t->verbose >= 2) fprintf (stderr, "running suite: %s\n", name); } void test_case_begin (test_t t, const char *name) { t->current_test_case = name; } void test_failled (void) { } void test_format_ (test_t t, const char *file, int line, char type, const char *ufmt, const char *fmt, ...) { if (type == 'F') test_failled (); if (t->verbose >= 4 || (t->verbose >= 3 && type == 'P') || (t->verbose >= 2 && type == 'F')) { fprintf (stderr, "%s:%d:%c:%s:%s: ", file, line, type, t->current_test_case ? t->current_test_case : "unknown", t->current_test); if (fmt) { va_list ap; va_start (ap, fmt); vfprintf (stderr, fmt, ap); va_end (ap); fputc ('\n', stderr); } else { fprintf (stderr, "%s\n", ufmt); } } } void test_debug_print (const char *msg, ...) { va_list ap; va_start (ap, msg); vfprintf (stderr, msg, ap); va_end (ap); fputc ('\n', stderr); }