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