2 * Types used in table printer
4 * (c) 2014 Robert Kessl <robert.kessl@economia.cz>
9 #include <ucw/xtypes-extra.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_instance test_column_order[] = { TBL_COL(TEST_COL3_BOOL), TBL_COL(TEST_COL4_DOUBLE),
18 TBL_COL(TEST_COL2_UINT), TBL_COL(TEST_COL1_INT), TBL_COL(TEST_COL0_STR), TBL_COL_ORDER_END };
20 static struct table_template test_tbl = {
22 [TEST_COL0_STR] = TBL_COL_STR("col0_str", 20),
23 [TEST_COL1_INT] = TBL_COL_INT("col1_int", 8),
24 [TEST_COL2_UINT] = TBL_COL_UINT("col2_uint", 9),
25 [TEST_COL3_BOOL] = TBL_COL_BOOL_FMT("col3_bool", 9, XTYPE_FMT_PRETTY),
26 [TEST_COL4_DOUBLE] = TBL_COL_DOUBLE_FMT("col4_double", 11, XTYPE_FMT_PRETTY),
27 [TEST_COL5_SIZE] = TBL_COL_SIZE("col5_size", 11),
28 [TEST_COL6_TIME] = TBL_COL_TIMESTAMP("col6_timestamp", 20),
31 TBL_COL_ORDER(test_column_order),
32 TBL_FMT_HUMAN_READABLE,
33 TBL_COL_DELIMITER("\t"),
36 enum test_default_order_cols {
37 TEST_DEFAULT_ORDER_COL0_INT, TEST_DEFAULT_ORDER_COL1_INT, TEST_DEFAULT_ORDER_COL2_INT
40 static struct table_template test_default_order_tbl = {
42 [TEST_DEFAULT_ORDER_COL0_INT] = TBL_COL_INT("col0_int", 8),
43 [TEST_DEFAULT_ORDER_COL1_INT] = TBL_COL_INT("col1_int", 9),
44 [TEST_DEFAULT_ORDER_COL2_INT] = TBL_COL_INT("col2_int", 9),
47 TBL_FMT_HUMAN_READABLE,
48 TBL_COL_DELIMITER("\t"),
51 static void do_default_order_test(struct fastbuf *out)
53 struct table *tbl = table_init(&test_default_order_tbl);
55 table_start(tbl, out);
57 table_col_int(tbl, TEST_DEFAULT_ORDER_COL0_INT, 0);
58 table_col_int(tbl, TEST_DEFAULT_ORDER_COL1_INT, 1);
59 table_col_int(tbl, TEST_DEFAULT_ORDER_COL2_INT, 2);
62 table_col_int(tbl, TEST_DEFAULT_ORDER_COL0_INT, 10);
63 table_col_int(tbl, TEST_DEFAULT_ORDER_COL1_INT, 11);
64 table_col_int(tbl, TEST_DEFAULT_ORDER_COL2_INT, 12);
72 * tests: table_col_int, table_col_uint, table_col_bool, table_col_double, table_col_printf
74 static void do_print1(struct table *test_tbl)
76 table_col_str(test_tbl, TEST_COL0_STR, "sdsdf");
77 table_col_int(test_tbl, TEST_COL1_INT, -10);
78 table_col_int(test_tbl, TEST_COL1_INT, 10000);
79 table_col_uint(test_tbl, TEST_COL2_UINT, 10);
80 table_col_printf(test_tbl, TEST_COL2_UINT, "XXX-%u", 22222);
81 table_col_bool(test_tbl, TEST_COL3_BOOL, 1);
82 table_col_double(test_tbl, TEST_COL4_DOUBLE, 1.5);
83 table_col_size(test_tbl, TEST_COL5_SIZE, (1024LLU*1024LLU*1024LLU*5LLU));
84 table_col_timestamp(test_tbl, TEST_COL6_TIME, 1404305876);
85 table_end_row(test_tbl);
87 table_col_str(test_tbl, TEST_COL0_STR, "test");
88 table_col_int(test_tbl, TEST_COL1_INT, -100);
89 table_col_uint(test_tbl, TEST_COL2_UINT, 100);
90 table_col_bool(test_tbl, TEST_COL3_BOOL, 0);
91 table_col_double(test_tbl, TEST_COL4_DOUBLE, 1.5);
92 table_col_size(test_tbl, TEST_COL5_SIZE, (1024LLU*1024LLU*1024LLU*2LLU));
93 table_col_timestamp(test_tbl, TEST_COL6_TIME, 1404305909);
94 table_end_row(test_tbl);
97 static char **cli_table_opts;
100 TEST_DEFAULT_COLUMN_ORDER = 1,
101 TEST_INVALID_OPTION = 2,
102 TEST_INVALID_ORDER = 3
105 static int test_to_perform = -1;
107 static struct opt_section table_printer_opts = {
109 OPT_HELP("Options:"),
110 OPT_STRING_MULTIPLE('T', "table", cli_table_opts, OPT_REQUIRED_VALUE, "\tSets options for the table."),
111 OPT_SWITCH('d', 0, test_to_perform, TEST_DEFAULT_COLUMN_ORDER, OPT_SINGLE, "\tRun the test that uses the default column order."),
112 OPT_SWITCH('i', 0, test_to_perform, TEST_INVALID_OPTION, OPT_SINGLE, "\tTest the output for invalid option."),
113 OPT_SWITCH('n', 0, test_to_perform, TEST_INVALID_ORDER, OPT_SINGLE, "\tTest the output for invalid names of columns for the 'cols' option."),
118 static void process_command_line_opts(char *argv[], struct table *tbl)
120 GARY_INIT(cli_table_opts, 0);
122 opt_parse(&table_printer_opts, argv+1);
123 const char *err = table_set_gary_options(tbl, cli_table_opts);
125 opt_failure("error while setting cmd line options: %s", err);
128 GARY_FREE(cli_table_opts);
131 static bool user_defined_option(struct table *tbl UNUSED, const char *key, const char *value, const char **err UNUSED)
133 if(value == NULL && strcmp(key, "novaluekey") == 0) {
134 printf("setting key: %s; value: (null)\n", key);
137 if(value != NULL && strcmp(value, "value") == 0 &&
138 key != NULL && strcmp(key, "valuekey") == 0) {
139 printf("setting key: %s; value: %s\n", key, value);
145 static void test_option_parser(struct table *tbl)
147 struct table_formatter test_option_fmtr = table_fmt_human_readable;
148 test_option_fmtr.process_option = user_defined_option;
150 const struct table_formatter *tmp_fmtr = tbl->formatter;
151 tbl->formatter = &test_option_fmtr;
153 const char *rv = table_set_option(tbl, "invalid:option");
154 if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
156 rv = table_set_option(tbl, "invalid");
157 if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
159 rv = table_set_option(tbl, "novaluekey");
160 if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
162 rv = table_set_option(tbl, "valuekey:value");
163 if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
165 tbl->formatter = tmp_fmtr;
168 int main(int argc UNUSED, char **argv)
171 out = bfdopen_shared(1, 4096);
173 struct table *tbl = table_init(&test_tbl);
175 process_command_line_opts(argv, tbl);
177 const char *rv = NULL;
178 switch(test_to_perform) {
179 case TEST_INVALID_ORDER:
180 rv = table_set_option(tbl, "cols:test_col0_str,test_col1_int,xxx");
181 if(rv) printf("Tableprinter option parser returned: '%s'.\n", rv);
183 case TEST_DEFAULT_COLUMN_ORDER:
184 do_default_order_test(out);
187 case TEST_INVALID_OPTION:
188 test_option_parser(tbl);
193 table_start(tbl, out);