2 * Unit tests of table printer
4 * (c) 2014 Robert Kessl <robert.kessl@economia.cz>
13 enum test_table_cols {
14 test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
17 static struct table_template test_tbl = {
19 [test_col0_str] = TBL_COL_STR("col0_str", 30 | CELL_ALIGN_LEFT),
20 [test_col1_int] = TBL_COL_INT("col1_int", 8),
21 [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
22 [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9 | CELL_ALIGN_LEFT),
23 [test_col4_double] = TBL_COL_DOUBLE_FMT("col4_double", 11 | CELL_ALIGN_LEFT, XTYPE_FMT_DEFAULT),
26 TBL_FMT_HUMAN_READABLE,
27 TBL_COL_DELIMITER("\t"),
30 static char **cli_table_opts;
32 static struct opt_section table_printer_opts = {
35 OPT_STRING_MULTIPLE('T', "table", cli_table_opts, OPT_REQUIRED_VALUE, "\tSets options for the table."),
41 static void process_command_line_opts(char *argv[], struct table *tbl)
43 GARY_INIT(cli_table_opts, 0);
45 opt_parse(&table_printer_opts, argv+1);
46 table_set_gary_options(tbl, cli_table_opts);
48 GARY_FREE(cli_table_opts);
51 static void print_table(struct table *tbl, struct fastbuf *out)
53 table_start(tbl, out);
55 struct fastbuf *colfb = table_col_fbstart(tbl, test_col0_str);
56 bputs(colfb, "HELLO");
57 bprintf(colfb, ",col_idx:%d", test_col0_str);
60 table_col_int(tbl, test_col1_int, -10);
61 table_col_uint(tbl, test_col2_uint, 10);
62 table_col_bool(tbl, test_col3_bool, 0);
63 table_col_double(tbl, test_col4_double, 3.1415926535897);
68 colfb = table_col_fbstart(tbl, test_col0_str);
70 bprintf(colfb, ",col_idx:%d", test_col0_str);
73 table_col_int(tbl, test_col1_int, -12345);
74 table_col_uint(tbl, test_col2_uint, 0xFF);
75 table_col_bool(tbl, test_col3_bool, 1);
76 table_col_double(tbl, test_col4_double, 1.61803398875);
81 colfb = table_col_fbstart(tbl, test_col0_str);
83 bprintf(colfb, ",col_idx:%d", test_col0_str);
86 table_col_int(tbl, test_col1_int, -54321);
87 table_col_uint(tbl, test_col2_uint, 0xFF00);
88 table_col_bool(tbl, test_col3_bool, 0);
89 table_col_double(tbl, test_col4_double, 2.718281828459045);
96 int main(int argc UNUSED, char **argv)
99 out = bfdopen_shared(1, 4096);
101 struct table *tbl = table_init(&test_tbl);
102 process_command_line_opts(argv, tbl);
104 print_table(tbl, out);