]> git.ozlabs.org Git - ccan/blobdiff - ccan/tal/str/test/run-strreg.c
tal/str: always create strings which have tal_count() == strlen() + 1.
[ccan] / ccan / tal / str / test / run-strreg.c
index 47671efd9270ec4a65d2ec7253fb65db5f31a257..55e244c61e7bba769326a331dee91622bcdca478 100644 (file)
@@ -1,6 +1,7 @@
 #include <ccan/tal/str/str.h>
 #include <ccan/tal/str/str.c>
 #include <ccan/tap/tap.h>
+#include "helper.h"
 
 static bool find_parent(tal_t *child, tal_t *parent)
 {
@@ -13,14 +14,14 @@ static bool find_parent(tal_t *child, tal_t *parent)
        return false;
 }
 
-int main(int argc, char *argv[])
+int main(void)
 {
        void *ctx = tal_strdup(NULL, "toplevel");
        char *a, *b;
        /* If it accesses this, it will crash. */
        char **invalid = (char **)1L;
 
-       plan_tests(40);
+       plan_tests(54);
        /* Simple matching. */
        ok1(tal_strreg(ctx, "hello world!", "hello") == true);
        ok1(tal_strreg(ctx, "hello world!", "hi") == false);
@@ -35,12 +36,15 @@ int main(int argc, char *argv[])
        ok1(streq(a, "hello"));
        /* Allocated off ctx */
        ok1(find_parent(a, ctx));
+       ok1(tal_count(a) == strlen(a) + 1);
        tal_free(a);
 
        ok1(tal_strreg(ctx, "hello world!", "([a-z]*) ([a-z]+)",
                       &a, &b, invalid) == true);
        ok1(streq(a, "hello"));
        ok1(streq(b, "world"));
+       ok1(tal_count(a) == strlen(a) + 1);
+       ok1(tal_count(b) == strlen(b) + 1);
        ok1(find_parent(a, ctx));
        ok1(find_parent(b, ctx));
        tal_free(a);
@@ -51,6 +55,8 @@ int main(int argc, char *argv[])
                       &a, &b, invalid) == true);
        ok1(streq(a, "o"));
        ok1(streq(b, "world"));
+       ok1(tal_count(a) == strlen(a) + 1);
+       ok1(tal_count(b) == strlen(b) + 1);
        tal_free(a);
        tal_free(b);
 
@@ -59,6 +65,8 @@ int main(int argc, char *argv[])
                       &a, &b, invalid) == true);
        ok1(streq(a, "hello world"));
        ok1(streq(b, "hello"));
+       ok1(tal_count(a) == strlen(a) + 1);
+       ok1(tal_count(b) == strlen(b) + 1);
        tal_free(a);
        tal_free(b);
 
@@ -67,6 +75,8 @@ int main(int argc, char *argv[])
                       &a, &b, invalid) == true);
        ok1(streq(a, "hello world"));
        ok1(streq(b, "hello"));
+       ok1(tal_count(a) == strlen(a) + 1);
+       ok1(tal_count(b) == strlen(b) + 1);
        tal_free(a);
        tal_free(b);
 
@@ -74,10 +84,11 @@ int main(int argc, char *argv[])
        ok1(tal_strreg(ctx, "hello world!", "((hello|goodbye) world)",
                       &a, NULL, invalid) == true);
        ok1(streq(a, "hello world"));
+       ok1(tal_count(a) == strlen(a) + 1);
        tal_free(a);
 
        /* No leaks! */
-       ok1(!tal_first(ctx));
+       ok1(no_children(ctx));
 
        /* NULL arg with take means always fail. */
        ok1(tal_strreg(ctx, take(NULL), "((hello|goodbye) world)",
@@ -87,33 +98,40 @@ int main(int argc, char *argv[])
        a = tal_strdup(ctx, "hello world!");
        ok1(tal_strreg(ctx, take(a), "([a-z]+)", &b, invalid) == true);
        ok1(streq(b, "hello"));
+       ok1(tal_count(b) == strlen(b) + 1);
        ok1(tal_parent(b) == ctx);
        tal_free(b);
-       ok1(tal_first(ctx) == NULL);
+       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_count(b) == strlen(b) + 1);
        ok1(tal_parent(b) == ctx);
        tal_free(b);
-       ok1(tal_first(ctx) == NULL);
+       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_count(b) == strlen(b) + 1);
        ok1(tal_parent(b) == ctx);
        tal_free(b);
-       ok1(tal_first(ctx) == NULL);
+       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(tal_first(ctx) == NULL);
+       ok1(no_children(ctx));
        tal_free(ctx);
 
+       /* Don't get fooled by \(! */
+       ok1(tal_strreg(ctx, "(hello) (world)!", "\\([a-z]*\\) \\([a-z]+\\)",
+                      invalid) == true);
+
        return exit_status();
 }