94cd0b9ba78a93ac4d716ef8f639d0af4e4418ca
[protos/libecoli.git] / include / ecoli_test.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #ifndef ECOLI_TEST_
6 #define ECOLI_TEST_
7
8 #include <sys/queue.h>
9
10 #include <ecoli_log.h>
11
12 struct ec_node;
13 enum ec_comp_type;
14
15 #define EC_TEST_REGISTER(t)                                             \
16         static void ec_test_init_##t(void);                             \
17         static void __attribute__((constructor, used))                  \
18         ec_test_init_##t(void)                                          \
19         {                                                               \
20                 if (ec_test_register(&t) < 0)                           \
21                         fprintf(stderr, "cannot register test %s\n",    \
22                                 t.name);                                \
23         }
24
25 /**
26  * Type of test function. Return 0 on success, -1 on error.
27  */
28 typedef int (ec_test_t)(void);
29
30 TAILQ_HEAD(ec_test_list, ec_test);
31
32 /**
33  * A structure describing a test case.
34  */
35 struct ec_test {
36         TAILQ_ENTRY(ec_test) next;  /**< Next in list. */
37         const char *name;           /**< Test name. */
38         ec_test_t *test;            /**< Test function. */
39 };
40
41 /**
42  * Register a test case.
43  *
44  * @param test
45  *   A pointer to a ec_test structure describing the test
46  *   to be registered.
47  * @return
48  *   0 on success, -1 on error (errno is set).
49  */
50 int ec_test_register(struct ec_test *test);
51
52 int ec_test_all(void);
53 int ec_test_one(const char *name);
54
55 /* expected == -1 means no match */
56 int ec_test_check_parse(struct ec_node *node, int expected, ...);
57
58 #define EC_TEST_ERR(fmt, ...)                                           \
59         EC_LOG(EC_LOG_ERR, "%s:%d: error: " fmt "\n",                   \
60                 __FILE__, __LINE__, ##__VA_ARGS__);                     \
61
62 #define EC_TEST_CHECK(cond, fmt, ...) ({                                \
63         int ret_ = 0;                                                   \
64         if (!(cond)) {                                                  \
65                 EC_TEST_ERR("(" #cond ") is wrong. " fmt                \
66                         ##__VA_ARGS__);                                 \
67                 ret_ = -1;                                              \
68         }                                                               \
69         ret_;                                                           \
70 })
71
72 /* node, input, [expected1, expected2, ...] */
73 #define EC_TEST_CHECK_PARSE(node, args...) ({                           \
74         int ret_ = ec_test_check_parse(node, args, EC_NODE_ENDLIST);    \
75         if (ret_)                                                       \
76                 EC_TEST_ERR("parse test failed");                       \
77         ret_;                                                           \
78 })
79
80 int ec_test_check_complete(struct ec_node *node,
81                         enum ec_comp_type type, ...);
82
83 #define EC_TEST_CHECK_COMPLETE(node, args...) ({                        \
84         int ret_ = ec_test_check_complete(node, EC_COMP_FULL, args);    \
85         if (ret_)                                                       \
86                 EC_TEST_ERR("complete test failed");                    \
87         ret_;                                                           \
88 })
89
90 #define EC_TEST_CHECK_COMPLETE_PARTIAL(node, args...) ({                \
91         int ret_ = ec_test_check_complete(node, EC_COMP_PARTIAL, args); \
92         if (ret_)                                                       \
93                 EC_TEST_ERR("complete test failed");                    \
94         ret_;                                                           \
95 })
96
97 #endif