changeset 508:fe5e7bf704e5

Improvements to config tests.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 26 Dec 2019 08:06:38 +0200
parents 8bb0817210a0
children b506bff0a7ab
files tests.c
diffstat 1 files changed, 91 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/tests.c	Thu Dec 26 08:05:37 2019 +0200
+++ b/tests.c	Thu Dec 26 08:06:38 2019 +0200
@@ -17,6 +17,7 @@
     TST_SUPERFLUOUS = 0x0001,
     TST_CORNERCASE  = 0x0002,
     TST_OVERFLOW    = 0x0004,
+    TST_BROKEN      = 0x1000,
     TST_ALL         = 0xffff,
 };
 
@@ -314,45 +315,89 @@
     } while (0)
 
 
-void test_config_values(th_cfgitem_t *cfg)
+void test_config_values(th_cfgitem_t *cfg, int *nsubtest)
 {
     th_cfgitem_t *item;
+    test_ctx ctx;
 
-    test_ctx ctx;
-    test_start(&ctx, "Test configuration value search #1");
+    test_start(&ctx, "Test configuration value search #%d", ++(*nsubtest));
     test_result(&ctx, (item = th_cfg_find(cfg, "inside_sect", "intval", -1)) != NULL && *item->v.val_int == 112);
     test_end(&ctx);
-    test_start(&ctx, "Test configuration value search #2");
+    test_start(&ctx, "Test configuration value search #%d", ++(*nsubtest));
     test_result(&ctx, (item = th_cfg_find(cfg, "another_sect", "boolval", -1)) != NULL && *item->v.val_bool);
     test_end(&ctx);
-    test_start(&ctx, "Test configuration value search #3");
+    test_start(&ctx, "Test configuration value search #%d", ++(*nsubtest));
     test_result(&ctx, th_cfg_find(cfg, "no_match", NULL, -1) == NULL);
     test_end(&ctx);
-    test_start(&ctx, "Test configuration value search #4");
+    test_start(&ctx, "Test configuration value search #%d", ++(*nsubtest));
     test_result(&ctx, th_cfg_find(cfg, NULL, "no_match", -1) == NULL);
     test_end(&ctx);
-    test_start(&ctx, "Test configuration value search #5");
+    test_start(&ctx, "Test configuration value search #%d", ++(*nsubtest));
     test_result(&ctx, (item = th_cfg_find(cfg, NULL, "hexval", -1)) != NULL && *item->v.val_uint == 0x11223344);
     test_end(&ctx);
+    test_start(&ctx, "Test configuration value search #%d", ++(*nsubtest));
+    test_result(&ctx, (item = th_cfg_find(cfg, NULL, "a_string_setting", -1)) != NULL && strcmp(*item->v.val_str, "v_str1") == 0);
+    test_end(&ctx);
 }
 
 
+void test_free(th_llist_t *node)
+{
+    th_free_r(&node->data);
+}
+
+
+void test_ioctx_error(th_ioctx *fh, const int val, const char *msg)
+{
+    (void) fh;
+    (void) val;
+    tprint(0, "IOCTX ERROR: %s", msg);
+}
+
+
+void test_ioctx_msg(th_ioctx *fh, const int val, const char *msg)
+{
+    (void) fh;
+    (void) val;
+    tprint(1, "IOCTX MSG: %s", msg);
+}
+
+
+int test_strcmp(const void *v1, const void *v2)
+{
+    return strcmp((const char *) v1, (const char *) v2);
+}
+
+
+static const char *test_strings[] =
+{
+    "zoo", "foo", "bar",
+};
+
+static const int ntest_strings = sizeof(test_strings) / sizeof(test_strings[0]);
+
+
 void test_config(void)
 {
+    test_ctx ctx;
     th_ioctx *fh = NULL;
-    th_cfgitem_t *sect1, *sect2, *cfg = NULL;
+    th_cfgitem_t *sect1, *sect2, *cfg = NULL, *item;
     char *v_str1 = NULL;
     unsigned int v_uint1;
     int v_int1;
     BOOL v_bool1, v_bool2;
     th_llist_t *v_str_list = NULL;
+    int nsubtest = 0;
 
     static const char *filename = "config.test";
 
+    // Create v_str_list
+    for (int n = 0; n < ntest_strings; n++)
+        th_llist_append(&v_str_list, th_strdup(test_strings[n]));
+
     // Create the configuration structure
     tprint(2, "Creating configuration structure\n");
     sect1 = NULL;
-//    th_cfg_add_comment(&sect1, "A comment that\nspans multiple\nlines automatically");
     th_cfg_add_comment(&sect1, "A comment");
     th_cfg_add_string(&sect1, "a_string_setting", &v_str1, "v_str1");
 
@@ -363,7 +408,7 @@
     th_cfg_add_bool(&sect1, "boolval", &v_bool1, FALSE);
 
     th_cfg_add_comment(&sect1, "A string list");
-    th_cfg_add_string_list(&sect1, "ignore_list", &v_str_list);
+    th_cfg_add_string_list(&sect1, "string_list", &v_str_list);
 
     th_cfg_add_section(&cfg, "general", sect1);
 
@@ -378,12 +423,11 @@
 
     th_cfg_add_section(&cfg, "another_sect", sect1);
 
-
     // Test value finding
-    test_config_values(cfg);
+    test_config_values(cfg, &nsubtest);
 
     // Attempt to write the file
-    //th_io_set_handlers(fh, test_ioctx_errfunc, test_ioctx_msgfunc);
+    th_io_set_handlers(fh, test_ioctx_error, test_ioctx_msg);
 
     if (th_io_fopen(&fh, &th_stdio_io_ops, filename, "w") != THERR_OK)
     {
@@ -398,6 +442,11 @@
     th_io_close(fh);
 
 
+    // Free the data for v_str_list
+    th_llist_free_func_node(v_str_list, test_free);
+    v_str_list = NULL;
+
+
     // Attempt to read the previously written file
     if (th_io_fopen(&fh, &th_stdio_io_ops, filename, "r") != THERR_OK)
     {
@@ -411,9 +460,28 @@
     th_cfg_read(fh, cfg);
 
     // Retest values
-    test_config_values(cfg);
+    test_config_values(cfg, &nsubtest);
+
+    // Additional tests
+    test_start(&ctx, "Test configuration string list ptr");
+    test_result(&ctx, (item = th_cfg_find(cfg, NULL, "string_list", -1)) != NULL && item->v.data == &v_str_list);
+    test_end(&ctx);
+
+    for (nsubtest = 0; nsubtest < ntest_strings; nsubtest++)
+    {
+        test_start(&ctx, "Test configuration string list values #%d", nsubtest);
+        test_result(&ctx, th_llist_find_func(v_str_list, test_strings[nsubtest], test_strcmp) != NULL);
+        test_end(&ctx);
+    }
+
+    test_start(&ctx, "Test configuration string list values #%d", ++nsubtest);
+    test_result(&ctx, th_llist_find_func(v_str_list, "opas", test_strcmp) == NULL);
+    test_end(&ctx);
+
+    test_end(&ctx);
 
 out:
+    th_llist_free_func_node(v_str_list, test_free);
     th_io_free(fh);
 }
 
@@ -591,14 +659,17 @@
 
     // Tests that test for things that do not work correctly yet
     // Unicode / multibyte UTF-8 causes problems here
-    if (test_set_start("Invalid UTF-8 handling"))
+    if ((optFlags & TST_BROKEN) &&
+        test_set_start("Invalid UTF-8 handling"))
     {
         TEST2(th_strcasecmp, "ÖÄÅ", "öäå", FALSE); // SHOULD match
         TEST3(th_strncasecmp, "Aäöå", "aöå", 2, TRUE); // should NOT match
         TEST2B(th_strmatch, "öriÖRI! lol", "?ri?RI!*", FALSE); // should match
     }
 
-    // printf PRI* format specifiers, also a compile time test
+    //
+    // printf() PRI* format specifiers, also a compile time test
+    //
     if (test_set_start("PRI* specifiers"))
     {
         char tmp[32];
@@ -627,7 +698,10 @@
     }
 
 
-    if (test_set_start("Config file parsing"))
+    //
+    // Configuration file handling
+    //
+    if (test_set_start("Config file handling"))
     {
         test_config();
     }