]> mj.ucw.cz Git - libucw.git/blob - ucw/xtypes-test.c
517e7f2c1c8c11b206bd2e4c61c377a83cdb2822
[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/xtypes-extra.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, XT_SIZE_FMT_UNIT(i), 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     "X1KB",
67     "1XKB",
68     "1KBX",
69     "\0",
70     NULL
71   };
72
73   uint i = 0;
74   struct mempool *pool = mp_new(4096);
75
76   while(size_strs[i] != NULL) {
77     u64 result;
78     const char *parse_err = xt_size.parse(size_strs[i], &result, pool);
79     if(parse_err == NULL) {
80       bprintf(out, "xt_size.parse incorrectly did not result in error while parsing: '%s'.\n", size_strs[i]);
81     } else {
82       bprintf(out, "xt_size.parse error: '%s'.\n", parse_err);
83     }
84
85     i++;
86   }
87
88   mp_delete(pool);
89 }
90
91 static void test_bool_parse_correct(struct fastbuf *out)
92 {
93   static const char *bool_strs[] = {
94     "0",
95     "1",
96     "false",
97     "true",
98     NULL
99   };
100
101   static bool bool_parsed[] = {
102     false,
103     true,
104     false,
105     true
106   };
107
108   struct mempool *pool = mp_new(4096);
109   uint i = 0;
110
111   while(bool_strs[i] != NULL) {
112     bool result;
113     const char *err_str = xt_bool.parse(bool_strs[i], &result, pool);
114     if(err_str != NULL) {
115       die("Unexpected error in xt_bool.parse %s", err_str);
116     }
117     if(bool_parsed[i] != result) {
118       die("xt_bool.parse parsed an incorrect value.");
119     }
120     bprintf(out, "%s %s\n", bool_strs[i], result ? "true" : "false");
121     i++;
122   }
123
124   mp_delete(pool);
125 }
126
127 static void test_timestamp_parse_correct(struct fastbuf *out)
128 {
129   static const char *timestamp_strs[] = {
130     "1403685533",
131     "2014-06-25 08:38:53",
132     NULL
133   };
134
135   static u64 timestamp_parsed[] = {
136     1403685533,
137     1403678333,
138   };
139
140   struct mempool *pool = mp_new(4096);
141   uint i = 0;
142
143   while(timestamp_strs[i]) {
144     u64 result;
145     const char *err_str = xt_timestamp.parse(timestamp_strs[i], &result, pool);
146     if(err_str != NULL) {
147       die("Unexpected error in xt_timestamp.parse: %s", err_str);
148     }
149     if(timestamp_parsed[i] != result) {
150       die("Expected: %" PRIu64 " but got %" PRIu64, timestamp_parsed[i], result);
151     }
152
153     bprintf(out, "%" PRIu64 " %" PRIu64 "\n", timestamp_parsed[i], result);
154
155     i++;
156   }
157
158   mp_delete(pool);
159 }
160
161 static void test_timestamp_parse_errors(struct fastbuf *out)
162 {
163   static const char *timestamp_strs[] = {
164     "1403685533X",
165     "2014X-06-25 08:38:53",
166     "2X014-06-25 08:38:53",
167     "2014-06-25 08:38:53X",
168     "X2014-06-25 08:38:53",
169     "X1403685533",
170     "14X03685533",
171     "1403685533X",
172     NULL
173   };
174
175   struct mempool *pool = mp_new(4096);
176   uint i = 0;
177
178   while(timestamp_strs[i]) {
179     u64 result;
180     const char *err_str = xt_timestamp.parse(timestamp_strs[i], &result, pool);
181
182     if(err_str == NULL) {
183       bprintf(out, "xt_timestamp.parse incorrectly did not result in error while parsing: '%s'.\n", timestamp_strs[i]);
184     } else {
185       bprintf(out, "xt_timestamp.parse error: '%s'.\n", err_str);
186     }
187
188     i++;
189   }
190
191   mp_delete(pool);
192 }
193
194 int main(void)
195 {
196   struct fastbuf *out;
197   out = bfdopen_shared(1, 4096);
198
199   test_size_parse_correct(out);
200   test_size_parse_errors(out);
201   test_bool_parse_correct(out);
202   test_timestamp_parse_correct(out);
203   test_timestamp_parse_errors(out);
204   bclose(out);
205
206   return 0;
207 }