expr.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. static int test(struct expr_parse_ctx *ctx, const char *e, double val2)
  9. {
  10. double val;
  11. if (expr__parse(&val, ctx, e, 1))
  12. TEST_ASSERT_VAL("parse test failed", 0);
  13. TEST_ASSERT_VAL("unexpected value", val == val2);
  14. return 0;
  15. }
  16. int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused)
  17. {
  18. struct expr_id_data *val_ptr;
  19. const char *p;
  20. double val;
  21. int ret;
  22. struct expr_parse_ctx ctx;
  23. expr__ctx_init(&ctx);
  24. expr__add_id_val(&ctx, strdup("FOO"), 1);
  25. expr__add_id_val(&ctx, strdup("BAR"), 2);
  26. ret = test(&ctx, "1+1", 2);
  27. ret |= test(&ctx, "FOO+BAR", 3);
  28. ret |= test(&ctx, "(BAR/2)%2", 1);
  29. ret |= test(&ctx, "1 - -4", 5);
  30. ret |= test(&ctx, "(FOO-1)*2 + (BAR/2)%2 - -4", 5);
  31. ret |= test(&ctx, "1-1 | 1", 1);
  32. ret |= test(&ctx, "1-1 & 1", 0);
  33. ret |= test(&ctx, "min(1,2) + 1", 2);
  34. ret |= test(&ctx, "max(1,2) + 1", 3);
  35. ret |= test(&ctx, "1+1 if 3*4 else 0", 2);
  36. ret |= test(&ctx, "1.1 + 2.1", 3.2);
  37. ret |= test(&ctx, ".1 + 2.", 2.1);
  38. ret |= test(&ctx, "d_ratio(1, 2)", 0.5);
  39. ret |= test(&ctx, "d_ratio(2.5, 0)", 0);
  40. ret |= test(&ctx, "1.1 < 2.2", 1);
  41. ret |= test(&ctx, "2.2 > 1.1", 1);
  42. ret |= test(&ctx, "1.1 < 1.1", 0);
  43. ret |= test(&ctx, "2.2 > 2.2", 0);
  44. ret |= test(&ctx, "2.2 < 1.1", 0);
  45. ret |= test(&ctx, "1.1 > 2.2", 0);
  46. if (ret)
  47. return ret;
  48. p = "FOO/0";
  49. ret = expr__parse(&val, &ctx, p, 1);
  50. TEST_ASSERT_VAL("division by zero", ret == -1);
  51. p = "BAR/";
  52. ret = expr__parse(&val, &ctx, p, 1);
  53. TEST_ASSERT_VAL("missing operand", ret == -1);
  54. expr__ctx_clear(&ctx);
  55. TEST_ASSERT_VAL("find other",
  56. expr__find_other("FOO + BAR + BAZ + BOZO", "FOO",
  57. &ctx, 1) == 0);
  58. TEST_ASSERT_VAL("find other", hashmap__size(&ctx.ids) == 3);
  59. TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BAR",
  60. (void **)&val_ptr));
  61. TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BAZ",
  62. (void **)&val_ptr));
  63. TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "BOZO",
  64. (void **)&val_ptr));
  65. expr__ctx_clear(&ctx);
  66. TEST_ASSERT_VAL("find other",
  67. expr__find_other("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@",
  68. NULL, &ctx, 3) == 0);
  69. TEST_ASSERT_VAL("find other", hashmap__size(&ctx.ids) == 2);
  70. TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "EVENT1,param=3/",
  71. (void **)&val_ptr));
  72. TEST_ASSERT_VAL("find other", hashmap__find(&ctx.ids, "EVENT2,param=3/",
  73. (void **)&val_ptr));
  74. expr__ctx_clear(&ctx);
  75. return 0;
  76. }