1 // SPDX-License-Identifier: GPL-2.0
2 #include "util/debug.h"
7 #include <linux/zalloc.h>
9 static int test(struct expr_parse_ctx *ctx, const char *e, double val2)
13 if (expr__parse(&val, ctx, e, 1))
14 TEST_ASSERT_VAL("parse test failed", 0);
15 TEST_ASSERT_VAL("unexpected value", val == val2);
19 int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused)
24 struct expr_parse_ctx ctx;
27 expr__add_id(&ctx, strdup("FOO"), 1);
28 expr__add_id(&ctx, strdup("BAR"), 2);
30 ret = test(&ctx, "1+1", 2);
31 ret |= test(&ctx, "FOO+BAR", 3);
32 ret |= test(&ctx, "(BAR/2)%2", 1);
33 ret |= test(&ctx, "1 - -4", 5);
34 ret |= test(&ctx, "(FOO-1)*2 + (BAR/2)%2 - -4", 5);
35 ret |= test(&ctx, "1-1 | 1", 1);
36 ret |= test(&ctx, "1-1 & 1", 0);
37 ret |= test(&ctx, "min(1,2) + 1", 2);
38 ret |= test(&ctx, "max(1,2) + 1", 3);
39 ret |= test(&ctx, "1+1 if 3*4 else 0", 2);
40 ret |= test(&ctx, "1.1 + 2.1", 3.2);
41 ret |= test(&ctx, ".1 + 2.", 2.1);
47 ret = expr__parse(&val, &ctx, p, 1);
48 TEST_ASSERT_VAL("division by zero", ret == -1);
51 ret = expr__parse(&val, &ctx, p, 1);
52 TEST_ASSERT_VAL("missing operand", ret == -1);
54 expr__ctx_clear(&ctx);
55 TEST_ASSERT_VAL("find other",
56 expr__find_other("FOO + BAR + BAZ + BOZO", "FOO",
58 TEST_ASSERT_VAL("find other", hashmap__size(&ctx.ids) == 3);
59 TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BAR",
61 TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BAZ",
63 TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BOZO",
66 expr__ctx_clear(&ctx);
67 TEST_ASSERT_VAL("find other",
68 expr__find_other("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@",
70 TEST_ASSERT_VAL("find other", hashmap__size(&ctx.ids) == 2);
71 TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "EVENT1,param=3/",
73 TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "EVENT2,param=3/",
76 expr__ctx_clear(&ctx);