Commit | Line | Data |
---|---|---|
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 | ||
24 | #include <src/common/utils.h> | |
25 | ||
26 | /* For lttngerr.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 | }; | |
47 | static const int num_valid_tests = sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]); | |
48 | ||
49 | /* Invalid test cases */ | |
50 | static char *invalid_tests_inputs[] = { "", "-1", "k", "4611686018427387904G" }; | |
51 | static const int num_invalid_tests = sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]); | |
52 | ||
53 | static void test_utils_parse_size_suffix(void) | |
54 | { | |
55 | uint64_t result; | |
56 | int ret; | |
57 | int i; | |
58 | ||
59 | /* Test valid cases */ | |
60 | for (i = 0; i < num_valid_tests; i++) { | |
61 | char name[100]; | |
62 | sprintf(name, "valid test case: %s", valid_tests_inputs[i].input); | |
63 | ||
64 | ret = utils_parse_size_suffix(valid_tests_inputs[i].input, &result); | |
65 | ok(ret == 0 && result == valid_tests_inputs[i].expected_result, name); | |
66 | } | |
67 | ||
68 | /* Test invalid cases */ | |
69 | for (i = 0; i < num_invalid_tests; i++) { | |
70 | char name[100]; | |
71 | sprintf(name, "invalid test case: %s", invalid_tests_inputs[i]); | |
72 | ||
73 | ret = utils_parse_size_suffix(invalid_tests_inputs[i], &result); | |
74 | ok(ret != 0, name); | |
75 | } | |
76 | } | |
77 | ||
78 | int main(int argc, char **argv) | |
79 | { | |
80 | plan_tests(num_valid_tests + num_invalid_tests); | |
81 | ||
82 | diag("utils_parse_size_suffix tests"); | |
83 | ||
84 | test_utils_parse_size_suffix(); | |
85 | ||
86 | return exit_status(); | |
87 | } |