Rename C++ header files to .hpp
[lttng-tools.git] / tests / unit / test_utils_parse_time_suffix.cpp
1 /*
2 * Copyright (C) 2015 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <string.h>
9 #include <stdio.h>
10 #include <inttypes.h>
11
12 #include <tap/tap.h>
13
14 #include <common/utils.hpp>
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 {
22 const char *input;
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 { "1234us", 1234 },
31 { "16ms", 16000 },
32 { "128ms", 128000 },
33 { "32s", 32000000 },
34 { "1m", 60000000 },
35 { "20m", 1200000000 },
36 { "1h", 3600000000 },
37 { "5h", 18000000000 },
38 { "00", 0 },
39 { "0us", 0 },
40 { "0ms", 0 },
41 { "0s", 0 },
42 { "0m", 0 },
43 { "0h", 0 },
44 { "00us", 0 },
45 { "00ms", 0 },
46 { "00s", 0 },
47 { "00m", 0 },
48 { "00h", 0 },
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 */
58 static const char *invalid_tests_inputs[] = {
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",
81 "0u",
82 "5mS",
83 "5Ms",
84 "12ussr",
85 "67msrp",
86 "14si",
87 "12mo",
88 "53hi",
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++) {
100 char name[256];
101
102 ret = utils_parse_time_suffix(valid_tests_inputs[i].input, &result);
103 sprintf(name, "valid test case: %s expected %" PRIu64, valid_tests_inputs[i].input, result);
104 ok(ret == 0 && result == valid_tests_inputs[i].expected_result, "%s", name);
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);
114 ok(ret != 0, "%s", name);
115 }
116 }
117
118 int main(void)
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 }
This page took 0.035712 seconds and 5 git commands to generate.