blob: 6c6a3749aaf6bf5e7f54585e2872bcdcfc12c0a3 [file] [log] [blame]
Andi Kleen07516732017-03-20 13:17:05 -07001#include "util/debug.h"
2#include "util/expr.h"
3#include "tests.h"
4#include <stdlib.h>
5
6static int test(struct parse_ctx *ctx, const char *e, double val2)
7{
8 double val;
9
10 if (expr__parse(&val, ctx, &e))
11 TEST_ASSERT_VAL("parse test failed", 0);
12 TEST_ASSERT_VAL("unexpected value", val == val2);
13 return 0;
14}
15
16int test__expr(int subtest __maybe_unused)
17{
18 const char *p;
19 const char **other;
20 double val;
21 int ret;
22 struct parse_ctx ctx;
23 int num_other;
24
25 expr__ctx_init(&ctx);
26 expr__add_id(&ctx, "FOO", 1);
27 expr__add_id(&ctx, "BAR", 2);
28
29 ret = test(&ctx, "1+1", 2);
30 ret |= test(&ctx, "FOO+BAR", 3);
31 ret |= test(&ctx, "(BAR/2)%2", 1);
32 ret |= test(&ctx, "1 - -4", 5);
33 ret |= test(&ctx, "(FOO-1)*2 + (BAR/2)%2 - -4", 5);
34
35 if (ret)
36 return ret;
37
38 p = "FOO/0";
39 ret = expr__parse(&val, &ctx, &p);
40 TEST_ASSERT_VAL("division by zero", ret == 1);
41
42 p = "BAR/";
43 ret = expr__parse(&val, &ctx, &p);
44 TEST_ASSERT_VAL("missing operand", ret == 1);
45
46 TEST_ASSERT_VAL("find other",
47 expr__find_other("FOO + BAR + BAZ + BOZO", "FOO", &other, &num_other) == 0);
48 TEST_ASSERT_VAL("find other", num_other == 3);
49 TEST_ASSERT_VAL("find other", !strcmp(other[0], "BAR"));
50 TEST_ASSERT_VAL("find other", !strcmp(other[1], "BAZ"));
51 TEST_ASSERT_VAL("find other", !strcmp(other[2], "BOZO"));
52 TEST_ASSERT_VAL("find other", other[3] == NULL);
53 free((void *)other);
54
55 return 0;
56}