2 * UCW Library -- Test of Tableprinter 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/table-types.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, i | SIZE_UNITS_FIXED, 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[] = {
71 struct mempool *pool = mp_new(4096);
73 while(size_strs[i] != NULL) {
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]);
79 bprintf(out, "xt_size.parse error: '%s'.\n", parse_err);
88 static void test_bool_parse_correct(struct fastbuf *out)
90 static const char *bool_strs[] = {
98 static bool bool_parsed[] = {
105 struct mempool *pool = mp_new(4096);
108 while(bool_strs[i] != NULL) {
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);
114 if(bool_parsed[i] != result) {
115 die("xt_bool.parse parsed an incorrect value.");
117 bprintf(out, "%s %s\n", bool_strs[i], result ? "true" : "false");
124 static void test_timestamp_parse_correct(struct fastbuf *out)
126 static const char *timestamp_strs[] = {
128 "2014-06-25 08:38:53",
132 static u64 timestamp_parsed[] = {
137 struct mempool *pool = mp_new(4096);
140 while(timestamp_strs[i]) {
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);
146 if(timestamp_parsed[i] != result) {
147 die("Expected: %" PRIu64 " but got %" PRIu64, timestamp_parsed[i], result);
150 bprintf(out, "%" PRIu64 " %" PRIu64 "\n", timestamp_parsed[i], result);
158 static void test_timestamp_parse_errors(struct fastbuf *out)
160 static const char *timestamp_strs[] = {
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",
172 struct mempool *pool = mp_new(4096);
175 while(timestamp_strs[i]) {
177 const char *err_str = xt_timestamp.parse(timestamp_strs[i], &result, pool);
179 if(err_str == NULL) {
180 bprintf(out, "xt_timestamp.parse incorrectly did not result in error while parsing: '%s'.\n", timestamp_strs[i]);
182 bprintf(out, "xt_timestamp.parse error: '%s'.\n", err_str);
194 out = bfdopen_shared(1, 4096);
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);