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) {
44 node = ec_parse_get_node(var);
45 if (ec_node_int_getval(node, ec_strvec_val(vec, 0), &val) < 0)
48 eval = ec_malloc(sizeof(*eval));
53 EC_LOG(EC_LOG_DEBUG, "eval var %d\n", eval->val);
60 ec_node_expr_test_eval_pre_op(void **result, void *userctx, void *operand,
61 const struct ec_parse *operator)
63 const struct ec_strvec *vec;
64 struct my_eval_result *eval = operand;;
68 /* get parsed string vector, it should contain only one str */
69 vec = ec_parse_strvec(operator);
70 if (ec_strvec_len(vec) != 1) {
75 if (!strcmp(ec_strvec_val(vec, 0), "!")) {
76 eval->val = !eval->val;
83 EC_LOG(EC_LOG_DEBUG, "eval pre_op %d\n", eval->val);
90 ec_node_expr_test_eval_post_op(void **result, void *userctx, void *operand,
91 const struct ec_parse *operator)
93 const struct ec_strvec *vec;
94 struct my_eval_result *eval = operand;;
98 /* get parsed string vector, it should contain only one str */
99 vec = ec_parse_strvec(operator);
100 if (ec_strvec_len(vec) != 1) {
105 if (!strcmp(ec_strvec_val(vec, 0), "^")) {
106 eval->val = eval->val * eval->val;
112 EC_LOG(EC_LOG_DEBUG, "eval post_op %d\n", eval->val);
119 ec_node_expr_test_eval_bin_op(void **result, void *userctx, void *operand1,
120 const struct ec_parse *operator, void *operand2)
123 const struct ec_strvec *vec;
124 struct my_eval_result *eval1 = operand1;;
125 struct my_eval_result *eval2 = operand2;;
129 /* get parsed string vector, it should contain only one str */
130 vec = ec_parse_strvec(operator);
131 if (ec_strvec_len(vec) != 1) {
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;
145 EC_LOG(EC_LOG_DEBUG, "eval bin_op %d\n", eval1->val);
153 ec_node_expr_test_eval_parenthesis(void **result, void *userctx,
154 const struct ec_parse *open_paren,
155 const struct ec_parse *close_paren,
162 EC_LOG(EC_LOG_DEBUG, "eval paren\n");
169 ec_node_expr_test_eval_free(void *result, void *userctx)
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,
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)
190 struct my_eval_result *eval;
193 p = ec_node_parse(lex_node, str);
197 ret = ec_node_expr_eval(&result, expr_node, p, &test_ops, NULL);
202 /* the parse value is an integer */
204 assert(eval != NULL);
206 EC_LOG(EC_LOG_DEBUG, "result: %d (expected %d)\n", eval->val, val);
207 if (eval->val == val)
217 /* LCOV_EXCL_START */
218 static int ec_node_expr_testcase(void)
220 struct ec_node *node = NULL, *lex_node = NULL;
223 node = ec_node("expr", "my_expr");
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", "*",
245 testres |= EC_TEST_CHECK_PARSE(
246 node, 10, "!", "(", "1", "*", "(", "1", "+", "1", ")", ")");
247 testres |= EC_TEST_CHECK_PARSE(node, 5, "1", "+", "!", "1", "^");
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)
254 testres |= ec_node_re_lex_add(lex_node, "[0-9]+", 1); /* vars */
255 testres |= ec_node_re_lex_add(lex_node, "[+*!^()]", 1); /* operators */
256 testres |= ec_node_re_lex_add(lex_node, "[ ]+", 0); /* spaces */
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^");
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)");
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);
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);
292 ec_node_free(lex_node);
297 ec_node_free(lex_node);
303 static struct ec_test ec_node_expr_test = {
305 .test = ec_node_expr_testcase,
308 EC_TEST_REGISTER(ec_node_expr_test);