]> mj.ucw.cz Git - libucw.git/blob - ucw/xtypes-test.c
xtypes&tableprinter: added parsing of size and 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   const char *size_strs[] = {
23     "4",
24     "4KB",
25     "4MB",
26     "4GB",
27     "4TB",
28     NULL
29   };
30
31   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     if(parse_err) {
46       abort();
47     }
48     ASSERT_MSG(size_parsed[i] == result, "xt_size.parse parsed an incorrect value.");
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   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 int main(void)
86 {
87   struct fastbuf *out;
88   out = bfdopen_shared(1, 4096);
89
90   test_size_correct(out);
91   test_size_parse_errors(out);
92
93   bclose(out);
94
95   return 0;
96 }