45a87b06ce3641a89cc881afb145cb52c6c613f2
[lttng-tools.git] / tests / unit / test_utils_parse_size_suffix.c
1 /*
2 * Copyright (C) - 2013 Simon Marchi <simon.marchi@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by as
6 * published by the Free Software Foundation; only version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #include <assert.h>
19 #include <string.h>
20 #include <stdio.h>
21
22 #include <tap/tap.h>
23
24 #include <src/common/utils.h>
25
26 /* For error.h */
27 int lttng_opt_quiet = 1;
28 int lttng_opt_verbose = 3;
29
30 struct valid_test_input {
31 char *input;
32 uint64_t expected_result;
33 };
34
35 /* Valid test cases */
36 static struct valid_test_input valid_tests_inputs[] = {
37 { "0", 0 },
38 { "1234", 1234 },
39 { "0x400", 1024 },
40 { "0300", 192 },
41 { "16k", 16384 },
42 { "128K", 131072 },
43 { "0x1234k", 4771840 },
44 { "32M", 33554432 },
45 { "1024G", 1099511627776 },
46 { "0X400", 1024 },
47 { "0x40a", 1034 },
48 { "0X40b", 1035 },
49 { "0x40C", 1036 },
50 { "0X40D", 1037 },
51 { "0x40e", 1038 },
52 { "0X40f", 1039 },
53 { "00", 0 },
54 { "0k", 0 },
55 { "0K", 0 },
56 { "0M", 0 },
57 { "0G", 0 },
58 { "00k", 0 },
59 { "00K", 0 },
60 { "00M", 0 },
61 { "00G", 0 },
62 { "0x0", 0 },
63 { "0X0", 0 },
64 { "0x0k", 0 },
65 { "0X0K", 0 },
66 { "0x0M", 0 },
67 { "0X0G", 0 },
68 { "0X40G", 68719476736 },
69 { "0300k", 196608 },
70 { "0300K", 196608 },
71 { "030M", 25165824 },
72 { "020G", 17179869184 },
73 { "0xa0k", 163840 },
74 { "0xa0K", 163840 },
75 { "0XA0M", 167772160 },
76 { "0xA0G", 171798691840 },
77 };
78 static const int num_valid_tests = sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]);
79
80 /* Invalid test cases */
81 static char *invalid_tests_inputs[] = {
82 "",
83 " ",
84 "-1",
85 "k",
86 "4611686018427387904G",
87 "0x40g",
88 "08",
89 "09",
90 "0x",
91 "x0",
92 "0xx0",
93 "07kK",
94 "0xk",
95 "0XM",
96 "0xG",
97 "0x0MM",
98 "0X0GG",
99 "0a",
100 "0B",
101 };
102
103 static const int num_invalid_tests = sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]);
104
105 static void test_utils_parse_size_suffix(void)
106 {
107 uint64_t result;
108 int ret;
109 int i;
110
111 /* Test valid cases */
112 for (i = 0; i < num_valid_tests; i++) {
113 char name[100];
114 sprintf(name, "valid test case: %s", valid_tests_inputs[i].input);
115
116 ret = utils_parse_size_suffix(valid_tests_inputs[i].input, &result);
117 ok(ret == 0 && result == valid_tests_inputs[i].expected_result, name);
118 }
119
120 /* Test invalid cases */
121 for (i = 0; i < num_invalid_tests; i++) {
122 char name[100];
123 sprintf(name, "invalid test case: %s", invalid_tests_inputs[i]);
124
125 ret = utils_parse_size_suffix(invalid_tests_inputs[i], &result);
126 ok(ret != 0, name);
127 }
128 }
129
130 int main(int argc, char **argv)
131 {
132 plan_tests(num_valid_tests + num_invalid_tests);
133
134 diag("utils_parse_size_suffix tests");
135
136 test_utils_parse_size_suffix();
137
138 return exit_status();
139 }
This page took 0.03103 seconds and 3 git commands to generate.