]> mj.ucw.cz Git - libucw.git/blob - ucw/xtypes-test.c
xtypes: update of tests, code cleanup
[libucw.git] / ucw / xtypes-test.c
1 /*
2  *      UCW Library -- Test of Tableprinter Types
3  *
4  *      (c) 2014 Robert Kessl <robert.kessl@economia.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include <ucw/lib.h>
11 #include <ucw/mempool.h>
12 #include <ucw/xtypes.h>
13 #include <ucw/table-types.h>
14
15 #include <errno.h>
16 #include <stdlib.h>
17 #include <inttypes.h>
18
19 static void test_size_parse_correct(struct fastbuf *out)
20 {
21   static const char *size_strs[] = {
22     "4",
23     "4KB",
24     "4MB",
25     "4GB",
26     "4TB",
27     NULL
28   };
29
30   static u64 size_parsed[] = {
31     4LLU,
32     4 * 1024LLU,
33     4 * 1024LLU * 1024LLU,
34     4 * 1024LLU * 1024LLU * 1024LLU,
35     4 * 1024LLU * 1024LLU * 1024LLU * 1024LLU
36   };
37
38   struct mempool *pool = mp_new(4096);
39
40   uint i = 0;
41   while(size_strs[i] != NULL) {
42     u64 result;
43     const char *parse_err = xt_size.parse(size_strs[i], &result, pool);
44
45     if(parse_err != NULL) {
46       die("Unexpected error in xt_size.parse");
47     }
48     if(size_parsed[i] != result) {
49       die("xt_size.parse parsed an incorrect value.");
50     }
51
52     const char *result_str = xt_size.format(&result, i | SIZE_UNITS_FIXED, pool);
53     bprintf(out, "%s %s\n", size_strs[i], result_str);
54
55     i++;
56   }
57
58   mp_delete(pool);
59 }
60
61 static void test_size_parse_errors(struct fastbuf *out)
62 {
63   static const char *size_strs[] = {
64     "1X",
65     "KB",
66     "\0",
67     NULL
68   };
69
70   uint i = 0;
71   struct mempool *pool = mp_new(4096);
72
73   while(size_strs[i] != NULL) {
74     u64 result;
75     const char *parse_err = xt_size.parse(size_strs[i], &result, pool);
76     if(parse_err == NULL) {
77       bprintf(out, "xt_size.parse incorrectly did not result in error while parsing: '%s'.\n", size_strs[i]);
78     } else {
79       bprintf(out, "xt_size.parse error: '%s'.\n", parse_err);
80     }
81
82     i++;
83   }
84
85   mp_delete(pool);
86 }
87
88 static void test_bool_parse_correct(struct fastbuf *out)
89 {
90   static const char *bool_strs[] = {
91     "0",
92     "1",
93     "false",
94     "true",
95     NULL
96   };
97
98   static bool bool_parsed[] = {
99     false,
100     true,
101     false,
102     true
103   };
104
105   struct mempool *pool = mp_new(4096);
106   uint i = 0;
107
108   while(bool_strs[i] != NULL) {
109     bool result;
110     const char *err_str = xt_bool.parse(bool_strs[i], &result, pool);
111     if(err_str != NULL) {
112       die("Unexpected error in xt_bool.parse %s", err_str);
113     }
114     if(bool_parsed[i] != result) {
115       die("xt_bool.parse parsed an incorrect value.");
116     }
117     bprintf(out, "%s %s\n", bool_strs[i], result ? "true" : "false");
118     i++;
119   }
120
121   mp_delete(pool);
122 }
123
124 static void test_timestamp_parse_correct(struct fastbuf *out)
125 {
126   static const char *timestamp_strs[] = {
127     "1403685533",
128     "2014-06-25 08:38:53",
129     NULL
130   };
131
132   static u64 timestamp_parsed[] = {
133     1403685533,
134     1403678333,
135   };
136
137   struct mempool *pool = mp_new(4096);
138   uint i = 0;
139
140   while(timestamp_strs[i]) {
141     u64 result;
142     const char *err_str = xt_timestamp.parse(timestamp_strs[i], &result, pool);
143     if(err_str != NULL) {
144       die("Unexpected error in xt_timestamp.parse: %s", err_str);
145     }
146     if(timestamp_parsed[i] != result) {
147       die("Expected: %" PRIu64 " but got %" PRIu64, timestamp_parsed[i], result);
148     }
149
150     bprintf(out, "%" PRIu64 " %" PRIu64 "\n", timestamp_parsed[i], result);
151
152     i++;
153   }
154
155   mp_delete(pool);
156 }
157
158 static void test_timestamp_parse_errors(struct fastbuf *out)
159 {
160   static const char *timestamp_strs[] = {
161     "1403685533X",
162     "2014X-06-25 08:38:53",
163     "2X014-06-25 08:38:53",
164     "2014-06-25 08:38:53X",
165     "X2014-06-25 08:38:53",
166     "X1403685533",
167     "14X03685533",
168     "1403685533X",
169     NULL
170   };
171
172   struct mempool *pool = mp_new(4096);
173   uint i = 0;
174
175   while(timestamp_strs[i]) {
176     u64 result;
177     const char *err_str = xt_timestamp.parse(timestamp_strs[i], &result, pool);
178
179     if(err_str == NULL) {
180       bprintf(out, "xt_timestamp.parse incorrectly did not result in error while parsing: '%s'.\n", timestamp_strs[i]);
181     } else {
182       bprintf(out, "xt_timestamp.parse error: '%s'.\n", err_str);
183     }
184
185     i++;
186   }
187
188   mp_delete(pool);
189 }
190
191 int main(void)
192 {
193   struct fastbuf *out;
194   out = bfdopen_shared(1, 4096);
195
196   test_size_parse_correct(out);
197   test_size_parse_errors(out);
198   test_bool_parse_correct(out);
199   test_timestamp_parse_correct(out);
200   test_timestamp_parse_errors(out);
201   bclose(out);
202
203   return 0;
204 }