1 // SPDX-License-Identifier: GPL-2.0
2 #include "util/debug.h"
4 #include "util/header.h"
10 #include <linux/zalloc.h>
12 static int test_ids_union(void)
14 struct hashmap *ids1, *ids2;
18 TEST_ASSERT_VAL("ids__new", ids1);
20 TEST_ASSERT_VAL("ids__new", ids2);
22 ids1 = ids__union(ids1, ids2);
23 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 0);
25 /* Union {foo, bar} against {}. */
27 TEST_ASSERT_VAL("ids__new", ids2);
29 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("foo")), 0);
30 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("bar")), 0);
32 ids1 = ids__union(ids1, ids2);
33 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2);
35 /* Union {foo, bar} against {foo}. */
37 TEST_ASSERT_VAL("ids__new", ids2);
38 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("foo")), 0);
40 ids1 = ids__union(ids1, ids2);
41 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2);
43 /* Union {foo, bar} against {bar,baz}. */
45 TEST_ASSERT_VAL("ids__new", ids2);
46 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("bar")), 0);
47 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("baz")), 0);
49 ids1 = ids__union(ids1, ids2);
50 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 3);
57 static int test(struct expr_parse_ctx *ctx, const char *e, double val2)
61 if (expr__parse(&val, ctx, e))
62 TEST_ASSERT_VAL("parse test failed", 0);
63 TEST_ASSERT_VAL("unexpected value", val == val2);
67 static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
69 struct expr_id_data *val_ptr;
71 double val, num_cpus, num_cores, num_dies, num_packages;
73 struct expr_parse_ctx *ctx;
74 bool is_intel = false;
77 if (!get_cpuid(buf, sizeof(buf)))
78 is_intel = strstr(buf, "Intel") != NULL;
80 TEST_ASSERT_EQUAL("ids_union", test_ids_union(), 0);
82 ctx = expr__ctx_new();
83 TEST_ASSERT_VAL("expr__ctx_new", ctx);
84 expr__add_id_val(ctx, strdup("FOO"), 1);
85 expr__add_id_val(ctx, strdup("BAR"), 2);
87 ret = test(ctx, "1+1", 2);
88 ret |= test(ctx, "FOO+BAR", 3);
89 ret |= test(ctx, "(BAR/2)%2", 1);
90 ret |= test(ctx, "1 - -4", 5);
91 ret |= test(ctx, "(FOO-1)*2 + (BAR/2)%2 - -4", 5);
92 ret |= test(ctx, "1-1 | 1", 1);
93 ret |= test(ctx, "1-1 & 1", 0);
94 ret |= test(ctx, "min(1,2) + 1", 2);
95 ret |= test(ctx, "max(1,2) + 1", 3);
96 ret |= test(ctx, "1+1 if 3*4 else 0", 2);
97 ret |= test(ctx, "1.1 + 2.1", 3.2);
98 ret |= test(ctx, ".1 + 2.", 2.1);
99 ret |= test(ctx, "d_ratio(1, 2)", 0.5);
100 ret |= test(ctx, "d_ratio(2.5, 0)", 0);
101 ret |= test(ctx, "1.1 < 2.2", 1);
102 ret |= test(ctx, "2.2 > 1.1", 1);
103 ret |= test(ctx, "1.1 < 1.1", 0);
104 ret |= test(ctx, "2.2 > 2.2", 0);
105 ret |= test(ctx, "2.2 < 1.1", 0);
106 ret |= test(ctx, "1.1 > 2.2", 0);
107 ret |= test(ctx, "1.1e10 < 1.1e100", 1);
108 ret |= test(ctx, "1.1e2 > 1.1e-2", 1);
116 ret = expr__parse(&val, ctx, p);
117 TEST_ASSERT_VAL("division by zero", ret == -1);
120 ret = expr__parse(&val, ctx, p);
121 TEST_ASSERT_VAL("missing operand", ret == -1);
123 expr__ctx_clear(ctx);
124 TEST_ASSERT_VAL("find ids",
125 expr__find_ids("FOO + BAR + BAZ + BOZO", "FOO",
127 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 3);
128 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAR",
130 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAZ",
132 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BOZO",
135 expr__ctx_clear(ctx);
137 TEST_ASSERT_VAL("find ids",
138 expr__find_ids("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@",
140 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2);
141 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1,param=3@",
143 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT2,param=3@",
146 expr__ctx_clear(ctx);
147 TEST_ASSERT_VAL("find ids",
148 expr__find_ids("dash\\-event1 - dash\\-event2",
150 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2);
151 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event1",
153 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event2",
156 /* Only EVENT1 or EVENT2 need be measured depending on the value of smt_on. */
157 expr__ctx_clear(ctx);
158 TEST_ASSERT_VAL("find ids",
159 expr__find_ids("EVENT1 if #smt_on else EVENT2",
161 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
162 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids,
163 smt_on() ? "EVENT1" : "EVENT2",
166 /* The expression is a constant 1.0 without needing to evaluate EVENT1. */
167 expr__ctx_clear(ctx);
168 TEST_ASSERT_VAL("find ids",
169 expr__find_ids("1.0 if EVENT1 > 100.0 else 1.0",
171 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
173 /* Test toplogy constants appear well ordered. */
174 expr__ctx_clear(ctx);
175 TEST_ASSERT_VAL("#num_cpus", expr__parse(&num_cpus, ctx, "#num_cpus") == 0);
176 TEST_ASSERT_VAL("#num_cores", expr__parse(&num_cores, ctx, "#num_cores") == 0);
177 TEST_ASSERT_VAL("#num_cpus >= #num_cores", num_cpus >= num_cores);
178 TEST_ASSERT_VAL("#num_dies", expr__parse(&num_dies, ctx, "#num_dies") == 0);
179 TEST_ASSERT_VAL("#num_cores >= #num_dies", num_cores >= num_dies);
180 TEST_ASSERT_VAL("#num_packages", expr__parse(&num_packages, ctx, "#num_packages") == 0);
182 if (num_dies) // Some platforms do not have CPU die support, for example s390
183 TEST_ASSERT_VAL("#num_dies >= #num_packages", num_dies >= num_packages);
185 TEST_ASSERT_VAL("#system_tsc_freq", expr__parse(&val, ctx, "#system_tsc_freq") == 0);
187 TEST_ASSERT_VAL("#system_tsc_freq > 0", val > 0);
189 TEST_ASSERT_VAL("#system_tsc_freq == 0", fpclassify(val) == FP_ZERO);
192 * Source count returns the number of events aggregating in a leader
193 * event including the leader. Check parsing yields an id.
195 expr__ctx_clear(ctx);
196 TEST_ASSERT_VAL("source count",
197 expr__find_ids("source_count(EVENT1)",
199 TEST_ASSERT_VAL("source count", hashmap__size(ctx->ids) == 1);
200 TEST_ASSERT_VAL("source count", hashmap__find(ctx->ids, "EVENT1",
208 DEFINE_SUITE("Simple expression parser", expr);