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