expr.c (7e06a5e30a0c5155291efab8cf866ffea052f829) expr.c (114a9d6e396eeb061fa532803ff9a6fd3a966ad8)
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>
6#include <string.h>
7#include <linux/zalloc.h>
8
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>
6#include <string.h>
7#include <linux/zalloc.h>
8
9static int test_ids_union(void)
10{
11 struct hashmap *ids1, *ids2;
12
13 /* Empty union. */
14 ids1 = ids__new();
15 TEST_ASSERT_VAL("ids__new", ids1);
16 ids2 = ids__new();
17 TEST_ASSERT_VAL("ids__new", ids2);
18
19 ids1 = ids__union(ids1, ids2);
20 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 0);
21
22 /* Union {foo, bar} against {}. */
23 ids2 = ids__new();
24 TEST_ASSERT_VAL("ids__new", ids2);
25
26 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("foo"), NULL), 0);
27 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("bar"), NULL), 0);
28
29 ids1 = ids__union(ids1, ids2);
30 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2);
31
32 /* Union {foo, bar} against {foo}. */
33 ids2 = ids__new();
34 TEST_ASSERT_VAL("ids__new", ids2);
35 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("foo"), NULL), 0);
36
37 ids1 = ids__union(ids1, ids2);
38 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2);
39
40 /* Union {foo, bar} against {bar,baz}. */
41 ids2 = ids__new();
42 TEST_ASSERT_VAL("ids__new", ids2);
43 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("bar"), NULL), 0);
44 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("baz"), NULL), 0);
45
46 ids1 = ids__union(ids1, ids2);
47 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 3);
48
49 ids__free(ids1);
50
51 return 0;
52}
53
9static int test(struct expr_parse_ctx *ctx, const char *e, double val2)
10{
11 double val;
12
13 if (expr__parse(&val, ctx, e, 1))
14 TEST_ASSERT_VAL("parse test failed", 0);
15 TEST_ASSERT_VAL("unexpected value", val == val2);
16 return 0;
17}
18
19int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused)
20{
21 struct expr_id_data *val_ptr;
22 const char *p;
23 double val;
24 int ret;
25 struct expr_parse_ctx *ctx;
26
54static int test(struct expr_parse_ctx *ctx, const char *e, double val2)
55{
56 double val;
57
58 if (expr__parse(&val, ctx, e, 1))
59 TEST_ASSERT_VAL("parse test failed", 0);
60 TEST_ASSERT_VAL("unexpected value", val == val2);
61 return 0;
62}
63
64int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused)
65{
66 struct expr_id_data *val_ptr;
67 const char *p;
68 double val;
69 int ret;
70 struct expr_parse_ctx *ctx;
71
72 TEST_ASSERT_EQUAL("ids_union", test_ids_union(), 0);
73
27 ctx = expr__ctx_new();
28 TEST_ASSERT_VAL("expr__ctx_new", ctx);
29 expr__add_id_val(ctx, strdup("FOO"), 1);
30 expr__add_id_val(ctx, strdup("BAR"), 2);
31
32 ret = test(ctx, "1+1", 2);
33 ret |= test(ctx, "FOO+BAR", 3);
34 ret |= test(ctx, "(BAR/2)%2", 1);

--- 57 unchanged lines hidden ---
74 ctx = expr__ctx_new();
75 TEST_ASSERT_VAL("expr__ctx_new", ctx);
76 expr__add_id_val(ctx, strdup("FOO"), 1);
77 expr__add_id_val(ctx, strdup("BAR"), 2);
78
79 ret = test(ctx, "1+1", 2);
80 ret |= test(ctx, "FOO+BAR", 3);
81 ret |= test(ctx, "(BAR/2)%2", 1);

--- 57 unchanged lines hidden ---