run-strreg.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <ccan/str_talloc/str_talloc.h>
  2. #include <ccan/str_talloc/str_talloc.c>
  3. #include <ccan/tap/tap.h>
  4. int main(int argc, char *argv[])
  5. {
  6. void *ctx = talloc_init("toplevel");
  7. unsigned int top_blocks = talloc_total_blocks(ctx);
  8. char *a, *b;
  9. /* If it accesses this, it will crash. */
  10. char **invalid = (char **)1L;
  11. plan_tests(25);
  12. /* Simple matching. */
  13. ok1(strreg(ctx, "hello world!", "hello") == true);
  14. ok1(strreg(ctx, "hello world!", "hi") == false);
  15. /* No parentheses means we don't use any extra args. */
  16. ok1(strreg(ctx, "hello world!", "hello", invalid) == true);
  17. ok1(strreg(ctx, "hello world!", "hi", invalid) == false);
  18. ok1(strreg(ctx, "hello world!", "[a-z]+", invalid) == true);
  19. ok1(strreg(ctx, "hello world!", "([a-z]+)", &a, invalid) == true);
  20. /* Found string */
  21. ok1(streq(a, "hello"));
  22. /* Allocated off ctx */
  23. ok1(talloc_find_parent_byname(a, "toplevel") == ctx);
  24. talloc_free(a);
  25. ok1(strreg(ctx, "hello world!", "([a-z]*) ([a-z]+)",
  26. &a, &b, invalid) == true);
  27. ok1(streq(a, "hello"));
  28. ok1(streq(b, "world"));
  29. ok1(talloc_find_parent_byname(a, "toplevel") == ctx);
  30. ok1(talloc_find_parent_byname(b, "toplevel") == ctx);
  31. talloc_free(a);
  32. talloc_free(b);
  33. /* * after parentheses returns last match. */
  34. ok1(strreg(ctx, "hello world!", "([a-z])* ([a-z]+)",
  35. &a, &b, invalid) == true);
  36. ok1(streq(a, "o"));
  37. ok1(streq(b, "world"));
  38. talloc_free(a);
  39. talloc_free(b);
  40. /* Nested parentheses are ordered by open brace. */
  41. ok1(strreg(ctx, "hello world!", "(([a-z]*) world)",
  42. &a, &b, invalid) == true);
  43. ok1(streq(a, "hello world"));
  44. ok1(streq(b, "hello"));
  45. talloc_free(a);
  46. talloc_free(b);
  47. /* Nested parentheses are ordered by open brace. */
  48. ok1(strreg(ctx, "hello world!", "(([a-z]*) world)",
  49. &a, &b, invalid) == true);
  50. ok1(streq(a, "hello world"));
  51. ok1(streq(b, "hello"));
  52. talloc_free(a);
  53. talloc_free(b);
  54. /* NULL means we're not interested. */
  55. ok1(strreg(ctx, "hello world!", "((hello|goodbye) world)",
  56. &a, NULL, invalid) == true);
  57. ok1(streq(a, "hello world"));
  58. talloc_free(a);
  59. /* No leaks! */
  60. ok1(talloc_total_blocks(ctx) == top_blocks);
  61. talloc_free(ctx);
  62. talloc_disable_null_tracking();
  63. return exit_status();
  64. }