2 * UCW Library -- Test of Extended Types
4 * (c) 2014 Robert Kessl <robert.kessl@economia.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include <ucw/mempool.h>
12 #include <ucw/xtypes.h>
13 #include <ucw/xtypes-extra.h>
19 static void test_size_parse_correct(struct fastbuf *out)
21 static const char *size_strs[] = {
30 static u64 size_parsed[] = {
33 4 * 1024LLU * 1024LLU,
34 4 * 1024LLU * 1024LLU * 1024LLU,
35 4 * 1024LLU * 1024LLU * 1024LLU * 1024LLU
38 struct mempool *pool = mp_new(4096);
41 while(size_strs[i] != NULL) {
43 const char *parse_err = xt_size.parse(size_strs[i], &result, pool);
45 if(parse_err != NULL) {
46 die("Unexpected error in xt_size.parse");
48 if(size_parsed[i] != result) {
49 die("xt_size.parse parsed an incorrect value.");
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);
61 static void test_size_parse_errors(struct fastbuf *out)
63 static const char *size_strs[] = {
74 struct mempool *pool = mp_new(4096);
76 while(size_strs[i] != NULL) {
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]);
82 bprintf(out, "xt_size.parse error: '%s'.\n", parse_err);
91 static void test_bool_parse_correct(struct fastbuf *out)
93 static const char *bool_strs[] = {
101 static bool bool_parsed[] = {
108 struct mempool *pool = mp_new(4096);
111 while(bool_strs[i] != NULL) {
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);
117 if(bool_parsed[i] != result) {
118 die("xt_bool.parse parsed an incorrect value.");
120 bprintf(out, "%s %s\n", bool_strs[i], result ? "true" : "false");
127 static void test_timestamp_parse_correct(struct fastbuf *out)
129 static const char *timestamp_strs[] = {
131 "2014-06-25 08:38:53",
135 static u64 timestamp_parsed[] = {
140 struct mempool *pool = mp_new(4096);
143 while(timestamp_strs[i]) {
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);
149 if(timestamp_parsed[i] != result) {
150 die("Expected: %" PRIu64 " but got %" PRIu64, timestamp_parsed[i], result);
153 bprintf(out, "%" PRIu64 " %" PRIu64 "\n", timestamp_parsed[i], result);
161 static void test_timestamp_parse_errors(struct fastbuf *out)
163 static const char *timestamp_strs[] = {
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",
175 struct mempool *pool = mp_new(4096);
178 while(timestamp_strs[i]) {
180 const char *err_str = xt_timestamp.parse(timestamp_strs[i], &result, pool);
182 if(err_str == NULL) {
183 bprintf(out, "xt_timestamp.parse incorrectly did not result in error while parsing: '%s'.\n", timestamp_strs[i]);
185 bprintf(out, "xt_timestamp.parse error: '%s'.\n", err_str);
197 out = bfdopen_shared(1, 4096);
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);