^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include "util/debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include "util/expr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include "tests.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) static int test(struct expr_parse_ctx *ctx, const char *e, double val2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) double val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) if (expr__parse(&val, ctx, e, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) TEST_ASSERT_VAL("parse test failed", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) TEST_ASSERT_VAL("unexpected value", val == val2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct expr_id_data *val_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) const char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) double val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct expr_parse_ctx ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) expr__ctx_init(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) expr__add_id_val(&ctx, strdup("FOO"), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) expr__add_id_val(&ctx, strdup("BAR"), 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) ret = test(&ctx, "1+1", 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) ret |= test(&ctx, "FOO+BAR", 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) ret |= test(&ctx, "(BAR/2)%2", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) ret |= test(&ctx, "1 - -4", 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) ret |= test(&ctx, "(FOO-1)*2 + (BAR/2)%2 - -4", 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) ret |= test(&ctx, "1-1 | 1", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) ret |= test(&ctx, "1-1 & 1", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) ret |= test(&ctx, "min(1,2) + 1", 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) ret |= test(&ctx, "max(1,2) + 1", 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) ret |= test(&ctx, "1+1 if 3*4 else 0", 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) ret |= test(&ctx, "1.1 + 2.1", 3.2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) ret |= test(&ctx, ".1 + 2.", 2.1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) ret |= test(&ctx, "d_ratio(1, 2)", 0.5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) ret |= test(&ctx, "d_ratio(2.5, 0)", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ret |= test(&ctx, "1.1 < 2.2", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) ret |= test(&ctx, "2.2 > 1.1", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ret |= test(&ctx, "1.1 < 1.1", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ret |= test(&ctx, "2.2 > 2.2", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ret |= test(&ctx, "2.2 < 1.1", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ret |= test(&ctx, "1.1 > 2.2", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) p = "FOO/0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ret = expr__parse(&val, &ctx, p, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) TEST_ASSERT_VAL("division by zero", ret == -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) p = "BAR/";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) ret = expr__parse(&val, &ctx, p, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) TEST_ASSERT_VAL("missing operand", ret == -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) expr__ctx_clear(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) TEST_ASSERT_VAL("find other",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) expr__find_other("FOO + BAR + BAZ + BOZO", "FOO",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) &ctx, 1) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) TEST_ASSERT_VAL("find other", hashmap__size(&ctx.ids) == 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BAR",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) (void **)&val_ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BAZ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) (void **)&val_ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BOZO",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) (void **)&val_ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) expr__ctx_clear(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) TEST_ASSERT_VAL("find other",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) expr__find_other("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) NULL, &ctx, 3) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) TEST_ASSERT_VAL("find other", hashmap__size(&ctx.ids) == 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "EVENT1,param=3/",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) (void **)&val_ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "EVENT2,param=3/",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) (void **)&val_ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) expr__ctx_clear(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }