aboutsummaryrefslogtreecommitdiffstats
path: root/camel/tests/lib/camel-test.c
blob: b60ba1fb3454656d76ed52473a1b0de83cccd5dc (plain) (blame)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240

#include "camel-test.h"

#include <stdio.h>
#include <signal.h>

struct _stack {
    struct _stack *next;
    char *what;
};

static int setup;
static struct _stack *state;
static struct _stack *nonfatal;
static int ok;

int camel_test_verbose;

static void die(int sig)
{
    static indie = 0;
    struct _stack *node;

    if (!indie) {
        indie = 1;
        printf("\n\nReceived fatal signal %d\n", sig);
        node = state;
        if (node) {
            printf("Current action:\n");
            while (node) {
                printf("\t%s\n", node->what);
                node = node->next;
            }
        }
    }

    _exit(1);
}

void camel_test_init(int argc, char **argv)
{
    void camel_init(void);
    int i;

    setup = 1;

    camel_init();

    /* yeah, we do need ot thread init, even though camel isn't compiled with enable threads */
    g_thread_init(NULL);

    signal(SIGSEGV, die);
    signal(SIGABRT, die);

    /* default, just say what, how well we did, unless fail, then abort */
    camel_test_verbose = 1;

    for (i=0;i<argc;i++) {
        if (argv[i][0] == '-') {
            switch (argv[i][1]) {
            case 'v':
                camel_test_verbose = strlen(argv[i]);
                break;
            case 'q':
                camel_test_verbose = 0;
                break;
            }
        }
    }
}

void camel_test_start(const char *what)
{
    if (!setup)
        camel_test_init(0, 0);

    ok = 1;

    if (camel_test_verbose > 0) {
        printf("Test: %s ... ", what);
        fflush(stdout);
    }
}

void camel_test_push(const char *what, ...)
{
    struct _stack *node;
    va_list ap;
    char *text;

    va_start(ap, what);
    text = g_strdup_vprintf(what, ap);
    va_end(ap);

    if (camel_test_verbose > 3)
        printf("Start step: %s\n", text);

    node = g_malloc(sizeof(*node));
    node->what = text;
    node->next = state;
    state = node;
}

void camel_test_pull(void)
{
    struct _stack *node;

    g_assert(state);

    if (camel_test_verbose > 3)
        printf("Finish step: %s\n", state->what);

    node = state;
    state = node->next;
    g_free(node->what);
    g_free(node);
}

void camel_test_fail(const char *why, ...)
{
    struct _stack *node;
    va_list ap;
    char *text;

    va_start(ap, why);
    text = g_strdup_vprintf(why, ap);
    va_end(ap);

    if ((nonfatal == NULL && camel_test_verbose > 0)
        || (nonfatal && camel_test_verbose > 1)) {
        printf("Failed: %s\n", text);
    }

    g_free(text);

    if ((nonfatal == NULL && camel_test_verbose > 0)
        || (nonfatal && camel_test_verbose > 2)) {
        node = state;
        if (node) {
            printf("Current action:\n");
            while (node) {
                printf("\t%s\n", node->what);
                node = node->next;
            }
        }
    }

    if (nonfatal == NULL) {
        exit(1);
    } else {
        ok=0;
        if (camel_test_verbose > 1) {
            printf("Known problem (ignored): %s\n", nonfatal->what);
        }
    }
}

void camel_test_nonfatal(const char *why, ...)
{
    struct _stack *node;
    va_list ap;
    char *text;

    va_start(ap, why);
    text = g_strdup_vprintf(why, ap);
    va_end(ap);

    if (camel_test_verbose>3)
        printf("Start nonfatal: %s\n", text);

    node = g_malloc(sizeof(*node));
    node->what = text;
    node->next = nonfatal;
    nonfatal = node;
}

/* dont ask me why but the compiler just can't seem to find the prototypes for this */
void camel_test_fatal()
{
    struct _stack *node;

    g_assert(nonfatal);

    if (camel_test_verbose>3)
        printf("Finish nonfatal: %s\n", nonfatal->what);

    node = nonfatal;
    nonfatal = node->next;
    g_free(node->what);
    g_free(node);
}

void camel_test_end(void)
{
    if (camel_test_verbose > 0) {
        if (ok)
            printf("Ok\n");
        else
            printf("Partial success\n");
    }

    fflush(stdout);
}




/* compare strings, ignore whitespace though */
int string_equal(const char *a, const char *b)
{
    const char *ap, *bp;
    int cmp;

    ap = a;
    bp = b;

    while (*ap && *bp) {
        while (*ap == ' ' || *ap == '\n' || *ap == '\t')
            ap++;
        while (*bp == ' ' || *bp == '\n' || *bp == '\t')
            bp++;

        a = ap;
        b = bp;

        while (*ap && *ap != ' ' && *ap != '\n' && *ap != '\t')
            ap++;
        while (*bp && *bp != ' ' && *bp != '\n' && *bp != '\t')
            bp++;

        if (ap - a != bp - a
            && ap - 1 > 0
            && memcmp(a, b, ap-a) != 0) {
            return 0;
        }
    }

    return 1;
}