Fix: Don't override user variables within the build system
[lttng-tools.git] / tests / unit / test_utils_parse_size_suffix.c
CommitLineData
70d0b120
SM
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
343af227 24#include <common/utils.h>
70d0b120 25
ad7c9c18 26/* For error.h */
70d0b120
SM
27int lttng_opt_quiet = 1;
28int lttng_opt_verbose = 3;
c7e35b03 29int lttng_opt_mi;
70d0b120
SM
30
31struct valid_test_input {
32 char *input;
33 uint64_t expected_result;
34};
35
36/* Valid test cases */
37static struct valid_test_input valid_tests_inputs[] = {
38 { "0", 0 },
39 { "1234", 1234 },
40 { "0x400", 1024 },
41 { "0300", 192 },
42 { "16k", 16384 },
43 { "128K", 131072 },
44 { "0x1234k", 4771840 },
45 { "32M", 33554432 },
07955a0a 46 { "1024G", 1099511627776ULL },
5983a922
SM
47 { "0X400", 1024 },
48 { "0x40a", 1034 },
49 { "0X40b", 1035 },
50 { "0x40C", 1036 },
51 { "0X40D", 1037 },
52 { "0x40e", 1038 },
53 { "0X40f", 1039 },
54 { "00", 0 },
55 { "0k", 0 },
56 { "0K", 0 },
57 { "0M", 0 },
58 { "0G", 0 },
59 { "00k", 0 },
60 { "00K", 0 },
61 { "00M", 0 },
62 { "00G", 0 },
63 { "0x0", 0 },
64 { "0X0", 0 },
65 { "0x0k", 0 },
66 { "0X0K", 0 },
67 { "0x0M", 0 },
68 { "0X0G", 0 },
07955a0a 69 { "0X40G", 68719476736ULL },
5983a922
SM
70 { "0300k", 196608 },
71 { "0300K", 196608 },
72 { "030M", 25165824 },
07955a0a 73 { "020G", 17179869184ULL },
5983a922
SM
74 { "0xa0k", 163840 },
75 { "0xa0K", 163840 },
76 { "0XA0M", 167772160 },
07955a0a 77 { "0xA0G", 171798691840ULL },
70d0b120
SM
78};
79static const int num_valid_tests = sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]);
80
81/* Invalid test cases */
5983a922
SM
82static char *invalid_tests_inputs[] = {
83 "",
84 " ",
85 "-1",
86 "k",
87 "4611686018427387904G",
88 "0x40g",
89 "08",
90 "09",
91 "0x",
92 "x0",
93 "0xx0",
94 "07kK",
95 "0xk",
96 "0XM",
97 "0xG",
98 "0x0MM",
99 "0X0GG",
100 "0a",
101 "0B",
102};
103
70d0b120
SM
104static const int num_invalid_tests = sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]);
105
106static void test_utils_parse_size_suffix(void)
107{
108 uint64_t result;
109 int ret;
110 int i;
111
112 /* Test valid cases */
113 for (i = 0; i < num_valid_tests; i++) {
114 char name[100];
115 sprintf(name, "valid test case: %s", valid_tests_inputs[i].input);
116
117 ret = utils_parse_size_suffix(valid_tests_inputs[i].input, &result);
118 ok(ret == 0 && result == valid_tests_inputs[i].expected_result, name);
119 }
120
121 /* Test invalid cases */
122 for (i = 0; i < num_invalid_tests; i++) {
123 char name[100];
124 sprintf(name, "invalid test case: %s", invalid_tests_inputs[i]);
125
126 ret = utils_parse_size_suffix(invalid_tests_inputs[i], &result);
127 ok(ret != 0, name);
128 }
129}
130
131int main(int argc, char **argv)
132{
133 plan_tests(num_valid_tests + num_invalid_tests);
134
135 diag("utils_parse_size_suffix tests");
136
137 test_utils_parse_size_suffix();
138
139 return exit_status();
140}
This page took 0.045889 seconds and 5 git commands to generate.