4a00cdf7d6ca89591bd94071cf7978768a869883
[protos/libecoli.git] / lib / ecoli_tk_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_tk_int.h>
37 #include <ecoli_tk_str.h>
38 #include <ecoli_tk_re_lex.h>
39 #include <ecoli_tk_expr.h>
40
41 struct my_eval_result {
42         int val;
43 };
44
45 static int
46 ec_tk_expr_test_eval_var(void **result, void *userctx,
47         const struct ec_parsed_tk *var)
48 {
49         const struct ec_strvec *vec;
50         struct my_eval_result *eval;
51
52         (void)userctx;
53
54         /* get parsed string vector, it should contain only one str */
55         vec = ec_parsed_tk_strvec(var);
56         if (ec_strvec_len(vec) != 1)
57                 return -EINVAL;
58
59         eval = ec_malloc(sizeof(*eval));
60         if (eval == NULL)
61                 return -ENOMEM;
62
63         eval->val = atoi(ec_strvec_val(vec, 0)); // XXX use strtol
64         printf("eval var %d\n", eval->val);
65         *result = eval;
66
67         return 0;
68 }
69
70 static int
71 ec_tk_expr_test_eval_pre_op(void **result, void *userctx, void *operand,
72         const struct ec_parsed_tk *operator)
73 {
74         const struct ec_strvec *vec;
75         struct my_eval_result *eval = operand;;
76
77         (void)userctx;
78
79         /* get parsed string vector, it should contain only one str */
80         vec = ec_parsed_tk_strvec(operator);
81         if (ec_strvec_len(vec) != 1)
82                 return -EINVAL;
83
84         if (!strcmp(ec_strvec_val(vec, 0), "!"))
85                 eval->val = !eval->val;
86         else
87                 return -EINVAL;
88
89         printf("eval pre_op %d\n", eval->val);
90         *result = eval;
91
92         return 0;
93 }
94
95 static int
96 ec_tk_expr_test_eval_post_op(void **result, void *userctx, void *operand,
97         const struct ec_parsed_tk *operator)
98 {
99         const struct ec_strvec *vec;
100         struct my_eval_result *eval = operand;;
101
102         (void)userctx;
103
104         /* get parsed string vector, it should contain only one str */
105         vec = ec_parsed_tk_strvec(operator);
106         if (ec_strvec_len(vec) != 1)
107                 return -EINVAL;
108
109         if (!strcmp(ec_strvec_val(vec, 0), "^"))
110                 eval->val = eval->val * eval->val;
111         else
112                 return -EINVAL;
113
114         printf("eval post_op %d\n", eval->val);
115         *result = eval;
116
117         return 0;
118 }
119
120 static int
121 ec_tk_expr_test_eval_bin_op(void **result, void *userctx, void *operand1,
122         const struct ec_parsed_tk *operator, void *operand2)
123
124 {
125         const struct ec_strvec *vec;
126         struct my_eval_result *eval1 = operand1;;
127         struct my_eval_result *eval2 = operand2;;
128
129         (void)userctx;
130
131         /* get parsed string vector, it should contain only one str */
132         vec = ec_parsed_tk_strvec(operator);
133         if (ec_strvec_len(vec) != 1)
134                 return -EINVAL;
135
136         if (!strcmp(ec_strvec_val(vec, 0), "+"))
137                 eval1->val = eval1->val + eval2->val;
138         else if (!strcmp(ec_strvec_val(vec, 0), "*"))
139                 eval1->val = eval1->val * eval2->val;
140         else
141                 return -EINVAL;
142
143         printf("eval bin_op %d\n", eval1->val);
144         ec_free(eval2);
145         *result = eval1;
146
147         return 0;
148 }
149
150 static int
151 ec_tk_expr_test_eval_parenthesis(void **result, void *userctx,
152         const struct ec_parsed_tk *open_paren,
153         const struct ec_parsed_tk *close_paren,
154         void *value)
155 {
156         (void)userctx;
157         (void)open_paren;
158         (void)close_paren;
159
160         printf("eval paren\n");
161         *result = value;
162
163         return 0;
164 }
165
166 static void
167 ec_tk_expr_test_eval_free(void *result, void *userctx)
168 {
169         (void)userctx;
170         ec_free(result);
171 }
172
173 static const struct ec_tk_expr_eval_ops test_ops = {
174         .eval_var = ec_tk_expr_test_eval_var,
175         .eval_pre_op = ec_tk_expr_test_eval_pre_op,
176         .eval_post_op = ec_tk_expr_test_eval_post_op,
177         .eval_bin_op = ec_tk_expr_test_eval_bin_op,
178         .eval_parenthesis = ec_tk_expr_test_eval_parenthesis,
179         .eval_free = ec_tk_expr_test_eval_free,
180 };
181
182 static int ec_tk_expr_test_eval(struct ec_tk *lex_tk,
183         const struct ec_tk *expr_tk,
184         const char *str, int val)
185 {
186         struct ec_parsed_tk *p;
187         void *result;
188         struct my_eval_result *eval;
189         int ret;
190
191         /* XXX check tk type (again and again) */
192
193         p = ec_tk_parse(lex_tk, str);
194         if (p == NULL)
195                 return -1;
196
197         ret = ec_tk_expr_eval(&result, expr_tk, p, &test_ops, NULL);
198         ec_parsed_tk_free(p);
199         if (ret < 0)
200                 return -1;
201
202         /* the parsed value is an integer */
203         eval = result;
204         assert(eval != NULL);
205
206         printf("result: %d (expected %d)\n", eval->val, val);
207         if (eval->val == val)
208                 ret = 0;
209         else
210                 ret = -1;
211
212         ec_free(eval);
213
214         return ret;
215 }
216
217 static int ec_tk_expr_testcase(void)
218 {
219         struct ec_tk *tk = NULL, *lex_tk = NULL;
220         int ret = 0;
221
222         tk = ec_tk_new("expr", "my_expr");
223         if (tk == NULL)
224                 return -1;
225
226         ec_tk_expr_set_val_tk(tk, ec_tk_int(NULL, 0, UCHAR_MAX, 0));
227         ec_tk_expr_add_bin_op(tk, ec_tk_str(NULL, "+"));
228         ec_tk_expr_add_bin_op(tk, ec_tk_str(NULL, "*"));
229         ec_tk_expr_add_pre_op(tk, ec_tk_str(NULL, "!"));  /* not */
230         ec_tk_expr_add_post_op(tk, ec_tk_str(NULL, "^")); /* square */
231         ec_tk_expr_add_parenthesis(tk, ec_tk_str(NULL, "("),
232                 ec_tk_str(NULL, ")"));
233         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "1");
234         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "1", "1");
235         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "1", "*");
236         ret |= EC_TEST_CHECK_TK_PARSE(tk, 3, "1", "*", "1");
237         ret |= EC_TEST_CHECK_TK_PARSE(tk, 3, "1", "*", "1", "*");
238         ret |= EC_TEST_CHECK_TK_PARSE(tk, 4, "1", "+", "!", "1");
239         ret |= EC_TEST_CHECK_TK_PARSE(tk, 4, "1", "^", "+", "1");
240         ret |= EC_TEST_CHECK_TK_PARSE(tk, 5, "1", "*", "1", "*", "1");
241         ret |= EC_TEST_CHECK_TK_PARSE(tk, 5, "1", "*", "1", "+", "1");
242         ret |= EC_TEST_CHECK_TK_PARSE(tk, 7, "1", "*", "1", "*", "1", "*", "1");
243         ret |= EC_TEST_CHECK_TK_PARSE(
244                 tk, 10, "!", "(", "1", "*", "(", "1", "+", "1", ")", ")");
245         ret |= EC_TEST_CHECK_TK_PARSE(tk, 5, "1", "+", "!", "1", "^");
246
247         /* prepend a lexer to the expression token */
248         lex_tk = ec_tk_re_lex(NULL, ec_tk_clone(tk));
249         if (lex_tk == NULL)
250                 goto fail;
251
252         ret |= ec_tk_re_lex_add(lex_tk, "[0-9]+", 1); /* vars */
253         ret |= ec_tk_re_lex_add(lex_tk, "[+*!^()]", 1); /* operators */
254         ret |= ec_tk_re_lex_add(lex_tk, "[      ]+", 0); /* spaces */
255
256         /* valid expressions */
257         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, 1, "!1");
258         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, 1, "1^");
259         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, 1, "1^ + 1");
260         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, 1, "1 + 4 * (2 + 3^)^");
261         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, 1, "(1)");
262         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, 1, "3*!3+!3*(2+ 2)");
263         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, 1, "!!(!1)^ + !(4 + (2*3))");
264         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, 1, "(1 + 1)^ * 1^");
265
266         /* invalid expressions */
267         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, -1, "");
268         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, -1, "()");
269         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, -1, "(");
270         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, -1, ")");
271         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, -1, "+1");
272         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, -1, "1+");
273         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, -1, "1+*1");
274         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, -1, "1+(1*1");
275         ret |= EC_TEST_CHECK_TK_PARSE(lex_tk, -1, "1+!1!1)");
276
277         ret |= ec_tk_expr_test_eval(lex_tk, tk, "1^", 1);
278         ret |= ec_tk_expr_test_eval(lex_tk, tk, "2^", 4);
279         ret |= ec_tk_expr_test_eval(lex_tk, tk, "!1", 0);
280         ret |= ec_tk_expr_test_eval(lex_tk, tk, "!0", 1);
281
282         ret |= ec_tk_expr_test_eval(lex_tk, tk, "1+1", 2);
283         ret |= ec_tk_expr_test_eval(lex_tk, tk, "1+1*2", 4);
284         ret |= ec_tk_expr_test_eval(lex_tk, tk, "2 * 2^", 8);
285         ret |= ec_tk_expr_test_eval(lex_tk, tk, "(1 + !0)^ * !0^", 4);
286         ret |= ec_tk_expr_test_eval(lex_tk, tk, "(1 + !1) * 3", 3);
287
288         ec_tk_free(tk);
289         ec_tk_free(lex_tk);
290
291         return ret;
292
293 fail:
294         ec_tk_free(lex_tk);
295         ec_tk_free(tk);
296         return -1;
297 }
298
299 static struct ec_test ec_tk_expr_test = {
300         .name = "tk_expr",
301         .test = ec_tk_expr_testcase,
302 };
303
304 EC_TEST_REGISTER(ec_tk_expr_test);