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