]> mj.ucw.cz Git - libucw.git/blob - ucw/xtypes-test.c
Build: Added support for custom PKG_CONFIG_PATH in UCW::Configure::PkgConfig().
[libucw.git] / ucw / xtypes-test.c
1 /*
2  *      UCW Library -- Test of Extended 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 <inttypes.h>
17 #include <stdlib.h>
18 #include <time.h>
19
20 static void test_size_parse_correct(struct fastbuf *out)
21 {
22   static const char *size_strs[] = {
23     "4",
24     "4KB",
25     "4MB",
26     "4GB",
27     "4TB",
28     NULL
29   };
30
31   static u64 size_parsed[] = {
32     4LLU,
33     4 * 1024LLU,
34     4 * 1024LLU * 1024LLU,
35     4 * 1024LLU * 1024LLU * 1024LLU,
36     4 * 1024LLU * 1024LLU * 1024LLU * 1024LLU
37   };
38
39   struct mempool *pool = mp_new(4096);
40
41   uint i = 0;
42   while(size_strs[i] != NULL) {
43     u64 result;
44     const char *parse_err = xt_size.parse(size_strs[i], &result, pool);
45
46     if(parse_err != NULL) {
47       die("Unexpected error in xt_size.parse");
48     }
49     if(size_parsed[i] != result) {
50       die("xt_size.parse parsed an incorrect value.");
51     }
52
53     const char *result_str = xt_size.format(&result, XT_SIZE_FMT_UNIT(i), pool);
54     bprintf(out, "%s %s\n", size_strs[i], result_str);
55
56     i++;
57   }
58
59   mp_delete(pool);
60 }
61
62 static void test_size_parse_errors(struct fastbuf *out)
63 {
64   static const char *size_strs[] = {
65     "1X",
66     "KB",
67     "X1KB",
68     "1XKB",
69     "1KBX",
70     "\0",
71     NULL
72   };
73
74   uint i = 0;
75   struct mempool *pool = mp_new(4096);
76
77   while(size_strs[i] != NULL) {
78     u64 result;
79     const char *parse_err = xt_size.parse(size_strs[i], &result, pool);
80     if(parse_err == NULL) {
81       bprintf(out, "xt_size.parse incorrectly did not result in error while parsing: '%s'.\n", size_strs[i]);
82     } else {
83       bprintf(out, "xt_size.parse error: '%s'.\n", parse_err);
84     }
85
86     i++;
87   }
88
89   mp_delete(pool);
90 }
91
92 static void test_bool_parse_correct(struct fastbuf *out)
93 {
94   static const char *bool_strs[] = {
95     "0",
96     "1",
97     "false",
98     "true",
99     NULL
100   };
101
102   static bool bool_parsed[] = {
103     false,
104     true,
105     false,
106     true
107   };
108
109   struct mempool *pool = mp_new(4096);
110   uint i = 0;
111
112   while(bool_strs[i] != NULL) {
113     bool result;
114     const char *err_str = xt_bool.parse(bool_strs[i], &result, pool);
115     if(err_str != NULL) {
116       die("Unexpected error in xt_bool.parse %s", err_str);
117     }
118     if(bool_parsed[i] != result) {
119       die("xt_bool.parse parsed an incorrect value.");
120     }
121     bprintf(out, "%s %s\n", bool_strs[i], result ? "true" : "false");
122     i++;
123   }
124
125   mp_delete(pool);
126 }
127
128 static void test_timestamp_parse_correct(struct fastbuf *out)
129 {
130   static const char *timestamp_strs[] = {
131     "1403685533",
132     "2014-06-25 08:38:53",
133     NULL
134   };
135
136   static u64 timestamp_parsed[] = {
137     1403685533,
138     0,
139   };
140
141   // fill the value of timestamp_parsed as the timestamp in the
142   // current time zone.
143   {
144     struct tm parsed_time;
145     strptime(timestamp_strs[1], "%F %T", &parsed_time);
146     timestamp_parsed[1] = mktime(&parsed_time);
147   }
148
149   struct mempool *pool = mp_new(4096);
150   uint i = 0;
151
152   while(timestamp_strs[i]) {
153     u64 result;
154     const char *err_str = xt_timestamp.parse(timestamp_strs[i], &result, pool);
155     if(err_str != NULL) {
156       die("Unexpected error in xt_timestamp.parse: %s", err_str);
157     }
158     if(timestamp_parsed[i] != result) {
159       die("Expected: %" PRIu64 " but got %" PRIu64, timestamp_parsed[i], result);
160     }
161
162     bprintf(out, "%" PRIu64 " %" PRIu64 "\n", timestamp_parsed[i], result);
163
164     i++;
165   }
166
167   mp_delete(pool);
168 }
169
170 static void test_timestamp_parse_errors(struct fastbuf *out)
171 {
172   static const char *timestamp_strs[] = {
173     "1403685533X",
174     "2014X-06-25 08:38:53",
175     "2X014-06-25 08:38:53",
176     "2014-06-25 08:38:53X",
177     "X2014-06-25 08:38:53",
178     "X1403685533",
179     "14X03685533",
180     "1403685533X",
181     NULL
182   };
183
184   struct mempool *pool = mp_new(4096);
185   uint i = 0;
186
187   while(timestamp_strs[i]) {
188     u64 result;
189     const char *err_str = xt_timestamp.parse(timestamp_strs[i], &result, pool);
190
191     if(err_str == NULL) {
192       bprintf(out, "xt_timestamp.parse incorrectly did not result in error while parsing: '%s'.\n", timestamp_strs[i]);
193     } else {
194       bprintf(out, "xt_timestamp.parse error: '%s'.\n", err_str);
195     }
196
197     i++;
198   }
199
200   mp_delete(pool);
201 }
202
203 int main(void)
204 {
205   struct fastbuf *out;
206   out = bfdopen_shared(1, 4096);
207
208   test_size_parse_correct(out);
209   test_size_parse_errors(out);
210   test_bool_parse_correct(out);
211   test_timestamp_parse_correct(out);
212   test_timestamp_parse_errors(out);
213   bclose(out);
214
215   return 0;
216 }