hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/tools/perf/tests/expr.c
....@@ -3,12 +3,14 @@
33 #include "util/expr.h"
44 #include "tests.h"
55 #include <stdlib.h>
6
+#include <string.h>
7
+#include <linux/zalloc.h>
68
7
-static int test(struct parse_ctx *ctx, const char *e, double val2)
9
+static int test(struct expr_parse_ctx *ctx, const char *e, double val2)
810 {
911 double val;
1012
11
- if (expr__parse(&val, ctx, &e))
13
+ if (expr__parse(&val, ctx, e, 1))
1214 TEST_ASSERT_VAL("parse test failed", 0);
1315 TEST_ASSERT_VAL("unexpected value", val == val2);
1416 return 0;
....@@ -16,16 +18,15 @@
1618
1719 int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused)
1820 {
21
+ struct expr_id_data *val_ptr;
1922 const char *p;
20
- const char **other;
2123 double val;
22
- int i, ret;
23
- struct parse_ctx ctx;
24
- int num_other;
24
+ int ret;
25
+ struct expr_parse_ctx ctx;
2526
2627 expr__ctx_init(&ctx);
27
- expr__add_id(&ctx, "FOO", 1);
28
- expr__add_id(&ctx, "BAR", 2);
28
+ expr__add_id_val(&ctx, strdup("FOO"), 1);
29
+ expr__add_id_val(&ctx, strdup("BAR"), 2);
2930
3031 ret = test(&ctx, "1+1", 2);
3132 ret |= test(&ctx, "FOO+BAR", 3);
....@@ -37,29 +38,51 @@
3738 ret |= test(&ctx, "min(1,2) + 1", 2);
3839 ret |= test(&ctx, "max(1,2) + 1", 3);
3940 ret |= test(&ctx, "1+1 if 3*4 else 0", 2);
41
+ ret |= test(&ctx, "1.1 + 2.1", 3.2);
42
+ ret |= test(&ctx, ".1 + 2.", 2.1);
43
+ ret |= test(&ctx, "d_ratio(1, 2)", 0.5);
44
+ ret |= test(&ctx, "d_ratio(2.5, 0)", 0);
45
+ ret |= test(&ctx, "1.1 < 2.2", 1);
46
+ ret |= test(&ctx, "2.2 > 1.1", 1);
47
+ ret |= test(&ctx, "1.1 < 1.1", 0);
48
+ ret |= test(&ctx, "2.2 > 2.2", 0);
49
+ ret |= test(&ctx, "2.2 < 1.1", 0);
50
+ ret |= test(&ctx, "1.1 > 2.2", 0);
4051
4152 if (ret)
4253 return ret;
4354
4455 p = "FOO/0";
45
- ret = expr__parse(&val, &ctx, &p);
46
- TEST_ASSERT_VAL("division by zero", ret == 1);
56
+ ret = expr__parse(&val, &ctx, p, 1);
57
+ TEST_ASSERT_VAL("division by zero", ret == -1);
4758
4859 p = "BAR/";
49
- ret = expr__parse(&val, &ctx, &p);
50
- TEST_ASSERT_VAL("missing operand", ret == 1);
60
+ ret = expr__parse(&val, &ctx, p, 1);
61
+ TEST_ASSERT_VAL("missing operand", ret == -1);
5162
63
+ expr__ctx_clear(&ctx);
5264 TEST_ASSERT_VAL("find other",
53
- expr__find_other("FOO + BAR + BAZ + BOZO", "FOO", &other, &num_other) == 0);
54
- TEST_ASSERT_VAL("find other", num_other == 3);
55
- TEST_ASSERT_VAL("find other", !strcmp(other[0], "BAR"));
56
- TEST_ASSERT_VAL("find other", !strcmp(other[1], "BAZ"));
57
- TEST_ASSERT_VAL("find other", !strcmp(other[2], "BOZO"));
58
- TEST_ASSERT_VAL("find other", other[3] == NULL);
65
+ expr__find_other("FOO + BAR + BAZ + BOZO", "FOO",
66
+ &ctx, 1) == 0);
67
+ TEST_ASSERT_VAL("find other", hashmap__size(&ctx.ids) == 3);
68
+ TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BAR",
69
+ (void **)&val_ptr));
70
+ TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BAZ",
71
+ (void **)&val_ptr));
72
+ TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BOZO",
73
+ (void **)&val_ptr));
5974
60
- for (i = 0; i < num_other; i++)
61
- free((void *)other[i]);
62
- free((void *)other);
75
+ expr__ctx_clear(&ctx);
76
+ TEST_ASSERT_VAL("find other",
77
+ expr__find_other("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@",
78
+ NULL, &ctx, 3) == 0);
79
+ TEST_ASSERT_VAL("find other", hashmap__size(&ctx.ids) == 2);
80
+ TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "EVENT1,param=3/",
81
+ (void **)&val_ptr));
82
+ TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "EVENT2,param=3/",
83
+ (void **)&val_ptr));
84
+
85
+ expr__ctx_clear(&ctx);
6386
6487 return 0;
6588 }