Commit | Line | Data |
---|---|---|
70d0b120 | 1 | /* |
9d16b343 | 2 | * Copyright (C) 2013 Simon Marchi <simon.marchi@polymtl.ca> |
70d0b120 | 3 | * |
9d16b343 | 4 | * SPDX-License-Identifier: GPL-2.0-only |
70d0b120 | 5 | * |
70d0b120 SM |
6 | */ |
7 | ||
8 | #include <assert.h> | |
9 | #include <string.h> | |
10 | #include <stdio.h> | |
11 | ||
12 | #include <tap/tap.h> | |
13 | ||
343af227 | 14 | #include <common/utils.h> |
70d0b120 | 15 | |
ad7c9c18 | 16 | /* For error.h */ |
70d0b120 SM |
17 | int lttng_opt_quiet = 1; |
18 | int lttng_opt_verbose = 3; | |
c7e35b03 | 19 | int lttng_opt_mi; |
70d0b120 SM |
20 | |
21 | struct valid_test_input { | |
b53d4e59 | 22 | const char *input; |
70d0b120 SM |
23 | uint64_t expected_result; |
24 | }; | |
25 | ||
26 | /* Valid test cases */ | |
27 | static struct valid_test_input valid_tests_inputs[] = { | |
28 | { "0", 0 }, | |
29 | { "1234", 1234 }, | |
30 | { "0x400", 1024 }, | |
31 | { "0300", 192 }, | |
32 | { "16k", 16384 }, | |
33 | { "128K", 131072 }, | |
34 | { "0x1234k", 4771840 }, | |
35 | { "32M", 33554432 }, | |
07955a0a | 36 | { "1024G", 1099511627776ULL }, |
5983a922 SM |
37 | { "0X400", 1024 }, |
38 | { "0x40a", 1034 }, | |
39 | { "0X40b", 1035 }, | |
40 | { "0x40C", 1036 }, | |
41 | { "0X40D", 1037 }, | |
42 | { "0x40e", 1038 }, | |
43 | { "0X40f", 1039 }, | |
44 | { "00", 0 }, | |
45 | { "0k", 0 }, | |
46 | { "0K", 0 }, | |
47 | { "0M", 0 }, | |
48 | { "0G", 0 }, | |
49 | { "00k", 0 }, | |
50 | { "00K", 0 }, | |
51 | { "00M", 0 }, | |
52 | { "00G", 0 }, | |
53 | { "0x0", 0 }, | |
54 | { "0X0", 0 }, | |
55 | { "0x0k", 0 }, | |
56 | { "0X0K", 0 }, | |
57 | { "0x0M", 0 }, | |
58 | { "0X0G", 0 }, | |
07955a0a | 59 | { "0X40G", 68719476736ULL }, |
5983a922 SM |
60 | { "0300k", 196608 }, |
61 | { "0300K", 196608 }, | |
62 | { "030M", 25165824 }, | |
07955a0a | 63 | { "020G", 17179869184ULL }, |
5983a922 SM |
64 | { "0xa0k", 163840 }, |
65 | { "0xa0K", 163840 }, | |
66 | { "0XA0M", 167772160 }, | |
07955a0a | 67 | { "0xA0G", 171798691840ULL }, |
70d0b120 SM |
68 | }; |
69 | static const int num_valid_tests = sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]); | |
70 | ||
71 | /* Invalid test cases */ | |
b53d4e59 | 72 | static const char *invalid_tests_inputs[] = { |
5983a922 SM |
73 | "", |
74 | " ", | |
75 | "-1", | |
76 | "k", | |
77 | "4611686018427387904G", | |
78 | "0x40g", | |
79 | "08", | |
80 | "09", | |
81 | "0x", | |
82 | "x0", | |
83 | "0xx0", | |
84 | "07kK", | |
85 | "0xk", | |
86 | "0XM", | |
87 | "0xG", | |
88 | "0x0MM", | |
89 | "0X0GG", | |
90 | "0a", | |
91 | "0B", | |
92 | }; | |
93 | ||
70d0b120 SM |
94 | static const int num_invalid_tests = sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]); |
95 | ||
96 | static void test_utils_parse_size_suffix(void) | |
97 | { | |
98 | uint64_t result; | |
99 | int ret; | |
100 | int i; | |
101 | ||
102 | /* Test valid cases */ | |
103 | for (i = 0; i < num_valid_tests; i++) { | |
104 | char name[100]; | |
105 | sprintf(name, "valid test case: %s", valid_tests_inputs[i].input); | |
106 | ||
107 | ret = utils_parse_size_suffix(valid_tests_inputs[i].input, &result); | |
108 | ok(ret == 0 && result == valid_tests_inputs[i].expected_result, name); | |
109 | } | |
110 | ||
111 | /* Test invalid cases */ | |
112 | for (i = 0; i < num_invalid_tests; i++) { | |
113 | char name[100]; | |
114 | sprintf(name, "invalid test case: %s", invalid_tests_inputs[i]); | |
115 | ||
116 | ret = utils_parse_size_suffix(invalid_tests_inputs[i], &result); | |
117 | ok(ret != 0, name); | |
118 | } | |
119 | } | |
120 | ||
121 | int main(int argc, char **argv) | |
122 | { | |
123 | plan_tests(num_valid_tests + num_invalid_tests); | |
124 | ||
125 | diag("utils_parse_size_suffix tests"); | |
126 | ||
127 | test_utils_parse_size_suffix(); | |
128 | ||
129 | return exit_status(); | |
130 | } |