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>
20 static void test_size_parse_correct(struct fastbuf *out)
22 static const char *size_strs[] = {
31 static u64 size_parsed[] = {
34 4 * 1024LLU * 1024LLU,
35 4 * 1024LLU * 1024LLU * 1024LLU,
36 4 * 1024LLU * 1024LLU * 1024LLU * 1024LLU
39 struct mempool *pool = mp_new(4096);
42 while(size_strs[i] != NULL) {
44 const char *parse_err = xt_size.parse(size_strs[i], &result, pool);
46 if(parse_err != NULL) {
47 die("Unexpected error in xt_size.parse");
49 if(size_parsed[i] != result) {
50 die("xt_size.parse parsed an incorrect value.");
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);
62 static void test_size_parse_errors(struct fastbuf *out)
64 static const char *size_strs[] = {
75 struct mempool *pool = mp_new(4096);
77 while(size_strs[i] != NULL) {
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]);
83 bprintf(out, "xt_size.parse error: '%s'.\n", parse_err);
92 static void test_bool_parse_correct(struct fastbuf *out)
94 static const char *bool_strs[] = {
102 static bool bool_parsed[] = {
109 struct mempool *pool = mp_new(4096);
112 while(bool_strs[i] != NULL) {
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);
118 if(bool_parsed[i] != result) {
119 die("xt_bool.parse parsed an incorrect value.");
121 bprintf(out, "%s %s\n", bool_strs[i], result ? "true" : "false");
128 static void test_timestamp_parse_correct(struct fastbuf *out)
130 static const char *timestamp_strs[] = {
132 "2014-06-25 08:38:53",
136 static u64 timestamp_parsed[] = {
141 // fill the value of timestamp_parsed as the timestamp in the
142 // current time zone.
144 struct tm parsed_time;
145 strptime(timestamp_strs[1], "%F %T", &parsed_time);
146 timestamp_parsed[1] = mktime(&parsed_time);
149 struct mempool *pool = mp_new(4096);
152 while(timestamp_strs[i]) {
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);
158 if(timestamp_parsed[i] != result) {
159 die("Expected: %" PRIu64 " but got %" PRIu64, timestamp_parsed[i], result);
162 bprintf(out, "%" PRIu64 " %" PRIu64 "\n", timestamp_parsed[i], result);
170 static void test_timestamp_parse_errors(struct fastbuf *out)
172 static const char *timestamp_strs[] = {
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",
184 struct mempool *pool = mp_new(4096);
187 while(timestamp_strs[i]) {
189 const char *err_str = xt_timestamp.parse(timestamp_strs[i], &result, pool);
191 if(err_str == NULL) {
192 bprintf(out, "xt_timestamp.parse incorrectly did not result in error while parsing: '%s'.\n", timestamp_strs[i]);
194 bprintf(out, "xt_timestamp.parse error: '%s'.\n", err_str);
205 setenv("TZ", "Europe/Prague", 1);
207 out = bfdopen_shared(1, 4096);
209 test_size_parse_correct(out);
210 test_size_parse_errors(out);
211 test_bool_parse_correct(out);
212 test_timestamp_parse_correct(out);
213 test_timestamp_parse_errors(out);