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