]> mj.ucw.cz Git - libucw.git/blob - ucw/table-test.c
tableprinter: code cleanup, update of formats of double
[libucw.git] / ucw / table-test.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/table-types.h>
10 #include <ucw/opt.h>
11 #include <stdio.h>
12
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
15 };
16
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) };
18
19 static struct table_template test_tbl = {
20   TBL_COLUMNS {
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("col3_bool", 9),
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),
28     TBL_COL_END
29   },
30   TBL_COL_ORDER(test_column_order),
31   TBL_OUTPUT_HUMAN_READABLE,
32   TBL_COL_DELIMITER("\t"),
33 };
34
35 enum test_default_order_cols {
36   TEST_DEFAULT_ORDER_COL0_INT, TEST_DEFAULT_ORDER_COL1_INT, TEST_DEFAULT_ORDER_COL2_INT
37 };
38
39 static struct table_template test_default_order_tbl = {
40   TBL_COLUMNS {
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),
44     TBL_COL_END
45   },
46   TBL_OUTPUT_HUMAN_READABLE,
47   TBL_COL_DELIMITER("\t"),
48 };
49
50 static void do_default_order_test(struct fastbuf *out)
51 {
52   struct table *tbl = table_init(&test_default_order_tbl);
53
54   table_start(tbl, out);
55
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);
59   table_end_row(tbl);
60
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);
64   table_end_row(tbl);
65
66   table_end(tbl);
67   table_cleanup(tbl);
68 }
69
70 /**
71  * tests: table_col_int, table_col_uint, table_col_bool, table_col_double, table_col_printf
72  **/
73 static void do_print1(struct table *test_tbl)
74 {
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);
85
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);
94 }
95
96 static char **cli_table_opts;
97
98 enum test_type_t {
99   TEST_DEFAULT_COLUMN_ORDER = 1,
100   TEST_INVALID_OPTION = 2,
101   TEST_INVALID_ORDER = 3
102 };
103
104 static int test_to_perform = -1;
105
106 static struct opt_section table_printer_opts = {
107   OPT_ITEMS {
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."),
113     OPT_END
114   }
115 };
116
117 static void process_command_line_opts(char *argv[], struct table *tbl)
118 {
119   GARY_INIT(cli_table_opts, 0);
120
121   opt_parse(&table_printer_opts, argv+1);
122   const char *err = table_set_gary_options(tbl, cli_table_opts);
123   if(err) {
124     opt_failure("error while setting cmd line options: %s", err);
125   }
126
127   GARY_FREE(cli_table_opts);
128 }
129
130 static bool user_defined_option(struct table *tbl UNUSED, const char *key, const char *value, const char **err UNUSED)
131 {
132   if(value == NULL && strcmp(key, "novaluekey") == 0) {
133     printf("setting key: %s; value: (null)\n", key);
134     return 1;
135   }
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);
139     return 1;
140   }
141   return 0;
142 }
143
144 static void test_option_parser(struct table *tbl)
145 {
146   tbl->formatter->process_option = user_defined_option;
147   const char *rv = table_set_option(tbl, "invalid:option");
148   if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
149
150   rv = table_set_option(tbl, "invalid");
151   if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
152
153   rv = table_set_option(tbl, "novaluekey");
154   if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
155
156   rv = table_set_option(tbl, "valuekey:value");
157   if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
158 }
159
160 int main(int argc UNUSED, char **argv)
161 {
162   struct fastbuf *out;
163   out = bfdopen_shared(1, 4096);
164
165   struct table *tbl = table_init(&test_tbl);
166
167   process_command_line_opts(argv, tbl);
168
169   const char *rv = NULL;
170   switch(test_to_perform) {
171   case TEST_INVALID_ORDER:
172     rv = table_set_option(tbl, "cols:test_col0_str,test_col1_int,xxx");
173     if(rv) printf("Tableprinter option parser returned: '%s'.\n", rv);
174     return 0;
175   case TEST_DEFAULT_COLUMN_ORDER:
176     do_default_order_test(out);
177     bclose(out);
178     return 0;
179   case TEST_INVALID_OPTION:
180     test_option_parser(tbl);
181     bclose(out);
182     return 0;
183   };
184
185   table_start(tbl, out);
186   do_print1(tbl);
187   table_end(tbl);
188   table_cleanup(tbl);
189
190   bclose(out);
191
192   return 0;
193 }