diff tests.c @ 613:2e3b81ae8c8a

More work on regexes.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 16 Jan 2020 16:01:27 +0200
parents a0e8d9c6300b
children afcaf5e38f56
line wrap: on
line diff
--- a/tests.c	Thu Jan 16 12:50:28 2020 +0200
+++ b/tests.c	Thu Jan 16 16:01:27 2020 +0200
@@ -524,7 +524,9 @@
 
     // Test ptr
     test_start(&ctx, "Test configuration string list ptr");
-    test_result(&ctx, (item = th_cfg_find(cfg, NULL, "string_list", -1)) != NULL && item->v.list == &v_str_list);
+    test_result(&ctx,
+        (item = th_cfg_find(cfg, NULL, "string_list", -1)) != NULL &&
+        item->v.list == &v_str_list);
     test_end(&ctx);
 
     // Test value finding
@@ -567,12 +569,12 @@
 } test_regex_def;
 
 
-void test_regex_list(const th_regex_char *pattern, const test_regex_def *list)
+void test_regex_list(const test_regex_def *list, const th_regex_char *pattern)
 {
     th_regex_ctx *reg = NULL;
     int res;
 
-
+    printf("========================================\n");
     printf("pattern '%s'\n", pattern);
     if ((res = th_regex_compile(&reg, pattern)) != THERR_OK)
     {
@@ -586,7 +588,8 @@
         th_regex_match_node *matches = NULL;
         size_t nmatches;
 
-        if ((res = th_regex_match(reg, def->str, &nmatches, &matches, -1, def->flags)) != THERR_OK)
+        if ((res = th_regex_match(reg, def->str,
+            &nmatches, &matches, -1, def->flags)) != THERR_OK)
         {
             THERR("Regex match returned error: %s\n",
                 th_error_str(res));
@@ -602,7 +605,7 @@
             m != NULL; m = (th_regex_match_node *) m->node.next)
         {
             char *tmp = th_strndup(def->str + m->start, m->len);
-            printf("  match [%" PRIu_SIZE_T " ++ %" PRIu_SIZE_T "]: '%s'\n",
+            printf("      match [%" PRIu_SIZE_T " ++ %" PRIu_SIZE_T "]: '%s'\n",
                 m->start, m->len, tmp);
             th_free(tmp);
         }
@@ -827,7 +830,7 @@
 #elif TH_ARCH == 64
             0xaabbccdd11223344;
 #else
-#error Unsupported TH_ARCH value.
+#    error Unsupported TH_ARCH value.
 #endif
 
         snprintf(tmp, sizeof(tmp), "%16" PRIx_SIZE_T "h", usiz);
@@ -873,20 +876,64 @@
         th_regex_ctx *reg = NULL;
         int res;
 
+#if 0
         res = th_regex_compile(&reg, "z*k+abba fabboa? k{4} [gz]{1,2} foo(bar|zoo)?");
         if (res != THERR_OK)
             printf("result: %s\n", th_error_str(res));
         th_regex_free(reg);
 
-        test_regex_def tst1[] =
+        //
+        {
+            static const test_regex_def tlist[] =
+            {
+                { "abcfoabccg"                   , 1, 0 },
+                { "abcbcfoabccg"                 , 1, 0 },
+                { "abcbcfoabccgabcbcfoabccg"     , 2, 0 },
+                { "ffdsafS abcbcfoabccg zasdf"   , 1, 0 },
+                { NULL                           , 0, 0 }
+            };
+
+            test_regex_list(tlist, "a(bc){1,2}fo[oab]*cc?g");
+        }
+
         {
-            { "abcfoabccg"        , 1, 0 },
-            { "abcbcfoabccg"      , 1, 0 },
-            { "abcbcfoabccgabcbcfoabccg"   , 2, 0 },
-            { NULL, 0, 0 }
-        };
+            static const test_regex_def tlist[] =
+            {
+                { "abcfoabccg"                   , 1, 0 },
+                { "abcbcfoabccg"                 , 1, 0 },
+                { "abcbcfoabccgabcbcfoabccg"     , 2, 0 },
+                { "ffdsafS abcbcfoabccg zasdf"   , 0, 0 },
+                { NULL                           , 0, 0 }
+            };
+
+            test_regex_list(tlist, "^a(bc){1,2}fo[oab]*cc?g");
+        }
 
-        test_regex_list("a(bc){1,2}fo[oab]*cc?g", tst1);
+        {
+            static const test_regex_def tlist[] =
+            {
+                { "cg"                           , 1, 0 },
+                { "g"                            , 1, 0 },
+                { ""                             , 0, 0 },
+                { "c"                            , 0, 0 },
+                { NULL                           , 0, 0 }
+            };
+
+            test_regex_list(tlist, "g$");
+        }
+#endif
+
+        {
+            static const test_regex_def tlist[] =
+            {
+//                { "zoobar"                       , 1, 0 },
+                { "zoo lol bar"                  , 1, 0 },
+//                { "hoho zoo lol lol bar bar"     , 1, 0 },
+                { NULL                           , 0, 0 }
+            };
+
+            test_regex_list(tlist, "zoo.*?bar");
+        }
     }
 
     //