Commit | Line | Data |
---|---|---|
7010c033 SM |
1 | /* |
2 | * Copyright (C) - 2015 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> | |
81684730 | 21 | #include <inttypes.h> |
7010c033 SM |
22 | |
23 | #include <tap/tap.h> | |
24 | ||
25 | #include <common/utils.h> | |
26 | ||
27 | /* For error.h */ | |
28 | int lttng_opt_quiet = 1; | |
29 | int lttng_opt_verbose = 3; | |
30 | int lttng_opt_mi; | |
31 | ||
32 | struct valid_test_input { | |
33 | char *input; | |
34 | uint64_t expected_result; | |
35 | }; | |
36 | ||
37 | /* Valid test cases */ | |
38 | static struct valid_test_input valid_tests_inputs[] = { | |
39 | { "0", 0 }, | |
40 | { "1234", 1234 }, | |
81684730 JR |
41 | { "1234us", 1234 }, |
42 | { "16ms", 16000 }, | |
43 | { "128ms", 128000 }, | |
7010c033 | 44 | { "32s", 32000000 }, |
81684730 JR |
45 | { "1m", 60000000 }, |
46 | { "20m", 1200000000 }, | |
47 | { "1h", 3600000000 }, | |
48 | { "5h", 18000000000 }, | |
7010c033 | 49 | { "00", 0 }, |
81684730 JR |
50 | { "0us", 0 }, |
51 | { "0ms", 0 }, | |
7010c033 | 52 | { "0s", 0 }, |
81684730 JR |
53 | { "0m", 0 }, |
54 | { "0h", 0 }, | |
55 | { "00us", 0 }, | |
56 | { "00ms", 0 }, | |
7010c033 | 57 | { "00s", 0 }, |
81684730 JR |
58 | { "00m", 0 }, |
59 | { "00h", 0 }, | |
7010c033 SM |
60 | { "12ms", 12000 }, |
61 | { "3597us", 3597 }, | |
62 | { "+5", 5 }, | |
63 | { "08", 8 }, | |
64 | { "0145us", 145 }, | |
65 | }; | |
66 | static const int num_valid_tests = sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]); | |
67 | ||
68 | /* Invalid test cases */ | |
69 | static char *invalid_tests_inputs[] = { | |
70 | "", | |
71 | " ", | |
72 | "-1", | |
73 | "m", | |
74 | "4611686018427387904s", | |
75 | "0x40M", | |
76 | "0x", | |
77 | "x0", | |
78 | "0xx0", | |
79 | "07mm", | |
80 | "0xm", | |
81 | "0Xs", | |
82 | "0x0ss", | |
83 | "0a", | |
84 | "0B", | |
85 | "0x3 s", | |
86 | "0xbs ", | |
87 | "14ns", | |
88 | "0xbs", | |
89 | "14ns", | |
90 | "14ms garbage after value", | |
91 | "0x14s", | |
81684730 JR |
92 | "0u", |
93 | "5mS", | |
94 | "5Ms", | |
95 | "12ussr", | |
96 | "67msrp", | |
97 | "14si", | |
98 | "12mo", | |
99 | "53hi", | |
7010c033 SM |
100 | }; |
101 | static const int num_invalid_tests = sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]); | |
102 | ||
103 | static void test_utils_parse_time_suffix(void) | |
104 | { | |
105 | uint64_t result; | |
106 | int ret; | |
107 | int i; | |
108 | ||
109 | /* Test valid cases */ | |
110 | for (i = 0; i < num_valid_tests; i++) { | |
81684730 | 111 | char name[256]; |
7010c033 SM |
112 | |
113 | ret = utils_parse_time_suffix(valid_tests_inputs[i].input, &result); | |
81684730 | 114 | sprintf(name, "valid test case: %s expected %" PRIu64, valid_tests_inputs[i].input, result); |
7010c033 SM |
115 | ok(ret == 0 && result == valid_tests_inputs[i].expected_result, name); |
116 | } | |
117 | ||
118 | /* Test invalid cases */ | |
119 | for (i = 0; i < num_invalid_tests; i++) { | |
120 | char name[100]; | |
121 | ||
122 | sprintf(name, "invalid test case: %s", invalid_tests_inputs[i]); | |
123 | ||
124 | ret = utils_parse_time_suffix(invalid_tests_inputs[i], &result); | |
125 | ok(ret != 0, name); | |
126 | } | |
127 | } | |
128 | ||
129 | int main(int argc, char **argv) | |
130 | { | |
131 | plan_tests(num_valid_tests + num_invalid_tests); | |
132 | ||
133 | diag("utils_parse_time_suffix tests"); | |
134 | ||
135 | test_utils_parse_time_suffix(); | |
136 | ||
137 | return exit_status(); | |
138 | } |