070d99b5d65f27ae441345271ccbb2c90615d052
[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
33 #include <ecoli_log.h>
34 #include <ecoli_malloc.h>
35 #include <ecoli_test.h>
36 #include <ecoli_strvec.h>
37 #include <ecoli_node.h>
38 #include <ecoli_parsed.h>
39 #include <ecoli_completed.h>
40 #include <ecoli_parsed.h>
41
42 static struct ec_test_list test_list = TAILQ_HEAD_INITIALIZER(test_list);
43
44 /* register a driver */
45 void ec_test_register(struct ec_test *test)
46 {
47         TAILQ_INSERT_TAIL(&test_list, test, next);
48         // XXX check if already exist, like for type
49 }
50
51 int ec_test_check_parse(struct ec_node *tk, int expected, ...)
52 {
53         struct ec_parsed *p;
54         struct ec_strvec *vec = NULL;
55         const char *s;
56         int ret = -1, match;
57         va_list ap;
58
59         va_start(ap, expected);
60
61         /* build a string vector */
62         vec = ec_strvec();
63         if (vec == NULL)
64                 goto out;
65
66         for (s = va_arg(ap, const char *);
67              s != EC_NODE_ENDLIST;
68              s = va_arg(ap, const char *)) {
69                 if (s == NULL)
70                         goto out;
71
72                 if (ec_strvec_add(vec, s) < 0)
73                         goto out;
74         }
75
76         p = ec_node_parse_strvec(tk, vec);
77         ec_parsed_dump(stdout, p); /* XXX only for debug */
78         if (p == NULL) {
79                 ec_log(EC_LOG_ERR, "parsed is NULL\n");
80         }
81         if (ec_parsed_matches(p))
82                 match = ec_parsed_len(p);
83         else
84                 match = -1;
85         if (expected == match) {
86                 ret = 0;
87         } else {
88                 ec_log(EC_LOG_ERR,
89                         "tk parsed len (%d) does not match expected (%d)\n",
90                         match, expected);
91         }
92
93         ec_parsed_free(p);
94
95 out:
96         ec_strvec_free(vec);
97         va_end(ap);
98         return ret;
99 }
100
101 int ec_test_check_complete(struct ec_node *tk, ...)
102 {
103         struct ec_completed *c = NULL;
104         struct ec_strvec *vec = NULL;
105         const char *expected, *s;
106         char *smallest_start = NULL;
107         int ret = 0;
108         unsigned int count = 0;
109         va_list ap;
110
111         va_start(ap, tk);
112
113         /* build a string vector */
114         vec = ec_strvec();
115         if (vec == NULL)
116                 goto out;
117
118         for (s = va_arg(ap, const char *);
119              s != EC_NODE_ENDLIST;
120              s = va_arg(ap, const char *)) {
121                 if (s == NULL)
122                         goto out;
123
124                 if (ec_strvec_add(vec, s) < 0)
125                         goto out;
126         }
127
128         c = ec_node_complete_strvec(tk, vec);
129         if (c == NULL) {
130                 ret = -1;
131                 goto out;
132         }
133
134         /* for each expected completion, check it is there */
135         for (s = va_arg(ap, const char *);
136              s != EC_NODE_ENDLIST;
137              s = va_arg(ap, const char *)) {
138                 struct ec_completed_iter *iter;
139                 const struct ec_completed_item *item;
140
141                 if (s == NULL) {
142                         ret = -1;
143                         goto out;
144                 }
145
146                 count++;
147
148                 /* only check matching completions */
149                 iter = ec_completed_iter(c, EC_MATCH);
150                 while ((item = ec_completed_iter_next(iter)) != NULL) {
151                         if (item->add != NULL && strcmp(item->add, s) == 0)
152                                 break;
153                 }
154
155                 if (item == NULL) {
156                         ec_log(EC_LOG_ERR,
157                                 "completion <%s> not in list\n", s);
158                         ret = -1;
159                 }
160                 ec_completed_iter_free(iter);
161         }
162
163         /* check if we have more completions (or less) than expected */
164         if (count != ec_completed_count(c, EC_MATCH)) {
165                 ec_log(EC_LOG_ERR,
166                         "nb_completion (%d) does not match (%d)\n",
167                         count, ec_completed_count(c, EC_MATCH));
168                 ec_completed_dump(stdout, c);
169                 ret = -1;
170         } else
171                 ec_completed_dump(stdout, c); //XXX
172
173         /* check the expected smallest start */
174         expected = va_arg(ap, const char *);
175         smallest_start = ec_completed_smallest_start(c);
176         if (smallest_start == NULL)
177                 goto out;
178         if (strcmp(smallest_start, expected)) {
179                 ret = -1;
180                 ec_log(EC_LOG_ERR,
181                         "should complete with <%s> but completes with <%s>\n",
182                         expected, s);
183         }
184
185 out:
186         ec_strvec_free(vec);
187         ec_completed_free(c);
188         ec_free(smallest_start);
189         va_end(ap);
190         return ret;
191 }
192
193 static int launch_test(const char *name)
194 {
195         struct ec_test *test;
196         int ret = 0;
197         unsigned int count = 0;
198
199         TAILQ_FOREACH(test, &test_list, next) {
200                 if (name != NULL && strcmp(name, test->name))
201                         continue;
202
203                 ec_log(EC_LOG_INFO, "== starting test %-20s\n", test->name);
204
205                 count++;
206                 if (test->test() == 0) {
207                         ec_log(EC_LOG_INFO, "== test %-20s success\n",
208                                 test->name);
209                 } else {
210                         ec_log(EC_LOG_INFO, "== test %-20s failed\n",
211                                 test->name);
212                         ret = -1;
213                 }
214         }
215
216         if (name != NULL && count == 0) {
217                 ec_log(EC_LOG_WARNING, "== test %s not found\n", name);
218                 ret = -1;
219         }
220
221         return ret;
222 }
223
224 int ec_test_all(void)
225 {
226         return launch_test(NULL);
227 }
228
229 int ec_test_one(const char *name)
230 {
231         return launch_test(name);
232 }