1 // SPDX-License-Identifier: GPL-2.0
2 #include "util/debug.h"
7 static int test(struct parse_ctx *ctx, const char *e, double val2)
11 if (expr__parse(&val, ctx, &e))
12 TEST_ASSERT_VAL("parse test failed", 0);
13 TEST_ASSERT_VAL("unexpected value", val == val2);
17 int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused)
27 expr__add_id(&ctx, "FOO", 1);
28 expr__add_id(&ctx, "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);
45 ret = expr__parse(&val, &ctx, &p);
46 TEST_ASSERT_VAL("division by zero", ret == 1);
49 ret = expr__parse(&val, &ctx, &p);
50 TEST_ASSERT_VAL("missing operand", ret == 1);
52 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);
60 for (i = 0; i < num_other; i++)
61 free((void *)other[i]);