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