107516736SAndi Kleen #include "util/debug.h" 207516736SAndi Kleen #include "util/expr.h" 307516736SAndi Kleen #include "tests.h" 407516736SAndi Kleen #include <stdlib.h> 507516736SAndi Kleen 607516736SAndi Kleen static int test(struct parse_ctx *ctx, const char *e, double val2) 707516736SAndi Kleen { 807516736SAndi Kleen double val; 907516736SAndi Kleen 1007516736SAndi Kleen if (expr__parse(&val, ctx, &e)) 1107516736SAndi Kleen TEST_ASSERT_VAL("parse test failed", 0); 1207516736SAndi Kleen TEST_ASSERT_VAL("unexpected value", val == val2); 1307516736SAndi Kleen return 0; 1407516736SAndi Kleen } 1507516736SAndi Kleen 1681f17c90SArnaldo Carvalho de Melo int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused) 1707516736SAndi Kleen { 1807516736SAndi Kleen const char *p; 1907516736SAndi Kleen const char **other; 2007516736SAndi Kleen double val; 2107516736SAndi Kleen int ret; 2207516736SAndi Kleen struct parse_ctx ctx; 2307516736SAndi Kleen int num_other; 2407516736SAndi Kleen 2507516736SAndi Kleen expr__ctx_init(&ctx); 2607516736SAndi Kleen expr__add_id(&ctx, "FOO", 1); 2707516736SAndi Kleen expr__add_id(&ctx, "BAR", 2); 2807516736SAndi Kleen 2907516736SAndi Kleen ret = test(&ctx, "1+1", 2); 3007516736SAndi Kleen ret |= test(&ctx, "FOO+BAR", 3); 3107516736SAndi Kleen ret |= test(&ctx, "(BAR/2)%2", 1); 3207516736SAndi Kleen ret |= test(&ctx, "1 - -4", 5); 3307516736SAndi Kleen ret |= test(&ctx, "(FOO-1)*2 + (BAR/2)%2 - -4", 5); 3407516736SAndi Kleen 3507516736SAndi Kleen if (ret) 3607516736SAndi Kleen return ret; 3707516736SAndi Kleen 3807516736SAndi Kleen p = "FOO/0"; 3907516736SAndi Kleen ret = expr__parse(&val, &ctx, &p); 4007516736SAndi Kleen TEST_ASSERT_VAL("division by zero", ret == 1); 4107516736SAndi Kleen 4207516736SAndi Kleen p = "BAR/"; 4307516736SAndi Kleen ret = expr__parse(&val, &ctx, &p); 4407516736SAndi Kleen TEST_ASSERT_VAL("missing operand", ret == 1); 4507516736SAndi Kleen 4607516736SAndi Kleen TEST_ASSERT_VAL("find other", 4707516736SAndi Kleen expr__find_other("FOO + BAR + BAZ + BOZO", "FOO", &other, &num_other) == 0); 4807516736SAndi Kleen TEST_ASSERT_VAL("find other", num_other == 3); 4907516736SAndi Kleen TEST_ASSERT_VAL("find other", !strcmp(other[0], "BAR")); 5007516736SAndi Kleen TEST_ASSERT_VAL("find other", !strcmp(other[1], "BAZ")); 5107516736SAndi Kleen TEST_ASSERT_VAL("find other", !strcmp(other[2], "BOZO")); 5207516736SAndi Kleen TEST_ASSERT_VAL("find other", other[3] == NULL); 5307516736SAndi Kleen free((void *)other); 5407516736SAndi Kleen 5507516736SAndi Kleen return 0; 5607516736SAndi Kleen } 57