]> mj.ucw.cz Git - libucw.git/blob - ucw/table-test.c
Xtypes: Documentation
[libucw.git] / ucw / table-test.c
1 /*
2  *      Types used in 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/xtypes-extra.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),
18                        TBL_COL(TEST_COL2_UINT), TBL_COL(TEST_COL1_INT), TBL_COL(TEST_COL0_STR), TBL_COL_ORDER_END };
19
20 static struct table_template test_tbl = {
21   TBL_COLUMNS {
22     [TEST_COL0_STR] = TBL_COL_STR("col0_str", 20),
23     [TEST_COL1_INT] = TBL_COL_INT("col1_int", 8),
24     [TEST_COL2_UINT] = TBL_COL_UINT("col2_uint", 9),
25     [TEST_COL3_BOOL] = TBL_COL_BOOL_FMT("col3_bool", 9, XTYPE_FMT_PRETTY),
26     [TEST_COL4_DOUBLE] = TBL_COL_DOUBLE_FMT("col4_double", 11, XTYPE_FMT_PRETTY),
27     [TEST_COL5_SIZE] = TBL_COL_SIZE("col5_size", 11),
28     [TEST_COL6_TIME] = TBL_COL_TIMESTAMP("col6_timestamp", 20),
29     TBL_COL_END
30   },
31   TBL_COL_ORDER(test_column_order),
32   TBL_FMT_HUMAN_READABLE,
33   TBL_COL_DELIMITER("\t"),
34 };
35
36 enum test_default_order_cols {
37   TEST_DEFAULT_ORDER_COL0_INT, TEST_DEFAULT_ORDER_COL1_INT, TEST_DEFAULT_ORDER_COL2_INT
38 };
39
40 static struct table_template test_default_order_tbl = {
41   TBL_COLUMNS {
42     [TEST_DEFAULT_ORDER_COL0_INT] = TBL_COL_INT("col0_int", 8),
43     [TEST_DEFAULT_ORDER_COL1_INT] = TBL_COL_INT("col1_int", 9),
44     [TEST_DEFAULT_ORDER_COL2_INT] = TBL_COL_INT("col2_int", 9),
45     TBL_COL_END
46   },
47   TBL_FMT_HUMAN_READABLE,
48   TBL_COL_DELIMITER("\t"),
49 };
50
51 static void do_default_order_test(struct fastbuf *out)
52 {
53   struct table *tbl = table_init(&test_default_order_tbl);
54
55   table_start(tbl, out);
56
57   table_col_int(tbl, TEST_DEFAULT_ORDER_COL0_INT, 0);
58   table_col_int(tbl, TEST_DEFAULT_ORDER_COL1_INT, 1);
59   table_col_int(tbl, TEST_DEFAULT_ORDER_COL2_INT, 2);
60   table_end_row(tbl);
61
62   table_col_int(tbl, TEST_DEFAULT_ORDER_COL0_INT, 10);
63   table_col_int(tbl, TEST_DEFAULT_ORDER_COL1_INT, 11);
64   table_col_int(tbl, TEST_DEFAULT_ORDER_COL2_INT, 12);
65   table_end_row(tbl);
66
67   table_end(tbl);
68   table_cleanup(tbl);
69 }
70
71 /**
72  * tests: table_col_int, table_col_uint, table_col_bool, table_col_double, table_col_printf
73  **/
74 static void do_print1(struct table *test_tbl)
75 {
76   table_col_str(test_tbl, TEST_COL0_STR, "sdsdf");
77   table_col_int(test_tbl, TEST_COL1_INT, -10);
78   table_col_int(test_tbl, TEST_COL1_INT, 10000);
79   table_col_uint(test_tbl, TEST_COL2_UINT, 10);
80   table_col_printf(test_tbl, TEST_COL2_UINT, "XXX-%u", 22222);
81   table_col_bool(test_tbl, TEST_COL3_BOOL, 1);
82   table_col_double(test_tbl, TEST_COL4_DOUBLE, 1.5);
83   table_col_size(test_tbl, TEST_COL5_SIZE, (1024LLU*1024LLU*1024LLU*5LLU));
84   table_col_timestamp(test_tbl, TEST_COL6_TIME, 1404305876);
85   table_end_row(test_tbl);
86
87   table_col_str(test_tbl, TEST_COL0_STR, "test");
88   table_col_int(test_tbl, TEST_COL1_INT, -100);
89   table_col_uint(test_tbl, TEST_COL2_UINT, 100);
90   table_col_bool(test_tbl, TEST_COL3_BOOL, 0);
91   table_col_double(test_tbl, TEST_COL4_DOUBLE, 1.5);
92   table_col_size(test_tbl, TEST_COL5_SIZE, (1024LLU*1024LLU*1024LLU*2LLU));
93   table_col_timestamp(test_tbl, TEST_COL6_TIME, 1404305909);
94   table_end_row(test_tbl);
95 }
96
97 static char **cli_table_opts;
98
99 enum test_type_t {
100   TEST_DEFAULT_COLUMN_ORDER = 1,
101   TEST_INVALID_OPTION = 2,
102   TEST_INVALID_ORDER = 3
103 };
104
105 static int test_to_perform = -1;
106
107 static struct opt_section table_printer_opts = {
108   OPT_ITEMS {
109     OPT_HELP("Options:"),
110     OPT_STRING_MULTIPLE('T', "table", cli_table_opts, OPT_REQUIRED_VALUE, "\tSets options for the table."),
111     OPT_SWITCH('d', 0, test_to_perform, TEST_DEFAULT_COLUMN_ORDER, OPT_SINGLE, "\tRun the test that uses the default column order."),
112     OPT_SWITCH('i', 0, test_to_perform, TEST_INVALID_OPTION, OPT_SINGLE, "\tTest the output for invalid option."),
113     OPT_SWITCH('n', 0, test_to_perform, TEST_INVALID_ORDER, OPT_SINGLE, "\tTest the output for invalid names of columns for the 'cols' option."),
114     OPT_END
115   }
116 };
117
118 static void process_command_line_opts(char *argv[], struct table *tbl)
119 {
120   GARY_INIT(cli_table_opts, 0);
121
122   opt_parse(&table_printer_opts, argv+1);
123   const char *err = table_set_gary_options(tbl, cli_table_opts);
124   if(err) {
125     opt_failure("error while setting cmd line options: %s", err);
126   }
127
128   GARY_FREE(cli_table_opts);
129 }
130
131 static bool user_defined_option(struct table *tbl UNUSED, const char *key, const char *value, const char **err UNUSED)
132 {
133   if(value == NULL && strcmp(key, "novaluekey") == 0) {
134     printf("setting key: %s; value: (null)\n", key);
135     return 1;
136   }
137   if(value != NULL && strcmp(value, "value") == 0 &&
138      key != NULL && strcmp(key, "valuekey") == 0) {
139     printf("setting key: %s; value: %s\n", key, value);
140     return 1;
141   }
142   return 0;
143 }
144
145 static void test_option_parser(struct table *tbl)
146 {
147   struct table_formatter test_option_fmtr = table_fmt_human_readable;
148   test_option_fmtr.process_option = user_defined_option;
149
150   const struct table_formatter *tmp_fmtr = tbl->formatter;
151   tbl->formatter = &test_option_fmtr;
152
153   const char *rv = table_set_option(tbl, "invalid:option");
154   if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
155
156   rv = table_set_option(tbl, "invalid");
157   if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
158
159   rv = table_set_option(tbl, "novaluekey");
160   if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
161
162   rv = table_set_option(tbl, "valuekey:value");
163   if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
164
165   tbl->formatter = tmp_fmtr;
166 }
167
168 int main(int argc UNUSED, char **argv)
169 {
170   struct fastbuf *out;
171   out = bfdopen_shared(1, 4096);
172
173   struct table *tbl = table_init(&test_tbl);
174
175   process_command_line_opts(argv, tbl);
176
177   const char *rv = NULL;
178   switch(test_to_perform) {
179   case TEST_INVALID_ORDER:
180     rv = table_set_option(tbl, "cols:test_col0_str,test_col1_int,xxx");
181     if(rv) printf("Tableprinter option parser returned: '%s'.\n", rv);
182     return 0;
183   case TEST_DEFAULT_COLUMN_ORDER:
184     do_default_order_test(out);
185     bclose(out);
186     return 0;
187   case TEST_INVALID_OPTION:
188     test_option_parser(tbl);
189     bclose(out);
190     return 0;
191   };
192
193   table_start(tbl, out);
194   do_print1(tbl);
195   table_end(tbl);
196   table_cleanup(tbl);
197
198   bclose(out);
199
200   return 0;
201 }