X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftal%2Fstr%2Ftest%2Frun-strreg.c;h=6d126f3970a13acdbed21cbb63e0115e1aa9d477;hp=a2a7dd95ae7410a752d717442c45d50d9d2da5f8;hb=97d99004bec31012400b6c1d9bad045ae1c9b075;hpb=d873aaec1339baf45c37db7cbaa2d687656343ba diff --git a/ccan/tal/str/test/run-strreg.c b/ccan/tal/str/test/run-strreg.c index a2a7dd95..6d126f39 100644 --- a/ccan/tal/str/test/run-strreg.c +++ b/ccan/tal/str/test/run-strreg.c @@ -1,16 +1,7 @@ #include #include #include - -static unsigned int tal_total_blocks(tal_t *ctx) -{ - unsigned int num = 1; - tal_t *i; - - for (i = tal_first(ctx); i; i = tal_next(ctx, i)) - num++; - return num; -} +#include "helper.h" static bool find_parent(tal_t *child, tal_t *parent) { @@ -26,30 +17,29 @@ static bool find_parent(tal_t *child, tal_t *parent) int main(int argc, char *argv[]) { void *ctx = tal_strdup(NULL, "toplevel"); - unsigned int top_blocks = tal_total_blocks(ctx); char *a, *b; /* If it accesses this, it will crash. */ char **invalid = (char **)1L; - plan_tests(25); + plan_tests(40); /* Simple matching. */ - ok1(strreg(ctx, "hello world!", "hello") == true); - ok1(strreg(ctx, "hello world!", "hi") == false); + ok1(tal_strreg(ctx, "hello world!", "hello") == true); + ok1(tal_strreg(ctx, "hello world!", "hi") == false); /* No parentheses means we don't use any extra args. */ - ok1(strreg(ctx, "hello world!", "hello", invalid) == true); - ok1(strreg(ctx, "hello world!", "hi", invalid) == false); + ok1(tal_strreg(ctx, "hello world!", "hello", invalid) == true); + ok1(tal_strreg(ctx, "hello world!", "hi", invalid) == false); - ok1(strreg(ctx, "hello world!", "[a-z]+", invalid) == true); - ok1(strreg(ctx, "hello world!", "([a-z]+)", &a, invalid) == true); + ok1(tal_strreg(ctx, "hello world!", "[a-z]+", invalid) == true); + ok1(tal_strreg(ctx, "hello world!", "([a-z]+)", &a, invalid) == true); /* Found string */ ok1(streq(a, "hello")); /* Allocated off ctx */ ok1(find_parent(a, ctx)); tal_free(a); - ok1(strreg(ctx, "hello world!", "([a-z]*) ([a-z]+)", - &a, &b, invalid) == true); + ok1(tal_strreg(ctx, "hello world!", "([a-z]*) ([a-z]+)", + &a, &b, invalid) == true); ok1(streq(a, "hello")); ok1(streq(b, "world")); ok1(find_parent(a, ctx)); @@ -58,37 +48,72 @@ int main(int argc, char *argv[]) tal_free(b); /* * after parentheses returns last match. */ - ok1(strreg(ctx, "hello world!", "([a-z])* ([a-z]+)", - &a, &b, invalid) == true); + ok1(tal_strreg(ctx, "hello world!", "([a-z])* ([a-z]+)", + &a, &b, invalid) == true); ok1(streq(a, "o")); ok1(streq(b, "world")); tal_free(a); tal_free(b); /* Nested parentheses are ordered by open brace. */ - ok1(strreg(ctx, "hello world!", "(([a-z]*) world)", - &a, &b, invalid) == true); + ok1(tal_strreg(ctx, "hello world!", "(([a-z]*) world)", + &a, &b, invalid) == true); ok1(streq(a, "hello world")); ok1(streq(b, "hello")); tal_free(a); tal_free(b); /* Nested parentheses are ordered by open brace. */ - ok1(strreg(ctx, "hello world!", "(([a-z]*) world)", - &a, &b, invalid) == true); + ok1(tal_strreg(ctx, "hello world!", "(([a-z]*) world)", + &a, &b, invalid) == true); ok1(streq(a, "hello world")); ok1(streq(b, "hello")); tal_free(a); tal_free(b); /* NULL means we're not interested. */ - ok1(strreg(ctx, "hello world!", "((hello|goodbye) world)", - &a, NULL, invalid) == true); + ok1(tal_strreg(ctx, "hello world!", "((hello|goodbye) world)", + &a, NULL, invalid) == true); ok1(streq(a, "hello world")); tal_free(a); /* No leaks! */ - ok1(tal_total_blocks(ctx) == top_blocks); + ok1(no_children(ctx)); + + /* NULL arg with take means always fail. */ + ok1(tal_strreg(ctx, take(NULL), "((hello|goodbye) world)", + &b, NULL, invalid) == false); + + /* Take string. */ + a = tal_strdup(ctx, "hello world!"); + ok1(tal_strreg(ctx, take(a), "([a-z]+)", &b, invalid) == true); + ok1(streq(b, "hello")); + ok1(tal_parent(b) == ctx); + tal_free(b); + ok1(no_children(ctx)); + + /* Take regex. */ + a = tal_strdup(ctx, "([a-z]+)"); + ok1(tal_strreg(ctx, "hello world!", take(a), &b, invalid) == true); + ok1(streq(b, "hello")); + ok1(tal_parent(b) == ctx); + tal_free(b); + ok1(no_children(ctx)); + + /* Take both. */ + a = tal_strdup(ctx, "([a-z]+)"); + ok1(tal_strreg(ctx, take(tal_strdup(ctx, "hello world!")), + take(a), &b, invalid) == true); + ok1(streq(b, "hello")); + ok1(tal_parent(b) == ctx); + tal_free(b); + ok1(no_children(ctx)); + + /* ... even if we fail to match. */ + a = tal_strdup(ctx, "([a-z]+)"); + ok1(tal_strreg(ctx, take(tal_strdup(ctx, "HELLO WORLD!")), + take(a), &b, invalid) == false); + ok1(no_children(ctx)); tal_free(ctx); return exit_status();