]> mj.ucw.cz Git - libucw.git/blob - ucw/table-test.c
b033720838e5d83964660057600b85051e82e4c5
[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/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 uint test_column_order[] = { test_col3_bool, test_col4_double, test_col2_uint,test_col1_int, test_col0_str };
17
18 static struct table test_tbl = {
19   TBL_COLUMNS {
20     TBL_COL_STR(test, col0_str, 20),
21     TBL_COL_INT(test, col1_int, 8),
22     TBL_COL_UINT(test, col2_uint, 9),
23     TBL_COL_BOOL(test, col3_bool, 9),
24     TBL_COL_DOUBLE(test, col4_double, 11, 2),
25     TBL_COL_END
26   },
27   TBL_COL_ORDER(test_column_order),
28   TBL_OUTPUT_HUMAN_READABLE,
29   TBL_COL_DELIMITER("\t"),
30 };
31
32 enum test_default_order_cols {
33   test_default_order_col0_int, test_default_order_col1_int, test_default_order_col2_int
34 };
35
36 static struct table test_default_order_tbl = {
37   TBL_COLUMNS {
38     TBL_COL_INT(test_default_order, col0_int, 8),
39     TBL_COL_INT(test_default_order, col1_int, 9),
40     TBL_COL_INT(test_default_order, col2_int, 9),
41     TBL_COL_END
42   },
43   TBL_OUTPUT_HUMAN_READABLE,
44   TBL_COL_DELIMITER("\t"),
45 };
46
47 static void do_default_order_test(struct fastbuf *out)
48 {
49   table_init(&test_default_order_tbl, out);
50   table_start(&test_default_order_tbl);
51
52   table_set_int(&test_default_order_tbl, test_default_order_col0_int, 0);
53   table_set_int(&test_default_order_tbl, test_default_order_col1_int, 1);
54   table_set_int(&test_default_order_tbl, test_default_order_col2_int, 2);
55   table_end_row(&test_default_order_tbl);
56
57   table_set_int(&test_default_order_tbl, test_default_order_col0_int, 10);
58   table_set_int(&test_default_order_tbl, test_default_order_col1_int, 11);
59   table_set_int(&test_default_order_tbl, test_default_order_col2_int, 12);
60   table_end_row(&test_default_order_tbl);
61
62   table_end(&test_default_order_tbl);
63   table_cleanup(&test_default_order_tbl);
64 }
65
66 /**
67  * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
68  **/
69 static void do_print1(struct table *test_tbl)
70 {
71   table_set_str(test_tbl, test_col0_str, "sdsdf");
72   table_append_str(test_tbl, "aaaaa");
73   table_set_int(test_tbl, test_col1_int, -10);
74   table_set_int(test_tbl, test_col1_int, 10000);
75   table_set_uint(test_tbl, test_col2_uint, 10);
76   table_set_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
77   table_set_bool(test_tbl, test_col3_bool, 1);
78   table_set_double(test_tbl, test_col4_double, 1.5);
79   table_end_row(test_tbl);
80
81   table_set_str(test_tbl, test_col0_str, "test");
82   table_append_str(test_tbl, "bbbbb");
83   table_set_int(test_tbl, test_col1_int, -100);
84   table_set_uint(test_tbl, test_col2_uint, 100);
85   table_set_bool(test_tbl, test_col3_bool, 0);
86   table_set_double(test_tbl, test_col4_double, 1.5);
87   table_end_row(test_tbl);
88 }
89
90 static char **cli_table_opts;
91 static int test_default_column_order;
92 static int test_invalid_option;
93 static int test_invalid_order;
94
95 static struct opt_section table_printer_opts = {
96   OPT_ITEMS {
97     OPT_HELP("Options:"),
98     OPT_STRING_MULTIPLE('T', "table", cli_table_opts, OPT_REQUIRED_VALUE, "\tSets options for the table."),
99     OPT_BOOL('d', 0, test_default_column_order, 0, "\tRun the test that uses the default column order."),
100     OPT_BOOL('i', 0, test_invalid_option, 0, "\tTest the output for invalid option."),
101     OPT_BOOL('n', 0, test_invalid_order, 0, "\tTest the output for invalid names of columns for the 'cols' option."),
102     OPT_END
103   }
104 };
105
106 static void process_command_line_opts(char *argv[], struct table *tbl)
107 {
108   GARY_INIT(cli_table_opts, 0);
109
110   opt_parse(&table_printer_opts, argv+1);
111
112   for(uint i = 0; i < GARY_SIZE(cli_table_opts); i++) {
113     const char *rv = table_set_option(tbl, cli_table_opts[i]);
114     ASSERT_MSG(rv == NULL, "Tableprinter option parser returned error: '%s'.", rv);
115   }
116
117   GARY_FREE(cli_table_opts);
118 }
119
120 static bool user_defined_option(struct table *tbl UNUSED, const char *key, const char *value, const char **err UNUSED)
121 {
122   if(value == NULL && strcmp(key, "novaluekey") == 0) {
123     printf("setting key: %s; value: (null)\n", key);
124     return 1;
125   }
126   if(value != NULL && strcmp(value, "value") == 0 &&
127      key != NULL && strcmp(key, "valuekey") == 0) {
128     printf("setting key: %s; value: %s\n", key, value);
129     return 1;
130   }
131   return 0;
132 }
133
134 static void test_option_parser(struct table *tbl)
135 {
136   tbl->formatter->process_option = user_defined_option;
137   const char *rv = table_set_option(tbl, "invalid:option");
138   if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
139
140   rv = table_set_option(tbl, "invalid");
141   if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
142
143   rv = table_set_option(tbl, "novaluekey");
144   if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
145
146   rv = table_set_option(tbl, "valuekey:value");
147   if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv);
148 }
149
150 int main(int argc UNUSED, char **argv)
151 {
152   struct fastbuf *out;
153   out = bfdopen_shared(1, 4096);
154
155   table_init(&test_tbl, out);
156
157   process_command_line_opts(argv, &test_tbl);
158
159   if(test_invalid_order == 1) {
160     const char *rv = table_set_option(&test_tbl, "cols:test_col0_str,test_col1_int,xxx");
161     if(rv) printf("Tableprinter option parser returned: '%s'.\n", rv);
162     return 0;
163   } else if(test_default_column_order == 1) {
164     do_default_order_test(out);
165     bclose(out);
166     return 0;
167   } else if(test_invalid_option == 1) {
168     test_option_parser(&test_tbl);
169     bclose(out);
170     return 0;
171   }
172
173   table_start(&test_tbl);
174   do_print1(&test_tbl);
175   table_end(&test_tbl);
176   table_cleanup(&test_tbl);
177
178   bclose(out);
179
180   return 0;
181 }