1 #include <check.h>
2 #include "../src/common.h"
3 #include "../src/utils.h"
4 
5 typedef struct S_EXPR_TEST_ITEM
6 {
7 	const gchar *input;
8 	guint peek_index;
9 	const gchar *expected;
10 }EXPR_TEST_ITEM;
11 
12 EXPR_TEST_ITEM expr_test_items [] =
13 {
14 	{ " variable ",         sizeof(" var")-1,            "variable"           },
15 	{ " (variable) ",       sizeof(" (var")-1,           "variable"           },
16 	{ "struct.item",        sizeof("str")-1,             "struct"             },
17 	{ "struct.item",        sizeof("str")-1,             "struct"             },
18 	{ "struct.item",        sizeof("struct.it")-1,       "struct.item"        },
19 	{ "struct->item",       sizeof("str")-1,             "struct"             },
20 	{ "struct->item",       sizeof("struct->it")-1,      "struct->item"       },
21 	{ "&(struct->item)",    sizeof("&(str")-1,           "struct"             },
22 	{ "&(struct->item)",    sizeof("&(struct->it")-1,    "struct->item"       },
23 	{ "foobar(item)",       sizeof("foobar(it")-1,       "item"               },
24 	{ "sizeof(item)",       sizeof("sizeof(it")-1,       "sizeof(item)"       },
25 	{ "array[5]",           sizeof("arr")-1,             "array"              },
26 	{ "array[5]",           sizeof("array[")-1,          "array[5]"           },
27 	{ "*pointer",           sizeof("*poi")-1,            "pointer"            },
28 	{ "*pointer",           0,                           "*pointer"           },
29 	{ "&variable",          sizeof("&var")-1,            "variable"           },
30 	{ "&variable",          0,                           "&variable"          },
31 	{ "foo variable",       sizeof("foo var")-1,         "variable"           },
32 	{ "variable foo",       sizeof("var")-1,             "variable"           },
33 	{ "int var_a, var_b;",  sizeof("int var")-1,         "var_a"              },
34 	{ "int var_a, var_b;",  sizeof("int var_a, va")-1,   "var_b"              },
35 	{ "foo(var_a, var_b);", sizeof("foo(var")-1,         "var_a"              },
36 	{ "foo(var_a, var_b);", sizeof("foo(var_a, va")-1,   "var_b"              },
37 	{ "array[index].item",  sizeof("array[index].i")-1,  "array[index].item"  },
38 	{ "array[index]->item", sizeof("array[index]->i")-1, "array[index]->item" },
39 	{ NULL,                 0,                         NULL           },
40 };
41 
START_TEST(test_parser_for_evaluate)42 START_TEST(test_parser_for_evaluate)
43 {
44 	EXPR_TEST_ITEM *test_item;
45 	guint index = 0;
46 	gchar *expr, *chunk;
47 
48 	test_item = &(expr_test_items[index]);
49 	while (test_item->input != NULL)
50 	{
51 		chunk = g_strdup(test_item->input);
52 		expr = utils_evaluate_expr_from_string (chunk, test_item->peek_index);
53 		ck_assert_ptr_ne(expr, NULL);
54 		if (g_strcmp0(expr, test_item->expected) != 0)
55 		{
56 			ck_abort_msg("Index #%lu: Input: '%s', Peek-Ind.: %lu, Result: '%s' != '%s' (expected)",
57 				index, test_item->input, test_item->peek_index, expr, test_item->expected);
58 		}
59 		g_free(chunk);
60 		g_free(expr);
61 
62 		index++;
63 		test_item = &(expr_test_items[index]);
64 	}
65 }
66 END_TEST;
67 
utils_create_tests(void)68 TCase *utils_create_tests(void)
69 {
70 	TCase *tc_utils = tcase_create("utils");
71 	tcase_add_test(tc_utils, test_parser_for_evaluate);
72 	return tc_utils;
73 }
74