Introduce utils_parse_time_suffix
[lttng-tools.git] / tests / unit / test_utils_parse_time_suffix.c
CommitLineData
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>
21
22#include <tap/tap.h>
23
24#include <common/utils.h>
25
26/* For error.h */
27int lttng_opt_quiet = 1;
28int lttng_opt_verbose = 3;
29int lttng_opt_mi;
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 { "0u", 0 },
41 { "1234u", 1234 },
42 { "16m", 16000 },
43 { "128m", 128000 },
44 { "32s", 32000000 },
45 { "00", 0 },
46 { "0m", 0 },
47 { "0s", 0 },
48 { "00m", 0 },
49 { "00s", 0 },
50 { "12ms", 12000 },
51 { "3597us", 3597 },
52 { "+5", 5 },
53 { "08", 8 },
54 { "0145us", 145 },
55};
56static const int num_valid_tests = sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]);
57
58/* Invalid test cases */
59static char *invalid_tests_inputs[] = {
60 "",
61 " ",
62 "-1",
63 "m",
64 "4611686018427387904s",
65 "0x40M",
66 "0x",
67 "x0",
68 "0xx0",
69 "07mm",
70 "0xm",
71 "0Xs",
72 "0x0ss",
73 "0a",
74 "0B",
75 "0x3 s",
76 "0xbs ",
77 "14ns",
78 "0xbs",
79 "14ns",
80 "14ms garbage after value",
81 "0x14s",
82};
83static const int num_invalid_tests = sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]);
84
85static void test_utils_parse_time_suffix(void)
86{
87 uint64_t result;
88 int ret;
89 int i;
90
91 /* Test valid cases */
92 for (i = 0; i < num_valid_tests; i++) {
93 char name[100];
94
95 sprintf(name, "valid test case: %s", valid_tests_inputs[i].input);
96
97 ret = utils_parse_time_suffix(valid_tests_inputs[i].input, &result);
98 ok(ret == 0 && result == valid_tests_inputs[i].expected_result, name);
99 }
100
101 /* Test invalid cases */
102 for (i = 0; i < num_invalid_tests; i++) {
103 char name[100];
104
105 sprintf(name, "invalid test case: %s", invalid_tests_inputs[i]);
106
107 ret = utils_parse_time_suffix(invalid_tests_inputs[i], &result);
108 ok(ret != 0, name);
109 }
110}
111
112int main(int argc, char **argv)
113{
114 plan_tests(num_valid_tests + num_invalid_tests);
115
116 diag("utils_parse_time_suffix tests");
117
118 test_utils_parse_time_suffix();
119
120 return exit_status();
121}
This page took 0.027185 seconds and 4 git commands to generate.