18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include "util/debug.h"
38c2ecf20Sopenharmony_ci#include "util/expr.h"
48c2ecf20Sopenharmony_ci#include "tests.h"
58c2ecf20Sopenharmony_ci#include <stdlib.h>
68c2ecf20Sopenharmony_ci#include <string.h>
78c2ecf20Sopenharmony_ci#include <linux/zalloc.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_cistatic int test(struct expr_parse_ctx *ctx, const char *e, double val2)
108c2ecf20Sopenharmony_ci{
118c2ecf20Sopenharmony_ci	double val;
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci	if (expr__parse(&val, ctx, e, 1))
148c2ecf20Sopenharmony_ci		TEST_ASSERT_VAL("parse test failed", 0);
158c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("unexpected value", val == val2);
168c2ecf20Sopenharmony_ci	return 0;
178c2ecf20Sopenharmony_ci}
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ciint test__expr(struct test *t __maybe_unused, int subtest __maybe_unused)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	struct expr_id_data *val_ptr;
228c2ecf20Sopenharmony_ci	const char *p;
238c2ecf20Sopenharmony_ci	double val;
248c2ecf20Sopenharmony_ci	int ret;
258c2ecf20Sopenharmony_ci	struct expr_parse_ctx ctx;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	expr__ctx_init(&ctx);
288c2ecf20Sopenharmony_ci	expr__add_id_val(&ctx, strdup("FOO"), 1);
298c2ecf20Sopenharmony_ci	expr__add_id_val(&ctx, strdup("BAR"), 2);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	ret = test(&ctx, "1+1", 2);
328c2ecf20Sopenharmony_ci	ret |= test(&ctx, "FOO+BAR", 3);
338c2ecf20Sopenharmony_ci	ret |= test(&ctx, "(BAR/2)%2", 1);
348c2ecf20Sopenharmony_ci	ret |= test(&ctx, "1 - -4",  5);
358c2ecf20Sopenharmony_ci	ret |= test(&ctx, "(FOO-1)*2 + (BAR/2)%2 - -4",  5);
368c2ecf20Sopenharmony_ci	ret |= test(&ctx, "1-1 | 1", 1);
378c2ecf20Sopenharmony_ci	ret |= test(&ctx, "1-1 & 1", 0);
388c2ecf20Sopenharmony_ci	ret |= test(&ctx, "min(1,2) + 1", 2);
398c2ecf20Sopenharmony_ci	ret |= test(&ctx, "max(1,2) + 1", 3);
408c2ecf20Sopenharmony_ci	ret |= test(&ctx, "1+1 if 3*4 else 0", 2);
418c2ecf20Sopenharmony_ci	ret |= test(&ctx, "1.1 + 2.1", 3.2);
428c2ecf20Sopenharmony_ci	ret |= test(&ctx, ".1 + 2.", 2.1);
438c2ecf20Sopenharmony_ci	ret |= test(&ctx, "d_ratio(1, 2)", 0.5);
448c2ecf20Sopenharmony_ci	ret |= test(&ctx, "d_ratio(2.5, 0)", 0);
458c2ecf20Sopenharmony_ci	ret |= test(&ctx, "1.1 < 2.2", 1);
468c2ecf20Sopenharmony_ci	ret |= test(&ctx, "2.2 > 1.1", 1);
478c2ecf20Sopenharmony_ci	ret |= test(&ctx, "1.1 < 1.1", 0);
488c2ecf20Sopenharmony_ci	ret |= test(&ctx, "2.2 > 2.2", 0);
498c2ecf20Sopenharmony_ci	ret |= test(&ctx, "2.2 < 1.1", 0);
508c2ecf20Sopenharmony_ci	ret |= test(&ctx, "1.1 > 2.2", 0);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	if (ret)
538c2ecf20Sopenharmony_ci		return ret;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	p = "FOO/0";
568c2ecf20Sopenharmony_ci	ret = expr__parse(&val, &ctx, p, 1);
578c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("division by zero", ret == -1);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	p = "BAR/";
608c2ecf20Sopenharmony_ci	ret = expr__parse(&val, &ctx, p, 1);
618c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("missing operand", ret == -1);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	expr__ctx_clear(&ctx);
648c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("find other",
658c2ecf20Sopenharmony_ci			expr__find_other("FOO + BAR + BAZ + BOZO", "FOO",
668c2ecf20Sopenharmony_ci					 &ctx, 1) == 0);
678c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("find other", hashmap__size(&ctx.ids) == 3);
688c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BAR",
698c2ecf20Sopenharmony_ci						    (void **)&val_ptr));
708c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BAZ",
718c2ecf20Sopenharmony_ci						    (void **)&val_ptr));
728c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BOZO",
738c2ecf20Sopenharmony_ci						    (void **)&val_ptr));
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	expr__ctx_clear(&ctx);
768c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("find other",
778c2ecf20Sopenharmony_ci			expr__find_other("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@",
788c2ecf20Sopenharmony_ci					 NULL, &ctx, 3) == 0);
798c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("find other", hashmap__size(&ctx.ids) == 2);
808c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "EVENT1,param=3/",
818c2ecf20Sopenharmony_ci						    (void **)&val_ptr));
828c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "EVENT2,param=3/",
838c2ecf20Sopenharmony_ci						    (void **)&val_ptr));
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	expr__ctx_clear(&ctx);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	return 0;
888c2ecf20Sopenharmony_ci}
89