]> mj.ucw.cz Git - libucw.git/blob - ucw/table-test-align.c
tableprinter: table_init now makes a copy of its argument
[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
13 enum test_table_cols {
14   test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
15 };
16
17 static struct table test_tbl = {
18   TBL_COLUMNS {
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("col4_double", 11 | CELL_ALIGN_LEFT, 5),
24     TBL_COL_END
25   },
26   TBL_OUTPUT_HUMAN_READABLE,
27   TBL_COL_DELIMITER("\t"),
28 };
29
30 static int test_to_perform = -1;
31 static char **cli_table_opts;
32
33 static struct opt_section table_printer_opts = {
34   OPT_ITEMS {
35     OPT_HELP("Options:"),
36     OPT_STRING_MULTIPLE('T', "table", cli_table_opts, OPT_REQUIRED_VALUE, "\tSets options for the table."),
37     OPT_END
38   }
39 };
40
41
42 static void process_command_line_opts(char *argv[], struct table *tbl)
43 {
44   GARY_INIT(cli_table_opts, 0);
45
46   opt_parse(&table_printer_opts, argv+1);
47   table_set_gary_options(tbl, cli_table_opts);
48
49   GARY_FREE(cli_table_opts);
50 }
51
52 static void print_table(struct table *tbl, struct fastbuf *out)
53 {
54   table_start(tbl, out);
55
56   struct fastbuf *colfb = table_col_fbstart(tbl, test_col0_str);
57   bputs(colfb, "HELLO");
58   bprintf(colfb, ",col_idx:%d", test_col0_str);
59   table_col_fbend(tbl);
60
61   table_col_int(tbl, test_col1_int, -10);
62   table_col_uint(tbl, test_col2_uint, 10);
63   table_col_bool(tbl, test_col3_bool, 0);
64   table_col_double(tbl, test_col4_double, 3.1415926535897);
65   table_end_row(tbl);
66
67
68
69   colfb = table_col_fbstart(tbl, test_col0_str);
70   bputs(colfb, "EHLO");
71   bprintf(colfb, ",col_idx:%d", test_col0_str);
72   table_col_fbend(tbl);
73
74   table_col_int(tbl, test_col1_int, -12345);
75   table_col_uint(tbl, test_col2_uint, 0xFF);
76   table_col_bool(tbl, test_col3_bool, 1);
77   table_col_double(tbl, test_col4_double, 1.61803398875);
78   table_end_row(tbl);
79
80
81
82   colfb = table_col_fbstart(tbl, test_col0_str);
83   bputs(colfb, "AHOJ");
84   bprintf(colfb, ",col_idx:%d", test_col0_str);
85   table_col_fbend(tbl);
86
87   table_col_int(tbl, test_col1_int, -54321);
88   table_col_uint(tbl, test_col2_uint, 0xFF00);
89   table_col_bool(tbl, test_col3_bool, 0);
90   table_col_double(tbl, test_col4_double, 2.718281828459045);
91   table_end_row(tbl);
92
93   table_end(tbl);
94 }
95
96
97 int main(int argc UNUSED, char **argv)
98 {
99   struct fastbuf *out;
100   out = bfdopen_shared(1, 4096);
101
102   struct table *tbl = table_init(&test_tbl);
103   process_command_line_opts(argv, tbl);
104
105   print_table(tbl, out);
106   table_cleanup(tbl);
107   bclose(out);
108
109   return 0;
110 }
111