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