]> mj.ucw.cz Git - libucw.git/blob - ucw/xtypes-test.c
xtypes: updated tests
[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/table-types.h>
14
15 #include <errno.h>
16 #include <stdlib.h>
17 #include <inttypes.h>
18
19
20 static void test_size_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     ASSERT_MSG(parse_err == NULL, "Unexpected error in xt_size.parse");
47     ASSERT_MSG(size_parsed[i] == result, "xt_size.parse parsed an incorrect value.");
48
49     const char *result_str = xt_size.format(&result, i | SIZE_UNITS_FIXED, pool);
50     bprintf(out, "%s %s\n", size_strs[i], result_str);
51
52     i++;
53   }
54
55   mp_delete(pool);
56 }
57
58 static void test_size_parse_errors(struct fastbuf *out)
59 {
60   static const char *size_strs[] = {
61     "1X",
62     "KB",
63     "\0",
64     NULL
65   };
66
67   uint i = 0;
68   struct mempool *pool = mp_new(4096);
69
70   while(size_strs[i] != NULL) {
71     u64 result;
72     const char *parse_err = xt_size.parse(size_strs[i], &result, pool);
73     if(parse_err == NULL) {
74       bprintf(out, "xt_size.parse did not result in error while parsing: '%s'.\n", size_strs[i]);
75     } else {
76       bprintf(out, "xt_size.parse error: '%s'.\n", parse_err);
77     }
78
79     i++;
80   }
81
82   mp_delete(pool);
83 }
84
85 static void test_bool_correct(struct fastbuf *out UNUSED)
86 {
87   static const char *bool_strs[] = {
88     "0",
89     "1",
90     "false",
91     "true",
92     NULL
93   };
94
95   static bool bool_parsed[] = {
96     false,
97     true,
98     false,
99     true
100   };
101
102   struct mempool *pool = mp_new(4096);
103   uint i = 0;
104
105   while(bool_strs[i] != NULL) {
106     bool result;
107     const char *err_str = xt_bool.parse(bool_strs[i], &result, pool);
108     ASSERT_MSG(err_str == NULL, "Unexpected error in xt_bool.parse.");
109     ASSERT_MSG(bool_parsed[i] == result, "xt_bool.parse parsed an incorrect value.");
110     bprintf(out, "%s %s\n", bool_strs[i], result ? "true" : "false");
111     i++;
112   }
113
114   mp_delete(pool);
115 }
116
117 int main(void)
118 {
119   struct fastbuf *out;
120   out = bfdopen_shared(1, 4096);
121
122   test_size_correct(out);
123   test_size_parse_errors(out);
124   test_bool_correct(out);
125   bclose(out);
126
127   return 0;
128 }