2 * Unit tests of table printer
4 * (c) 2014 Robert Kessl <robert.kessl@economia.cz>
9 #include <ucw/table-types.h>
13 enum test_table_cols {
14 test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double, test_col5_size, test_col6_time
17 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) };
19 static struct table test_tbl = {
21 [test_col0_str] = TBL_COL_STR("col0_str", 20),
22 [test_col1_int] = TBL_COL_INT("col1_int", 8),
23 [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
24 [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
25 [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
26 [test_col5_size] = TBL_COL_SIZE("col5_size", 11),
27 [test_col6_time] = TBL_COL_TIMESTAMP("col6_timestamp", 20),
30 TBL_COL_ORDER(test_column_order),
31 TBL_OUTPUT_HUMAN_READABLE,
32 TBL_COL_DELIMITER("\t"),
35 enum test_default_order_cols {
36 test_default_order_col0_int, test_default_order_col1_int, test_default_order_col2_int
39 static struct table test_default_order_tbl = {
41 [test_default_order_col0_int] = TBL_COL_INT("col0_int", 8),
42 [test_default_order_col1_int] = TBL_COL_INT("col1_int", 9),
43 [test_default_order_col2_int] = TBL_COL_INT("col2_int", 9),
46 TBL_OUTPUT_HUMAN_READABLE,
47 TBL_COL_DELIMITER("\t"),
50 static void do_default_order_test(struct fastbuf *out)
52 table_init(&test_default_order_tbl);
54 table_start(&test_default_order_tbl, out);
56 table_col_int(&test_default_order_tbl, test_default_order_col0_int, 0);
57 table_col_int(&test_default_order_tbl, test_default_order_col1_int, 1);
58 table_col_int(&test_default_order_tbl, test_default_order_col2_int, 2);
59 table_end_row(&test_default_order_tbl);
61 table_col_int(&test_default_order_tbl, test_default_order_col0_int, 10);
62 table_col_int(&test_default_order_tbl, test_default_order_col1_int, 11);
63 table_col_int(&test_default_order_tbl, test_default_order_col2_int, 12);
64 table_end_row(&test_default_order_tbl);
66 table_end(&test_default_order_tbl);
67 table_cleanup(&test_default_order_tbl);
71 * tests: table_col_int, table_col_uint, table_col_bool, table_col_double, table_col_printf
73 static void do_print1(struct table *test_tbl)
75 table_col_str(test_tbl, test_col0_str, "sdsdf");
76 table_col_int(test_tbl, test_col1_int, -10);
77 table_col_int(test_tbl, test_col1_int, 10000);
78 table_col_uint(test_tbl, test_col2_uint, 10);
79 table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
80 table_col_bool(test_tbl, test_col3_bool, 1);
81 table_col_double(test_tbl, test_col4_double, 1.5);
82 table_col_size(test_tbl, test_col5_size, (1024LLU*1024LLU*1024LLU*5LLU));
83 table_col_timestamp(test_tbl, test_col6_time, 1404305876);
84 table_end_row(test_tbl);
86 table_col_str(test_tbl, test_col0_str, "test");
87 table_col_int(test_tbl, test_col1_int, -100);
88 table_col_uint(test_tbl, test_col2_uint, 100);
89 table_col_bool(test_tbl, test_col3_bool, 0);
90 table_col_double(test_tbl, test_col4_double, 1.5);
91 table_col_size(test_tbl, test_col5_size, (1024LLU*1024LLU*1024LLU*2LLU));
92 table_col_timestamp(test_tbl, test_col6_time, 1404305909);
93 table_end_row(test_tbl);
96 static char **cli_table_opts;
99 TEST_DEFAULT_COLUMN_ORDER = 1,
100 TEST_INVALID_OPTION = 2,
101 TEST_INVALID_ORDER = 3
104 static int test_to_perform = -1;
106 static struct opt_section table_printer_opts = {
108 OPT_HELP("Options:"),
109 OPT_STRING_MULTIPLE('T', "table", cli_table_opts, OPT_REQUIRED_VALUE, "\tSets options for the table."),
110 OPT_SWITCH('d', 0, test_to_perform, TEST_DEFAULT_COLUMN_ORDER, OPT_SINGLE, "\tRun the test that uses the default column order."),
111 OPT_SWITCH('i', 0, test_to_perform, TEST_INVALID_OPTION, OPT_SINGLE, "\tTest the output for invalid option."),
112 OPT_SWITCH('n', 0, test_to_perform, TEST_INVALID_ORDER, OPT_SINGLE, "\tTest the output for invalid names of columns for the 'cols' option."),
117 static void process_command_line_opts(char *argv[], struct table *tbl)
119 GARY_INIT(cli_table_opts, 0);
121 opt_parse(&table_printer_opts, argv+1);
122 const char *err = table_set_gary_options(tbl, cli_table_opts);
124 opt_failure("error while setting cmd line options: %s", err);
127 GARY_FREE(cli_table_opts);
130 static bool user_defined_option(struct table *tbl UNUSED, const char *key, const char *value, const char **err UNUSED)
132 if(value == NULL && strcmp(key, "novaluekey") == 0) {
133 printf("setting key: %s; value: (null)\n", key);
136 if(value != NULL && strcmp(value, "value") == 0 &&
137 key != NULL && strcmp(key, "valuekey") == 0) {
138 printf("setting key: %s; value: %s\n", key, value);
144 static void test_option_parser(struct table *tbl)
146 tbl->formatter->process_option = user_defined_option;
147 const char *rv = table_set_option(tbl, "invalid:option");
148 if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
150 rv = table_set_option(tbl, "invalid");
151 if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
153 rv = table_set_option(tbl, "novaluekey");
154 if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
156 rv = table_set_option(tbl, "valuekey:value");
157 if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
160 int main(int argc UNUSED, char **argv)
163 out = bfdopen_shared(1, 4096);
165 table_init(&test_tbl);
167 process_command_line_opts(argv, &test_tbl);
169 const char *rv = NULL;
170 switch(test_to_perform) {
171 case TEST_INVALID_ORDER:
172 rv = table_set_option(&test_tbl, "cols:test_col0_str,test_col1_int,xxx");
173 if(rv) printf("Tableprinter option parser returned: '%s'.\n", rv);
175 case TEST_DEFAULT_COLUMN_ORDER:
176 do_default_order_test(out);
179 case TEST_INVALID_OPTION:
180 test_option_parser(&test_tbl);
185 table_start(&test_tbl, out);
186 do_print1(&test_tbl);
187 table_end(&test_tbl);
188 table_cleanup(&test_tbl);