X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Ftable-test.c;h=c13ae2e369e4471cf64f94ac50286a48876e4add;hb=db2e817025638b9738e5ff8f8f5ccc1fbf68a2f3;hp=ee019541a5e65b337dafe69de300fadbbb7e865e;hpb=1ac31d623afa6873c1cbe691bf8429c03d289ac2;p=libucw.git diff --git a/ucw/table-test.c b/ucw/table-test.c index ee019541..c13ae2e3 100644 --- a/ucw/table-test.c +++ b/ucw/table-test.c @@ -6,22 +6,25 @@ #include #include +#include #include #include enum test_table_cols { - test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double + test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double, test_col5_size, test_col6_time }; -static uint test_column_order[] = { test_col3_bool, test_col4_double, test_col2_uint,test_col1_int, test_col0_str }; +static struct table_col_info test_column_order[] = { TBL_COL(test_col3_bool), TBL_COL(test_col4_double), TBL_COL(test_col2_uint), TBL_COL(test_col1_int), TBL_COL(test_col0_str) }; static struct table test_tbl = { TBL_COLUMNS { - TBL_COL_STR(test, col0_str, 20), - TBL_COL_INT(test, col1_int, 8), - TBL_COL_UINT(test, col2_uint, 9), - TBL_COL_BOOL(test, col3_bool, 9), - TBL_COL_DOUBLE(test, col4_double, 11, 2), + [test_col0_str] = TBL_COL_STR("col0_str", 20), + [test_col1_int] = TBL_COL_INT("col1_int", 8), + [test_col2_uint] = TBL_COL_UINT("col2_uint", 9), + [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9), + [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2), + [test_col5_size] = TBL_COL_SIZE("col5_size", 11), + [test_col6_time] = TBL_COL_TIMESTAMP("col6_timestamp", 20), TBL_COL_END }, TBL_COL_ORDER(test_column_order), @@ -35,9 +38,9 @@ enum test_default_order_cols { static struct table test_default_order_tbl = { TBL_COLUMNS { - TBL_COL_INT(test_default_order, col0_int, 8), - TBL_COL_INT(test_default_order, col1_int, 9), - TBL_COL_INT(test_default_order, col2_int, 9), + [test_default_order_col0_int] = TBL_COL_INT("col0_int", 8), + [test_default_order_col1_int] = TBL_COL_INT("col1_int", 9), + [test_default_order_col2_int] = TBL_COL_INT("col2_int", 9), TBL_COL_END }, TBL_OUTPUT_HUMAN_READABLE, @@ -46,17 +49,18 @@ static struct table test_default_order_tbl = { static void do_default_order_test(struct fastbuf *out) { - table_init(&test_default_order_tbl, out); - table_start(&test_default_order_tbl); + table_init(&test_default_order_tbl); - table_set_int(&test_default_order_tbl, test_default_order_col0_int, 0); - table_set_int(&test_default_order_tbl, test_default_order_col1_int, 1); - table_set_int(&test_default_order_tbl, test_default_order_col2_int, 2); + table_start(&test_default_order_tbl, out); + + table_col_int(&test_default_order_tbl, test_default_order_col0_int, 0); + table_col_int(&test_default_order_tbl, test_default_order_col1_int, 1); + table_col_int(&test_default_order_tbl, test_default_order_col2_int, 2); table_end_row(&test_default_order_tbl); - table_set_int(&test_default_order_tbl, test_default_order_col0_int, 10); - table_set_int(&test_default_order_tbl, test_default_order_col1_int, 11); - table_set_int(&test_default_order_tbl, test_default_order_col2_int, 12); + table_col_int(&test_default_order_tbl, test_default_order_col0_int, 10); + table_col_int(&test_default_order_tbl, test_default_order_col1_int, 11); + table_col_int(&test_default_order_tbl, test_default_order_col2_int, 12); table_end_row(&test_default_order_tbl); table_end(&test_default_order_tbl); @@ -64,41 +68,48 @@ static void do_default_order_test(struct fastbuf *out) } /** - * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf + * tests: table_col_int, table_col_uint, table_col_bool, table_col_double, table_col_printf **/ static void do_print1(struct table *test_tbl) { - table_set_str(test_tbl, test_col0_str, "sdsdf"); - table_append_str(test_tbl, "aaaaa"); - table_set_int(test_tbl, test_col1_int, -10); - table_set_int(test_tbl, test_col1_int, 10000); - table_set_uint(test_tbl, test_col2_uint, 10); - table_set_printf(test_tbl, test_col2_uint, "XXX-%u", 22222); - table_set_bool(test_tbl, test_col3_bool, 1); - table_set_double(test_tbl, test_col4_double, 1.5); + table_col_str(test_tbl, test_col0_str, "sdsdf"); + table_col_int(test_tbl, test_col1_int, -10); + table_col_int(test_tbl, test_col1_int, 10000); + table_col_uint(test_tbl, test_col2_uint, 10); + table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222); + table_col_bool(test_tbl, test_col3_bool, 1); + table_col_double(test_tbl, test_col4_double, 1.5); + table_col_size(test_tbl, test_col5_size, (1024LLU*1024LLU*1024LLU*5LLU)); + table_col_timestamp(test_tbl, test_col6_time, 1404305876); table_end_row(test_tbl); - table_set_str(test_tbl, test_col0_str, "test"); - table_append_str(test_tbl, "bbbbb"); - table_set_int(test_tbl, test_col1_int, -100); - table_set_uint(test_tbl, test_col2_uint, 100); - table_set_bool(test_tbl, test_col3_bool, 0); - table_set_double(test_tbl, test_col4_double, 1.5); + table_col_str(test_tbl, test_col0_str, "test"); + table_col_int(test_tbl, test_col1_int, -100); + table_col_uint(test_tbl, test_col2_uint, 100); + table_col_bool(test_tbl, test_col3_bool, 0); + table_col_double(test_tbl, test_col4_double, 1.5); + table_col_size(test_tbl, test_col5_size, (1024LLU*1024LLU*1024LLU*2LLU)); + table_col_timestamp(test_tbl, test_col6_time, 1404305909); table_end_row(test_tbl); } static char **cli_table_opts; -static int test_default_column_order; -static int test_invalid_option; -static int test_invalid_order; + +enum test_type_t { + TEST_DEFAULT_COLUMN_ORDER = 1, + TEST_INVALID_OPTION = 2, + TEST_INVALID_ORDER = 3 +}; + +static int test_to_perform = -1; static struct opt_section table_printer_opts = { OPT_ITEMS { OPT_HELP("Options:"), OPT_STRING_MULTIPLE('T', "table", cli_table_opts, OPT_REQUIRED_VALUE, "\tSets options for the table."), - OPT_BOOL('d', 0, test_default_column_order, 0, "\tRun the test that uses the default column order."), - OPT_BOOL('i', 0, test_invalid_option, 0, "\tTest the output for invalid option."), - OPT_BOOL('n', 0, test_invalid_order, 0, "\tTest the output for invalid names of columns for the 'cols' option."), + OPT_SWITCH('d', 0, test_to_perform, TEST_DEFAULT_COLUMN_ORDER, OPT_SINGLE, "\tRun the test that uses the default column order."), + OPT_SWITCH('i', 0, test_to_perform, TEST_INVALID_OPTION, OPT_SINGLE, "\tTest the output for invalid option."), + OPT_SWITCH('n', 0, test_to_perform, TEST_INVALID_ORDER, OPT_SINGLE, "\tTest the output for invalid names of columns for the 'cols' option."), OPT_END } }; @@ -108,32 +119,31 @@ static void process_command_line_opts(char *argv[], struct table *tbl) GARY_INIT(cli_table_opts, 0); opt_parse(&table_printer_opts, argv+1); - - for(uint i = 0; i < GARY_SIZE(cli_table_opts); i++) { - const char *rv = table_set_option(tbl, cli_table_opts[i]); - ASSERT_MSG(rv == NULL, "Tableprinter option parser returned error: '%s'.", rv); + const char *err = table_set_gary_options(tbl, cli_table_opts); + if(err) { + opt_failure("error while setting cmd line options: %s", err); } GARY_FREE(cli_table_opts); } -static int user_defined_option(struct table *tbl UNUSED, const char *key, const char *value) +static bool user_defined_option(struct table *tbl UNUSED, const char *key, const char *value, const char **err UNUSED) { if(value == NULL && strcmp(key, "novaluekey") == 0) { printf("setting key: %s; value: (null)\n", key); - return 0; + return 1; } if(value != NULL && strcmp(value, "value") == 0 && key != NULL && strcmp(key, "valuekey") == 0) { printf("setting key: %s; value: %s\n", key, value); - return 0; + return 1; } - return 1; + return 0; } static void test_option_parser(struct table *tbl) { - tbl->callbacks->process_option = user_defined_option; + tbl->formatter->process_option = user_defined_option; const char *rv = table_set_option(tbl, "invalid:option"); if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv); @@ -152,25 +162,27 @@ int main(int argc UNUSED, char **argv) struct fastbuf *out; out = bfdopen_shared(1, 4096); - table_init(&test_tbl, out); + table_init(&test_tbl); process_command_line_opts(argv, &test_tbl); - if(test_invalid_order == 1) { - const char *rv = table_set_option(&test_tbl, "cols:test_col0_str,test_col1_int,xxx"); + const char *rv = NULL; + switch(test_to_perform) { + case TEST_INVALID_ORDER: + rv = table_set_option(&test_tbl, "cols:test_col0_str,test_col1_int,xxx"); if(rv) printf("Tableprinter option parser returned: '%s'.\n", rv); return 0; - } else if(test_default_column_order == 1) { + case TEST_DEFAULT_COLUMN_ORDER: do_default_order_test(out); bclose(out); return 0; - } else if(test_invalid_option == 1) { + case TEST_INVALID_OPTION: test_option_parser(&test_tbl); bclose(out); return 0; - } + }; - table_start(&test_tbl); + table_start(&test_tbl, out); do_print1(&test_tbl); table_end(&test_tbl); table_cleanup(&test_tbl);