check for double test registration
[protos/libecoli.git] / lib / ecoli_test.c
1 /*
2  * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <errno.h>
33
34 #include <ecoli_log.h>
35 #include <ecoli_malloc.h>
36 #include <ecoli_test.h>
37 #include <ecoli_strvec.h>
38 #include <ecoli_node.h>
39 #include <ecoli_parsed.h>
40 #include <ecoli_completed.h>
41 #include <ecoli_parsed.h>
42
43 static struct ec_test_list test_list = TAILQ_HEAD_INITIALIZER(test_list);
44
45 EC_LOG_TYPE_REGISTER(test);
46
47 static struct ec_test *ec_test_lookup(const char *name)
48 {
49         struct ec_test *test;
50
51         TAILQ_FOREACH(test, &test_list, next) {
52                 if (!strcmp(name, test->name))
53                         return test;
54         }
55
56         errno = EEXIST;
57         return NULL;
58 }
59
60 int ec_test_register(struct ec_test *test)
61 {
62         if (ec_test_lookup(test->name) != NULL)
63                 return -1;
64
65         TAILQ_INSERT_TAIL(&test_list, test, next);
66
67         return 0;
68 }
69
70 int ec_test_check_parse(struct ec_node *tk, int expected, ...)
71 {
72         struct ec_parsed *p;
73         struct ec_strvec *vec = NULL;
74         const char *s;
75         int ret = -1, match;
76         va_list ap;
77
78         va_start(ap, expected);
79
80         /* build a string vector */
81         vec = ec_strvec();
82         if (vec == NULL)
83                 goto out;
84
85         for (s = va_arg(ap, const char *);
86              s != EC_NODE_ENDLIST;
87              s = va_arg(ap, const char *)) {
88                 if (s == NULL)
89                         goto out;
90
91                 if (ec_strvec_add(vec, s) < 0)
92                         goto out;
93         }
94
95         p = ec_node_parse_strvec(tk, vec);
96         if (p == NULL) {
97                 EC_LOG(EC_LOG_ERR, "parsed is NULL\n");
98         }
99         if (ec_parsed_matches(p))
100                 match = ec_parsed_len(p);
101         else
102                 match = -1;
103         if (expected == match) {
104                 ret = 0;
105         } else {
106                 EC_LOG(EC_LOG_ERR,
107                         "tk parsed len (%d) does not match expected (%d)\n",
108                         match, expected);
109         }
110
111         ec_parsed_free(p);
112
113 out:
114         ec_strvec_free(vec);
115         va_end(ap);
116         return ret;
117 }
118
119 int ec_test_check_complete(struct ec_node *tk, enum ec_completed_type type, ...)
120 {
121         struct ec_completed *c = NULL;
122         struct ec_strvec *vec = NULL;
123         const char *s;
124         int ret = 0;
125         unsigned int count = 0;
126         va_list ap;
127
128         va_start(ap, type);
129
130         /* build a string vector */
131         vec = ec_strvec();
132         if (vec == NULL)
133                 goto out;
134
135         for (s = va_arg(ap, const char *);
136              s != EC_NODE_ENDLIST;
137              s = va_arg(ap, const char *)) {
138                 if (s == NULL)
139                         goto out;
140
141                 if (ec_strvec_add(vec, s) < 0)
142                         goto out;
143         }
144
145         c = ec_node_complete_strvec(tk, vec);
146         if (c == NULL) {
147                 ret = -1;
148                 goto out;
149         }
150
151         /* for each expected completion, check it is there */
152         for (s = va_arg(ap, const char *);
153              s != EC_NODE_ENDLIST;
154              s = va_arg(ap, const char *)) {
155                 struct ec_completed_iter *iter;
156                 const struct ec_completed_item *item;
157
158                 if (s == NULL) {
159                         ret = -1;
160                         goto out;
161                 }
162
163                 count++;
164
165                 /* only check matching completions */
166                 iter = ec_completed_iter(c, type);
167                 while ((item = ec_completed_iter_next(iter)) != NULL) {
168                         const char *str = ec_completed_item_get_str(item);
169                         if (str != NULL && strcmp(str, s) == 0)
170                                 break;
171                 }
172
173                 if (item == NULL) {
174                         EC_LOG(EC_LOG_ERR,
175                                 "completion <%s> not in list\n", s);
176                         ret = -1;
177                 }
178                 ec_completed_iter_free(iter);
179         }
180
181         /* check if we have more completions (or less) than expected */
182         if (count != ec_completed_count(c, type)) {
183                 EC_LOG(EC_LOG_ERR,
184                         "nb_completion (%d) does not match (%d)\n",
185                         count, ec_completed_count(c, type));
186                 ec_completed_dump(stdout, c);
187                 ret = -1;
188         }
189
190 out:
191         ec_strvec_free(vec);
192         ec_completed_free(c);
193         va_end(ap);
194         return ret;
195 }
196
197 static int launch_test(const char *name)
198 {
199         struct ec_test *test;
200         int ret = 0;
201         unsigned int count = 0;
202
203         TAILQ_FOREACH(test, &test_list, next) {
204                 if (name != NULL && strcmp(name, test->name))
205                         continue;
206
207                 EC_LOG(EC_LOG_INFO, "== starting test %-20s\n",
208                         test->name);
209
210                 count++;
211                 if (test->test() == 0) {
212                         EC_LOG(EC_LOG_INFO,
213                                 "== test %-20s success\n",
214                                 test->name);
215                 } else {
216                         EC_LOG(EC_LOG_INFO,
217                                 "== test %-20s failed\n",
218                                 test->name);
219                         ret = -1;
220                 }
221         }
222
223         if (name != NULL && count == 0) {
224                 EC_LOG(EC_LOG_WARNING,
225                         "== test %s not found\n", name);
226                 ret = -1;
227         }
228
229         return ret;
230 }
231
232 int ec_test_all(void)
233 {
234         return launch_test(NULL);
235 }
236
237 int ec_test_one(const char *name)
238 {
239         return launch_test(name);
240 }