Mercurial > hg > th-libs
changeset 512:5c3bfe034915
Improve config file handling tests.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 26 Dec 2019 09:01:32 +0200 |
parents | 934369fafd5d |
children | e412a39e2b7a |
files | tests.c |
diffstat | 1 files changed, 74 insertions(+), 51 deletions(-) [+] |
line wrap: on
line diff
--- a/tests.c Thu Dec 26 08:26:10 2019 +0200 +++ b/tests.c Thu Dec 26 09:01:32 2019 +0200 @@ -315,27 +315,28 @@ } while (0) -void test_config_values(th_cfgitem_t *cfg, int *nsubtest) +void test_config_values(th_cfgitem_t *cfg) { th_cfgitem_t *item; + int nsubtest = 0; test_ctx ctx; - test_start(&ctx, "Test configuration value search #%d", ++(*nsubtest)); + 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 #%d", ++(*nsubtest)); + 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 #%d", ++(*nsubtest)); + 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 #%d", ++(*nsubtest)); + 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 #%d", ++(*nsubtest)); + 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_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); } @@ -377,8 +378,63 @@ static const int ntest_strings = sizeof(test_strings) / sizeof(test_strings[0]); +void test_config_read(th_cfgitem_t *cfg, const char *filename) +{ + int nsubtest; + th_ioctx *fh = NULL; + test_ctx ctx; + th_cfgitem_t *item; + + // Attempt to read the previously written file + if (th_io_fopen(&fh, &th_stdio_io_ops, filename, "r") != THERR_OK) + { + int err = th_get_error(); + THERR("Could not open configuration file '%s', %d: %s\n", + filename, err, th_error_str(err)); + goto out; + } + + th_cfg_read(fh, cfg); + + // Test read values against expected values + test_config_values(cfg); + + // Additional tests + item = th_cfg_find(cfg, NULL, "string_list", -1); + test_result(&ctx, item != NULL); + if (item != NULL) + { + static const char *nostr = "not to be found"; + th_llist_t **plist = (th_llist_t **) item->v.list; + + for (nsubtest = 0; nsubtest < ntest_strings; nsubtest++) + { + test_start(&ctx, "Test configuration string list values #%d: '%s'", + nsubtest + 1, test_strings[nsubtest]); + + test_result(&ctx, + th_llist_find_func(*plist, + test_strings[nsubtest], test_strcmp) != NULL); + test_end(&ctx); + } + + test_start(&ctx, "Test configuration string list values #%d: '%s'", + ++nsubtest, nostr); + test_result(&ctx, th_llist_find_func(*plist, nostr, test_strcmp) == NULL); + test_end(&ctx); + + th_llist_free_func_node(*plist, test_free); + *plist = NULL; + } + +out: + th_io_free(fh); +} + + void test_config(void) { + static const char *filename = "tmp.config"; test_ctx ctx; th_ioctx *fh = NULL; th_cfgitem_t *sect1, *sect2, *cfg = NULL, *item; @@ -387,9 +443,6 @@ 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++) @@ -423,8 +476,13 @@ th_cfg_add_section(&cfg, "another_sect", sect1); + // 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_end(&ctx); + // Test value finding - test_config_values(cfg, &nsubtest); + test_config_values(cfg); // Attempt to write the file th_io_set_handlers(fh, test_ioctx_error, test_ioctx_msg); @@ -434,55 +492,20 @@ int err = th_get_error(); THERR("Could not create configuration to file '%s', %d: %s\n", filename, err, th_error_str(err)); - goto out; } th_cfg_write(fh, cfg); - 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; + // Test against written configuration file + test_config_read(cfg, filename); - // Attempt to read the previously written file - if (th_io_fopen(&fh, &th_stdio_io_ops, filename, "r") != THERR_OK) - { - int err = th_get_error(); - THERR("Could not open configuration file '%s', %d: %s\n", - filename, err, th_error_str(err)); - - goto out; - } - - th_cfg_read(fh, cfg); - - // Retest values - 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: + // Free the data for v_str_list + th_io_free(fh); th_llist_free_func_node(v_str_list, test_free); - th_io_free(fh); + v_str_list = NULL; }