pacemaker  2.1.4-dc6eb4362
Scalable High-Availability cluster resource manager
crm_get_msec_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2021 the Pacemaker project contributors
3  *
4  * The version control history for this file may have further details.
5  *
6  * This source code is licensed under the GNU Lesser General Public License
7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdint.h>
15 #include <setjmp.h>
16 #include <cmocka.h>
17 
18 static void
19 bad_input(void **state) {
20  assert_int_equal(crm_get_msec(NULL), PCMK__PARSE_INT_DEFAULT);
21  assert_int_equal(crm_get_msec(" "), PCMK__PARSE_INT_DEFAULT);
22  assert_int_equal(crm_get_msec("abcxyz"), PCMK__PARSE_INT_DEFAULT);
23  assert_int_equal(crm_get_msec("100xs"), PCMK__PARSE_INT_DEFAULT);
24  assert_int_equal(crm_get_msec(" 100 xs "), PCMK__PARSE_INT_DEFAULT);
25  assert_int_equal(crm_get_msec("-100ms"), PCMK__PARSE_INT_DEFAULT);
26 }
27 
28 static void
29 good_input(void **state) {
30  assert_int_equal(crm_get_msec("100"), 100000);
31  assert_int_equal(crm_get_msec(" 100 "), 100000);
32  assert_int_equal(crm_get_msec("\t100\n"), 100000);
33 
34  assert_int_equal(crm_get_msec("100ms"), 100);
35  assert_int_equal(crm_get_msec("100 MSEC"), 100);
36  assert_int_equal(crm_get_msec("1000US"), 1);
37  assert_int_equal(crm_get_msec("1000usec"), 1);
38  assert_int_equal(crm_get_msec("12s"), 12000);
39  assert_int_equal(crm_get_msec("12 sec"), 12000);
40  assert_int_equal(crm_get_msec("1m"), 60000);
41  assert_int_equal(crm_get_msec("13 min"), 780000);
42  assert_int_equal(crm_get_msec("2\th"), 7200000);
43  assert_int_equal(crm_get_msec("1 hr"), 3600000);
44 }
45 
46 static void
47 overflow(void **state) {
48  assert_int_equal(crm_get_msec("9223372036854775807s"), LLONG_MAX);
49 }
50 
51 int
52 main(int argc, char **argv)
53 {
54  const struct CMUnitTest tests[] = {
55  cmocka_unit_test(bad_input),
56  cmocka_unit_test(good_input),
57  cmocka_unit_test(overflow),
58  };
59 
60  cmocka_set_message_output(CM_OUTPUT_TAP);
61  return cmocka_run_group_tests(tests, NULL, NULL);
62 }
long long crm_get_msec(const char *input)
Parse a time+units string and return milliseconds equivalent.
Definition: strings.c:364
#define PCMK__PARSE_INT_DEFAULT
int main(int argc, char **argv)