ee67cbf855535c79b799b7e829e18be14d82c312
[protos/libecoli.git] / lib / ecoli_node_expr_test.c
1 /*
2  * Copyright (c) 2016-2017, 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 <errno.h>
29 #include <limits.h>
30 #include <stdint.h>
31 #include <assert.h>
32
33 #include <ecoli_malloc.h>
34 #include <ecoli_strvec.h>
35 #include <ecoli_test.h>
36 #include <ecoli_node.h>
37 #include <ecoli_parsed.h>
38 #include <ecoli_node_int.h>
39 #include <ecoli_node_str.h>
40 #include <ecoli_node_re_lex.h>
41 #include <ecoli_node_expr.h>
42
43 EC_LOG_TYPE_REGISTER(node_expr);
44
45 struct my_eval_result {
46         int val;
47 };
48
49 static int
50 ec_node_expr_test_eval_var(void **result, void *userctx,
51         const struct ec_parsed *var)
52 {
53         const struct ec_strvec *vec;
54         const struct ec_node *node;
55         struct my_eval_result *eval = NULL;
56         int64_t val;
57
58         (void)userctx;
59
60         /* get parsed string vector, it should contain only one str */
61         vec = ec_parsed_strvec(var);
62         if (ec_strvec_len(vec) != 1)
63                 return -EINVAL;
64
65         node = ec_parsed_get_node(var);
66         if (ec_node_int_getval(node, ec_strvec_val(vec, 0), &val) < 0)
67                 return -EINVAL;
68
69         eval = ec_malloc(sizeof(*eval));
70         if (eval == NULL)
71                 return -ENOMEM;
72
73         eval->val = val;
74         printf("eval var %d\n", eval->val);
75         *result = eval;
76
77         return 0;
78 }
79
80 static int
81 ec_node_expr_test_eval_pre_op(void **result, void *userctx, void *operand,
82         const struct ec_parsed *operator)
83 {
84         const struct ec_strvec *vec;
85         struct my_eval_result *eval = operand;;
86
87         (void)userctx;
88
89         /* get parsed string vector, it should contain only one str */
90         vec = ec_parsed_strvec(operator);
91         if (ec_strvec_len(vec) != 1)
92                 return -EINVAL;
93
94         if (!strcmp(ec_strvec_val(vec, 0), "!"))
95                 eval->val = !eval->val;
96         else
97                 return -EINVAL;
98
99         printf("eval pre_op %d\n", eval->val);
100         *result = eval;
101
102         return 0;
103 }
104
105 static int
106 ec_node_expr_test_eval_post_op(void **result, void *userctx, void *operand,
107         const struct ec_parsed *operator)
108 {
109         const struct ec_strvec *vec;
110         struct my_eval_result *eval = operand;;
111
112         (void)userctx;
113
114         /* get parsed string vector, it should contain only one str */
115         vec = ec_parsed_strvec(operator);
116         if (ec_strvec_len(vec) != 1)
117                 return -EINVAL;
118
119         if (!strcmp(ec_strvec_val(vec, 0), "^"))
120                 eval->val = eval->val * eval->val;
121         else
122                 return -EINVAL;
123
124         printf("eval post_op %d\n", eval->val);
125         *result = eval;
126
127         return 0;
128 }
129
130 static int
131 ec_node_expr_test_eval_bin_op(void **result, void *userctx, void *operand1,
132         const struct ec_parsed *operator, void *operand2)
133
134 {
135         const struct ec_strvec *vec;
136         struct my_eval_result *eval1 = operand1;;
137         struct my_eval_result *eval2 = operand2;;
138
139         (void)userctx;
140
141         /* get parsed string vector, it should contain only one str */
142         vec = ec_parsed_strvec(operator);
143         if (ec_strvec_len(vec) != 1)
144                 return -EINVAL;
145
146         if (!strcmp(ec_strvec_val(vec, 0), "+"))
147                 eval1->val = eval1->val + eval2->val;
148         else if (!strcmp(ec_strvec_val(vec, 0), "*"))
149                 eval1->val = eval1->val * eval2->val;
150         else
151                 return -EINVAL;
152
153         printf("eval bin_op %d\n", eval1->val);
154         ec_free(eval2);
155         *result = eval1;
156
157         return 0;
158 }
159
160 static int
161 ec_node_expr_test_eval_parenthesis(void **result, void *userctx,
162         const struct ec_parsed *open_paren,
163         const struct ec_parsed *close_paren,
164         void *value)
165 {
166         (void)userctx;
167         (void)open_paren;
168         (void)close_paren;
169
170         printf("eval paren\n");
171         *result = value;
172
173         return 0;
174 }
175
176 static void
177 ec_node_expr_test_eval_free(void *result, void *userctx)
178 {
179         (void)userctx;
180         ec_free(result);
181 }
182
183 static const struct ec_node_expr_eval_ops test_ops = {
184         .eval_var = ec_node_expr_test_eval_var,
185         .eval_pre_op = ec_node_expr_test_eval_pre_op,
186         .eval_post_op = ec_node_expr_test_eval_post_op,
187         .eval_bin_op = ec_node_expr_test_eval_bin_op,
188         .eval_parenthesis = ec_node_expr_test_eval_parenthesis,
189         .eval_free = ec_node_expr_test_eval_free,
190 };
191
192 static int ec_node_expr_test_eval(struct ec_node *lex_node,
193         const struct ec_node *expr_node,
194         const char *str, int val)
195 {
196         struct ec_parsed *p;
197         void *result;
198         struct my_eval_result *eval;
199         int ret;
200
201         p = ec_node_parse(lex_node, str);
202         if (p == NULL)
203                 return -1;
204
205         ret = ec_node_expr_eval(&result, expr_node, p, &test_ops, NULL);
206         ec_parsed_free(p);
207         if (ret < 0)
208                 return -1;
209
210         /* the parsed value is an integer */
211         eval = result;
212         assert(eval != NULL);
213
214         printf("result: %d (expected %d)\n", eval->val, val);
215         if (eval->val == val)
216                 ret = 0;
217         else
218                 ret = -1;
219
220         ec_free(eval);
221
222         return ret;
223 }
224
225 /* LCOV_EXCL_START */
226 static int ec_node_expr_testcase(void)
227 {
228         struct ec_node *node = NULL, *lex_node = NULL;
229         int ret = 0;
230
231         node = ec_node("expr", "my_expr");
232         if (node == NULL)
233                 return -1;
234
235         ec_node_expr_set_val_node(node, ec_node_int(EC_NO_ID, 0, UCHAR_MAX, 0));
236         ec_node_expr_add_bin_op(node, ec_node_str(EC_NO_ID, "+"));
237         ec_node_expr_add_bin_op(node, ec_node_str(EC_NO_ID, "*"));
238         ec_node_expr_add_pre_op(node, ec_node_str(EC_NO_ID, "!"));  /* not */
239         ec_node_expr_add_post_op(node, ec_node_str(EC_NO_ID, "^")); /* square */
240         ec_node_expr_add_parenthesis(node, ec_node_str(EC_NO_ID, "("),
241                 ec_node_str(EC_NO_ID, ")"));
242         ret |= EC_TEST_CHECK_PARSE(node, 1, "1");
243         ret |= EC_TEST_CHECK_PARSE(node, 1, "1", "1");
244         ret |= EC_TEST_CHECK_PARSE(node, 1, "1", "*");
245         ret |= EC_TEST_CHECK_PARSE(node, 3, "1", "*", "1");
246         ret |= EC_TEST_CHECK_PARSE(node, 3, "1", "*", "1", "*");
247         ret |= EC_TEST_CHECK_PARSE(node, 4, "1", "+", "!", "1");
248         ret |= EC_TEST_CHECK_PARSE(node, 4, "1", "^", "+", "1");
249         ret |= EC_TEST_CHECK_PARSE(node, 5, "1", "*", "1", "*", "1");
250         ret |= EC_TEST_CHECK_PARSE(node, 5, "1", "*", "1", "+", "1");
251         ret |= EC_TEST_CHECK_PARSE(node, 7, "1", "*", "1", "*", "1", "*", "1");
252         ret |= EC_TEST_CHECK_PARSE(
253                 node, 10, "!", "(", "1", "*", "(", "1", "+", "1", ")", ")");
254         ret |= EC_TEST_CHECK_PARSE(node, 5, "1", "+", "!", "1", "^");
255
256         /* prepend a lexer to the expression node */
257         lex_node = ec_node_re_lex(EC_NO_ID, ec_node_clone(node));
258         if (lex_node == NULL)
259                 goto fail;
260
261         ret |= ec_node_re_lex_add(lex_node, "[0-9]+", 1); /* vars */
262         ret |= ec_node_re_lex_add(lex_node, "[+*!^()]", 1); /* operators */
263         ret |= ec_node_re_lex_add(lex_node, "[  ]+", 0); /* spaces */
264
265         /* valid expressions */
266         ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "!1");
267         ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "1^");
268         ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "1^ + 1");
269         ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "1 + 4 * (2 + 3^)^");
270         ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "(1)");
271         ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "3*!3+!3*(2+ 2)");
272         ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "!!(!1)^ + !(4 + (2*3))");
273         ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "(1 + 1)^ * 1^");
274
275         /* invalid expressions */
276         ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "");
277         ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "()");
278         ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "(");
279         ret |= EC_TEST_CHECK_PARSE(lex_node, -1, ")");
280         ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "+1");
281         ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+");
282         ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+*1");
283         ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+(1*1");
284         ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+!1!1)");
285
286         ret |= ec_node_expr_test_eval(lex_node, node, "1^", 1);
287         ret |= ec_node_expr_test_eval(lex_node, node, "2^", 4);
288         ret |= ec_node_expr_test_eval(lex_node, node, "!1", 0);
289         ret |= ec_node_expr_test_eval(lex_node, node, "!0", 1);
290
291         ret |= ec_node_expr_test_eval(lex_node, node, "1+1", 2);
292         ret |= ec_node_expr_test_eval(lex_node, node, "1+2+3", 6);
293         ret |= ec_node_expr_test_eval(lex_node, node, "1+1*2", 4);
294         ret |= ec_node_expr_test_eval(lex_node, node, "2 * 2^", 8);
295         ret |= ec_node_expr_test_eval(lex_node, node, "(1 + !0)^ * !0^", 4);
296         ret |= ec_node_expr_test_eval(lex_node, node, "(1 + !1) * 3", 3);
297
298         ec_node_free(node);
299         ec_node_free(lex_node);
300
301         return ret;
302
303 fail:
304         ec_node_free(lex_node);
305         ec_node_free(node);
306         return -1;
307 }
308 /* LCOV_EXCL_STOP */
309
310 static struct ec_test ec_node_expr_test = {
311         .name = "expr",
312         .test = ec_node_expr_testcase,
313 };
314
315 EC_TEST_REGISTER(ec_node_expr_test);