SoW-2019-0002: Dynamic Snapshot
[lttng-tools.git] / tests / unit / test_utils_parse_time_suffix.c
1 /*
2 * Copyright (C) 2015 Simon Marchi <simon.marchi@polymtl.ca>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <assert.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <inttypes.h>
12
13 #include <tap/tap.h>
14
15 #include <common/utils.h>
16
17 /* For error.h */
18 int lttng_opt_quiet = 1;
19 int lttng_opt_verbose = 3;
20 int lttng_opt_mi;
21
22 struct valid_test_input {
23 const char *input;
24 uint64_t expected_result;
25 };
26
27 /* Valid test cases */
28 static struct valid_test_input valid_tests_inputs[] = {
29 { "0", 0 },
30 { "1234", 1234 },
31 { "1234us", 1234 },
32 { "16ms", 16000 },
33 { "128ms", 128000 },
34 { "32s", 32000000 },
35 { "1m", 60000000 },
36 { "20m", 1200000000 },
37 { "1h", 3600000000 },
38 { "5h", 18000000000 },
39 { "00", 0 },
40 { "0us", 0 },
41 { "0ms", 0 },
42 { "0s", 0 },
43 { "0m", 0 },
44 { "0h", 0 },
45 { "00us", 0 },
46 { "00ms", 0 },
47 { "00s", 0 },
48 { "00m", 0 },
49 { "00h", 0 },
50 { "12ms", 12000 },
51 { "3597us", 3597 },
52 { "+5", 5 },
53 { "08", 8 },
54 { "0145us", 145 },
55 };
56 static const int num_valid_tests = sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]);
57
58 /* Invalid test cases */
59 static const 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 "0u",
83 "5mS",
84 "5Ms",
85 "12ussr",
86 "67msrp",
87 "14si",
88 "12mo",
89 "53hi",
90 };
91 static const int num_invalid_tests = sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]);
92
93 static void test_utils_parse_time_suffix(void)
94 {
95 uint64_t result;
96 int ret;
97 int i;
98
99 /* Test valid cases */
100 for (i = 0; i < num_valid_tests; i++) {
101 char name[256];
102
103 ret = utils_parse_time_suffix(valid_tests_inputs[i].input, &result);
104 sprintf(name, "valid test case: %s expected %" PRIu64, valid_tests_inputs[i].input, result);
105 ok(ret == 0 && result == valid_tests_inputs[i].expected_result, name);
106 }
107
108 /* Test invalid cases */
109 for (i = 0; i < num_invalid_tests; i++) {
110 char name[100];
111
112 sprintf(name, "invalid test case: %s", invalid_tests_inputs[i]);
113
114 ret = utils_parse_time_suffix(invalid_tests_inputs[i], &result);
115 ok(ret != 0, name);
116 }
117 }
118
119 int main(int argc, char **argv)
120 {
121 plan_tests(num_valid_tests + num_invalid_tests);
122
123 diag("utils_parse_time_suffix tests");
124
125 test_utils_parse_time_suffix();
126
127 return exit_status();
128 }
This page took 0.04904 seconds and 5 git commands to generate.