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 struct my_eval_result {
48 ec_node_expr_test_eval_var(void **result, void *userctx,
49 const struct ec_parsed *var)
51 const struct ec_strvec *vec;
52 struct my_eval_result *eval;
56 /* get parsed string vector, it should contain only one str */
57 vec = ec_parsed_strvec(var);
58 if (ec_strvec_len(vec) != 1)
61 eval = ec_malloc(sizeof(*eval));
65 eval->val = atoi(ec_strvec_val(vec, 0)); // XXX use strtol
66 printf("eval var %d\n", eval->val);
73 ec_node_expr_test_eval_pre_op(void **result, void *userctx, void *operand,
74 const struct ec_parsed *operator)
76 const struct ec_strvec *vec;
77 struct my_eval_result *eval = operand;;
81 /* get parsed string vector, it should contain only one str */
82 vec = ec_parsed_strvec(operator);
83 if (ec_strvec_len(vec) != 1)
86 if (!strcmp(ec_strvec_val(vec, 0), "!"))
87 eval->val = !eval->val;
91 printf("eval pre_op %d\n", eval->val);
98 ec_node_expr_test_eval_post_op(void **result, void *userctx, void *operand,
99 const struct ec_parsed *operator)
101 const struct ec_strvec *vec;
102 struct my_eval_result *eval = operand;;
106 /* get parsed string vector, it should contain only one str */
107 vec = ec_parsed_strvec(operator);
108 if (ec_strvec_len(vec) != 1)
111 if (!strcmp(ec_strvec_val(vec, 0), "^"))
112 eval->val = eval->val * eval->val;
116 printf("eval post_op %d\n", eval->val);
123 ec_node_expr_test_eval_bin_op(void **result, void *userctx, void *operand1,
124 const struct ec_parsed *operator, void *operand2)
127 const struct ec_strvec *vec;
128 struct my_eval_result *eval1 = operand1;;
129 struct my_eval_result *eval2 = operand2;;
133 /* get parsed string vector, it should contain only one str */
134 vec = ec_parsed_strvec(operator);
135 if (ec_strvec_len(vec) != 1)
138 if (!strcmp(ec_strvec_val(vec, 0), "+"))
139 eval1->val = eval1->val + eval2->val;
140 else if (!strcmp(ec_strvec_val(vec, 0), "*"))
141 eval1->val = eval1->val * eval2->val;
145 printf("eval bin_op %d\n", eval1->val);
153 ec_node_expr_test_eval_parenthesis(void **result, void *userctx,
154 const struct ec_parsed *open_paren,
155 const struct ec_parsed *close_paren,
162 printf("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 /* XXX check node type (again and again) */
195 p = ec_node_parse(lex_node, str);
199 ret = ec_node_expr_eval(&result, expr_node, p, &test_ops, NULL);
204 /* the parsed value is an integer */
206 assert(eval != NULL);
208 printf("result: %d (expected %d)\n", eval->val, val);
209 if (eval->val == val)
219 static int ec_node_expr_testcase(void)
221 struct ec_node *node = NULL, *lex_node = NULL;
224 node = ec_node("expr", "my_expr");
228 ec_node_expr_set_val_node(node, ec_node_int(NULL, 0, UCHAR_MAX, 0));
229 ec_node_expr_add_bin_op(node, ec_node_str(NULL, "+"));
230 ec_node_expr_add_bin_op(node, ec_node_str(NULL, "*"));
231 ec_node_expr_add_pre_op(node, ec_node_str(NULL, "!")); /* not */
232 ec_node_expr_add_post_op(node, ec_node_str(NULL, "^")); /* square */
233 ec_node_expr_add_parenthesis(node, ec_node_str(NULL, "("),
234 ec_node_str(NULL, ")"));
235 ret |= EC_TEST_CHECK_PARSE(node, 1, "1");
236 ret |= EC_TEST_CHECK_PARSE(node, 1, "1", "1");
237 ret |= EC_TEST_CHECK_PARSE(node, 1, "1", "*");
238 ret |= EC_TEST_CHECK_PARSE(node, 3, "1", "*", "1");
239 ret |= EC_TEST_CHECK_PARSE(node, 3, "1", "*", "1", "*");
240 ret |= EC_TEST_CHECK_PARSE(node, 4, "1", "+", "!", "1");
241 ret |= EC_TEST_CHECK_PARSE(node, 4, "1", "^", "+", "1");
242 ret |= EC_TEST_CHECK_PARSE(node, 5, "1", "*", "1", "*", "1");
243 ret |= EC_TEST_CHECK_PARSE(node, 5, "1", "*", "1", "+", "1");
244 ret |= EC_TEST_CHECK_PARSE(node, 7, "1", "*", "1", "*", "1", "*", "1");
245 ret |= EC_TEST_CHECK_PARSE(
246 node, 10, "!", "(", "1", "*", "(", "1", "+", "1", ")", ")");
247 ret |= EC_TEST_CHECK_PARSE(node, 5, "1", "+", "!", "1", "^");
249 /* prepend a lexer to the expression node */
250 lex_node = ec_node_re_lex(NULL, ec_node_clone(node));
251 if (lex_node == NULL)
254 ret |= ec_node_re_lex_add(lex_node, "[0-9]+", 1); /* vars */
255 ret |= ec_node_re_lex_add(lex_node, "[+*!^()]", 1); /* operators */
256 ret |= ec_node_re_lex_add(lex_node, "[ ]+", 0); /* spaces */
258 /* valid expressions */
259 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "!1");
260 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "1^");
261 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "1^ + 1");
262 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "1 + 4 * (2 + 3^)^");
263 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "(1)");
264 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "3*!3+!3*(2+ 2)");
265 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "!!(!1)^ + !(4 + (2*3))");
266 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "(1 + 1)^ * 1^");
268 /* invalid expressions */
269 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "");
270 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "()");
271 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "(");
272 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, ")");
273 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "+1");
274 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+");
275 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+*1");
276 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+(1*1");
277 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+!1!1)");
279 ret |= ec_node_expr_test_eval(lex_node, node, "1^", 1);
280 ret |= ec_node_expr_test_eval(lex_node, node, "2^", 4);
281 ret |= ec_node_expr_test_eval(lex_node, node, "!1", 0);
282 ret |= ec_node_expr_test_eval(lex_node, node, "!0", 1);
284 ret |= ec_node_expr_test_eval(lex_node, node, "1+1", 2);
285 ret |= ec_node_expr_test_eval(lex_node, node, "1+2+3", 6);
286 ret |= ec_node_expr_test_eval(lex_node, node, "1+1*2", 4);
287 ret |= ec_node_expr_test_eval(lex_node, node, "2 * 2^", 8);
288 ret |= ec_node_expr_test_eval(lex_node, node, "(1 + !0)^ * !0^", 4);
289 ret |= ec_node_expr_test_eval(lex_node, node, "(1 + !1) * 3", 3);
292 ec_node_free(lex_node);
297 ec_node_free(lex_node);
302 static struct ec_test ec_node_expr_test = {
304 .test = ec_node_expr_testcase,
307 EC_TEST_REGISTER(ec_node_expr_test);