libamxp  1.4.0
Patterns C Implementation
test_expression_node.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 #include <sys/signalfd.h>
55 #include <signal.h>
56 #include <unistd.h>
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <string.h>
60 
61 #include <stdarg.h>
62 #include <stddef.h>
63 #include <setjmp.h>
64 #include <fcntl.h>
65 #include <cmocka.h>
66 
67 #include <amxc/amxc_macros.h>
68 #include <amxc/amxc.h>
69 #include <amxp/amxp_expression.h>
70 
71 #include "../../include_priv/amxp_expr_priv.h"
72 #include "test_expression_node.h"
73 
74 void test_node_tree_and(UNUSED void** state) {
75  amxp_expr_node_t* node = NULL;
76  amxp_expr_node_t* left = NULL;
77  amxp_expr_node_t* right = NULL;
78  amxc_var_t* b1 = NULL;
79  amxc_var_t* b2 = NULL;
80  amxp_expr_t expression;
81 
82  assert_int_equal(amxp_expr_init(&expression, ""), 0);
83 
87  amxp_expr_node_set_left(node, left);
88  amxp_expr_node_set_right(node, right);
89 
90  amxc_var_new(&b1);
91  amxc_var_new(&b2);
92 
93  amxc_var_set(bool, b1, true);
94  amxc_var_set(bool, b2, true);
95 
96  amxp_expr_node_set_value(left, b1);
97  amxp_expr_node_set_value(right, b2);
98 
99  assert_true(amxp_expr_node_eval(&expression, node));
100 
101  amxc_var_set(bool, b1, false);
102 
103  assert_false(amxp_expr_node_eval(&expression, node));
104 
105  amxc_var_set(bool, b1, true);
106  amxc_var_set(bool, b2, false);
107 
108  assert_false(amxp_expr_node_eval(&expression, node));
109 
110  amxp_expr_node_delete(&node);
111  amxp_expr_clean(&expression);
112 }
113 
114 void test_node_tree_or(UNUSED void** state) {
115  amxp_expr_node_t* node = NULL;
116  amxp_expr_node_t* left = NULL;
117  amxp_expr_node_t* right = NULL;
118  amxc_var_t* b1 = NULL;
119  amxc_var_t* b2 = NULL;
120  amxp_expr_t expression;
121 
122  assert_int_equal(amxp_expr_init(&expression, ""), 0);
123 
127  amxp_expr_node_set_left(node, left);
128  amxp_expr_node_set_right(node, right);
129 
130  amxc_var_new(&b1);
131  amxc_var_new(&b2);
132 
133  amxc_var_set(bool, b1, true);
134  amxc_var_set(bool, b2, true);
135 
136  amxp_expr_node_set_value(left, b1);
137  amxp_expr_node_set_value(right, b2);
138 
139  assert_true(amxp_expr_node_eval(&expression, node));
140 
141  amxc_var_set(bool, b1, false);
142 
143  assert_true(amxp_expr_node_eval(&expression, node));
144 
145  amxc_var_set(bool, b1, true);
146  amxc_var_set(bool, b2, false);
147 
148  assert_true(amxp_expr_node_eval(&expression, node));
149 
150  amxc_var_set(bool, b1, false);
151 
152  assert_false(amxp_expr_node_eval(&expression, node));
153 
154  amxp_expr_node_delete(&node);
155  amxp_expr_clean(&expression);
156 }
157 
158 void test_node_tree_not(UNUSED void** state) {
159  amxp_expr_node_t* node = NULL;
160  amxp_expr_node_t* left = NULL;
161  amxc_var_t* b1 = NULL;
162  amxp_expr_t expression;
163 
164  assert_int_equal(amxp_expr_init(&expression, ""), 0);
165 
168  amxp_expr_node_set_left(node, left);
169 
170  amxc_var_new(&b1);
171 
172  amxc_var_set(bool, b1, true);
173 
174  amxp_expr_node_set_value(left, b1);
175 
176  assert_false(amxp_expr_node_eval(&expression, node));
177 
178  amxc_var_set(bool, b1, false);
179 
180  assert_true(amxp_expr_node_eval(&expression, node));
181 
182  amxp_expr_node_delete(&node);
183  amxp_expr_clean(&expression);
184 }
185 
186 void test_node_tree_compop(UNUSED void** state) {
187  amxp_expr_node_t* node = NULL;
188  amxp_expr_node_t* left = NULL;
189  amxp_expr_node_t* right = NULL;
190  amxc_var_t* b1 = NULL;
191  amxc_var_t* b2 = NULL;
192  amxp_expr_t expression;
193 
194  assert_int_equal(amxp_expr_init(&expression, ""), 0);
195 
199  amxp_expr_node_set_left(node, left);
200  amxp_expr_node_set_right(node, right);
201 
202  amxc_var_new(&b1);
203  amxc_var_new(&b2);
204 
205  amxc_var_set(int32_t, b1, 1);
206  amxc_var_set(int32_t, b2, 10);
207 
208  amxp_expr_node_set_value(left, b1);
209  amxp_expr_node_set_value(right, b2);
211 
212  assert_false(amxp_expr_node_eval(&expression, node));
213 
215 
216  assert_true(amxp_expr_node_eval(&expression, node));
217 
219 
220  assert_true(amxp_expr_node_eval(&expression, node));
221 
223 
224  assert_false(amxp_expr_node_eval(&expression, node));
225 
226  amxp_expr_node_delete(&node);
227  amxp_expr_clean(&expression);
228 }
229 
230 void test_node_tree_field(UNUSED void** state) {
231  amxp_expr_node_t* node = NULL;
232  amxp_expr_node_t* left = NULL;
233  amxp_expr_node_t* right = NULL;
234  amxc_var_t* b1 = NULL;
235  amxc_var_t* b2 = NULL;
236  amxc_var_t* t = NULL;
237  amxp_expr_t expression;
238 
239  amxc_var_new(&b1);
240  amxc_var_new(&b2);
241 
242  assert_int_equal(amxp_expr_init(&expression, ""), 0);
243  expression.priv = b1;
244  expression.get_field = amxp_expr_get_field_var;
245 
249  amxp_expr_node_set_left(node, left);
250  amxp_expr_node_set_right(node, right);
251 
252  amxc_var_set_type(b1, AMXC_VAR_ID_HTABLE);
253  t = amxc_var_add_key(amxc_htable_t, b1, "test", NULL);
254  amxc_var_add_key(int32_t, t, "value", 1);
255  amxc_var_set(int32_t, b2, 10);
256 
257  amxp_expr_node_set_field(left, strdup("test.value"));
258  amxp_expr_node_set_value(right, b2);
260 
261  assert_false(amxp_expr_node_eval(&expression, node));
262 
264 
265  assert_true(amxp_expr_node_eval(&expression, node));
266 
268 
269  assert_true(amxp_expr_node_eval(&expression, node));
270 
272 
273  assert_false(amxp_expr_node_eval(&expression, node));
274 
275  amxp_expr_node_delete(&node);
276  amxp_expr_clean(&expression);
277  amxc_var_delete(&b1);
278 }
279 
280 
281 void test_node_tree_value_list(UNUSED void** state) {
282  amxp_expr_node_t* node = NULL;
283  amxp_expr_node_t* left = NULL;
284  amxp_expr_node_t* right = NULL;
285  amxp_expr_node_t* value = NULL;
286  amxp_expr_t expression;
287 
288  amxc_var_t* b1 = NULL;
289  amxc_var_t* b2 = NULL;
290 
291  assert_int_equal(amxp_expr_init(&expression, ""), 0);
292 
296  amxp_expr_node_set_left(node, left);
297  amxp_expr_node_set_right(node, right);
298 
299  amxc_var_new(&b1);
300  amxc_var_set(int32_t, b1, 1);
301  amxp_expr_node_set_value(left, b1);
302 
303  amxc_var_new(&b2);
305  amxc_var_set(int32_t, b2, 1);
306  amxp_expr_node_set_value(value, b2);
307  amxp_expr_node_add_value(right, value);
308 
309  amxc_var_new(&b2);
311  amxc_var_set(int32_t, b2, 2);
312  amxp_expr_node_set_value(value, b2);
313  amxp_expr_node_add_value(right, value);
314 
315  amxc_var_new(&b2);
317  amxc_var_set(int32_t, b2, 3);
318  amxp_expr_node_set_value(value, b2);
319  amxp_expr_node_add_value(right, value);
320 
322 
323  assert_true(amxp_expr_node_eval(&expression, node));
324 
325  amxc_var_set(int32_t, b1, 10);
326 
327  assert_false(amxp_expr_node_eval(&expression, node));
328 
329  amxc_var_set(int32_t, b1, 3);
330 
331  assert_true(amxp_expr_node_eval(&expression, node));
332 
333  amxp_expr_node_delete(&node);
334  amxp_expr_clean(&expression);
335 }
336 
337 void test_node_tree_bool_func(UNUSED void** state) {
338  amxp_expr_node_t* node = NULL;
339  amxp_expr_node_t* value = NULL;
340  amxp_expr_t expression;
341  amxc_var_t* data = NULL;
342  amxc_var_t* list = NULL;
343 
344  amxc_var_new(&data);
345  amxc_var_set_type(data, AMXC_VAR_ID_HTABLE);
346  list = amxc_var_add_key(amxc_llist_t, data, "list", NULL);
347 
348  assert_int_equal(amxp_expr_init(&expression, ""), 0);
349  expression.priv = data;
350  expression.get_field = amxp_expr_get_field_var;
351 
353  amxp_expr_node_set_function(node, strdup("is_empty"));
354 
356  amxp_expr_node_set_field(value, strdup("list"));
357  amxp_expr_node_add_value(node, value);
358 
359  assert_true(amxp_expr_node_eval(&expression, node));
360 
361  amxc_var_add(cstring_t, list, "123");
362 
363  assert_false(amxp_expr_node_eval(&expression, node));
364 
365  amxp_expr_node_delete(&node);
366  amxp_expr_clean(&expression);
367  amxc_var_delete(&data);
368 }
369 
370 void test_node_tree_value_func(UNUSED void** state) {
371  amxp_expr_node_t* node = NULL;
372  amxp_expr_node_t* left = NULL;
373  amxp_expr_node_t* right = NULL;
374  amxp_expr_node_t* value = NULL;
375  amxp_expr_t expression;
376  amxc_var_t* data = NULL;
377  amxc_var_t* number = NULL;
378 
379  amxc_var_new(&data);
380  amxc_var_set_type(data, AMXC_VAR_ID_HTABLE);
381  amxc_var_add_key(int32_t, data, "key1", 10);
382 
383  assert_int_equal(amxp_expr_init(&expression, ""), 0);
384  expression.priv = data;
385  expression.get_field = amxp_expr_get_field_var;
386 
390  amxp_expr_node_set_left(node, left);
391  amxp_expr_node_set_right(node, right);
392 
393  amxp_expr_node_set_function(right, strdup("first_existing"));
394 
395  amxc_var_new(&number);
396  amxc_var_set(int32_t, number, 10);
397  amxp_expr_node_set_value(left, number);
398 
400  amxp_expr_node_set_field(value, strdup("key1"));
401  amxp_expr_node_add_value(right, value);
403  amxp_expr_node_set_field(value, strdup("key2"));
404  amxp_expr_node_add_value(right, value);
405 
407 
408  assert_true(amxp_expr_node_eval(&expression, node));
409 
410  amxp_expr_node_delete(&node);
411  amxp_expr_clean(&expression);
412  amxc_var_delete(&data);
413 }
PRIVATE void amxp_expr_node_new(amxp_expr_node_t **node, amxp_expr_node_type_t type)
PRIVATE void amxp_expr_node_set_left(amxp_expr_node_t *node, amxp_expr_node_t *left)
PRIVATE void amxp_expr_node_set_right(amxp_expr_node_t *node, amxp_expr_node_t *right)
PRIVATE void amxp_expr_node_set_compop(amxp_expr_node_t *node, amxp_expr_comp_t comop)
PRIVATE void amxp_expr_node_set_field(amxp_expr_node_t *node, char *field)
PRIVATE void amxp_expr_node_delete(amxp_expr_node_t **node)
@ amxp_expr_value
@ amxp_expr_bool_func
@ amxp_expr_or
@ amxp_expr_not
@ amxp_expr_compare_oper
@ amxp_expr_value_func
@ amxp_expr_field
@ amxp_expr_and
PRIVATE void amxp_expr_node_set_function(amxp_expr_node_t *node, char *func_name)
PRIVATE void amxp_expr_node_add_value(amxp_expr_node_t *node, amxp_expr_node_t *value)
PRIVATE void amxp_expr_node_set_value(amxp_expr_node_t *node, amxc_var_t *value)
PRIVATE bool amxp_expr_node_eval(amxp_expr_t *expr, amxp_expr_node_t *node)
@ amxp_expr_comp_in
@ amxp_expr_comp_lesser
@ amxp_expr_comp_equal
@ amxp_expr_comp_bigger
@ amxp_expr_comp_not_equal
Ambiorix expression parser and evaluate API header file.
#define UNUSED
Definition: main.c:68
void amxp_expr_clean(amxp_expr_t *expr)
Clean-up the expression structure.
amxp_expr_status_t amxp_expr_get_field_var(amxp_expr_t *expr, amxc_var_t *value, const char *path, void *priv)
Variant field fetcher implementation.
amxp_expr_status_t amxp_expr_init(amxp_expr_t *expr, const char *expression)
Initializes an expression structure.
amxp_expr_get_field_t get_field
void test_node_tree_value_list(UNUSED void **state)
void test_node_tree_field(UNUSED void **state)
void test_node_tree_or(UNUSED void **state)
void test_node_tree_not(UNUSED void **state)
void test_node_tree_bool_func(UNUSED void **state)
void test_node_tree_value_func(UNUSED void **state)
void test_node_tree_compop(UNUSED void **state)
void test_node_tree_and(UNUSED void **state)