2 * Types used in 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_instance 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_template 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_FMT("col3_bool", 9, XTYPE_FMT_PRETTY),
25 [TEST_COL4_DOUBLE] = TBL_COL_DOUBLE_FMT("col4_double", 11, XTYPE_FMT_PRETTY),
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_FMT_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_template 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_FMT_HUMAN_READABLE,
47 TBL_COL_DELIMITER("\t"),
50 static void do_default_order_test(struct fastbuf *out)
52 struct table *tbl = table_init(&test_default_order_tbl);
54 table_start(tbl, out);
56 table_col_int(tbl, TEST_DEFAULT_ORDER_COL0_INT, 0);
57 table_col_int(tbl, TEST_DEFAULT_ORDER_COL1_INT, 1);
58 table_col_int(tbl, TEST_DEFAULT_ORDER_COL2_INT, 2);
61 table_col_int(tbl, TEST_DEFAULT_ORDER_COL0_INT, 10);
62 table_col_int(tbl, TEST_DEFAULT_ORDER_COL1_INT, 11);
63 table_col_int(tbl, TEST_DEFAULT_ORDER_COL2_INT, 12);
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 struct table_formatter test_option_fmtr = table_fmt_human_readable;
147 test_option_fmtr.process_option = user_defined_option;
149 const struct table_formatter *tmp_fmtr = tbl->formatter;
150 tbl->formatter = &test_option_fmtr;
152 const char *rv = table_set_option(tbl, "invalid:option");
153 if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
155 rv = table_set_option(tbl, "invalid");
156 if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
158 rv = table_set_option(tbl, "novaluekey");
159 if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
161 rv = table_set_option(tbl, "valuekey:value");
162 if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
164 tbl->formatter = tmp_fmtr;
167 int main(int argc UNUSED, char **argv)
170 out = bfdopen_shared(1, 4096);
172 struct table *tbl = table_init(&test_tbl);
174 process_command_line_opts(argv, tbl);
176 const char *rv = NULL;
177 switch(test_to_perform) {
178 case TEST_INVALID_ORDER:
179 rv = table_set_option(tbl, "cols:test_col0_str,test_col1_int,xxx");
180 if(rv) printf("Tableprinter option parser returned: '%s'.\n", rv);
182 case TEST_DEFAULT_COLUMN_ORDER:
183 do_default_order_test(out);
186 case TEST_INVALID_OPTION:
187 test_option_parser(tbl);
192 table_start(tbl, out);