1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
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>
20 EC_LOG_TYPE_REGISTER(node_expr);
22 struct my_eval_result {
27 ec_node_expr_test_eval_var(void **result, void *userctx,
28 const struct ec_parse *var)
30 const struct ec_strvec *vec;
31 const struct ec_node *node;
32 struct my_eval_result *eval = NULL;
37 /* get parsed string vector, it should contain only one str */
38 vec = ec_parse_strvec(var);
39 if (ec_strvec_len(vec) != 1)
42 node = ec_parse_get_node(var);
43 if (ec_node_int_getval(node, ec_strvec_val(vec, 0), &val) < 0)
46 eval = ec_malloc(sizeof(*eval));
51 EC_LOG(EC_LOG_DEBUG, "eval var %d\n", eval->val);
58 ec_node_expr_test_eval_pre_op(void **result, void *userctx, void *operand,
59 const struct ec_parse *operator)
61 const struct ec_strvec *vec;
62 struct my_eval_result *eval = operand;;
66 /* get parsed string vector, it should contain only one str */
67 vec = ec_parse_strvec(operator);
68 if (ec_strvec_len(vec) != 1)
71 if (!strcmp(ec_strvec_val(vec, 0), "!"))
72 eval->val = !eval->val;
76 EC_LOG(EC_LOG_DEBUG, "eval pre_op %d\n", eval->val);
83 ec_node_expr_test_eval_post_op(void **result, void *userctx, void *operand,
84 const struct ec_parse *operator)
86 const struct ec_strvec *vec;
87 struct my_eval_result *eval = operand;;
91 /* get parsed string vector, it should contain only one str */
92 vec = ec_parse_strvec(operator);
93 if (ec_strvec_len(vec) != 1)
96 if (!strcmp(ec_strvec_val(vec, 0), "^"))
97 eval->val = eval->val * eval->val;
101 EC_LOG(EC_LOG_DEBUG, "eval post_op %d\n", eval->val);
108 ec_node_expr_test_eval_bin_op(void **result, void *userctx, void *operand1,
109 const struct ec_parse *operator, void *operand2)
112 const struct ec_strvec *vec;
113 struct my_eval_result *eval1 = operand1;;
114 struct my_eval_result *eval2 = operand2;;
118 /* get parsed string vector, it should contain only one str */
119 vec = ec_parse_strvec(operator);
120 if (ec_strvec_len(vec) != 1)
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;
130 EC_LOG(EC_LOG_DEBUG, "eval bin_op %d\n", eval1->val);
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,
147 EC_LOG(EC_LOG_DEBUG, "eval paren\n");
154 ec_node_expr_test_eval_free(void *result, void *userctx)
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,
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)
175 struct my_eval_result *eval;
178 p = ec_node_parse(lex_node, str);
182 ret = ec_node_expr_eval(&result, expr_node, p, &test_ops, NULL);
187 /* the parse value is an integer */
189 assert(eval != NULL);
191 EC_LOG(EC_LOG_DEBUG, "result: %d (expected %d)\n", eval->val, val);
192 if (eval->val == val)
202 /* LCOV_EXCL_START */
203 static int ec_node_expr_testcase(void)
205 struct ec_node *node = NULL, *lex_node = NULL;
208 node = ec_node("expr", "my_expr");
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", "*",
230 testres |= EC_TEST_CHECK_PARSE(
231 node, 10, "!", "(", "1", "*", "(", "1", "+", "1", ")", ")");
232 testres |= EC_TEST_CHECK_PARSE(node, 5, "1", "+", "!", "1", "^");
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)
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 */
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^");
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)");
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);
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);
277 ec_node_free(lex_node);
282 ec_node_free(lex_node);
288 static struct ec_test ec_node_expr_test = {
290 .test = ec_node_expr_testcase,
293 EC_TEST_REGISTER(ec_node_expr_test);