File: test.h

package info (click to toggle)
lexbor 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,484 kB
  • sloc: ansic: 764,896; python: 2,795; perl: 1,735; sh: 101; makefile: 71; cpp: 44
file content (410 lines) | stat: -rw-r--r-- 21,258 bytes parent folder | download
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * Copyright (C) 2018 Alexander Borisov
 *
 * Author: Alexander Borisov <borisov@lexbor.com>
 */

#ifndef TEST_H
#define TEST_H

#ifdef __cplusplus
extern "C" {
#endif

#include <string.h>

#include <lexbor/core/def.h>
#include <lexbor/core/array.h>
#include <lexbor/core/dobject.h>
#include <lexbor/core/array_obj.h>


#define TEST_PRINT(...)                                                        \
    do {                                                                       \
        printf(__VA_ARGS__);                                                   \
    }                                                                          \
    while (0)

#define TEST_PRINTLN(...)                                                      \
    do {                                                                       \
        printf(__VA_ARGS__);                                                   \
        printf("\n");                                                          \
    }                                                                          \
    while (0)


#define TEST_FAILURE(...)                                                      \
    do {                                                                       \
        fprintf(stderr, __VA_ARGS__);                                          \
        fprintf(stderr, "\n");                                                 \
        exit(EXIT_FAILURE);                                                    \
    }                                                                          \
    while (0)

#define test_call_error()                                                      \
    do {                                                                       \
        TEST_OBJ_NAME->bad++;                                                  \
        TEST_OBJ_NAME->error = true;                                           \
                                                                               \
        fprintf(stdout, "Failure\n%s:%d:%s\n",                                 \
        __FILE__, __LINE__, __func__);                                         \
        TEST_STACK_PRINT();                                                    \
        return NULL;                                                           \
    }                                                                          \
    while (0)


#define TEST_RETVAL return_val
#define TEST_RETVAL_ARG void *return_val

#define TEST_OBJ_NAME my_test
#define TEST_OBJ_ARG test_t *TEST_OBJ_NAME


#define TEST_STACK_CLEAN() lexbor_array_obj_clean(TEST_OBJ_NAME->ao_stack)


#define TEST_STACK_PUSH()                                                      \
    do {                                                                       \
        test_stack_t *stack = lexbor_array_obj_push(TEST_OBJ_NAME->ao_stack);  \
        if (stack == NULL) {                                                   \
            TEST_FAILURE("Failed to allocate memory");                         \
        }                                                                      \
                                                                               \
        stack->file = __FILE__;                                                \
        stack->func = __func__;                                                \
        stack->line = __LINE__;                                                \
    }                                                                          \
    while (0)


#define TEST_STACK_PRINT()                                                     \
    do {                                                                       \
        test_stack_t *stack;                                                   \
        size_t i = lexbor_array_obj_length(TEST_OBJ_NAME->ao_stack);           \
                                                                               \
        while (i != 0) {                                                       \
            i--;                                                               \
            stack = (test_stack_t *) lexbor_array_obj_get(TEST_OBJ_NAME->ao_stack, i); \
            TEST_PRINTLN("%s:%d:%s",stack->file, stack->line, stack->func);    \
        }                                                                      \
    }                                                                          \
    while (0)


#define TEST_STACK_POP()                                                       \
    do {                                                                       \
        lexbor_array_obj_pop(TEST_OBJ_NAME->ao_stack);                         \
    }                                                                          \
    while (0)


#define TEST_CALL(name)                                                        \
    do {                                                                       \
        TEST_STACK_PUSH();                                                     \
        TEST_RETVAL = name(TEST_OBJ_ARG);                                      \
        TEST_STACK_POP();                                                      \
                                                                               \
        if (TEST_OBJ_NAME->error) {                                            \
            return NULL;                                                       \
        }                                                                      \
    }                                                                          \
    while (0)


#define TEST_CALL_ARGS(name, ...)                                              \
    do {                                                                       \
        TEST_STACK_PUSH();                                                     \
        TEST_RETVAL = name(TEST_OBJ_NAME, __VA_ARGS__);                        \
        TEST_STACK_POP();                                                      \
                                                                               \
        if (TEST_OBJ_NAME->error) {                                            \
            return NULL;                                                       \
        }                                                                      \
    }                                                                          \
    while (0)


#define TEST_BEGIN_ARGS(name, ...)                                             \
void *                                                                         \
name(TEST_OBJ_ARG, __VA_ARGS__);                                               \
void *                                                                         \
name(TEST_OBJ_ARG, __VA_ARGS__)                                                \
{                                                                              \
    TEST_RETVAL_ARG = NULL;                                                    \
    TEST_OBJ_NAME->error = false;                                              \
    (void) TEST_RETVAL;                                                        \


#define TEST_BEGIN(name)                                                       \
void *                                                                         \
name(TEST_OBJ_ARG);                                                            \
void *                                                                         \
name(TEST_OBJ_ARG)                                                             \
{                                                                              \
    TEST_RETVAL_ARG = NULL;                                                    \
    TEST_OBJ_NAME->error = false;                                              \
    (void) TEST_RETVAL;                                                        \


#define TEST_END                                                               \
    return NULL;                                                               \
}

#define TEST_END_RETVAL(return_val)                                            \
    return return_val;                                                         \
}


#define TEST_INIT()                                                            \
    TEST_OBJ_ARG;                                                              \
    do {                                                                       \
        TEST_OBJ_NAME = test_create();                                         \
        if (test_init(TEST_OBJ_NAME) != LXB_STATUS_OK) {                       \
            TEST_FAILURE("Failed to create Test object");                      \
        }                                                                      \
    }                                                                          \
    while (0)


#define TEST_ADD(test_name)                                                    \
    do {                                                                       \
        if (test_add(TEST_OBJ_NAME, test_name,                                 \
                     (char *) LEXBOR_STRINGIZE(test_name))                     \
            != LXB_STATUS_OK)                                                  \
        {                                                                      \
            TEST_FAILURE("Failed to add test to list");                        \
        }                                                                      \
    }                                                                          \
    while (0)


#define TEST_RUN(name)                                                         \
    test_run(TEST_OBJ_NAME, (char *) name)


#define TEST_RELEASE()                                                         \
    do {                                                                       \
        bool is_success = test_is_success(TEST_OBJ_NAME);                      \
        test_destroy(TEST_OBJ_NAME, true);                                     \
        return (is_success) ? EXIT_SUCCESS : EXIT_FAILURE;                     \
    }                                                                          \
    while (0)


#define test_eq(have, need)                                                    \
    do {                                                                       \
        if ((have) != (need)) {                                                \
            TEST_OBJ_NAME->bad++;                                              \
            TEST_OBJ_NAME->error = true;                                       \
                                                                               \
            fprintf(stdout, "Failure\n%s:%d:%s\n",                             \
                    __FILE__, __LINE__, __func__);                             \
            TEST_STACK_PRINT();                                                \
            return NULL;                                                       \
        }                                                                      \
    }                                                                          \
    while (0)


#define test_eq_type(have, need, type, act)                                    \
    do {                                                                       \
        if ((have) act (need)) {                                               \
            TEST_OBJ_NAME->bad++;                                              \
            TEST_OBJ_NAME->error = true;                                       \
                                                                               \
            fprintf(stdout, "Received '" type "', "                            \
                    "but expected '" type "'\n%s:%d:%s\n",                     \
                    (have), (need), __FILE__, __LINE__, __func__);             \
            TEST_STACK_PRINT();                                                \
            return NULL;                                                       \
        }                                                                      \
    }                                                                          \
    while (0)


#define test_eq_int(have, need) test_eq_type(have, need, "%d", !=)
#define test_eq_u_int(have, need) test_eq_type(have, need, "%u", !=)

#define test_eq_long(have, need) test_eq_type(have, need, "%l", !=)
#define test_eq_u_long(have, need) test_eq_type(have, need, "%lu", !=)

#define test_eq_size(have, need) test_eq_type(((size_t) have),                \
                                              ((size_t) need),                \
                                              LEXBOR_FORMAT_Z, !=)

#define test_ne_size(have, need) test_eq_type(((size_t) have),                \
                                              ((size_t) need),                \
                                              LEXBOR_FORMAT_Z, ==)

#define test_eq_double(have, need) test_eq_type(have, need, "%f", !=)
#define test_eq_float(have, need) test_eq_double((double) have, (double) need)

#define test_eq_short(have, need) test_eq_type((int) have, (int) need, "%d", !=)

#define test_eq_u_short(have, need)                                            \
    test_eq_type((unsigned int) have, (unsigned int) need, "%d", !=)

#define test_eq_char(have, need) test_eq_type(have, need, "%c", !=)
#define test_eq_u_char(have, need) test_eq_type(have, need, "%c", !=)

#define test_eq_bool(have, need)                                               \
    do {                                                                       \
        if ((have) != (need)) {                                                \
            TEST_OBJ_NAME->bad++;                                              \
            TEST_OBJ_NAME->error = true;                                       \
                                                                               \
            fprintf(stdout, "Received %s, "                                    \
                    "but expected %s\n%s:%d:%s\n",                             \
                    ((have) ? "True" : "False"), ((need) ? "True" : "False"),  \
                    __FILE__, __LINE__, __func__);                             \
            TEST_STACK_PRINT();                                                \
            return NULL;                                                       \
        }                                                                      \
    }                                                                          \
    while (0)

#define test_eq_str_n(have, hlen, need, nlen)                                  \
    do {                                                                       \
        if (hlen != nlen || memcmp((need), (have), (hlen)) != 0) {             \
            TEST_OBJ_NAME->bad++;                                              \
            TEST_OBJ_NAME->error = true;                                       \
                                                                               \
            fprintf(stdout, "Received:\n%.*s\n"                                \
                    "Expected:\n%.*s\n%s:%d:%s\n",                             \
                    (int) (hlen), (have), (int) (nlen), (need),                \
                    __FILE__, __LINE__, __func__);                             \
            TEST_STACK_PRINT();                                                \
            return NULL;                                                       \
        }                                                                      \
    }                                                                          \
    while (0)

#define test_ne_str_n(have, hlen, need, nlen)                                  \
    do {                                                                       \
        if (hlen == nlen && memcmp((need), (have), (hlen)) == 0) {             \
            TEST_OBJ_NAME->bad++;                                              \
            TEST_OBJ_NAME->error = true;                                       \
                                                                               \
            fprintf(stdout, "Received:\n%.*s\n"                                \
                    "Expected:\n%.*s\n%s:%d:%s\n",                             \
                    (int) (hlen), (char *) (have),                             \
                    (int) (nlen), (char *) (need),                             \
                    __FILE__, __LINE__, __func__);                             \
            TEST_STACK_PRINT();                                                \
            return NULL;                                                       \
        }                                                                      \
    }                                                                          \
    while (0)

#define test_eq_str(have, need)                                                \
    do {                                                                       \
        size_t hlen = strlen((const char *) (have));                           \
        size_t nlen = strlen((const char *) (need));                           \
        test_eq_str_n(have, hlen, need, nlen);                                 \
    }                                                                          \
    while (0)

#define test_eq_u_str_n(have, hlen, need, nlen)                                \
    test_eq_str_n(have, hlen, need, nlen)

#define test_eq_u_str(have, need)                                              \
    test_eq_str(have, need)

#define test_ne_str(have, need)                                                \
    do {                                                                       \
        size_t hlen = strlen((const char *) (have));                           \
        size_t nlen = strlen((const char *) (need));                           \
        test_ne_str_n(have, hlen, need, nlen);                                 \
    }                                                                          \
    while (0)

#define test_ne_u_str_n(have, hlen, need, nlen)                                \
    test_eq_str_n(have, hlen, need, nlen)

#define test_ne_u_str(have, need)                                              \
    test_ne_str(have, need)


#define test_ne(have, need)                                                    \
    do {                                                                       \
        if ((have) == (need)) {                                                \
            TEST_OBJ_NAME->bad++;                                              \
            TEST_OBJ_NAME->error = true;                                       \
                                                                               \
            fprintf(stdout, "Failure\n%s:%d:%s\n",                             \
                    __FILE__, __LINE__, __func__);                             \
            return NULL;                                                       \
        }                                                                      \
    }                                                                          \
    while (0)

/* > */
#define test_gt(have, need)                                                    \
    do {                                                                       \
        if ((have) < (need)) {                                                 \
            TEST_OBJ_NAME->bad++;                                              \
            TEST_OBJ_NAME->error = true;                                       \
                                                                               \
            fprintf(stdout, "Failure\n%s:%d:%s\n",                             \
                    __FILE__, __LINE__, __func__);                             \
            return NULL;                                                       \
        }                                                                      \
    }                                                                          \
    while (0)


typedef struct {
    lexbor_array_t     *list;
    lexbor_dobject_t   *entries;
    lexbor_array_obj_t *ao_stack;

    size_t             bad;
    bool               error;
}
test_t;

typedef void * (*test_func_t)(test_t *test);

typedef struct {
    test_func_t func;
    char        *name;
}
test_entry_t;

typedef struct {
    const char *file;
    const char *func;
    int        line;
}
test_stack_t;


LXB_API test_t *
test_create(void);

LXB_API lxb_status_t
test_init(test_t *test);

LXB_API void
test_clean(test_t *test);

LXB_API test_t *
test_destroy(test_t *test, bool self_destroy);


LXB_API lxb_status_t
test_add(test_t *test, test_func_t test_func, char *test_name);

LXB_API void
test_run(test_t *test, char *name);

LXB_API bool
test_is_success(test_t *test);


#ifdef __cplusplus
} /* extern "C" */
#endif

#endif /* TEST_H */