]> mj.ucw.cz Git - libucw.git/blob - ucw/table-test-align.c
8432449b21cc45a185107f63a499484787f1f900
[libucw.git] / ucw / table-test-align.c
1 /*
2  *      Unit tests of table printer
3  *
4  *      (c) 2014 Robert Kessl <robert.kessl@economia.cz>
5  */
6
7 #include <ucw/lib.h>
8 #include <ucw/table.h>
9 #include <ucw/opt.h>
10 #include <stdio.h>
11
12 enum test_table_cols {
13   test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
14 };
15
16 static struct table_template test_tbl = {
17   TBL_COLUMNS {
18     [test_col0_str] = TBL_COL_STR("col0_str", 30 | CELL_ALIGN_LEFT),
19     [test_col1_int] = TBL_COL_INT("col1_int", 8),
20     [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
21     [test_col3_bool] = TBL_COL_BOOL_FMT("col3_bool", 9 | CELL_ALIGN_LEFT, XTYPE_FMT_PRETTY),
22     [test_col4_double] = TBL_COL_DOUBLE_FMT("col4_double", 11 | CELL_ALIGN_LEFT, XTYPE_FMT_DEFAULT),
23     TBL_COL_END
24   },
25   TBL_FMT_HUMAN_READABLE,
26   TBL_COL_DELIMITER("\t"),
27 };
28
29 static char **cli_table_opts;
30
31 static struct opt_section table_printer_opts = {
32   OPT_ITEMS {
33     OPT_HELP("Options:"),
34     OPT_STRING_MULTIPLE('T', "table", cli_table_opts, OPT_REQUIRED_VALUE, "\tSets options for the table."),
35     OPT_END
36   }
37 };
38
39
40 static void process_command_line_opts(char *argv[], struct table *tbl)
41 {
42   GARY_INIT(cli_table_opts, 0);
43
44   opt_parse(&table_printer_opts, argv+1);
45   table_set_gary_options(tbl, cli_table_opts);
46
47   GARY_FREE(cli_table_opts);
48 }
49
50 static void print_table(struct table *tbl, struct fastbuf *out)
51 {
52   table_start(tbl, out);
53
54   struct fastbuf *colfb = table_col_fbstart(tbl, test_col0_str);
55   bputs(colfb, "HELLO");
56   bprintf(colfb, ",col_idx:%d", test_col0_str);
57   table_col_fbend(tbl);
58
59   table_col_int(tbl, test_col1_int, -10);
60   table_col_uint(tbl, test_col2_uint, 10);
61   table_col_bool(tbl, test_col3_bool, 0);
62   table_col_double(tbl, test_col4_double, 3.1415926535897);
63   table_end_row(tbl);
64
65
66
67   colfb = table_col_fbstart(tbl, test_col0_str);
68   bputs(colfb, "EHLO");
69   bprintf(colfb, ",col_idx:%d", test_col0_str);
70   table_col_fbend(tbl);
71
72   table_col_int(tbl, test_col1_int, -12345);
73   table_col_uint(tbl, test_col2_uint, 0xFF);
74   table_col_bool(tbl, test_col3_bool, 1);
75   table_col_double(tbl, test_col4_double, 1.61803398875);
76   table_end_row(tbl);
77
78
79
80   colfb = table_col_fbstart(tbl, test_col0_str);
81   bputs(colfb, "AHOJ");
82   bprintf(colfb, ",col_idx:%d", test_col0_str);
83   table_col_fbend(tbl);
84
85   table_col_int(tbl, test_col1_int, -54321);
86   table_col_uint(tbl, test_col2_uint, 0xFF00);
87   table_col_bool(tbl, test_col3_bool, 0);
88   table_col_double(tbl, test_col4_double, 2.718281828459045);
89   table_end_row(tbl);
90
91   table_end(tbl);
92 }
93
94
95 int main(int argc UNUSED, char **argv)
96 {
97   struct fastbuf *out;
98   out = bfdopen_shared(1, 4096);
99
100   struct table *tbl = table_init(&test_tbl);
101   process_command_line_opts(argv, tbl);
102
103   print_table(tbl, out);
104   table_cleanup(tbl);
105   bclose(out);
106
107   return 0;
108 }
109