]> mj.ucw.cz Git - libucw.git/blob - ucw/table-test-2.c
Tests: table-test now works on 32-bit architectures
[libucw.git] / ucw / table-test-2.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/xtypes-extra.h>
10 #include <ucw/opt.h>
11 #include <stdio.h>
12
13 enum test_table_cols {
14   TEST_COL0_SIZE, TEST_COL1_TS
15 };
16
17 static struct table_template test_tbl = {
18   TBL_COLUMNS {
19     [TEST_COL0_SIZE] = TBL_COL_SIZE_FMT("size", 15, XT_SIZE_FMT_UNIT(XT_SIZE_UNIT_BYTE)),
20     [TEST_COL1_TS] = TBL_COL_TIMESTAMP("ts", 20),
21     TBL_COL_END
22   },
23   TBL_FMT_HUMAN_READABLE,
24 };
25
26 static void do_test(void)
27 {
28   struct fastbuf *out;
29   out = bfdopen_shared(1, 4096);
30   struct table *tbl = table_init(&test_tbl);
31   table_start(tbl, out);
32
33   u64 test_time = 1403685533;
34   s64 test_size = 4LLU*(1024LLU * 1024LLU * 1024LLU);
35
36   table_col_size(tbl, TEST_COL0_SIZE, test_size);
37   table_col_timestamp(tbl, TEST_COL1_TS, test_time);
38   table_end_row(tbl);
39
40   tbl->column_order[TEST_COL0_SIZE].fmt = XT_SIZE_FMT_UNIT(XT_SIZE_UNIT_KILOBYTE);
41   table_col_size(tbl, TEST_COL0_SIZE, test_size);
42   table_col_timestamp(tbl, TEST_COL1_TS, test_time);
43   table_end_row(tbl);
44
45   tbl->column_order[TEST_COL0_SIZE].fmt = XT_SIZE_FMT_UNIT(XT_SIZE_UNIT_MEGABYTE);
46   table_col_size(tbl, TEST_COL0_SIZE, test_size);
47   table_col_timestamp(tbl, TEST_COL1_TS, test_time);
48   table_end_row(tbl);
49
50   tbl->column_order[TEST_COL0_SIZE].fmt = XT_SIZE_FMT_UNIT(XT_SIZE_UNIT_GIGABYTE);
51   tbl->column_order[TEST_COL1_TS].fmt = XT_TIMESTAMP_FMT_DATETIME;
52   table_col_size(tbl, TEST_COL0_SIZE, test_size);
53   table_col_timestamp(tbl, TEST_COL1_TS, test_time);
54   table_end_row(tbl);
55
56   test_size = test_size * 1024LU;
57   tbl->column_order[TEST_COL0_SIZE].fmt = XT_SIZE_FMT_UNIT(XT_SIZE_UNIT_TERABYTE);
58   tbl->column_order[TEST_COL1_TS].fmt = XT_TIMESTAMP_FMT_DATETIME;
59   table_col_size(tbl, TEST_COL0_SIZE, test_size);
60   table_col_timestamp(tbl, TEST_COL1_TS, test_time);
61   table_end_row(tbl);
62
63   table_end(tbl);
64   table_cleanup(tbl);
65
66   bclose(out);
67 }
68
69 static struct table_template test_tbl2 = {
70   TBL_COLUMNS {
71     [TEST_COL0_SIZE] = TBL_COL_SIZE_FMT("size", 15, XT_SIZE_FMT_UNIT(XT_SIZE_UNIT_BYTE)),
72     [TEST_COL1_TS] = TBL_COL_TIMESTAMP("ts", 20),
73     TBL_COL_END
74   },
75   TBL_FMT_HUMAN_READABLE,
76 };
77
78 static void do_test2(void)
79 {
80   struct fastbuf *out;
81   out = bfdopen_shared(1, 4096);
82   struct table *tbl = table_init(&test_tbl2);
83   table_set_col_order_by_name(tbl, "");
84   const char *err = table_set_option_value(tbl, "cols", "size[MB,TB,KB],size[MB],size[GB],size[TB],size[auto],ts[datetime],ts[timestamp],ts[],size[MB,KB]");
85   if(err) {
86     opt_failure("err in table_set_option_value: '%s'.", err);
87     abort();
88   }
89   table_start(tbl, out);
90
91   u64 test_time = 1403685533;
92   s64 test_size = 4LU*(1024LU * 1024LU);
93
94   table_col_size(tbl, TEST_COL0_SIZE, test_size);
95   table_col_timestamp(tbl, TEST_COL1_TS, test_time);
96   table_end_row(tbl);
97
98   test_size = test_size * 1024LU;
99
100   table_col_size(tbl, TEST_COL0_SIZE, test_size);
101   table_col_timestamp(tbl, TEST_COL1_TS, test_time);
102   table_end_row(tbl);
103
104   test_size = test_size * 1024LU;
105
106   table_col_size(tbl, TEST_COL0_SIZE, test_size);
107   table_col_timestamp(tbl, TEST_COL1_TS, test_time);
108   table_end_row(tbl);
109
110   table_end(tbl);
111   table_cleanup(tbl);
112
113   bclose(out);
114 }
115
116 static void do_test3(void)
117 {
118   struct fastbuf *out;
119   out = bfdopen_shared(1, 4096);
120
121   struct table *tbl = table_init(&test_tbl2);
122   table_set_col_order_by_name(tbl, "");
123   const char *err = table_set_option_value(tbl, "cols", "size[MB,TB,KB],size[MB],size[GB],size[TB],size[auto],ts[datetime],ts[timestamp],size[MB,KB]");
124   bprintf(out, "Error occured: %s\n", err);
125   table_cleanup(tbl);
126
127
128   tbl = table_init(&test_tbl2);
129   table_set_col_order_by_name(tbl, "");
130   err = table_set_option_value(tbl, "cols", "size[MB,TB,KB],size[MB],size[GB],size[TB],size[auto],ts[,,datetime,timestamp],ts[],size[MB,KB");
131   bprintf(out, "Error occured: %s\n", err);
132   table_cleanup(tbl);
133
134
135   tbl = table_init(&test_tbl2);
136   table_set_col_order_by_name(tbl, "");
137   err = table_set_option_value(tbl, "cols", "size[MB,TB,KB");
138   bprintf(out, "Error occured: %s\n", err);
139   table_cleanup(tbl);
140
141
142   tbl = table_init(&test_tbl2);
143   table_set_col_order_by_name(tbl, "");
144   err = table_set_option_value(tbl, "cols", "size[MB,TB,KB]");
145   bprintf(out, "Error occured: %s\n", err);
146   table_cleanup(tbl);
147
148   bclose(out);
149 }
150
151 int main(int argc UNUSED, char **argv UNUSED)
152 {
153   do_test();
154   do_test2();
155   do_test3();
156
157   return 0;
158 }
159