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