Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | #include "util/debug.h" |
| 3 | #include "util/expr.h" |
| 4 | #include "tests.h" |
| 5 | #include <stdlib.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6 | #include <string.h> |
| 7 | #include <linux/zalloc.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9 | static int test(struct expr_parse_ctx *ctx, const char *e, double val2) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10 | { |
| 11 | double val; |
| 12 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 13 | if (expr__parse(&val, ctx, e, 1)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 14 | TEST_ASSERT_VAL("parse test failed", 0); |
| 15 | TEST_ASSERT_VAL("unexpected value", val == val2); |
| 16 | return 0; |
| 17 | } |
| 18 | |
| 19 | int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused) |
| 20 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 21 | struct expr_id_data *val_ptr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 22 | const char *p; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 23 | double val; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 24 | int ret; |
| 25 | struct expr_parse_ctx ctx; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 26 | |
| 27 | expr__ctx_init(&ctx); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 28 | expr__add_id_val(&ctx, strdup("FOO"), 1); |
| 29 | expr__add_id_val(&ctx, strdup("BAR"), 2); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 30 | |
| 31 | ret = test(&ctx, "1+1", 2); |
| 32 | ret |= test(&ctx, "FOO+BAR", 3); |
| 33 | ret |= test(&ctx, "(BAR/2)%2", 1); |
| 34 | ret |= test(&ctx, "1 - -4", 5); |
| 35 | ret |= test(&ctx, "(FOO-1)*2 + (BAR/2)%2 - -4", 5); |
| 36 | ret |= test(&ctx, "1-1 | 1", 1); |
| 37 | ret |= test(&ctx, "1-1 & 1", 0); |
| 38 | ret |= test(&ctx, "min(1,2) + 1", 2); |
| 39 | ret |= test(&ctx, "max(1,2) + 1", 3); |
| 40 | ret |= test(&ctx, "1+1 if 3*4 else 0", 2); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 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); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 51 | |
| 52 | if (ret) |
| 53 | return ret; |
| 54 | |
| 55 | p = "FOO/0"; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 56 | ret = expr__parse(&val, &ctx, p, 1); |
| 57 | TEST_ASSERT_VAL("division by zero", ret == -1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 58 | |
| 59 | p = "BAR/"; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 60 | ret = expr__parse(&val, &ctx, p, 1); |
| 61 | TEST_ASSERT_VAL("missing operand", ret == -1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 62 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 63 | expr__ctx_clear(&ctx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 64 | TEST_ASSERT_VAL("find other", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 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)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 74 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 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); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 86 | |
| 87 | return 0; |
| 88 | } |