# HG changeset patch # User Matti Hamalainen # Date 1577334906 -7200 # Node ID 50d71fc84831d1318bb583c6df203575d63e2a7b # Parent 3a0864eb358f0449255a7c97522f84e9938079ae Add simple tests for th_config. diff -r 3a0864eb358f -r 50d71fc84831 tests.c --- a/tests.c Thu Dec 26 06:34:48 2019 +0200 +++ b/tests.c Thu Dec 26 06:35:06 2019 +0200 @@ -3,6 +3,7 @@ #include "th_util.h" #include "th_string.h" #include "th_crypto.h" +#include "th_config.h" #define SET_BUF_SIZE 128 @@ -313,6 +314,110 @@ } while (0) +void test_config_values(const th_cfgitem_t *cfg) +{ + th_cfgitem_t *item; + + test_ctx ctx; + test_start(&ctx, "Test configuration value search #1"); + 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_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_result(&ctx, th_cfg_find(cfg, "no_match", NULL, -1) == NULL); + test_end(&ctx); + test_start(&ctx, "Test configuration value search #4"); + test_result(&ctx, th_cfg_find(cfg, NULL, "no_match", -1) == NULL); + test_end(&ctx); + test_start(&ctx, "Test configuration value search #5"); + test_result(&ctx, (item = th_cfg_find(cfg, NULL, "hexval", -1)) != NULL && *item->v.val_uint == 0x11223344); + test_end(&ctx); +} + + +void test_config(void) +{ + test_ctx ctx; + th_ioctx *fh = NULL; + th_cfgitem_t *sect1, *sect2, *cfg = NULL; + char *v_str1 = NULL; + unsigned int v_uint1; + int v_int1; + BOOL v_bool1, v_bool2; + th_llist_t *v_str_list = NULL; + + static const char *filename = "config.test"; + + // Create the configuration structure + tprint(2, "Creating configuration structure\n"); + sect1 = NULL; +// th_cfg_add_comment(§1, "A comment that\nspans multiple\nlines automatically"); + th_cfg_add_comment(§1, "A comment"); + th_cfg_add_string(§1, "a_string_setting", &v_str1, "v_str1"); + + th_cfg_add_comment(§1, "Hex triplet value setting"); + th_cfg_add_hexvalue(§1, "hexval", &v_uint1, 0x11223344); + + th_cfg_add_comment(§1, "A boolean value"); + th_cfg_add_bool(§1, "boolval", &v_bool1, FALSE); + + th_cfg_add_comment(§1, "A string list"); + th_cfg_add_string_list(§1, "ignore_list", &v_str_list); + + th_cfg_add_section(&cfg, "general", sect1); + + sect1 = NULL; + th_cfg_add_comment(§1, "Another section"); + th_cfg_add_bool(§1, "boolval", &v_bool2, TRUE); + + sect2 = NULL; + th_cfg_add_comment(§2, "Section inside a section"); + th_cfg_add_int(§2, "intval", &v_int1, 112); + th_cfg_add_section(§1, "inside_sect", sect2); + + th_cfg_add_section(&cfg, "another_sect", sect1); + + + // Test value finding + test_config_values(cfg); + + // Attempt to write the file + //th_io_set_handlers(fh, test_ioctx_errfunc, test_ioctx_msgfunc); + + if (th_io_fopen(&fh, &th_stdio_io_ops, filename, "w") != THERR_OK) + { + 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); + + + // 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); + +out: + th_io_free(fh); +} + int main(int argc, char *argv[]) { @@ -322,7 +427,7 @@ // // Initialization // - th_init("th-test", "th-libs unit tests", "0.1", NULL, NULL); + th_init("th-test", "th-libs unit tests", "0.2", NULL, NULL); th_verbosity = 0; if (sizeof(char) != sizeof(unsigned char)) @@ -522,6 +627,12 @@ TEST2(strcmp, tmp, "aabbccdd11223344h", TRUE); } + + if (test_set_start("Config file parsing")) + { + test_config(); + } + // // Print summary and exit //