summaryrefslogtreecommitdiff
path: root/cesar/lib/src/test.c
blob: 13a14f3b19230ed2de44a07fe8ad057d0c4e01b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \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 <stdio.h>
#include <stdarg.h>
#include <signal.h>

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);
}