2 * Copyright (c) 2016-2017, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
33 #include <ecoli_malloc.h>
34 #include <ecoli_strvec.h>
35 #include <ecoli_test.h>
36 #include <ecoli_node.h>
37 #include <ecoli_parsed.h>
38 #include <ecoli_node_int.h>
39 #include <ecoli_node_str.h>
40 #include <ecoli_node_re_lex.h>
41 #include <ecoli_node_expr.h>
43 EC_LOG_TYPE_REGISTER(node_expr);
45 struct my_eval_result {
50 ec_node_expr_test_eval_var(void **result, void *userctx,
51 const struct ec_parsed *var)
53 const struct ec_strvec *vec;
54 const struct ec_node *node;
55 struct my_eval_result *eval = NULL;
60 /* get parsed string vector, it should contain only one str */
61 vec = ec_parsed_strvec(var);
62 if (ec_strvec_len(vec) != 1)
65 node = ec_parsed_get_node(var);
66 if (ec_node_int_getval(node, ec_strvec_val(vec, 0), &val) < 0)
69 eval = ec_malloc(sizeof(*eval));
74 printf("eval var %d\n", eval->val);
81 ec_node_expr_test_eval_pre_op(void **result, void *userctx, void *operand,
82 const struct ec_parsed *operator)
84 const struct ec_strvec *vec;
85 struct my_eval_result *eval = operand;;
89 /* get parsed string vector, it should contain only one str */
90 vec = ec_parsed_strvec(operator);
91 if (ec_strvec_len(vec) != 1)
94 if (!strcmp(ec_strvec_val(vec, 0), "!"))
95 eval->val = !eval->val;
99 printf("eval pre_op %d\n", eval->val);
106 ec_node_expr_test_eval_post_op(void **result, void *userctx, void *operand,
107 const struct ec_parsed *operator)
109 const struct ec_strvec *vec;
110 struct my_eval_result *eval = operand;;
114 /* get parsed string vector, it should contain only one str */
115 vec = ec_parsed_strvec(operator);
116 if (ec_strvec_len(vec) != 1)
119 if (!strcmp(ec_strvec_val(vec, 0), "^"))
120 eval->val = eval->val * eval->val;
124 printf("eval post_op %d\n", eval->val);
131 ec_node_expr_test_eval_bin_op(void **result, void *userctx, void *operand1,
132 const struct ec_parsed *operator, void *operand2)
135 const struct ec_strvec *vec;
136 struct my_eval_result *eval1 = operand1;;
137 struct my_eval_result *eval2 = operand2;;
141 /* get parsed string vector, it should contain only one str */
142 vec = ec_parsed_strvec(operator);
143 if (ec_strvec_len(vec) != 1)
146 if (!strcmp(ec_strvec_val(vec, 0), "+"))
147 eval1->val = eval1->val + eval2->val;
148 else if (!strcmp(ec_strvec_val(vec, 0), "*"))
149 eval1->val = eval1->val * eval2->val;
153 printf("eval bin_op %d\n", eval1->val);
161 ec_node_expr_test_eval_parenthesis(void **result, void *userctx,
162 const struct ec_parsed *open_paren,
163 const struct ec_parsed *close_paren,
170 printf("eval paren\n");
177 ec_node_expr_test_eval_free(void *result, void *userctx)
183 static const struct ec_node_expr_eval_ops test_ops = {
184 .eval_var = ec_node_expr_test_eval_var,
185 .eval_pre_op = ec_node_expr_test_eval_pre_op,
186 .eval_post_op = ec_node_expr_test_eval_post_op,
187 .eval_bin_op = ec_node_expr_test_eval_bin_op,
188 .eval_parenthesis = ec_node_expr_test_eval_parenthesis,
189 .eval_free = ec_node_expr_test_eval_free,
192 static int ec_node_expr_test_eval(struct ec_node *lex_node,
193 const struct ec_node *expr_node,
194 const char *str, int val)
198 struct my_eval_result *eval;
201 p = ec_node_parse(lex_node, str);
205 ret = ec_node_expr_eval(&result, expr_node, p, &test_ops, NULL);
210 /* the parsed value is an integer */
212 assert(eval != NULL);
214 printf("result: %d (expected %d)\n", eval->val, val);
215 if (eval->val == val)
225 /* LCOV_EXCL_START */
226 static int ec_node_expr_testcase(void)
228 struct ec_node *node = NULL, *lex_node = NULL;
231 node = ec_node("expr", "my_expr");
235 ec_node_expr_set_val_node(node, ec_node_int(EC_NO_ID, 0, UCHAR_MAX, 0));
236 ec_node_expr_add_bin_op(node, ec_node_str(EC_NO_ID, "+"));
237 ec_node_expr_add_bin_op(node, ec_node_str(EC_NO_ID, "*"));
238 ec_node_expr_add_pre_op(node, ec_node_str(EC_NO_ID, "!")); /* not */
239 ec_node_expr_add_post_op(node, ec_node_str(EC_NO_ID, "^")); /* square */
240 ec_node_expr_add_parenthesis(node, ec_node_str(EC_NO_ID, "("),
241 ec_node_str(EC_NO_ID, ")"));
242 ret |= EC_TEST_CHECK_PARSE(node, 1, "1");
243 ret |= EC_TEST_CHECK_PARSE(node, 1, "1", "1");
244 ret |= EC_TEST_CHECK_PARSE(node, 1, "1", "*");
245 ret |= EC_TEST_CHECK_PARSE(node, 3, "1", "*", "1");
246 ret |= EC_TEST_CHECK_PARSE(node, 3, "1", "*", "1", "*");
247 ret |= EC_TEST_CHECK_PARSE(node, 4, "1", "+", "!", "1");
248 ret |= EC_TEST_CHECK_PARSE(node, 4, "1", "^", "+", "1");
249 ret |= EC_TEST_CHECK_PARSE(node, 5, "1", "*", "1", "*", "1");
250 ret |= EC_TEST_CHECK_PARSE(node, 5, "1", "*", "1", "+", "1");
251 ret |= EC_TEST_CHECK_PARSE(node, 7, "1", "*", "1", "*", "1", "*", "1");
252 ret |= EC_TEST_CHECK_PARSE(
253 node, 10, "!", "(", "1", "*", "(", "1", "+", "1", ")", ")");
254 ret |= EC_TEST_CHECK_PARSE(node, 5, "1", "+", "!", "1", "^");
256 /* prepend a lexer to the expression node */
257 lex_node = ec_node_re_lex(EC_NO_ID, ec_node_clone(node));
258 if (lex_node == NULL)
261 ret |= ec_node_re_lex_add(lex_node, "[0-9]+", 1); /* vars */
262 ret |= ec_node_re_lex_add(lex_node, "[+*!^()]", 1); /* operators */
263 ret |= ec_node_re_lex_add(lex_node, "[ ]+", 0); /* spaces */
265 /* valid expressions */
266 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "!1");
267 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "1^");
268 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "1^ + 1");
269 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "1 + 4 * (2 + 3^)^");
270 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "(1)");
271 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "3*!3+!3*(2+ 2)");
272 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "!!(!1)^ + !(4 + (2*3))");
273 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "(1 + 1)^ * 1^");
275 /* invalid expressions */
276 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "");
277 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "()");
278 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "(");
279 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, ")");
280 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "+1");
281 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+");
282 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+*1");
283 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+(1*1");
284 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+!1!1)");
286 ret |= ec_node_expr_test_eval(lex_node, node, "1^", 1);
287 ret |= ec_node_expr_test_eval(lex_node, node, "2^", 4);
288 ret |= ec_node_expr_test_eval(lex_node, node, "!1", 0);
289 ret |= ec_node_expr_test_eval(lex_node, node, "!0", 1);
291 ret |= ec_node_expr_test_eval(lex_node, node, "1+1", 2);
292 ret |= ec_node_expr_test_eval(lex_node, node, "1+2+3", 6);
293 ret |= ec_node_expr_test_eval(lex_node, node, "1+1*2", 4);
294 ret |= ec_node_expr_test_eval(lex_node, node, "2 * 2^", 8);
295 ret |= ec_node_expr_test_eval(lex_node, node, "(1 + !0)^ * !0^", 4);
296 ret |= ec_node_expr_test_eval(lex_node, node, "(1 + !1) * 3", 3);
299 ec_node_free(lex_node);
304 ec_node_free(lex_node);
310 static struct ec_test ec_node_expr_test = {
312 .test = ec_node_expr_testcase,
315 EC_TEST_REGISTER(ec_node_expr_test);